2016-09-20 18:37:16 +00:00
|
|
|
%{
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <limits.h>
|
|
|
|
#include "iburg.h"
|
2016-09-20 22:43:10 +00:00
|
|
|
|
|
|
|
#define YYDEBUG 1
|
|
|
|
|
2016-09-20 18:37:16 +00:00
|
|
|
static char rcsid[] = "$Id$";
|
2016-09-20 22:43:10 +00:00
|
|
|
static int nextesn = 0;
|
|
|
|
static int nextern = 0;
|
|
|
|
|
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-20 22:43:10 +00:00
|
|
|
Stringlist stringlist;
|
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 PAT
|
|
|
|
%term WHEN
|
|
|
|
%term EMIT
|
|
|
|
%term COST
|
|
|
|
|
|
|
|
%token <string> ID
|
|
|
|
%token <string> CFRAGMENT
|
|
|
|
%token <n> INT
|
|
|
|
|
|
|
|
%type <string> lhs
|
|
|
|
%type <tree> rhs
|
|
|
|
%type <n> cost
|
|
|
|
%type <stringlist> when
|
|
|
|
%type <stringlist> stringlist
|
2016-09-20 18:37:16 +00:00
|
|
|
%%
|
2016-09-20 22:43:10 +00:00
|
|
|
spec
|
|
|
|
: decls PPERCENT patterns
|
|
|
|
| decls
|
2016-09-20 18:37:16 +00:00
|
|
|
;
|
|
|
|
|
|
|
|
decls : /* lambda */
|
|
|
|
| decls decl
|
|
|
|
;
|
|
|
|
|
2016-09-20 22:43:10 +00:00
|
|
|
decl
|
|
|
|
: TERMINAL blist ';'
|
|
|
|
| START lhs ';'
|
|
|
|
{
|
|
|
|
if (nonterm($2)->number != 1)
|
|
|
|
yyerror("redeclaration of the start symbol\n");
|
|
|
|
}
|
|
|
|
| ';'
|
|
|
|
| error ';' { yyerrok; }
|
2016-09-20 18:37:16 +00:00
|
|
|
;
|
|
|
|
|
2016-09-20 22:43:10 +00:00
|
|
|
blist
|
|
|
|
: /* nothing */
|
|
|
|
| blist ID { term($2, nextesn++); }
|
|
|
|
;
|
2016-09-20 18:37:16 +00:00
|
|
|
|
2016-09-20 22:43:10 +00:00
|
|
|
patterns
|
|
|
|
: /* nothing */
|
|
|
|
| patterns pattern ';'
|
|
|
|
| patterns ';'
|
|
|
|
| patterns error ';' { yyerrok; }
|
2016-09-20 18:37:16 +00:00
|
|
|
;
|
|
|
|
|
2016-09-20 22:43:10 +00:00
|
|
|
pattern
|
|
|
|
: lhs '=' rhs when cost { rule($1, $3, nextern++, $4, $5); }
|
|
|
|
;
|
|
|
|
|
|
|
|
lhs
|
|
|
|
: ID { $$ = $1; nonterm($$); }
|
2016-09-20 18:37:16 +00:00
|
|
|
;
|
|
|
|
|
2016-09-20 22:43:10 +00:00
|
|
|
rhs
|
|
|
|
: ID { $$ = tree($1, NULL, NULL); }
|
|
|
|
| ID '(' rhs ')' { $$ = tree($1, $3, NULL); }
|
|
|
|
| ID '(' rhs ',' rhs ')' { $$ = tree($1, $3, $5); }
|
2016-09-20 18:37:16 +00:00
|
|
|
;
|
|
|
|
|
2016-09-20 22:43:10 +00:00
|
|
|
when
|
|
|
|
: /* nothing */ { $$ = NULL; }
|
|
|
|
| WHEN stringlist { $$ = $2; }
|
|
|
|
;
|
|
|
|
|
|
|
|
stringlist
|
|
|
|
: /* nothing */ { $$ = NULL; }
|
|
|
|
| CFRAGMENT stringlist { $$ = pushstring($1, $2); }
|
|
|
|
;
|
|
|
|
|
|
|
|
cost
|
|
|
|
: /* lambda */ { $$ = 0; }
|
|
|
|
| COST INT {
|
|
|
|
if ($2 > maxcost) {
|
|
|
|
yyerror("%d exceeds maximum cost of %d\n", $2, maxcost);
|
|
|
|
$$ = maxcost;
|
|
|
|
} else
|
|
|
|
$$ = $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);
|
|
|
|
}
|
2016-09-20 22:43:10 +00:00
|
|
|
|
|
|
|
/* vim: set sw=4 ts=4 expandtab : */
|