Manual
Manual
Manual
You also want an ePaper? Increase the reach of your titles
YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.
Chapter 3: Bison Grammar Files 51<br />
If the grammar is such that a declaration can be distinguished from a statement by the<br />
first token (which is true in C), then one solution which does work is to put the action after<br />
the open-brace, like this:<br />
compound: ’{’ { prepare_for_local_variables (); }<br />
declarations statements ’}’<br />
| ’{’ statements ’}’<br />
;<br />
Now the first token of the following declaration or statement, which would in any case tell<br />
Bison which rule to use, can still do so.<br />
Another solution is to bury the action inside a nonterminal symbol which serves as a<br />
subroutine:<br />
subroutine: /* empty */<br />
{ prepare_for_local_variables (); }<br />
;<br />
compound: subroutine<br />
’{’ declarations statements ’}’<br />
| subroutine<br />
’{’ statements ’}’<br />
;<br />
Now Bison can execute the action in the rule for subroutine without deciding which rule<br />
for compound it will eventually use.<br />
3.6 Tracking Locations<br />
Though grammar rules and semantic actions are enough to write a fully functional parser,<br />
it can be useful to process some additional information, especially symbol locations.<br />
The way locations are handled is defined by providing a data type, and actions to take<br />
when rules are matched.<br />
3.6.1 Data Type of Locations<br />
Defining a data type for locations is much simpler than for semantic values, since all tokens<br />
and groupings always use the same type.<br />
You can specify the type of locations by defining a macro called YYLTYPE, just as you<br />
can specify the semantic value type by defining YYSTYPE (see Section 3.5.1 [Value Type],<br />
page 46). When YYLTYPE is not defined, Bison uses a default structure type with four<br />
members:<br />
typedef struct YYLTYPE<br />
{<br />
int first_line;<br />
int first_column;<br />
int last_line;<br />
int last_column;<br />
} YYLTYPE;