⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 bison_7.htm

📁 Lex和Yacc的Manual
💻 HTM
📖 第 1 页 / 共 2 页
字号:
<A NAME="IDX130"></A>If you use a reentrant parser, you can optionally pass additionalparameter information to it in a reentrant way.  To do so, define themacro <CODE>YYPARSE_PARAM</CODE> as a variable name.  This modifies the<CODE>yyparse</CODE> function to accept one argument, of type <CODE>void *</CODE>,with that name.</P><P>When you call <CODE>yyparse</CODE>, pass the address of an object, casting theaddress to <CODE>void *</CODE>.  The grammar actions can refer to the contentsof the object by casting the pointer value back to its proper type andthen dereferencing it.  Here's an example.  Write this in the parser:</P><PRE>%{struct parser_control{  int nastiness;  int randomness;};#define YYPARSE_PARAM parm%}</PRE><P>Then call the parser like this:</P><PRE>struct parser_control{  int nastiness;  int randomness;};...{  struct parser_control foo;  ...  /* Store proper data in <CODE>foo</CODE>.  */  value = yyparse ((void *) &#38;foo);  ...}</PRE><P>In the grammar actions, use expressions like this to refer to the data:</P><PRE>((struct parser_control *) parm)-&#62;randomness</PRE><P><A NAME="IDX131"></A>If you wish to pass the additional parameter data to <CODE>yylex</CODE>,define the macro <CODE>YYLEX_PARAM</CODE> just like <CODE>YYPARSE_PARAM</CODE>, asshown here:</P><PRE>%{struct parser_control{  int nastiness;  int randomness;};#define YYPARSE_PARAM parm#define YYLEX_PARAM parm%}</PRE><P>You should then define <CODE>yylex</CODE> to accept one additionalargument--the value of <CODE>parm</CODE>.  (This makes either two or threearguments in total, depending on whether an argument of type<CODE>YYLTYPE</CODE> is passed.)  You can declare the argument as a pointer tothe proper object type, or you can declare it as <CODE>void *</CODE> andaccess the contents as shown above.</P><P>You can use <SAMP>`%pure_parser'</SAMP> to request a reentrant parser withoutalso using <CODE>YYPARSE_PARAM</CODE>.  Then you should call <CODE>yyparse</CODE>with no arguments, as usual.</P><H2><A NAME="SEC66" HREF="index.html#SEC66">The Error Reporting Function <CODE>yyerror</CODE></A></H2><P><A NAME="IDX132"></A><A NAME="IDX133"></A><A NAME="IDX134"></A><A NAME="IDX135"></A></P><P>The Bison parser detects a <STRONG>parse error</STRONG> or <STRONG>syntax error</STRONG>whenever it reads a token which cannot satisfy any syntax rule.  Aaction in the grammar can also explicitly proclaim an error, using themacro <CODE>YYERROR</CODE> (see section <A HREF="bison_7.html#SEC67">Special Features for Use in Actions</A>).</P><P>The Bison parser expects to report the error by calling an errorreporting function named <CODE>yyerror</CODE>, which you must supply.  It iscalled by <CODE>yyparse</CODE> whenever a syntax error is found, and itreceives one argument.  For a parse error, the string is normally<CODE>"parse error"</CODE>.</P><P><A NAME="IDX136"></A>If you define the macro <CODE>YYERROR_VERBOSE</CODE> in the Bison declarationssection (see section <A HREF="bison_6.html#SEC37">The Bison Declarations Section</A>), then Bison provides a more verboseand specific error message string instead of just plain <CODE>"parseerror"</CODE>.  It doesn't matter what definition you use for<CODE>YYERROR_VERBOSE</CODE>, just whether you define it.</P><P>The parser can detect one other kind of error: stack overflow.  Thishappens when the input contains constructions that are very deeplynested.  It isn't likely you will encounter this, since the Bisonparser extends its stack automatically up to a very large limit.  Butif overflow happens, <CODE>yyparse</CODE> calls <CODE>yyerror</CODE> in the usualfashion, except that the argument string is <CODE>"parser stackoverflow"</CODE>.</P><P>The following definition suffices in simple programs:</P><PRE>yyerror (s)     char *s;{  fprintf (stderr, "%s\n", s);}</PRE><P>After <CODE>yyerror</CODE> returns to <CODE>yyparse</CODE>, the latter will attempterror recovery if you have written suitable error recovery grammar rules(see section <A HREF="bison_9.html#SEC81">Error Recovery</A>).  If recovery is impossible, <CODE>yyparse</CODE> willimmediately return 1.</P><P><A NAME="IDX137"></A>The variable <CODE>yynerrs</CODE> contains the number of syntax errorsencountered so far.  Normally this variable is global; but if yourequest a pure parser (see section <A HREF="bison_6.html#SEC56">A Pure (Reentrant) Parser</A>) then it is a local variablewhich only the actions can access.</P><H2><A NAME="SEC67" HREF="index.html#SEC67">Special Features for Use in Actions</A></H2><P><A NAME="IDX138"></A><A NAME="IDX139"></A></P><P>Here is a table of Bison constructs, variables and macros thatare useful in actions.</P><DL COMPACT><DT><SAMP>`$$'</SAMP><DD>Acts like a variable that contains the semantic value for thegrouping made by the current rule.  See section <A HREF="bison_6.html#SEC46">Actions</A>.<DT><SAMP>`$<VAR>n</VAR>'</SAMP><DD>Acts like a variable that contains the semantic value for the<VAR>n</VAR>th component of the current rule.  See section <A HREF="bison_6.html#SEC46">Actions</A>.<DT><SAMP>`$&#60;<VAR>typealt</VAR>&#62;$'</SAMP><DD>Like <CODE>$$</CODE> but specifies alternative <VAR>typealt</VAR> in the unionspecified by the <CODE>%union</CODE> declaration.  See section <A HREF="bison_6.html#SEC47">Data Types of Values in Actions</A>.<DT><SAMP>`$&#60;<VAR>typealt</VAR>&#62;<VAR>n</VAR>'</SAMP><DD>Like <CODE>$<VAR>n</VAR></CODE> but specifies alternative <VAR>typealt</VAR> in theunion specified by the <CODE>%union</CODE> declaration.  See section <A HREF="bison_6.html#SEC47">Data Types of Values in Actions</A>.<DT><SAMP>`YYABORT;'</SAMP><DD>Return immediately from <CODE>yyparse</CODE>, indicating failure.See section <A HREF="bison_7.html#SEC60">The Parser Function <CODE>yyparse</CODE></A>.<DT><SAMP>`YYACCEPT;'</SAMP><DD>Return immediately from <CODE>yyparse</CODE>, indicating success.See section <A HREF="bison_7.html#SEC60">The Parser Function <CODE>yyparse</CODE></A>.<DT><SAMP>`YYBACKUP (<VAR>token</VAR>, <VAR>value</VAR>);'</SAMP><DD><A NAME="IDX140"></A>Unshift a token.  This macro is allowed only for rules that reducea single value, and only when there is no look-ahead token.It installs a look-ahead token with token type <VAR>token</VAR> andsemantic value <VAR>value</VAR>; then it discards the value that wasgoing to be reduced by this rule.If the macro is used when it is not valid, such as when there isa look-ahead token already, then it reports a syntax error witha message <SAMP>`cannot back up'</SAMP> and performs ordinary errorrecovery.In either case, the rest of the action is not executed.<DT><SAMP>`YYEMPTY'</SAMP><DD><A NAME="IDX141"></A>Value stored in <CODE>yychar</CODE> when there is no look-ahead token.<DT><SAMP>`YYERROR;'</SAMP><DD><A NAME="IDX142"></A>Cause an immediate syntax error.  This statement initiates errorrecovery just as if the parser itself had detected an error; however, itdoes not call <CODE>yyerror</CODE>, and does not print any message.  If youwant to print an error message, call <CODE>yyerror</CODE> explicitly beforethe <SAMP>`YYERROR;'</SAMP> statement.  See section <A HREF="bison_9.html#SEC81">Error Recovery</A>.<DT><SAMP>`YYRECOVERING'</SAMP><DD>This macro stands for an expression that has the value 1 when the parseris recovering from a syntax error, and 0 the rest of the time.See section <A HREF="bison_9.html#SEC81">Error Recovery</A>.<DT><SAMP>`yychar'</SAMP><DD>Variable containing the current look-ahead token.  (In a pure parser,this is actually a local variable within <CODE>yyparse</CODE>.)  When there isno look-ahead token, the value <CODE>YYEMPTY</CODE> is stored in the variable.See section <A HREF="bison_8.html#SEC69">Look-Ahead Tokens</A>.<DT><SAMP>`yyclearin;'</SAMP><DD>Discard the current look-ahead token.  This is useful primarily inerror rules.  See section <A HREF="bison_9.html#SEC81">Error Recovery</A>.<DT><SAMP>`yyerrok;'</SAMP><DD>Resume generating error messages immediately for subsequent syntaxerrors.  This is useful primarily in error rules.  See section <A HREF="bison_9.html#SEC81">Error Recovery</A>.<DT><SAMP>`@<VAR>n</VAR>'</SAMP><DD><A NAME="IDX143"></A>Acts like a structure variable containing information on the linenumbers and column numbers of the <VAR>n</VAR>th component of the currentrule.  The structure has four members, like this:<PRE>struct {  int first_line, last_line;  int first_column, last_column;};</PRE>Thus, to get the starting line number of the third component, use<SAMP>`@3.first_line'</SAMP>.In order for the members of this structure to contain valid information,you must make <CODE>yylex</CODE> supply this information about each token.If you need only certain members, then <CODE>yylex</CODE> need only fill inthose members.The use of this feature makes the parser noticeably slower.</DL><HR>Go to the <A HREF="bison_1.html">first</A>, <A HREF="bison_6.html">previous</A>, <A HREF="bison_8.html">next</A>, <A HREF="bison_15.html">last</A> section, <A HREF="index.html">table of contents</A>.</BODY></HTML>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -