06.01.2015 Views

Manual

Manual

Manual

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 8: Debugging Your Parser 91<br />

Bison then proceeds onto the automaton itself, describing each state with it set of items,<br />

also known as pointed rules. Each item is a production rule together with a point (marked<br />

by ‘.’) that the input cursor.<br />

state 0<br />

$accept -> . exp $ (rule 0)<br />

NUM shift, and go to state 1<br />

exp go to state 2<br />

This reads as follows: “state 0 corresponds to being at the very beginning of the parsing,<br />

in the initial rule, right before the start symbol (here, exp). When the parser returns to<br />

this state right after having reduced a rule that produced an exp, the control flow jumps<br />

to state 2. If there is no such transition on a nonterminal symbol, and the look-ahead is<br />

a NUM, then this token is shifted on the parse stack, and the control flow jumps to state 1.<br />

Any other look-ahead triggers a syntax error.”<br />

Even though the only active rule in state 0 seems to be rule 0, the report lists NUM as<br />

a look-ahead token because NUM can be at the beginning of any rule deriving an exp. By<br />

default Bison reports the so-called core or kernel of the item set, but if you want to see<br />

more detail you can invoke bison with ‘--report=itemset’ to list all the items, include<br />

those that can be derived:<br />

state 0<br />

In the state 1...<br />

state 1<br />

$accept -> . exp $ (rule 0)<br />

exp -> . exp ’+’ exp (rule 1)<br />

exp -> . exp ’-’ exp (rule 2)<br />

exp -> . exp ’*’ exp (rule 3)<br />

exp -> . exp ’/’ exp (rule 4)<br />

exp -> . NUM (rule 5)<br />

NUM shift, and go to state 1<br />

exp go to state 2<br />

exp -> NUM . (rule 5)<br />

$default<br />

reduce using rule 5 (exp)<br />

the rule 5, ‘exp: NUM;’, is completed. Whatever the look-ahead token (‘$default’), the<br />

parser will reduce it. If it was coming from state 0, then, after this reduction it will return<br />

to state 0, and will jump to state 2 (‘exp: go to state 2’).<br />

state 2<br />

$accept -> exp . $ (rule 0)

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

Saved successfully!

Ooh no, something went wrong!