ack/util/mcgg/gram.y

146 lines
2.8 KiB
Text
Raw Normal View History

2016-09-20 18:37:16 +00:00
%{
#include <string.h>
#include <stdio.h>
#include <stdbool.h>
2016-09-20 18:37:16 +00:00
#include <limits.h>
#include "iburg.h"
#define YYDEBUG 1
extern int yylex(void);
2016-09-24 17:03:55 +00:00
static int nextern = 1;
2016-09-20 18:37:16 +00:00
%}
%union {
int n;
char* string;
2016-09-20 18:37:16 +00:00
Tree tree;
2016-09-24 17:03:55 +00:00
Rule rule;
Stringlist stringlist;
2016-09-24 10:11:30 +00:00
char* stringpair[2];
2016-09-20 18:37:16 +00:00
}
%term TERMINAL
%term START
%term PPERCENT
%term PATTERNS
%term WHEN
%term EMIT
%term FRAGMENT
%term COST
%term INS
%term OUTS
2016-09-24 10:11:30 +00:00
%token <n> INT
%token <string> ID
%token <string> CFRAGMENT
%token <string> QFRAGMENT
2016-09-24 17:03:55 +00:00
%type <rule> pattern
%type <rule> emit
%type <stringlist> cfragments
%type <stringlist> qfragments
2016-09-24 10:11:30 +00:00
%type <tree> rhs
%type <stringpair> labelledid
2016-09-20 18:37:16 +00:00
%%
spec
2016-09-24 17:03:55 +00:00
: PATTERNS patterns
2016-09-20 18:37:16 +00:00
;
patterns
: /* nothing */
| patterns pattern ';'
| patterns ';'
2016-09-20 18:37:16 +00:00
;
pattern
2016-09-24 17:03:55 +00:00
: ID '=' rhs { nonterm($1); $$ = rule($1, $3, nextern++); }
| rhs { $$ = rule("stmt", $1, nextern++); }
| pattern WHEN cfragments { $$ = $1; $$->when = $3; }
2016-09-24 17:03:55 +00:00
| pattern INS ins { $$ = $1; }
| pattern OUTS outs { $$ = $1; }
| emit { $$ = $1; }
2016-09-24 17:03:55 +00:00
| pattern COST INT { $$ = $1; $$->cost = $3; }
;
rhs
2016-09-24 10:11:30 +00:00
: labelledid { $$ = tree($1[1], $1[0], NULL, NULL); }
| labelledid '(' rhs ')' { $$ = tree($1[1], $1[0], $3, NULL); }
| labelledid '(' rhs ',' rhs ')' { $$ = tree($1[1], $1[0], $3, $5); }
2016-09-20 18:37:16 +00:00
;
2016-09-24 10:11:30 +00:00
labelledid
: ID { $$[0] = NULL; $$[1] = $1; }
| ID ':' ID { $$[0] = $1; $$[1] = $3; }
2016-09-24 10:11:30 +00:00
;
cfragments
: /* nothing */ { $$ = NULL; }
| CFRAGMENT cfragments { $$ = pushstring($1, $2); }
;
ins
2016-09-24 17:03:55 +00:00
: ins ',' in
| in
;
in
: ID ':' ID
;
outs
2016-09-24 17:03:55 +00:00
: outs ',' out
| out
;
out
: ID
| ID ':' ID
;
emit
: pattern EMIT qfragments { $$ = $1; $$->code = $3; $$->is_fragment = false; }
| pattern FRAGMENT qfragments { $$ = $1; $$->code = $3; $$->is_fragment = true; }
;
qfragments
: QFRAGMENT { $$ = pushstring($1, NULL); }
| QFRAGMENT qfragments { $$ = pushstring($1, $2); }
;
2016-09-20 18:37:16 +00:00
%%
#include <stdarg.h>
#include <ctype.h>
int errcnt = 0;
FILE *infp = NULL;
FILE *outfp = NULL;
static char buf[BUFSIZ], *bp = buf;
static int ppercent = 0;
void yyerror(char *fmt, ...) {
va_list ap;
va_start(ap, fmt);
if (yylineno > 0)
fprintf(stderr, "line %d: ", yylineno);
vfprintf(stderr, fmt, ap);
if (fmt[strlen(fmt)-1] != '\n')
fprintf(stderr, "\n");
errcnt++;
}
void yywarn(char *fmt, ...) {
va_list ap;
va_start(ap, fmt);
if (yylineno > 0)
fprintf(stderr, "line %d: ", yylineno);
fprintf(stderr, "warning: ");
vfprintf(stderr, fmt, ap);
}
/* vim: set sw=4 ts=4 expandtab : */