📄 yacc.html
字号:
<i>yylval</i>.<p>If the parser and<i>yylex()</i>do not agree on these token numbers,reliable communication between them cannot occur.For (one character) literals, the token is simply the numericvalue of the character in the current character set.The numbers for other tokens can either be chosen by<i>yacc</i>,or chosen by the user.In either case, the<b>#define</b>construct of C is used to allow<i>yylex()</i>to return these numbers symbolically.The<b>#define</b>statements are put intothe code file, and the header fileif that file is requested.The set of characters permitted by<i>yacc</i>in an identifier is largerthan that permitted by C.Token names found to contain such characterswill not be included in the<b>#define</b>declarations.<p>If the token numbers are chosen by<i>yacc</i>,the tokens other than literals will be assignednumbers greater than 256,although no order is implied.A token can be explicitly assigned a number by followingits first appearance in thedeclarations section with a number.Names and literals not defined this way retain their default definition.All assigned token numbers will be unique and distinct from thetoken numbers used for literals.If duplicate token numberscause conflicts in parser generation,<i>yacc</i>will report an error;otherwise, it is unspecified whether the token assignment isaccepted or an error is reported.<p>The end of the input is marked by a special token called the<i>endmarker ,</i>which has a token number that is zero or negative.(These values are invalid for any other token.)All lexical analysers will return zero or negative as a token numberupon reaching the end of their input.If the tokens up to, but excluding, the endmarker form a structure thatmatches the start symbol, the parser will accept the input.If the endmarker is seen in any other context, it will be considered an error.<h5><a name = "tag_001_014_2954_010"> </a>Completing the Program</h5>In addition to<i>yyparse()</i>and<i>yylex()</i>,the functions<i>yyerror()</i>and<i>main()</i>are required to make a complete program.The application can supply<i>main()</i>and<i>yyerror()</i>,or those routines can be obtained from the<i>yacc</i>library.<h5><a name = "tag_001_014_2954_011"> </a>Yacc Library</h5><xref type="5" name="yacclib"></xref>The following functions appear only in the<i>yacc</i>library accessible through the<b>-l y</b>operand to<i><a href="cc.html">cc</a></i>or<i><a href="c89.html">c89</a></i>;they can therefore be redefined by a portable application:<dl compact><dt><b>int main(void)</b><dd>This function will call<i>yyparse()</i>and exit with an unspecified value.Other actions within this function are unspecified.<dt><b>int yyerror(const char *<i>s</i></b><dd>This functionwill write the NUL-terminated argument to standard error, followed by anewline character.</dl><p>The order of the<b>-l y</b>and<b>-l l</b>operands given to<i><a href="cc.html">cc</a></i>or<i><a href="c89.html">c89</a></i>is significant;the application must either provide its own<i>main()</i>function or ensure that<b>-l y</b>precedes<b>-l l</b>.<h5><a name = "tag_001_014_2954_012"> </a>Debugging the Parser</h5>The parser generated by<i>yacc</i>will have diagnostic facilities in it that can beoptionally enabled at either compile time or at run time (if enabledat compile time).The compilation of the runtime debugging code is under the control ofYYDEBUG,a preprocessor symbol.IfYYDEBUGhas a non-zero value, the debugging code will be included.If its value is zero, the code will not be included.<p>In parsers where the debugging code has been included, the externalint yydebugcan be used to turn debugging on (with a non-zero value) and off (zero value)at run time.The initial value of<i>yydebug</i>will be zero.<p>When<b>-t</b>is specified,the code file will be built such that, ifYYDEBUGis not already defined at compilation time (using the<i><a href="c89.html">c89</a></i><b>-D</b><b>YYDEBUG</b>option, for example),YYDEBUGwill be set explicitly to 1.When<b>-t</b>is not specified,the code file will be built such that, ifYYDEBUGis not already defined, it will be set explicitly to zero.<p>The format of the debugging output is unspecified butincludes at least enough information to determine the shift and reduce actions,and the input symbols.It also provides informationabout error recovery.<h5><a name = "tag_001_014_2954_013"> </a>Algorithms</h5><xref type="5" name="yaccalg"></xref>The parser constructed by<i>yacc</i>implements anLALR(1)parsing algorithm asdocumented in the literature.It is unspecified whether the parser is table-drivenor direct-coded.<p>A parser generated by<b>yacc</b>will never request an input symbol from<i>yylex()</i>while in a state where the only actions other thanthe error action are reductions by a single rule.<p>The literature of parsing theory defines these concepts.<h5><a name = "tag_001_014_2954_014"> </a>Limits</h5>The<i>yacc</i>utility may have several internal tables.The minimum maximums for these tables are shown inthe following table.The exact meaning of these values is implementation-dependent.The implementation will define the relationship between thesevalues and between them and any error messages that theimplementation may generate should it run out of space for anyinternal structure.An implementation may combine groups of theseresources into a single pool as long as the total available tothe user does not fall below thesum of the sizes specified by this section.<pre><table bordercolor=#000000 border=1 align=center><tr valign=top><th align=center><b>Limit</b><th align=center><b>Minimum<br>Maximum</b><th align=center><b>Description</b><tr valign=top><td align=left>{NTERMS}<td align=left>126<td align=left>Number of tokens.<tr valign=top><td align=left>{NNONTERM}<td align=left>200<td align=left>Number of non-terminals.<tr valign=top><td align=left>{NPROD}<td align=left>300<td align=left>Number of rules.<tr valign=top><td align=left>{NSTATES}<td align=left>600<td align=left>Number of states.<tr valign=top><td align=left>{MEMSIZE}<td align=left>5200<td align=left> Length of rules. The total length, in names (tokens and non-terminals), of all the rules of the grammar. The left-hand side is counted for each rule, even if it is not explicitly repeated, as specified in <tr valign=top><td align=left>{ACTSIZE}<td align=left>4000<td align=left> Number of actions. "Actions" here (and in the description file) refer to parser actions (shift, reduce, and so on) not to semantic actions defined in </table></pre><h6 align=center><xref table="Internal Limits in <I>yacc</i>"></xref>Table: Internal Limits in <i>yacc</i></h6><p></blockquote><h4><a name = "tag_001_014_2955"> </a>EXIT STATUS</h4><blockquote>The following exit values are returned:<dl compact><dt>0<dd>Successful completion.<dt>>0<dd>An error occurred.</dl></blockquote><h4><a name = "tag_001_014_2956"> </a>CONSEQUENCES OF ERRORS</h4><blockquote>If any errors are encountered, the run is aborted and<i>yacc</i>exits with a non-zero status.Partial code files and header files files may be produced.The summary information in the description filewill always be produced if the<b>-v</b>flag is present.</blockquote><h4><a name = "tag_001_014_2957"> </a>APPLICATION USAGE</h4><blockquote>Historical implementations experience name conflicts on the namesyacc.tmp,yacc.acts,yacc.debug,y.tab.c,y.tab.handy.outputif morethan one copy of<i>yacc</i>is running in a single directory at one time.The<b>-b</b>option was added to overcome this problem.The related problem of allowing multiple<i>yacc</i>parsers to be placed in the same file was addressedby adding a<b>-p</b>option to override the previously hard-codedyyvariable prefix.<p>The description of the<b>-p</b>option specifies the minimal set of function and variable namesthat cause conflict when multiple parsers are linked together.YYSTYPEdoes not need to be changed.Instead, the programmer can use<b>-b</b>to give theheader files for different parsers different names, and then the filewith the<i>yylex()</i>for a given parser can include the header for that parser.Names such as<i>yyclearerr</i>do not need to be changed becausethey are used only in the actions;they do not have linkage.It is possible that an implementation will have other names,either internal ones for implementing things such as<i>yyclearerr</i>,or providing non-standard features that it wants to change with<b>-p</b>.<p>Unary operators that are the same token as a binary operator in generalneed their precedence adjusted.This is handled by the<b>%prec</b>advisory symbol associated with the particular grammar rule definingthat unary operator.See<xref href=yaccgram><a href="#tag_001_014_2954_004">Grammar Rules in yacc</a></xref>.Applications are not required to use this operator for unaryoperators, but the grammars that do not require it are rare.<br></blockquote><h4><a name = "tag_001_014_2958"> </a>EXAMPLES</h4><blockquote>Access to the<i>yacc</i>library is obtained with library search operands to<i><a href="cc.html">cc</a></i>or<i><a href="c89.html">c89</a></i>.To use the<i>yacc</i>library<i>main()</i>:<pre><code>c89 y.tab.c -l y</code></pre><p>Both the<i><a href="lex.html">lex</a></i>library and the<i>yacc</i>library contain<i>main()</i>.To access the<i>yacc</i><i>main()</i>:<pre><code>c89 y.tab.c lex.yy.c -l y -l l</code></pre><br>This ensures that the<i>yacc</i>library is searched first, so that its<i>main()</i>is used.<p>The historical<i>yacc</i>libraries have contained two simple functions that arenormally coded by the application programmer.These library functions are similar to the following code:<pre><code>#include <locale.h>int main(void){ extern int yyparse(); setlocale(LC_ALL, ""); /* If the following parser is one created by lex, the application must be careful to ensure that LC_CTYPE and LC_COLLATE are set to the POSIX locale. */ (void) yyparse(); return (0);}#include <stdio.h>int yyerror(const char *msg){ (void) fprintf(stderr, "%s\n", msg); return (0);}</code></pre></blockquote><h4><a name = "tag_001_014_2959"> </a>FUTURE DIRECTIONS</h4><blockquote>The IEEE PASC 1003.2 Interpretations Committee has forwarded concerns aboutparts of this interface definition to the IEEE PASC Shell and Utilities Working Groupwhich is identifying the corrections.A future revision of this specification will align withIEEE Std. 1003.2b when finalised.</blockquote><h4><a name = "tag_001_014_2960"> </a>SEE ALSO</h4><blockquote><i><a href="cc.html">cc</a></i>,<i><a href="c89.html">c89</a></i>,<i><a href="lex.html">lex</a></i>.</blockquote><hr size=2 noshade><center><font size=2>UNIX ® is a registered Trademark of The Open Group.<br>Copyright © 1997 The Open Group<br> [ <a href="../index.html">Main Index</a> | <a href="../xshix.html">XSH</a> | <a href="../xcuix.html">XCU</a> | <a href="../xbdix.html">XBD</a> | <a href="../cursesix.html">XCURSES</a> | <a href="../xnsix.html">XNS</a> ]</font></center><hr size=2 noshade></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -