06.01.2015 Views

Manual

Manual

Manual

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 8: Debugging Your Parser 89<br />

8 Debugging Your Parser<br />

Developing a parser can be a challenge, especially if you don’t understand the algorithm<br />

(see Chapter 5 [The Bison Parser Algorithm], page 71). Even so, sometimes a detailed description<br />

of the automaton can help (see Section 8.1 [Understanding Your Parser], page 89),<br />

or tracing the execution of the parser can give some insight on why it behaves improperly<br />

(see Section 8.2 [Tracing Your Parser], page 95).<br />

8.1 Understanding Your Parser<br />

As documented elsewhere (see Chapter 5 [The Bison Parser Algorithm], page 71) Bison<br />

parsers are shift/reduce automata. In some cases (much more frequent than one would<br />

hope), looking at this automaton is required to tune or simply fix a parser. Bison provides<br />

two different representation of it, either textually or graphically (as a VCG file).<br />

The textual file is generated when the options ‘--report’ or ‘--verbose’ are specified,<br />

see See Chapter 9 [Invoking Bison], page 97. Its name is made by removing ‘.tab.c’ or ‘.c’<br />

from the parser output file name, and adding ‘.output’ instead. Therefore, if the input<br />

file is ‘foo.y’, then the parser file is called ‘foo.tab.c’ by default. As a consequence, the<br />

verbose output file is called ‘foo.output’.<br />

The following grammar file, ‘calc.y’, will be used in the sequel:<br />

%token NUM STR<br />

%left ’+’ ’-’<br />

%left ’*’<br />

%%<br />

exp: exp ’+’ exp<br />

| exp ’-’ exp<br />

| exp ’*’ exp<br />

| exp ’/’ exp<br />

| NUM<br />

;<br />

useless: STR;<br />

%%<br />

bison reports:<br />

calc.y: warning: 1 useless nonterminal and 1 useless rule<br />

calc.y:11.1-7: warning: useless nonterminal: useless<br />

calc.y:11.10-12: warning: useless rule: useless: STR<br />

calc.y: conflicts: 7 shift/reduce<br />

When given ‘--report=state’, in addition to ‘calc.tab.c’, it creates a file<br />

‘calc.output’ with contents detailed below. The order of the output and the exact<br />

presentation might vary, but the interpretation is the same.<br />

The first section includes details on conflicts that were solved thanks to precedence<br />

and/or associativity:<br />

Conflict in state 8 between rule 2 and token ’+’ resolved as reduce.<br />

Conflict in state 8 between rule 2 and token ’-’ resolved as reduce.<br />

Conflict in state 8 between rule 2 and token ’*’ resolved as shift.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!