Manual
Manual
Manual
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
Chapter 7: Handling Context Dependencies 87<br />
Here we assume that yylex looks at the value of hexflag; when it is nonzero, all integers<br />
are parsed in hexadecimal, and tokens starting with letters are parsed as integers if possible.<br />
The declaration of hexflag shown in the prologue of the parser file is needed to make it<br />
accessible to the actions (see Section 3.1.1 [The Prologue], page 41). You must also write<br />
the code in yylex to obey the flag.<br />
7.3 Lexical Tie-ins and Error Recovery<br />
Lexical tie-ins make strict demands on any error recovery rules you have. See Chapter 6<br />
[Error Recovery], page 83.<br />
The reason for this is that the purpose of an error recovery rule is to abort the parsing<br />
of one construct and resume in some larger construct. For example, in C-like languages, a<br />
typical error recovery rule is to skip tokens until the next semicolon, and then start a new<br />
statement, like this:<br />
stmt: expr ’;’<br />
| IF ’(’ expr ’)’ stmt { ... }<br />
...<br />
error ’;’<br />
{ hexflag = 0; }<br />
;<br />
If there is a syntax error in the middle of a ‘hex (expr)’ construct, this error rule will<br />
apply, and then the action for the completed ‘hex (expr)’ will never run. So hexflag<br />
would remain set for the entire rest of the input, or until the next hex keyword, causing<br />
identifiers to be misinterpreted as integers.<br />
To avoid this problem the error recovery rule itself clears hexflag.<br />
There may also be an error recovery rule that works within expressions. For example,<br />
there could be a rule which applies within parentheses and skips to the close-parenthesis:<br />
expr: ...<br />
| ’(’ expr ’)’<br />
{ $$ = $2; }<br />
| ’(’ error ’)’<br />
...<br />
If this rule acts within the hex construct, it is not going to abort that construct (since<br />
it applies to an inner level of parentheses within the construct). Therefore, it should not<br />
clear the flag: the rest of the hex construct should be parsed with the flag still in effect.<br />
What if there is an error recovery rule which might abort out of the hex construct or<br />
might not, depending on circumstances There is no way you can write the action to<br />
determine whether a hex construct is being aborted or not. So if you are using a lexical<br />
tie-in, you had better make sure your error recovery rules are not of this kind. Each rule<br />
must be such that you can be sure that it always will, or always won’t, have to clear the<br />
flag.