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

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

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

yacc: Compiler-Compiler <strong>XENIX</strong> Programming<br />

In these rules, IF and ELSE are tokens, cond is a nonterminal sy mbol describing<br />

conditional (logical) expressions, and stat is a nonterminal symbol describing statements.<br />

The first rule will be called the simple-if rule, and the second the if-else rule.<br />

These two rules form an ambiguous construction, since input of the form<br />

IF (C1 )IF(C2)51 ELSE 52<br />

can be structured according to these rules in two ways:<br />

or<br />

IF(C1 ){<br />

IF ( C2 ) 51<br />

}<br />

ELSE 52<br />

IF(C1 ) {<br />

IF ( C2 ) 51<br />

ELSE 52<br />

}<br />

The second interpretation is the one given in most programm ing languages having this<br />

construct. Each ELSE is associated with the last IF immediately preceding the ELSE.<br />

In this example, consider the situation where the parser has seen<br />

I F ( C 1 ) I F ( C2 ) 5 1<br />

and is looking at the ELSE. It can immediately reduce by the simple-if rule to get<br />

IF ( C1 ) stat<br />

and then read the remaining input<br />

ELSE 52<br />

and reduce<br />

IF ( C1 ) stat ELSE 52<br />

by the if-else rule. This leads to the first of the above groupings of the input.<br />

On the other hand, the ELSE may be shifted, S2 read, and then the right-hand portion of<br />

IF ( C 1 ) IF ( C2 ) .S 1 ELSE 52<br />

can be reduced by the if-else rule to get<br />

IF ( C1 ) stat<br />

which can be reduced by the simple-if rule. This leads to the second of the above<br />

groupings of the input, which is usually desired.<br />

10-16

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

Saved successfully!

Ooh no, something went wrong!