-
Notifications
You must be signed in to change notification settings - Fork 0
/
bison_notes.txt
90 lines (60 loc) · 1.51 KB
/
bison_notes.txt
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
78
79
80
81
82
83
84
85
86
87
88
89
90
Parsing Methods used by Bison:
--------------------------------
LALR: Look Ahead Left to Right (one token look ahead)
GLR: Generalized Left to Right
Basic concept:
------------------------
terminal symbols (or tokens):
cannot be subdivided
non-terminal symbols (or groupings):
built by grouping smaller constructs according to grammatical rules.
both tokens and non-terminals have value associated with them
The type of value is YYSTYPE or %union{ } member.
A bison rule:
<non-terminal> : <match> <action> ;
expr : expr '+' expr { $$=$1+$3; } ;
Bison Grammar File:
------------------
%{
PROLOGUE
%}
BISON DECLARATIONS
%%
GRAMMAR RULES
%%
EPILOGUE
YYSTYPE macro
----------------
stores symantic type
to change type: #define YYSTYPE char*
YYLTYPE macro
----------------
stores location type
The look ahead token
--------------------
stored in: yychar
semantic value in: yylval
location in: yylloc
Useful options
--------------------
-d option:
bison writes token-type macro defintions in a separate file:
for example: bison -d fname.y ---> fname.tab.h
yyerror and main
-----------------
void yyerror(char* s, ...)
{
va_list ap;
va_start(ap, s);
fprintf(stderr, "%d: error: ", yylineno);
vfprintf(stderr, s, ap);
fprintf(stderr, "\n");
}
int main()
{
printf("> ");
return yyparse();
}
Minimal Example:
----------------
see flexbison_test.l and flexbison_test.y in code_reference