Intel XENIX 286 Programmers Guide (86) - Tenox.tc
Intel XENIX 286 Programmers Guide (86) - Tenox.tc Intel XENIX 286 Programmers Guide (86) - Tenox.tc
XENIX Programming yacc: Compiler-Compiler An Advanced Example This section gives an example of a grammar using some of the advanced features discussed in earlier sections. The desk calculator example is modified to provide a desk calculator that does floating-point interval arithmetic. The calculator understands floating-point constants, the arithmetic operations +, -, *, /, unary -, and = (assignment), and has 26 floating-point variables, a through z. Moreover, it also understands intervals, written (X ' y) where x is less than or equal to y. There are 26 interval valued variables A through Z that may also be used. Assignments return no value, and print nothing, while expressions print the (floating or interval) value. This example explores a nu mber of interesting features of yacc and C. Intervals are represented by a structure, consisting of the left and right endpoint values, stored as double precision values. This structure is given a type name, INTERVAL, by using typedef. The yacc value stack can also contain floating-point scalars and integers (used to index into the arrays holding the variable values). Notice that this entire strategy depends strongly on being able to assign structures and unions in C. In fact, many of the actions call functions that return structures as well. It is also worth noting the use of YYERROR to handle the following error conditions: division by an interval containing 0 and an interval presented in the wrong order. In effect, the error recovery mechanism of yacc is used to throw away the rest of the offending line. In addition to the mixing of types on the value stack, this grammar also demonstrates an interesting use of syntax to keep track of the type (e.g., scalar or interval) of intermediate expressions. Note that a scalar can be automatically promoted to an interval if the context demands an interval value. This causes a large number of conflicts when the grammar is run through yacc: 18 shift/reduce and 26 reduce/reduce. The problem can be seen by looking at the two input lines: and 2.5 +(3.5-4.) 2.5 + ( 3.5 , 4. ) Notice that the 2.5 is to be used in an interval valued expression in the second example, but this fact is not known until the comma (,) is read; by this time, 2.5 is finished, and the parser cannot go back and change its mind. More generally, it might be necessary to look ahead an arbitrary number of tokens to decide whether to convert a scalar to an interval. This problem is circumvented by having two rules for each binary interval valued operator: one when the left operand is a scalar, and one when the left operand is an interval. In the second case, the right operand must be an interval, so the conversion will be applied automatically. However, there are still many cases where the conversion may be applied or not, leading to the above conflicts. They are resolved by listing the rules that yield scalars first in the specification file; in this way, the conflicts will be resolved in the direction of keeping scalar valued expressions scalar valued until they are forced to become intervals. 10-33
yacc: Compiler-Compiler XENIX Programming This way of handling multiple types is very instructive, but not very general. If there were many kinds of expression types, instead of just two, the number of rules needed would increase dramatically, and the conflicts even more dramatically. Thus, while this example is instructive, it is better practice in a more normal programm ing language environment to keep the type information as part of the value, and not as part of the grammar. Finally, a word about the lexical analysis. The only unusual feature is the treat ment of floating-point constants. The C library routine atof is used to do the actual conversion from a character string to a double precision value. If the lexical analyzer detects an error, it responds by returning a token that is illegal in the grammar, provoking a syntax error in the parser, and thence error recovery. 10-34 I* advanced desk calculator parser specification *I % { # include # include typedef struct interval { double lo, hi; } INTERVAL; INTERVAL vmul(), vd iv(); double atof(); double dreg[ 26 ]; INTERVAL vreg[ 26 ]; % } %start lines %union { int ivai; double dval; INTERVAL vval ; } %token DREG VREG I* indices into dreg, vreg arrays *I %token CONST I* floating-point constant *I %type dexp I* expression *I %type vexp I* interval expression *I
- Page 184 and 185: XENIX Programming lex: Lexical Anal
- Page 186 and 187: XENIX Programming lex: Lexical Anal
- Page 188 and 189: XENIX Programming lex: Lexical Anal
- Page 190 and 191: XENIX Programming lex: Lexical Anal
- Page 192 and 193: XENIX Programm ing lex: Lexical Ana
- Page 194 and 195: XENIX Programming Specifying Source
- Page 196 and 197: XENIX Programming lex: Lexical Anal
- Page 198 and 199: XENIX Programming lex: Lexical Anal
- Page 200 and 201: XENIX Programming The definitions s
- Page 202 and 203: CHAPTER 10 yacc: COMPILER-COMPILER
- Page 204 and 205: XENIX Programming yacc: Compiler-Co
- Page 206 and 207: XENIX Programming yacc: Compiler-Co
- Page 208 and 209: XENIX Programming yacc: Compiler-Co
- Page 210 and 211: XENIX Programming yacc: Compiler-Co
- Page 212 and 213: XENIX Programming yacc: Compiler-Co
- Page 214 and 215: XENIX Programming yacc: Compiler-Co
- Page 216 and 217: XENIX Programming yacc: Compiler-Co
- Page 218 and 219: XENIX Programming yacc: Compiler-Co
- Page 220 and 221: XENIX Programming yacc: Compiler-Co
- Page 222 and 223: XENIX Programming yacc: Compiler-Co
- Page 224 and 225: XENIX Programming yacc: Compiler-Co
- Page 226 and 227: XENIX Programming yacc: Compiler-Co
- Page 228 and 229: XENIX Programming yacc: Compiler-Co
- Page 230 and 231: XENIX Programming yacc: Compiler-Co
- Page 232 and 233: XENIX Programming yacc: Compiler-Co
- Page 236 and 237: XENIX Programming %left %left %left
- Page 238 and 239: XENIX Programming I* lexical analys
- Page 240: XENIX Programming yacc: Compiler-Co
- Page 243 and 244: m4: Macro Processor XENIX Programmi
- Page 245 and 246: m4: Macro Processor XENIX Programmi
- Page 247 and 248: m4: Macro Processor XENIX Programm
- Page 249 and 250: m4: Macro Processor XENIX Programmi
- Page 251 and 252: m4: Macro Processor XENIX Programmi
- Page 253 and 254: C Language Portability XENIX Progra
- Page 255 and 256: C Language Portability XENIX Progra
- Page 257 and 258: C Language Portability XENIX Progra
- Page 259 and 260: C Language Portability XENIX Progra
- Page 261 and 262: C Language Portability XENIX Progra
- Page 263 and 264: C Language Portability XENIX Progra
- Page 266 and 267: APPENDIX B PROG RAMMING COMMANDS Th
- Page 268 and 269: XENIX Programming ad b (continued)
- Page 270 and 271: XENIX Programming Programming Comma
- Page 272 and 273: XENIX Programming Programming Comma
- Page 274 and 275: XENIX Programming Programming Comma
- Page 276 and 277: XENIX Programming Programming Comma
- Page 278 and 279: XENIX Programming Programming Comma
- Page 280 and 281: XENIX Programming Programming Comma
- Page 282 and 283: XENIX Programming Programming Comma
<strong>XENIX</strong> Programming yacc: Compiler-Compiler<br />
An Advanced Example<br />
This section gives an example of a grammar using some of the advanced features<br />
discussed in earlier sections. The desk calculator example is modified to provide a desk<br />
calculator that does floating-point interval arithmetic. The calculator understands<br />
floating-point constants, the arithmetic operations +, -, *, /, unary -, and = (assignment),<br />
and has 26 floating-point variables, a through z. Moreover, it also understands intervals,<br />
written<br />
(X ' y)<br />
where x is less than or equal to y. There are 26 interval valued variables A through Z<br />
that may also be used. Assignments return no value, and print nothing, while<br />
expressions print the (floating or interval) value.<br />
This example explores a nu mber of interesting features of yacc and C. Intervals are<br />
represented by a structure, consisting of the left and right endpoint values, stored as<br />
double precision values. This structure is given a type name, INTERVAL, by using<br />
typedef. The yacc value stack can also contain floating-point scalars and integers (used<br />
to index into the arrays holding the variable values). Notice that this entire strategy<br />
depends strongly on being able to assign structures and unions in C. In fact, many of the<br />
actions call functions that return structures as well.<br />
It is also worth noting the use of YYERROR to handle the following error conditions:<br />
division by an interval containing 0 and an interval presented in the wrong order. In<br />
effect, the error recovery mechanism of yacc is used to throw away the rest of the<br />
offending line.<br />
In addition to the mixing of types on the value stack, this grammar also demonstrates an<br />
interesting use of syntax to keep track of the type (e.g., scalar or interval) of<br />
intermediate expressions. Note that a scalar can be automatically promoted to an<br />
interval if the context demands an interval value. This causes a large number of<br />
conflicts when the grammar is run through yacc: 18 shift/reduce and 26 reduce/reduce.<br />
The problem can be seen by looking at the two input lines:<br />
and<br />
2.5 +(3.5-4.)<br />
2.5 + ( 3.5 , 4. )<br />
Notice that the 2.5 is to be used in an interval valued expression in the second example,<br />
but this fact is not known until the comma (,) is read; by this time, 2.5 is finished, and<br />
the parser cannot go back and change its mind. More generally, it might be necessary to<br />
look ahead an arbitrary number of tokens to decide whether to convert a scalar to an<br />
interval. This problem is circumvented by having two rules for each binary interval<br />
valued operator: one when the left operand is a scalar, and one when the left operand is<br />
an interval. In the second case, the right operand must be an interval, so the conversion<br />
will be applied automatically. However, there are still many cases where the conversion<br />
may be applied or not, leading to the above conflicts. They are resolved by listing the<br />
rules that yield scalars first in the specification file; in this way, the conflicts will be<br />
resolved in the direction of keeping scalar valued expressions scalar valued until they<br />
are forced to become intervals.<br />
10-33