09.06.2013 Views

Intel XENIX 286 Programmers Guide (86) - Tenox.tc

Intel XENIX 286 Programmers Guide (86) - Tenox.tc

Intel XENIX 286 Programmers Guide (86) - Tenox.tc

SHOW MORE
SHOW LESS

Create successful ePaper yourself

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

yacc: Compiler-Compiler<br />

Consider the following:<br />

% {<br />

int dflag;<br />

% }<br />

... other declarations ...<br />

%%<br />

prog : decls stats<br />

decls :/* empty *I<br />

{ dflag = 1; }<br />

I decls declaration<br />

stats :/* empty *I<br />

{ dflag = 0; }<br />

I stats statement<br />

... other rules ...<br />

<strong>XENIX</strong> Programming<br />

The flag dflag is now 0 when reading statements, and 1 when reading declarations,<br />

except for the first token in the first statement. This token must be seen by the parser<br />

before it can tell that the declaration section has ended and the statements have begun.<br />

In many cases, this single token exception does not affect the lexical scan.<br />

This kind of approach can be overdone. Nevertheless, it represents a way of doing some<br />

things that are difficult to do otherwise.<br />

Handling Reserved Words<br />

Some programming languages permit the user to use words like if, that are normally<br />

reserved, as label or variable names, provided that such use does not conflict with the<br />

legal use of these names in the programming language. This is extremely hard to do in<br />

the framework of yacc; it is difficult to pass information to the lexical analyzer telling<br />

it "this instance of 'if' is a keyword, and that instance is a variable." It is best that<br />

keywords be reserved, that is, be forbidden for use as variable names.<br />

Simulating Error and Accept in Actions<br />

The parsing actions of error and accept can be simulated in an action by use of the<br />

macros YYACCEPT and YYERROR. YYACCEPT causes yyparse to return the value 0;<br />

YYERROR causes the parser to behave as if the current input symbol had been a syntax<br />

error; yyerror is called, and error recovery takes place. These mechanisms can be used<br />

to simulate parsers with multiple endmarkers or context-sensitive syntax checking.<br />

10-26

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

Saved successfully!

Ooh no, something went wrong!