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 If there are several grammar rules with the same left-hand side, then the vertical bar (I) can be used to avoid rewriting the left-hand side. In addition, the sem icolon at the end of a rule can be dropped before a vertical bar. Thus the gram mar rules A B C D A E F A G ; can be given to yacc as A : B C D I E F I G It is not necessary that all gram mar rules with the same left side appear together in the grammar rules section, although it makes the input much more readable and easier to change. If a nonterminal symbol matches the empty string, this can be indicated in the obvious way: empty : ; Names representing tokens must be declared; this is most simply done by writing %token name 1 name2 ... in the declarations section. Every nonterminal symbol must appear on the left side of at least one rule. Of all the nonterminal symbols, one, called the start symbol, has particular importance. The parser is designed to recognize the start symbol; thus, this symbol represents the largest, most general structure described by the grammar rules. By default, the start symbol is taken to be the left-hand side of the first grammar rule in the rules section. It is possible, and in fact desirable, to declare the start symbol explicitly in the declarations section using the %start keyword: %start symbol The end of the input to the parser is signaled by a special token called the endm arker. If the tokens up to, but not including, the endmarker form a structure that matches the start symbol, the parser function returns to its caller after the end marker is seen; it accepts the input. If the endmarker is seen in any other context, it is an error. It is the job of the user-supplied lexical analyzer to return the endmarker when appropriate. Usually the endmarker represents some reasonably obvious 1/0 status, such as the end of the file or end of the record. 10-5
yacc: Compiler-Compiler XENIX Programming Actions With each grammar rule, the user may associate actions to be performed each time the rule is recognized in the input process. These actions may return values and may obtain the values returned by previous actions. Moreover, the lexical analyzer can return values for tokens, if desired. An action is an arbitrary C statement and as such can do input and output, call subprograms, and alter external vectors and variables. An action is specified by one or more statements, enclosed in curly braces ({ and }). For example and A : '( ' 8 ') ' { hello( 1, "abc" ) ; } XXX : YYY ZZZ { printf(" a message\n"); flag = 25; } are gram mar rules with actions. To facilitate easy communication between the actions and the parser, the action statements are altered slightly. The dollar sign ($) is used as a signal to yacc in this context. To return a value, the action normally sets the pseudo-variable $$ to some value. For example, an action that does nothing but return the value 1 is { $$ = 1; } To obtain the values returned by previous actions and the lexical analyzer, the action may use the pseudo-variables $1, $2, ... , which refer to the values returned by the components of the right side of a rule, reading from left to right. Thus, if the rule is A : 8 C D ; for example, then $2 has the value returned by C and $3 the value returned by D. As a more concrete example, consider the rule expr : ' ( ' expr ' ) ' ; The value returned by this rule is usually the value of the expr in parentheses. This can be indicated by 10-6 expr : ' ( ' expr ' ) ' { $$ = $2 }
- Page 156 and 157: CHAPTER 8 csh : C SHEll The C shell
- Page 158 and 159: XENIX Programming csh: C Shell Some
- Page 160 and 161: XENIX Programm ing *w 32 * q % !c -
- Page 162 and 163: XENIX Programming csh: C Shell the
- Page 164 and 165: XENIX Programming csh: C Shell Usin
- Page 166 and 167: XENIX Programming csh: C Shell Usin
- Page 168 and 169: XENIX Programming csh: C Shell The
- Page 170 and 171: XENIX Programming csh: C Shell Note
- Page 172 and 173: XENIX Programming switch ( string c
- Page 174 and 175: XENIX Programming csh: C Shell Star
- Page 176 and 177: XENIX Programming csh: C Shell Spec
- Page 178 and 179: CHAPTER 9 lex : LEXICAL ANA LYZER G
- 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 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 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
<strong>XENIX</strong> Programming yacc: Compiler-Compiler<br />
If there are several grammar rules with the same left-hand side, then the vertical bar (I)<br />
can be used to avoid rewriting the left-hand side. In addition, the sem icolon at the end<br />
of a rule can be dropped before a vertical bar. Thus the gram mar rules<br />
A B C D<br />
A E F<br />
A G ;<br />
can be given to yacc as<br />
A : B C D<br />
I E F<br />
I G<br />
It is not necessary that all gram mar rules with the same left side appear together in the<br />
grammar rules section, although it makes the input much more readable and easier to<br />
change.<br />
If a nonterminal symbol ma<strong>tc</strong>hes the empty string, this can be indicated in the obvious<br />
way:<br />
empty : ;<br />
Names representing tokens must be declared; this is most simply done by writing<br />
%token name 1 name2 ...<br />
in the declarations section. Every nonterminal symbol must appear on the left side of at<br />
least one rule.<br />
Of all the nonterminal symbols, one, called the start symbol, has particular importance.<br />
The parser is designed to recognize the start symbol; thus, this symbol represents the<br />
largest, most general structure described by the grammar rules. By default, the start<br />
symbol is taken to be the left-hand side of the first grammar rule in the rules section.<br />
It is possible, and in fact desirable, to declare the start symbol explicitly in the<br />
declarations section using the %start keyword:<br />
%start symbol<br />
The end of the input to the parser is signaled by a special token called the endm arker.<br />
If the tokens up to, but not including, the endmarker form a structure that ma<strong>tc</strong>hes the<br />
start symbol, the parser function returns to its caller after the end marker is seen; it<br />
accepts the input. If the endmarker is seen in any other context, it is an error.<br />
It is the job of the user-supplied lexical analyzer to return the endmarker when<br />
appropriate. Usually the endmarker represents some reasonably obvious 1/0 status, such<br />
as the end of the file or end of the record.<br />
10-5