More grammar changes.

This commit is contained in:
David Given 2016-09-24 19:03:55 +02:00
parent 2acc4ed29d
commit c8fcbe282a
4 changed files with 29 additions and 97 deletions

View file

@ -1,37 +1,6 @@
%{
#if 0
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
#include <stdbool.h>
PATTERNS
#define TRACE
#define STATE_TYPE void*
typedef struct tree {
int op;
struct tree *kids[2];
STATE_TYPE state_label;
} *NODEPTR_TYPE;
#define OP_LABEL(p) ((p)->op)
#define LEFT_CHILD(p) ((p)->kids[0])
#define RIGHT_CHILD(p) ((p)->kids[1])
#define STATE_LABEL(p) ((p)->state_label)
#define PANIC printf
static void burm_trace(NODEPTR_TYPE p, int eruleno, int cost, int bestcost) {
#ifdef TRACE
extern const char *burm_string[];
fprintf(stderr, "0x%p matched %s with cost %d vs. %d\n", p,
burm_string[eruleno], cost, bestcost);
#endif
}
#endif
%}
%%
stm = STORE4(addr:address, value:reg)
STORE4(addr:address, value:reg)
ins value:GPR
emit "str %value, %addr"
cost 4;
@ -49,7 +18,7 @@ static void burm_trace(NODEPTR_TYPE p, int eruleno, int cost, int bestcost) {
ins addr:GPR
fragment "[%addr]";
stm = reg;
reg;
reg = ADD4(left:reg, right:aluparam)
ins left:GPR, right:GPR

View file

@ -7,14 +7,14 @@
#define YYDEBUG 1
static char rcsid[] = "$Id$";
static int nextesn = 0;
static int nextern = 0;
static int nextern = 1;
%}
%union {
int n;
char* string;
Tree tree;
Rule rule;
Stringlist stringlist;
char* stringpair[2];
}
@ -36,37 +36,16 @@ static int nextern = 0;
%token <string> STRING
%type <n> cost
%type <string> lhs
%type <rule> pattern
%type <stringlist> stringlist
%type <stringlist> when
%type <tree> rhs
%type <stringpair> labelledid
%%
spec
: decls PPERCENT patterns
| decls
: PATTERNS patterns
;
decls : /* lambda */
| decls decl
;
decl
: TERMINAL blist ';'
| START lhs ';'
{
if (nonterm($2)->number != 1)
yyerror("redeclaration of the start symbol\n");
}
| ';'
| error ';' { yyerrok; }
;
blist
: /* nothing */
| blist ID { term($2, nextesn++); }
;
patterns
: /* nothing */
| patterns pattern ';'
@ -75,13 +54,16 @@ patterns
;
pattern
: lhs '=' rhs when ins outs emits cost { rule($1, $3, nextern++, $4, $8); }
: ID '=' rhs { nonterm($1); $$ = rule($1, $3, nextern++); }
| rhs { $$ = rule("stmt", $1, nextern++); }
| pattern WHEN stringlist { $$ = $1; $$->when = $3; }
| pattern INS ins { $$ = $1; }
| pattern OUTS outs { $$ = $1; }
| pattern EMIT STRING { $$ = $1; }
| pattern FRAGMENT STRING { $$ = $1; }
| pattern COST INT { $$ = $1; $$->cost = $3; }
;
lhs
: ID { $$ = $1; nonterm($$); }
;
rhs
: labelledid { $$ = tree($1[1], $1[0], NULL, NULL); }
| labelledid '(' rhs ')' { $$ = tree($1[1], $1[0], $3, NULL); }
@ -104,12 +86,7 @@ stringlist
;
ins
: /* nothing */
| INS inslist
;
inslist
: inslist ',' in
: ins ',' in
| in
;
@ -118,12 +95,7 @@ in
;
outs
: /* nothing */
| OUTS outslist
;
outslist
: outslist ',' out
: outs ',' out
| out
;
@ -131,22 +103,6 @@ out
: ID ':' ID
;
emits
: /* nothing */
| EMIT STRING
| FRAGMENT STRING
;
cost
: /* lambda */ { $$ = 0; }
| COST INT {
if ($2 > maxcost) {
yyerror("%d exceeds maximum cost of %d\n", $2, maxcost);
$$ = maxcost;
} else
$$ = $2;
}
;
%%
#include <stdarg.h>
#include <ctype.h>

View file

@ -60,7 +60,7 @@ int main(int argc, char* argv[])
for (;;)
{
int opt = getopt(argc, argv, "p:i:o:");
int opt = getopt(argc, argv, "p:i:o:y");
if (opt == -1)
break;
@ -88,6 +88,13 @@ int main(int argc, char* argv[])
}
break;
case 'y':
{
extern int yydebug;
yydebug = 1;
break;
}
default:
yyerror("usage: %s [-p prefix] < input > output\n", argv[0]);
exit(1);
@ -97,6 +104,8 @@ int main(int argc, char* argv[])
emitheader();
registerterminals();
start = nonterm("stmt");
yyin = infp;
yyparse();
@ -308,14 +317,13 @@ Tree tree(const char* id, const char* label, Tree left, Tree right)
}
/* rule - create & initialize a rule with the given fields */
Rule rule(char* id, Tree pattern, int ern, Stringlist when, int cost)
Rule rule(char* id, Tree pattern, int ern)
{
Rule r = calloc(1, sizeof *r);
Rule *q;
Term p = pattern->op;
nrules++;
r->when = when;
r->lhs = nonterm(id);
r->packed = ++r->lhs->lhscount;
for (q = &r->lhs->rules; *q; q = &(*q)->decode)
@ -323,7 +331,6 @@ Rule rule(char* id, Tree pattern, int ern, Stringlist when, int cost)
*q = r;
r->pattern = pattern;
r->ern = ern;
r->cost = cost;
if (p->kind == TERM)
{
r->next = p->rules;

View file

@ -68,7 +68,7 @@ struct rule
Rule kids; /* next rule with same burm_kids pattern */
Stringlist when; /* C predicate string */
};
extern Rule rule(char* id, Tree pattern, int ern, Stringlist when, int cost);
extern Rule rule(char* id, Tree pattern, int ern);
extern int maxcost; /* maximum cost */
/* gram.y: */