2016-09-20 18:37:16 +00:00
|
|
|
%{
|
2016-09-25 10:18:39 +00:00
|
|
|
#include <stdlib.h>
|
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-29 20:32:43 +00:00
|
|
|
#include <stdint.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-20 18:37:16 +00:00
|
|
|
%}
|
|
|
|
%union {
|
2016-10-09 10:32:36 +00:00
|
|
|
arith n;
|
|
|
|
const char* string;
|
2016-09-25 20:17:14 +00:00
|
|
|
Nonterm nonterm;
|
2016-09-20 18:37:16 +00:00
|
|
|
Tree tree;
|
2016-09-24 17:03:55 +00:00
|
|
|
Rule rule;
|
2016-09-25 20:17:14 +00:00
|
|
|
struct reg* reg;
|
2016-09-25 10:18:39 +00:00
|
|
|
struct stringlist* stringlist;
|
2016-10-01 10:17:14 +00:00
|
|
|
struct terminfo terminfo;
|
2016-10-01 11:56:52 +00:00
|
|
|
struct expr* expr;
|
2016-10-02 19:51:25 +00:00
|
|
|
struct constraint* constraint;
|
2016-09-20 18:37:16 +00:00
|
|
|
}
|
|
|
|
|
2016-10-03 22:16:06 +00:00
|
|
|
%term COPY
|
2016-10-14 23:15:08 +00:00
|
|
|
%term CORRUPTED
|
2016-09-25 20:17:14 +00:00
|
|
|
%term COST
|
|
|
|
%term DECLARATIONS
|
2016-09-20 22:43:10 +00:00
|
|
|
%term EMIT
|
2016-10-02 19:51:25 +00:00
|
|
|
%term EQUALS
|
2016-09-24 11:08:17 +00:00
|
|
|
%term FRAGMENT
|
2016-10-02 19:51:25 +00:00
|
|
|
%term NOTEQUALS
|
2016-09-25 20:17:14 +00:00
|
|
|
%term PATTERNS
|
2016-10-01 11:56:52 +00:00
|
|
|
%term PREFERS
|
2016-10-02 19:51:25 +00:00
|
|
|
%term REGISTERS
|
|
|
|
%term WHEN
|
|
|
|
%term WITH
|
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
|
2016-09-24 22:21:46 +00:00
|
|
|
%token <string> QFRAGMENT
|
2016-09-20 22:43:10 +00:00
|
|
|
|
2016-10-02 19:51:25 +00:00
|
|
|
%type <constraint> constraint
|
|
|
|
%type <constraint> constraints
|
2016-10-09 10:32:36 +00:00
|
|
|
%type <expr> predicate
|
|
|
|
%type <expr> predicate_arg
|
|
|
|
%type <expr> predicate_args
|
2016-09-25 20:17:14 +00:00
|
|
|
%type <nonterm> declaration
|
|
|
|
%type <reg> register
|
2016-10-09 10:32:36 +00:00
|
|
|
%type <rule> pattern
|
2016-10-02 19:51:25 +00:00
|
|
|
%type <rule> pattern_constraints
|
|
|
|
%type <rule> pattern_emit
|
2016-09-24 22:21:46 +00:00
|
|
|
%type <stringlist> qfragments
|
2016-10-01 10:17:14 +00:00
|
|
|
%type <terminfo> terminfo
|
2016-09-25 20:17:14 +00:00
|
|
|
%type <tree> rhs
|
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-25 20:17:14 +00:00
|
|
|
: REGISTERS registers
|
|
|
|
DECLARATIONS declarations
|
|
|
|
PATTERNS patterns
|
2016-09-20 18:37:16 +00:00
|
|
|
;
|
|
|
|
|
2016-09-25 20:17:14 +00:00
|
|
|
registers
|
|
|
|
: /* nothing */
|
|
|
|
| registers register ';'
|
|
|
|
| register ';'
|
|
|
|
;
|
|
|
|
|
|
|
|
register
|
2016-10-15 09:42:47 +00:00
|
|
|
: ID QFRAGMENT { $$ = makereg($1, $2); }
|
2016-10-12 20:58:46 +00:00
|
|
|
| register ID { $$ = $1; addregattr($1, $2, false); }
|
|
|
|
| register ID '!' { $$ = $1; addregattr($1, $2, true); }
|
2016-09-25 20:17:14 +00:00
|
|
|
;
|
|
|
|
|
|
|
|
declarations
|
|
|
|
: /* nothing */
|
|
|
|
| declarations declaration ';'
|
|
|
|
| declaration ';'
|
|
|
|
;
|
|
|
|
|
|
|
|
declaration
|
|
|
|
: ID { $$ = nonterm($1, true); }
|
|
|
|
| declaration FRAGMENT { $$ = $1; $$->is_fragment = true; }
|
|
|
|
;
|
|
|
|
|
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-10-05 20:56:25 +00:00
|
|
|
: terminfo '=' rhs { nonterm($1.name, false); $$ = rule(&$1, $3); }
|
|
|
|
| rhs { $$ = rule(NULL, $1); }
|
2016-10-03 22:16:06 +00:00
|
|
|
| pattern PREFERS predicate { $$ = $1; array_append(&$$->prefers, $3); }
|
|
|
|
| pattern WHEN predicate { $$ = $1; array_append(&$$->requires, $3); }
|
|
|
|
| pattern COST INT { $$ = $1; $$->cost = $3; }
|
|
|
|
| pattern_constraints { $$ = $1; }
|
|
|
|
| pattern_emit { $$ = $1; }
|
2016-09-20 22:43:10 +00:00
|
|
|
;
|
|
|
|
|
|
|
|
rhs
|
2016-10-03 22:16:06 +00:00
|
|
|
: terminfo { $$ = tree(&$1, NULL, NULL); }
|
|
|
|
| terminfo '(' rhs ')' { $$ = tree(&$1, $3, NULL); }
|
|
|
|
| terminfo '(' rhs ',' rhs ')' { $$ = tree(&$1, $3, $5); }
|
2016-09-20 18:37:16 +00:00
|
|
|
;
|
|
|
|
|
2016-10-01 10:17:14 +00:00
|
|
|
terminfo
|
2016-10-03 22:16:06 +00:00
|
|
|
: ID { $$.name = $1; }
|
2016-10-05 20:56:25 +00:00
|
|
|
| '(' ID ')' ID { $$.attr = $2; $$.name = $4; }
|
2016-10-03 22:16:06 +00:00
|
|
|
| ID ':' ID { $$.label = $1; $$.name = $3; }
|
2016-10-05 20:56:25 +00:00
|
|
|
| ID ':' '(' ID ')' ID { $$.label = $1; $$.attr = $4; $$.name = $6; }
|
2016-09-24 10:11:30 +00:00
|
|
|
;
|
|
|
|
|
2016-10-02 19:51:25 +00:00
|
|
|
pattern_emit
|
2016-09-25 10:18:39 +00:00
|
|
|
: pattern EMIT qfragments {
|
|
|
|
$$ = $1;
|
2016-09-25 20:17:14 +00:00
|
|
|
if (!$$->lhs->is_fragment)
|
|
|
|
stringlist_add($3, "\n");
|
2016-09-25 10:18:39 +00:00
|
|
|
stringlist_addall(&$$->code, $3);
|
|
|
|
}
|
2016-09-24 22:21:46 +00:00
|
|
|
;
|
|
|
|
|
2016-10-02 19:51:25 +00:00
|
|
|
pattern_constraints
|
|
|
|
: pattern WITH constraints {
|
|
|
|
struct constraint* c = $3;
|
|
|
|
$$ = $1;
|
|
|
|
while (c)
|
|
|
|
{
|
|
|
|
array_append(&$$->constraints, c);
|
|
|
|
c = c->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
;
|
|
|
|
|
|
|
|
constraints
|
|
|
|
: constraint { $$ = $1; }
|
|
|
|
| constraints ',' constraint { $$ = $3; $$->next = $1; }
|
|
|
|
;
|
|
|
|
|
|
|
|
constraint
|
|
|
|
: '(' constraint ')' { $$ = $2; }
|
2016-10-14 20:17:02 +00:00
|
|
|
| '%' ID EQUALS '%' ID { $$ = calloc(1, sizeof(*$$));
|
|
|
|
$$->type = CONSTRAINT_EQUALS; $$->left = $2; $$->right = $5; }
|
2016-10-14 23:15:08 +00:00
|
|
|
| CORRUPTED '(' ID ')' { $$ = calloc(1, sizeof(*$$));
|
|
|
|
$$->type = CONSTRAINT_CORRUPTED_ATTR; $$->left = $3; }
|
2016-10-02 19:51:25 +00:00
|
|
|
;
|
|
|
|
|
2016-09-24 22:21:46 +00:00
|
|
|
qfragments
|
2016-09-25 20:17:14 +00:00
|
|
|
: /* nothing */ { $$ = calloc(1, sizeof *$$); }
|
|
|
|
| qfragments QFRAGMENT { $$ = $1; stringlist_add($$, $2); }
|
2016-09-24 11:08:17 +00:00
|
|
|
;
|
|
|
|
|
2016-10-01 11:56:52 +00:00
|
|
|
predicate
|
2016-10-09 10:32:36 +00:00
|
|
|
: ID '(' predicate_args ')' {
|
|
|
|
$$ = calloc(1, sizeof *$$);
|
|
|
|
$$->type = PREDICATE_FUNCTION;
|
|
|
|
$$->u.name = $1;
|
|
|
|
$$->next = $3;
|
|
|
|
}
|
2016-10-01 11:56:52 +00:00
|
|
|
;
|
|
|
|
|
|
|
|
predicate_args
|
2016-10-02 19:51:25 +00:00
|
|
|
: /* nothing */ { $$ = NULL; }
|
2016-10-09 10:32:36 +00:00
|
|
|
| predicate_arg { $$ = $1; }
|
|
|
|
| predicate_arg ',' predicate_args { $$ = $1; $$->next = $3; }
|
|
|
|
;
|
|
|
|
|
|
|
|
predicate_arg
|
|
|
|
: '%' ID { $$ = calloc(1, sizeof *$$); $$->type = PREDICATE_NODE; $$->u.name = $2; }
|
|
|
|
| INT { $$ = calloc(1, sizeof *$$); $$->type = PREDICATE_NUMBER; $$->u.number = $1; }
|
2016-10-01 11:56:52 +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++;
|
2016-10-01 11:56:52 +00:00
|
|
|
exit(1);
|
2016-09-20 18:37:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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 : */
|