Bison Internals

GNU/Bison is a LALR parser generator written in C. It can generate C and C++ code that implements LALR and GLR parsers. This project is an effort to document internal data structures of Bison.

Monday, June 19, 2006

A note on gram.c

gram.c contains many little useful functions that operate on the internal structure of grammar. A study of these functions will ground the understanding of bison's internal representation of grammar. There are functions to print the rules, dump the entire grammar, print out the RITEMS array in a useful way etc; I inserted calls to these functions in reader.c after packgram() finished its job; and what came our was amazing;

Here is my grammar:

S: NUM X
| Y
X: NUM
Y: 'a'
;

Here are the internals.. enjoy!

SYMBOL:Y NUMBER:8
SYMBOL:NUM NUMBER:3
SYMBOL:$end NUMBER:0
SYMBOL:error NUMBER:1
SYMBOL:'a' NUMBER:4
SYMBOL:$accept NUMBER:5
SYMBOL:S NUMBER:6
SYMBOL:$undefined NUMBER:2
SYMBOL:X NUMBER:7
********Trap: From packgram*********
Rule#0:
LHS:$accept, symNO: 5
RHS:6
Rule#1:
LHS:S, symNO: 6
RHS:3
Rule#2:
LHS:S, symNO: 6
RHS:8
Rule#3:
LHS:X, symNO: 7
RHS:3
Rule#4:
LHS:Y, symNO: 8
RHS:4
RITEM#0:
6
RITEM#1:
0
RITEM#2:
-1
RITEM#3:
3
RITEM#4:
7
RITEM#5:
-2
RITEM#6:
8
RITEM#7:
-3
RITEM#8:
3
RITEM#9:
-4
RITEM#10:
4
RITEM#11:
-5

RITEM
S $end (rule 0)
NUM X (rule 1)
Y (rule 2)
NUM (rule 3)
'a' (rule 4)


Grammar

0 $accept: S $end

1 S: NUM X
2 | Y

3 X: NUM

4 Y: 'a'


Dump1

ntokens = 5, nvars = 4, nsyms = 9, nrules = 5, nritems = 12

Variables
---------

Value Sprec Sassoc Tag
5 0 0 $accept
6 0 0 S
7 0 0 X
8 0 0 Y


Rules
-----

Num (Prec, Assoc, Useful, Ritem Range) Lhs -> Rhs (Ritem range) [Num]
0 ( 0, 0, 1, 0- 1) 5 -> 6 0 [0]
1 ( 0, 0, 1, 3- 4) 6 -> 3 7 [1]
2 ( 0, 0, 1, 6- 6) 6 -> 8 [2]
3 ( 0, 0, 1, 8- 8) 7 -> 3 [3]
4 ( 0, 0, 1, 10-10) 8 -> 4 [4]


Rules interpreted
-----------------

0 $accept: S $end
1 S: NUM X
2 S: Y
3 X: NUM
4 Y: 'a'

0 Comments:

Post a Comment

<< Home