2016-09-20 18:37:16 +00:00
|
|
|
%{
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdio.h>
|
2016-09-24 22:21:46 +00:00
|
|
|
#include <stdbool.h>
|
2016-09-20 18:37:16 +00:00
|
|
|
#include <limits.h>
|
|
|
|
#include "iburg.h"
|
2016-09-20 22:43:10 +00:00
|
|
|
|
|
|
|
#define YYDEBUG 1
|
|
|
|
|
2016-09-24 22:21:46 +00:00
|
|
|
extern int yylex(void);
|
|
|
|
|
2016-09-24 17:03:55 +00:00
|
|
|
static int nextern = 1;
|
2016-09-20 22:43:10 +00:00
|
|
|
|
2016-09-20 18:37:16 +00:00
|
|
|
%}
|
|
|
|
%union {
|
|
|
|
int n;
|
2016-09-20 22:43:10 +00:00
|
|
|
char* string;
|
2016-09-20 18:37:16 +00:00
|
|
|
Tree tree;
|
2016-09-24 17:03:55 +00:00
|
|
|
Rule rule;
|
2016-09-20 22:43:10 +00:00
|
|
|
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
|
|
|
|
|
2016-09-20 22:43:10 +00:00
|
|
|
%term PATTERNS
|
|
|
|
%term WHEN
|
|
|
|
%term EMIT
|
2016-09-24 11:08:17 +00:00
|
|
|
%term FRAGMENT
|
2016-09-20 22:43:10 +00:00
|
|
|
%term COST
|
2016-09-24 11:08:17 +00:00
|
|
|
%term INS
|
|
|
|
%term OUTS
|
2016-09-20 22:43:10 +00:00
|
|
|
|
2016-09-24 10:11:30 +00:00
|
|
|
%token <n> INT
|
2016-09-20 22:43:10 +00:00
|
|
|
%token <string> ID
|
|
|
|
%token <string> CFRAGMENT
|
2016-09-24 22:21:46 +00:00
|
|
|
%token <string> QFRAGMENT
|
2016-09-20 22:43:10 +00:00
|
|
|
|
2016-09-24 17:03:55 +00:00
|
|
|
%type <rule> pattern
|
2016-09-24 22:21:46 +00:00
|
|
|
%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
|
|
|
%%
|
2016-09-24 22:21:46 +00:00
|
|
|
|
2016-09-20 22:43:10 +00:00
|
|
|
spec
|
2016-09-24 17:03:55 +00:00
|
|
|
: PATTERNS patterns
|
2016-09-20 18:37:16 +00:00
|
|
|
;
|
|
|
|
|
2016-09-20 22:43:10 +00:00
|
|
|
patterns
|
|
|
|
: /* nothing */
|
|
|
|
| patterns pattern ';'
|
|
|
|
| patterns ';'
|
2016-09-20 18:37:16 +00:00
|
|
|
;
|
|
|
|
|
2016-09-20 22:43:10 +00:00
|
|
|
pattern
|
2016-09-24 17:03:55 +00:00
|
|
|
: ID '=' rhs { nonterm($1); $$ = rule($1, $3, nextern++); }
|
|
|
|
| rhs { $$ = rule("stmt", $1, nextern++); }
|
2016-09-24 22:21:46 +00:00
|
|
|
| pattern WHEN cfragments { $$ = $1; $$->when = $3; }
|
2016-09-24 17:03:55 +00:00
|
|
|
| pattern INS ins { $$ = $1; }
|
|
|
|
| pattern OUTS outs { $$ = $1; }
|
2016-09-24 22:21:46 +00:00
|
|
|
| emit { $$ = $1; }
|
2016-09-24 17:03:55 +00:00
|
|
|
| pattern COST INT { $$ = $1; $$->cost = $3; }
|
2016-09-20 22:43:10 +00:00
|
|
|
;
|
|
|
|
|
|
|
|
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
|
2016-09-24 11:08:17 +00:00
|
|
|
: ID { $$[0] = NULL; $$[1] = $1; }
|
|
|
|
| ID ':' ID { $$[0] = $1; $$[1] = $3; }
|
2016-09-24 10:11:30 +00:00
|
|
|
;
|
|
|
|
|
2016-09-24 22:21:46 +00:00
|
|
|
cfragments
|
2016-09-24 11:08:17 +00:00
|
|
|
: /* nothing */ { $$ = NULL; }
|
2016-09-24 22:21:46 +00:00
|
|
|
| CFRAGMENT cfragments { $$ = pushstring($1, $2); }
|
2016-09-20 22:43:10 +00:00
|
|
|
;
|
|
|
|
|
2016-09-24 11:08:17 +00:00
|
|
|
ins
|
2016-09-24 17:03:55 +00:00
|
|
|
: ins ',' in
|
2016-09-24 11:08:17 +00:00
|
|
|
| in
|
|
|
|
;
|
|
|
|
|
|
|
|
in
|
|
|
|
: ID ':' ID
|
|
|
|
;
|
|
|
|
|
|
|
|
outs
|
2016-09-24 17:03:55 +00:00
|
|
|
: outs ',' out
|
2016-09-24 11:08:17 +00:00
|
|
|
| out
|
|
|
|
;
|
|
|
|
|
|
|
|
out
|
2016-09-24 22:21:46 +00:00
|
|
|
: 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-24 11:08:17 +00:00
|
|
|
;
|
|
|
|
|
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);
|
|
|
|
}
|
2016-09-20 22:43:10 +00:00
|
|
|
|
|
|
|
/* vim: set sw=4 ts=4 expandtab : */
|