-
Notifications
You must be signed in to change notification settings - Fork 1
/
minimal_grammar.y
77 lines (67 loc) · 1.9 KB
/
minimal_grammar.y
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/* -*- coding: utf-8 -*- */
/*==================================================*/
/* PART 1: C declarations */
/*==================================================*/
%{
#include <stdio.h>
/* include the header file of the generated parser module */
#include "parser.h"
int yyerror(char *s);
/* externs */
extern int yylex();
extern int yyparse();
extern FILE *yyin;
%}
%union{
char* string;
}
%token <token> FOO BAR_START BAR_END
%token <string> ID STRING
%type <token> head contents foobar
%%
input:
head contents tail { printf("> Finished.\n"); }
;
head:
ID {printf("> head: <ID>: %s\n", $1); free($1); }
| head ID {printf("> head: <head ID>: %s\n", $2); free($2); }
;
contents:
foobar {printf("> contents: <foobar>\n");}
| contents foobar {printf("> contents: <contents foobar>\n");}
;
foobar:
foos {printf("> foobar: <foos>\n");}
| bars {printf("> foobar: <bars>\n");}
;
foos:
foo {printf("> foos: <foo>\n");}
| foos foo {printf("> foos: <foos foo>\n"); }
;
foo:
FOO STRING {printf("> FOO: %s\n", $2); free($2); }
;
bars:
bar {printf("> bars: <bar>\n");}
| bars bar {printf("> bars: <bars bar>\n");}
;
bar:
BAR_START ID STRING BAR_END
{
printf("> BAR: %s, %s.\n", $2, $3);
free($2); free($3);
}
;
tail:
STRING {printf("> tail <STRING>: %s\n", $1); free($1);}
| tail STRING {printf("> tail: <tail STRING>: %s\n", $2); free($2);}
;
%%
int yyerror(char *s)
{
extern int yylineno;
extern char *yytext;
printf("PARSER ERROR: %s at symbol %s\n", s, yytext);
printf(" on line %d\n", yylineno);
return -1;
}