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 A sample specification is given in a later section. The facilities in this subsection are not triggered until they are used: in particular, the use of 96type will turn on these mechanisms. When they are used, there is a fairly strict level of checking. For example, use of $ or $$ to refer to something with no defined type is diagnosed. If these facilities are not triggered, the yacc value stack is used to hold int's, as was true historically. A Small Desk Calcu lator This example gives the complete yacc specification for a small desk calculator: the desk calculator has 26 registers, labeled a through z, and accepts arithmetic expressions made up of the operators +, -, *, /, % (mod operator), & (bitwise and), I (bitwise or), and assignment. If an expression at the top level is an assignment, the value is not printed; otherwise it is. As in C, an integer that begins with 0 (zero) is assumed to be octal; otherwise, it is assumed to be decimal. As an example of a yacc specification, the desk calculator does a reasonable job of showing how precedences and ambiguities are used and demonstrating simple error recovery. The major oversimplifications are that the lexical analysis phase is much simpler than for most applications, and the output is produced immediately, line by line. Note the way that decimal and octal integers are read in by the grammar rules; this job is probably better done by the lexical analyzer. I* desk calculator parser specification *I %{ # include # include int regs[26]; int base; % } %start list %token DIGIT LETTER %left 'I' %left '&' %left I + I I-I %left'*' 'I' '%' %left UMINUS I* precedence for unary minus *I %% I* beginning of rules section *I list : I* empty *I I list stat '\n' I list error '\n' { yyerrok; } 10-29
yacc: Compiler-Compiler 10-30 stat : expr { printf( "%d\n" , $1 ); } I LETTER ' = ' expr { regs[$ 1] = $3; } expr : '(' expr ')' { $$ = $2; } I expr ' + ' expr { $$ = $1 + $3; } I expr '-' expr {$$ : $1 -$3; } I expr '*' expr { $$ = $1 * $3; } I expr '!' expr { $$ = $1 1$3; } I expr '%' expr { $$ = $1 % $3; } I expr '&' expr { $$ = $1 & $3; } I expr 'I' expr { $$ = $1 I $3; } I '-' expr %prec UMINUS { $$ = -$2; } I LETTER { $$ = regs[$1]; } I number number : DIGIT { $$ = $1; base = ($1 = = 0) ? 8 : 1 0; } I number DIGIT { $$ = base * $ 1 + $2; } %% I* start of programs * I yylex() { I* lexical analysis routine *I I* returns LETTER for a lowercase letter, *I I* yylval = 0 through 25 *I I* return DIGIT for a digit, */ !* yylval = 0 through 9 *I I* all other characters *I I* are returned immediately */ int c; while( (c = getchar( )) = = ' ') {I* skip blanks *I } !* cis now nonblank *I XENIX Programming
- Page 180 and 181: XENIX Programming lex: Lexical Anal
- Page 182 and 183: XENIX Programming lex: Lexical Anal
- 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 232 and 233: XENIX Programming yacc: Compiler-Co
- Page 234 and 235: 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
<strong>XENIX</strong> Programming yacc: Compiler-Compiler<br />
A sample specification is given in a later section. The facilities in this subsection are<br />
not triggered until they are used: in particular, the use of 96type will turn on these<br />
mechanisms. When they are used, there is a fairly strict level of checking. For<br />
example, use of $ or $$ to refer to something with no defined type is diagnosed. If these<br />
facilities are not triggered, the yacc value stack is used to hold int's, as was true<br />
historically.<br />
A Small Desk Calcu lator<br />
This example gives the complete yacc specification for a small desk calculator: the<br />
desk calculator has 26 registers, labeled a through z, and accepts arithmetic expressions<br />
made up of the operators +, -, *, /, % (mod operator), & (bitwise and), I (bitwise or), and<br />
assignment. If an expression at the top level is an assignment, the value is not printed;<br />
otherwise it is. As in C, an integer that begins with 0 (zero) is assumed to be octal;<br />
otherwise, it is assumed to be decimal.<br />
As an example of a yacc specification, the desk calculator does a reasonable job of<br />
showing how precedences and ambiguities are used and demonstrating simple error<br />
recovery. The major oversimplifications are that the lexical analysis phase is much<br />
simpler than for most applications, and the output is produced immediately, line by line.<br />
Note the way that decimal and octal integers are read in by the grammar rules; this job<br />
is probably better done by the lexical analyzer.<br />
I* desk calculator parser specification *I<br />
%{<br />
# include <br />
# include <br />
int regs[26];<br />
int base;<br />
% }<br />
%start list<br />
%token DIGIT LETTER<br />
%left 'I'<br />
%left '&'<br />
%left I + I I-I<br />
%left'*' 'I' '%'<br />
%left UMINUS I* precedence for unary minus *I<br />
%% I* beginning of rules section *I<br />
list : I* empty *I<br />
I list stat '\n'<br />
I list error '\n'<br />
{ yyerrok; }<br />
10-29