1*75897234Sdrh<html> 2*75897234Sdrh<head> 3*75897234Sdrh<title>The Lemon Parser Generator</title> 4*75897234Sdrh</head> 5*75897234Sdrh<body bgcolor=white> 6*75897234Sdrh<h1 align=center>The Lemon Parser Generator</h1> 7*75897234Sdrh 8*75897234Sdrh<p>Lemon is an LALR(1) parser generator for C or C++. 9*75897234SdrhIt does the same job as ``bison'' and ``yacc''. 10*75897234SdrhBut lemon is not another bison or yacc clone. It 11*75897234Sdrhuses a different grammar syntax which is designed to 12*75897234Sdrhreduce the number of coding errors. Lemon also uses a more 13*75897234Sdrhsophisticated parsing engine that is faster than yacc and 14*75897234Sdrhbison and which is both reentrant and thread-safe. 15*75897234SdrhFurthermore, Lemon implements features that can be used 16*75897234Sdrhto eliminate resource leaks, making is suitable for use 17*75897234Sdrhin long-running programs such as graphical user interfaces 18*75897234Sdrhor embedded controllers.</p> 19*75897234Sdrh 20*75897234Sdrh<p>This document is an introduction to the Lemon 21*75897234Sdrhparser generator.</p> 22*75897234Sdrh 23*75897234Sdrh<h2>Theory of Operation</h2> 24*75897234Sdrh 25*75897234Sdrh<p>The main goal of Lemon is to translate a context free grammar (CFG) 26*75897234Sdrhfor a particular language into C code that implements a parser for 27*75897234Sdrhthat language. 28*75897234SdrhThe program has two inputs: 29*75897234Sdrh<ul> 30*75897234Sdrh<li>The grammar specification. 31*75897234Sdrh<li>A parser template file. 32*75897234Sdrh</ul> 33*75897234SdrhTypically, only the grammar specification is supplied by the programmer. 34*75897234SdrhLemon comes with a default parser template which works fine for most 35*75897234Sdrhapplications. But the user is free to substitute a different parser 36*75897234Sdrhtemplate if desired.</p> 37*75897234Sdrh 38*75897234Sdrh<p>Depending on command-line options, Lemon will generate between 39*75897234Sdrhone and three files of outputs. 40*75897234Sdrh<ul> 41*75897234Sdrh<li>C code to implement the parser. 42*75897234Sdrh<li>A header file defining an integer ID for each terminal symbol. 43*75897234Sdrh<li>An information file that describes the states of the generated parser 44*75897234Sdrh automaton. 45*75897234Sdrh</ul> 46*75897234SdrhBy default, all three of these output files are generated. 47*75897234SdrhThe header file is suppressed if the ``-m'' command-line option is 48*75897234Sdrhused and the report file is omitted when ``-q'' is selected.</p> 49*75897234Sdrh 50*75897234Sdrh<p>The grammar specification file uses a ``.y'' suffix, by convention. 51*75897234SdrhIn the examples used in this document, we'll assume the name of the 52*75897234Sdrhgrammar file is ``gram.y''. A typical use of Lemon would be the 53*75897234Sdrhfollowing command: 54*75897234Sdrh<pre> 55*75897234Sdrh lemon gram.y 56*75897234Sdrh</pre> 57*75897234SdrhThis command will generate three output files named ``gram.c'', 58*75897234Sdrh``gram.h'' and ``gram.out''. 59*75897234SdrhThe first is C code to implement the parser. The second 60*75897234Sdrhis the header file that defines numerical values for all 61*75897234Sdrhterminal symbols, and the last is the report that explains 62*75897234Sdrhthe states used by the parser automaton.</p> 63*75897234Sdrh 64*75897234Sdrh<h3>Command Line Options</h3> 65*75897234Sdrh 66*75897234Sdrh<p>The behavior of Lemon can be modified using command-line options. 67*75897234SdrhYou can obtain a list of the available command-line options together 68*75897234Sdrhwith a brief explanation of what each does by typing 69*75897234Sdrh<pre> 70*75897234Sdrh lemon -? 71*75897234Sdrh</pre> 72*75897234SdrhAs of this writing, the following command-line options are supported: 73*75897234Sdrh<ul> 74*75897234Sdrh<li><tt>-b</tt> 75*75897234Sdrh<li><tt>-c</tt> 76*75897234Sdrh<li><tt>-g</tt> 77*75897234Sdrh<li><tt>-m</tt> 78*75897234Sdrh<li><tt>-q</tt> 79*75897234Sdrh<li><tt>-s</tt> 80*75897234Sdrh<li><tt>-x</tt> 81*75897234Sdrh</ul> 82*75897234SdrhThe ``-b'' option reduces the amount of text in the report file by 83*75897234Sdrhprinting only the basis of each parser state, rather than the full 84*75897234Sdrhconfiguration. 85*75897234SdrhThe ``-c'' option suppresses action table compression. Using -c 86*75897234Sdrhwill make the parser a little larger and slower but it will detect 87*75897234Sdrhsyntax errors sooner. 88*75897234SdrhThe ``-g'' option causes no output files to be generated at all. 89*75897234SdrhInstead, the input grammar file is printed on standard output but 90*75897234Sdrhwith all comments, actions and other extraneous text deleted. This 91*75897234Sdrhis a useful way to get a quick summary of a grammar. 92*75897234SdrhThe ``-m'' option causes the output C source file to be compatible 93*75897234Sdrhwith the ``makeheaders'' program. 94*75897234SdrhMakeheaders is a program that automatically generates header files 95*75897234Sdrhfrom C source code. When the ``-m'' option is used, the header 96*75897234Sdrhfile is not output since the makeheaders program will take care 97*75897234Sdrhof generated all header files automatically. 98*75897234SdrhThe ``-q'' option suppresses the report file. 99*75897234SdrhUsing ``-s'' causes a brief summary of parser statistics to be 100*75897234Sdrhprinted. Like this: 101*75897234Sdrh<pre> 102*75897234Sdrh Parser statistics: 74 terminals, 70 nonterminals, 179 rules 103*75897234Sdrh 340 states, 2026 parser table entries, 0 conflicts 104*75897234Sdrh</pre> 105*75897234SdrhFinally, the ``-x'' option causes Lemon to print its version number 106*75897234Sdrhand copyright information 107*75897234Sdrhand then stop without attempting to read the grammar or generate a parser.</p> 108*75897234Sdrh 109*75897234Sdrh<h3>The Parser Interface</h3> 110*75897234Sdrh 111*75897234Sdrh<p>Lemon doesn't generate a complete, working program. It only generates 112*75897234Sdrha few subroutines that implement a parser. This section describes 113*75897234Sdrhthe interface to those subroutines. It is up to the programmer to 114*75897234Sdrhcall these subroutines in an appropriate way in order to produce a 115*75897234Sdrhcomplete system.</p> 116*75897234Sdrh 117*75897234Sdrh<p>Before a program begins using a Lemon-generated parser, the program 118*75897234Sdrhmust first create the parser. 119*75897234SdrhA new parser is created as follows: 120*75897234Sdrh<pre> 121*75897234Sdrh void *pParser = ParseAlloc( malloc ); 122*75897234Sdrh</pre> 123*75897234SdrhThe ParseAlloc() routine allocates and initializes a new parser and 124*75897234Sdrhreturns a pointer to it. 125*75897234SdrhThe actual data structure used to represent a parser is opaque -- 126*75897234Sdrhits internal structure is not visible or usable by the calling routine. 127*75897234SdrhFor this reason, the ParseAlloc() routine returns a pointer to void 128*75897234Sdrhrather than a pointer to some particular structure. 129*75897234SdrhThe sole argument to the ParseAlloc() routine is a pointer to the 130*75897234Sdrhsubroutine used to allocate memory. Typically this means ``malloc()''.</p> 131*75897234Sdrh 132*75897234Sdrh<p>After a program is finished using a parser, it can reclaim all 133*75897234Sdrhmemory allocated by that parser by calling 134*75897234Sdrh<pre> 135*75897234Sdrh ParseFree(pParser, free); 136*75897234Sdrh</pre> 137*75897234SdrhThe first argument is the same pointer returned by ParseAlloc(). The 138*75897234Sdrhsecond argument is a pointer to the function used to release bulk 139*75897234Sdrhmemory back to the system.</p> 140*75897234Sdrh 141*75897234Sdrh<p>After a parser has been allocated using ParseAlloc(), the programmer 142*75897234Sdrhmust supply the parser with a sequence of tokens (terminal symbols) to 143*75897234Sdrhbe parsed. This is accomplished by calling the following function 144*75897234Sdrhonce for each token: 145*75897234Sdrh<pre> 146*75897234Sdrh Parse(pParser, hTokenID, sTokenData, pArg); 147*75897234Sdrh</pre> 148*75897234SdrhThe first argument to the Parse() routine is the pointer returned by 149*75897234SdrhParseAlloc(). 150*75897234SdrhThe second argument is a small positive integer that tells the parse the 151*75897234Sdrhtype of the next token in the data stream. 152*75897234SdrhThere is one token type for each terminal symbol in the grammar. 153*75897234SdrhThe gram.h file generated by Lemon contains #define statements that 154*75897234Sdrhmap symbolic terminal symbol names into appropriate integer values. 155*75897234Sdrh(A value of 0 for the second argument is a special flag to the 156*75897234Sdrhparser to indicate that the end of input has been reached.) 157*75897234SdrhThe third argument is the value of the given token. By default, 158*75897234Sdrhthe type of the third argument is integer, but the grammar will 159*75897234Sdrhusually redefine this type to be some kind of structure. 160*75897234SdrhTypically the second argument will be a broad category of tokens 161*75897234Sdrhsuch as ``identifier'' or ``number'' and the third argument will 162*75897234Sdrhbe the name of the identifier or the value of the number.</p> 163*75897234Sdrh 164*75897234Sdrh<p>The Parse() function may have either three or four arguments, 165*75897234Sdrhdepending on the grammar. If the grammar specification file request 166*75897234Sdrhit, the Parse() function will have a fourth parameter that can be 167*75897234Sdrhof any type chosen by the programmer. The parser doesn't do anything 168*75897234Sdrhwith this argument except to pass it through to action routines. 169*75897234SdrhThis is a convenient mechanism for passing state information down 170*75897234Sdrhto the action routines without having to use global variables.</p> 171*75897234Sdrh 172*75897234Sdrh<p>A typical use of a Lemon parser might look something like the 173*75897234Sdrhfollowing: 174*75897234Sdrh<pre> 175*75897234Sdrh 01 ParseTree *ParseFile(const char *zFilename){ 176*75897234Sdrh 02 Tokenizer *pTokenizer; 177*75897234Sdrh 03 void *pParser; 178*75897234Sdrh 04 Token sToken; 179*75897234Sdrh 05 int hTokenId; 180*75897234Sdrh 06 ParserState sState; 181*75897234Sdrh 07 182*75897234Sdrh 08 pTokenizer = TokenizerCreate(zFilename); 183*75897234Sdrh 09 pParser = ParseAlloc( malloc ); 184*75897234Sdrh 10 InitParserState(&sState); 185*75897234Sdrh 11 while( GetNextToken(pTokenizer, &hTokenId, &sToken) ){ 186*75897234Sdrh 12 Parse(pParser, hTokenId, sToken, &sState); 187*75897234Sdrh 13 } 188*75897234Sdrh 14 Parse(pParser, 0, sToken, &sState); 189*75897234Sdrh 15 ParseFree(pParser, free ); 190*75897234Sdrh 16 TokenizerFree(pTokenizer); 191*75897234Sdrh 17 return sState.treeRoot; 192*75897234Sdrh 18 } 193*75897234Sdrh</pre> 194*75897234SdrhThis example shows a user-written routine that parses a file of 195*75897234Sdrhtext and returns a pointer to the parse tree. 196*75897234Sdrh(We've omitted all error-handling from this example to keep it 197*75897234Sdrhsimple.) 198*75897234SdrhWe assume the existence of some kind of tokenizer which is created 199*75897234Sdrhusing TokenizerCreate() on line 8 and deleted by TokenizerFree() 200*75897234Sdrhon line 16. The GetNextToken() function on line 11 retrieves the 201*75897234Sdrhnext token from the input file and puts its type in the 202*75897234Sdrhinteger variable hTokenId. The sToken variable is assumed to be 203*75897234Sdrhsome kind of structure that contains details about each token, 204*75897234Sdrhsuch as its complete text, what line it occurs on, etc. </p> 205*75897234Sdrh 206*75897234Sdrh<p>This example also assumes the existence of structure of type 207*75897234SdrhParserState that holds state information about a particular parse. 208*75897234SdrhAn instance of such a structure is created on line 6 and initialized 209*75897234Sdrhon line 10. A pointer to this structure is passed into the Parse() 210*75897234Sdrhroutine as the optional 4th argument. 211*75897234SdrhThe action routine specified by the grammar for the parser can use 212*75897234Sdrhthe ParserState structure to hold whatever information is useful and 213*75897234Sdrhappropriate. In the example, we note that the treeRoot field of 214*75897234Sdrhthe ParserState structure is left pointing to the root of the parse 215*75897234Sdrhtree.</p> 216*75897234Sdrh 217*75897234Sdrh<p>The core of this example as it relates to Lemon is as follows: 218*75897234Sdrh<pre> 219*75897234Sdrh ParseFile(){ 220*75897234Sdrh pParser = ParseAlloc( malloc ); 221*75897234Sdrh while( GetNextToken(pTokenizer,&hTokenId, &sToken) ){ 222*75897234Sdrh Parse(pParser, hTokenId, sToken); 223*75897234Sdrh } 224*75897234Sdrh Parse(pParser, 0, sToken); 225*75897234Sdrh ParseFree(pParser, free ); 226*75897234Sdrh } 227*75897234Sdrh</pre> 228*75897234SdrhBasically, what a program has to do to use a Lemon-generated parser 229*75897234Sdrhis first create the parser, then send it lots of tokens obtained by 230*75897234Sdrhtokenizing an input source. When the end of input is reached, the 231*75897234SdrhParse() routine should be called one last time with a token type 232*75897234Sdrhof 0. This step is necessary to inform the parser that the end of 233*75897234Sdrhinput has been reached. Finally, we reclaim memory used by the 234*75897234Sdrhparser by calling ParseFree().</p> 235*75897234Sdrh 236*75897234Sdrh<p>There is one other interface routine that should be mentioned 237*75897234Sdrhbefore we move on. 238*75897234SdrhThe ParseTrace() function can be used to generate debugging output 239*75897234Sdrhfrom the parser. A prototype for this routine is as follows: 240*75897234Sdrh<pre> 241*75897234Sdrh ParseTrace(FILE *stream, char *zPrefix); 242*75897234Sdrh</pre> 243*75897234SdrhAfter this routine is called, a short (one-line) message is written 244*75897234Sdrhto the designated output stream every time the parser changes states 245*75897234Sdrhor calls an action routine. Each such message is prefaced using 246*75897234Sdrhthe text given by zPrefix. This debugging output can be turned off 247*75897234Sdrhby calling ParseTrace() again with a first argument of NULL (0).</p> 248*75897234Sdrh 249*75897234Sdrh<h3>Differences With YACC and BISON</h3> 250*75897234Sdrh 251*75897234Sdrh<p>Programmers who have previously used the yacc or bison parser 252*75897234Sdrhgenerator will notice several important differences between yacc and/or 253*75897234Sdrhbison and Lemon. 254*75897234Sdrh<ul> 255*75897234Sdrh<li>In yacc and bison, the parser calls the tokenizer. In Lemon, 256*75897234Sdrh the tokenizer calls the parser. 257*75897234Sdrh<li>Lemon uses no global variables. Yacc and bison use global variables 258*75897234Sdrh to pass information between the tokenizer and parser. 259*75897234Sdrh<li>Lemon allows multiple parsers to be running simultaneously. Yacc 260*75897234Sdrh and bison do not. 261*75897234Sdrh</ul> 262*75897234SdrhThese differences may cause some initial confusion for programmers 263*75897234Sdrhwith prior yacc and bison experience. 264*75897234SdrhBut after years of experience using Lemon, I firmly 265*75897234Sdrhbelieve that the Lemon way of doing things is better.</p> 266*75897234Sdrh 267*75897234Sdrh<h2>Input File Syntax</h2> 268*75897234Sdrh 269*75897234Sdrh<p>The main purpose of the grammar specification file for Lemon is 270*75897234Sdrhto define the grammar for the parser. But the input file also 271*75897234Sdrhspecifies additional information Lemon requires to do its job. 272*75897234SdrhMost of the work in using Lemon is in writing an appropriate 273*75897234Sdrhgrammar file.</p> 274*75897234Sdrh 275*75897234Sdrh<p>The grammar file for lemon is, for the most part, free format. 276*75897234SdrhIt does not have sections or divisions like yacc or bison. Any 277*75897234Sdrhdeclaration can occur at any point in the file. 278*75897234SdrhLemon ignores whitespace (except where it is needed to separate 279*75897234Sdrhtokens) and it honors the same commenting conventions as C and C++.</p> 280*75897234Sdrh 281*75897234Sdrh<h3>Terminals and Nonterminals</h3> 282*75897234Sdrh 283*75897234Sdrh<p>A terminal symbol (token) is any string of alphanumeric 284*75897234Sdrhand underscore characters 285*75897234Sdrhthat begins with an upper case letter. 286*75897234SdrhA terminal can contain lower class letters after the first character, 287*75897234Sdrhbut the usual convention is to make terminals all upper case. 288*75897234SdrhA nonterminal, on the other hand, is any string of alphanumeric 289*75897234Sdrhand underscore characters than begins with a lower case letter. 290*75897234SdrhAgain, the usual convention is to make nonterminals use all lower 291*75897234Sdrhcase letters.</p> 292*75897234Sdrh 293*75897234Sdrh<p>In Lemon, terminal and nonterminal symbols do not need to 294*75897234Sdrhbe declared or identified in a separate section of the grammar file. 295*75897234SdrhLemon is able to generate a list of all terminals and nonterminals 296*75897234Sdrhby examining the grammar rules, and it can always distinguish a 297*75897234Sdrhterminal from a nonterminal by checking the case of the first 298*75897234Sdrhcharacter of the name.</p> 299*75897234Sdrh 300*75897234Sdrh<p>Yacc and bison allow terminal symbols to have either alphanumeric 301*75897234Sdrhnames or to be individual characters included in single quotes, like 302*75897234Sdrhthis: ')' or '$'. Lemon does not allow this alternative form for 303*75897234Sdrhterminal symbols. With Lemon, all symbols, terminals and nonterminals, 304*75897234Sdrhmust have alphanumeric names.</p> 305*75897234Sdrh 306*75897234Sdrh<h3>Grammar Rules</h3> 307*75897234Sdrh 308*75897234Sdrh<p>The main component of a Lemon grammar file is a sequence of grammar 309*75897234Sdrhrules. 310*75897234SdrhEach grammar rule consists of a nonterminal symbol followed by 311*75897234Sdrhthe special symbol ``::='' and then a list of terminals and/or nonterminals. 312*75897234SdrhThe rule is terminated by a period. 313*75897234SdrhThe list of terminals and nonterminals on the right-hand side of the 314*75897234Sdrhrule can be empty. 315*75897234SdrhRules can occur in any order, except that the left-hand side of the 316*75897234Sdrhfirst rule is assumed to be the start symbol for the grammar (unless 317*75897234Sdrhspecified otherwise using the <tt>%start</tt> directive described below.) 318*75897234SdrhA typical sequence of grammar rules might look something like this: 319*75897234Sdrh<pre> 320*75897234Sdrh expr ::= expr PLUS expr. 321*75897234Sdrh expr ::= expr TIMES expr. 322*75897234Sdrh expr ::= LPAREN expr RPAREN. 323*75897234Sdrh expr ::= VALUE. 324*75897234Sdrh</pre> 325*75897234Sdrh</p> 326*75897234Sdrh 327*75897234Sdrh<p>There is one non-terminal in this example, ``expr'', and five 328*75897234Sdrhterminal symbols or tokens: ``PLUS'', ``TIMES'', ``LPAREN'', 329*75897234Sdrh``RPAREN'' and ``VALUE''.</p> 330*75897234Sdrh 331*75897234Sdrh<p>Like yacc and bison, Lemon allows the grammar to specify a block 332*75897234Sdrhof C code that will be executed whenever a grammar rule is reduced 333*75897234Sdrhby the parser. 334*75897234SdrhIn Lemon, this action is specified by putting the C code (contained 335*75897234Sdrhwithin curly braces <tt>{...}</tt>) immediately after the 336*75897234Sdrhperiod that closes the rule. 337*75897234SdrhFor example: 338*75897234Sdrh<pre> 339*75897234Sdrh expr ::= expr PLUS expr. { printf("Doing an addition...\n"); } 340*75897234Sdrh</pre> 341*75897234Sdrh</p> 342*75897234Sdrh 343*75897234Sdrh<p>In order to be useful, grammar actions must normally be linked to 344*75897234Sdrhtheir associated grammar rules. 345*75897234SdrhIn yacc and bison, this is accomplished by embedding a ``$$'' in the 346*75897234Sdrhaction to stand for the value of the left-hand side of the rule and 347*75897234Sdrhsymbols ``$1'', ``$2'', and so forth to stand for the value of 348*75897234Sdrhthe terminal or nonterminal at position 1, 2 and so forth on the 349*75897234Sdrhright-hand side of the rule. 350*75897234SdrhThis idea is very powerful, but it is also very error-prone. The 351*75897234Sdrhsingle most common source of errors in a yacc or bison grammar is 352*75897234Sdrhto miscount the number of symbols on the right-hand side of a grammar 353*75897234Sdrhrule and say ``$7'' when you really mean ``$8''.</p> 354*75897234Sdrh 355*75897234Sdrh<p>Lemon avoids the need to count grammar symbols by assigning symbolic 356*75897234Sdrhnames to each symbol in a grammar rule and then using those symbolic 357*75897234Sdrhnames in the action. 358*75897234SdrhIn yacc or bison, one would write this: 359*75897234Sdrh<pre> 360*75897234Sdrh expr -> expr PLUS expr { $$ = $1 + $3; }; 361*75897234Sdrh</pre> 362*75897234SdrhBut in Lemon, the same rule becomes the following: 363*75897234Sdrh<pre> 364*75897234Sdrh expr(A) ::= expr(B) PLUS expr(C). { A = B+C; } 365*75897234Sdrh</pre> 366*75897234SdrhIn the Lemon rule, any symbol in parentheses after a grammar rule 367*75897234Sdrhsymbol becomes a place holder for that symbol in the grammar rule. 368*75897234SdrhThis place holder can then be used in the associated C action to 369*75897234Sdrhstand for the value of that symbol.<p> 370*75897234Sdrh 371*75897234Sdrh<p>The Lemon notation for linking a grammar rule with its reduce 372*75897234Sdrhaction is superior to yacc/bison on several counts. 373*75897234SdrhFirst, as mentioned above, the Lemon method avoids the need to 374*75897234Sdrhcount grammar symbols. 375*75897234SdrhSecondly, if a terminal or nonterminal in a Lemon grammar rule 376*75897234Sdrhincludes a linking symbol in parentheses but that linking symbol 377*75897234Sdrhis not actually used in the reduce action, then an error message 378*75897234Sdrhis generated. 379*75897234SdrhFor example, the rule 380*75897234Sdrh<pre> 381*75897234Sdrh expr(A) ::= expr(B) PLUS expr(C). { A = B; } 382*75897234Sdrh</pre> 383*75897234Sdrhwill generate an error because the linking symbol ``C'' is used 384*75897234Sdrhin the grammar rule but not in the reduce action.</p> 385*75897234Sdrh 386*75897234Sdrh<p>The Lemon notation for linking grammar rules to reduce actions 387*75897234Sdrhalso facilitates the use of destructors for reclaiming memory 388*75897234Sdrhallocated by the values of terminals and nonterminals on the 389*75897234Sdrhright-hand side of a rule.</p> 390*75897234Sdrh 391*75897234Sdrh<h3>Precedence Rules</h3> 392*75897234Sdrh 393*75897234Sdrh<p>Lemon resolves parsing ambiguities in exactly the same way as 394*75897234Sdrhyacc and bison. A shift-reduce conflict is resolved in favor 395*75897234Sdrhof the shift, and a reduce-reduce conflict is resolved by reducing 396*75897234Sdrhwhichever rule comes first in the grammar file.</p> 397*75897234Sdrh 398*75897234Sdrh<p>Just like in 399*75897234Sdrhyacc and bison, Lemon allows a measure of control 400*75897234Sdrhover the resolution of paring conflicts using precedence rules. 401*75897234SdrhA precedence value can be assigned to any terminal symbol 402*75897234Sdrhusing the %left, %right or %nonassoc directives. Terminal symbols 403*75897234Sdrhmentioned in earlier directives have a lower precedence that 404*75897234Sdrhterminal symbols mentioned in later directives. For example:</p> 405*75897234Sdrh 406*75897234Sdrh<p><pre> 407*75897234Sdrh %left AND. 408*75897234Sdrh %left OR. 409*75897234Sdrh %nonassoc EQ NE GT GE LT LE. 410*75897234Sdrh %left PLUS MINUS. 411*75897234Sdrh %left TIMES DIVIDE MOD. 412*75897234Sdrh %right EXP NOT. 413*75897234Sdrh</pre></p> 414*75897234Sdrh 415*75897234Sdrh<p>In the preceding sequence of directives, the AND operator is 416*75897234Sdrhdefined to have the lowest precedence. The OR operator is one 417*75897234Sdrhprecedence level higher. And so forth. Hence, the grammar would 418*75897234Sdrhattempt to group the ambiguous expression 419*75897234Sdrh<pre> 420*75897234Sdrh a AND b OR c 421*75897234Sdrh</pre> 422*75897234Sdrhlike this 423*75897234Sdrh<pre> 424*75897234Sdrh a AND (b OR c). 425*75897234Sdrh</pre> 426*75897234SdrhThe associativity (left, right or nonassoc) is used to determine 427*75897234Sdrhthe grouping when the precedence is the same. AND is left-associative 428*75897234Sdrhin our example, so 429*75897234Sdrh<pre> 430*75897234Sdrh a AND b AND c 431*75897234Sdrh</pre> 432*75897234Sdrhis parsed like this 433*75897234Sdrh<pre> 434*75897234Sdrh (a AND b) AND c. 435*75897234Sdrh</pre> 436*75897234SdrhThe EXP operator is right-associative, though, so 437*75897234Sdrh<pre> 438*75897234Sdrh a EXP b EXP c 439*75897234Sdrh</pre> 440*75897234Sdrhis parsed like this 441*75897234Sdrh<pre> 442*75897234Sdrh a EXP (b EXP c). 443*75897234Sdrh</pre> 444*75897234SdrhThe nonassoc precedence is used for non-associative operators. 445*75897234SdrhSo 446*75897234Sdrh<pre> 447*75897234Sdrh a EQ b EQ c 448*75897234Sdrh</pre> 449*75897234Sdrhis an error.</p> 450*75897234Sdrh 451*75897234Sdrh<p>The precedence of non-terminals is transferred to rules as follows: 452*75897234SdrhThe precedence of a grammar rule is equal to the precedence of the 453*75897234Sdrhleft-most terminal symbol in the rule for which a precedence is 454*75897234Sdrhdefined. This is normally what you want, but in those cases where 455*75897234Sdrhyou want to precedence of a grammar rule to be something different, 456*75897234Sdrhyou can specify an alternative precedence symbol by putting the 457*75897234Sdrhsymbol in square braces after the period at the end of the rule and 458*75897234Sdrhbefore any C-code. For example:</p> 459*75897234Sdrh 460*75897234Sdrh<p><pre> 461*75897234Sdrh expr = MINUS expr. [NOT] 462*75897234Sdrh</pre></p> 463*75897234Sdrh 464*75897234Sdrh<p>This rule has a precedence equal to that of the NOT symbol, not the 465*75897234SdrhMINUS symbol as would have been the case by default.</p> 466*75897234Sdrh 467*75897234Sdrh<p>With the knowledge of how precedence is assigned to terminal 468*75897234Sdrhsymbols and individual 469*75897234Sdrhgrammar rules, we can now explain precisely how parsing conflicts 470*75897234Sdrhare resolved in Lemon. Shift-reduce conflicts are resolved 471*75897234Sdrhas follows: 472*75897234Sdrh<ul> 473*75897234Sdrh<li> If either the token to be shifted or the rule to be reduced 474*75897234Sdrh lacks precedence information, then resolve in favor of the 475*75897234Sdrh shift, but report a parsing conflict. 476*75897234Sdrh<li> If the precedence of the token to be shifted is greater than 477*75897234Sdrh the precedence of the rule to reduce, then resolve in favor 478*75897234Sdrh of the shift. No parsing conflict is reported. 479*75897234Sdrh<li> If the precedence of the token it be shifted is less than the 480*75897234Sdrh precedence of the rule to reduce, then resolve in favor of the 481*75897234Sdrh reduce action. No parsing conflict is reported. 482*75897234Sdrh<li> If the precedences are the same and the shift token is 483*75897234Sdrh right-associative, then resolve in favor of the shift. 484*75897234Sdrh No parsing conflict is reported. 485*75897234Sdrh<li> If the precedences are the same the the shift token is 486*75897234Sdrh left-associative, then resolve in favor of the reduce. 487*75897234Sdrh No parsing conflict is reported. 488*75897234Sdrh<li> Otherwise, resolve the conflict by doing the shift and 489*75897234Sdrh report the parsing conflict. 490*75897234Sdrh</ul> 491*75897234SdrhReduce-reduce conflicts are resolved this way: 492*75897234Sdrh<ul> 493*75897234Sdrh<li> If either reduce rule 494*75897234Sdrh lacks precedence information, then resolve in favor of the 495*75897234Sdrh rule that appears first in the grammar and report a parsing 496*75897234Sdrh conflict. 497*75897234Sdrh<li> If both rules have precedence and the precedence is different 498*75897234Sdrh then resolve the dispute in favor of the rule with the highest 499*75897234Sdrh precedence and do not report a conflict. 500*75897234Sdrh<li> Otherwise, resolve the conflict by reducing by the rule that 501*75897234Sdrh appears first in the grammar and report a parsing conflict. 502*75897234Sdrh</ul> 503*75897234Sdrh 504*75897234Sdrh<h3>Special Directives</h3> 505*75897234Sdrh 506*75897234Sdrh<p>The input grammar to Lemon consists of grammar rules and special 507*75897234Sdrhdirectives. We've described all the grammar rules, so now we'll 508*75897234Sdrhtalk about the special directives.</p> 509*75897234Sdrh 510*75897234Sdrh<p>Directives in lemon can occur in any order. You can put them before 511*75897234Sdrhthe grammar rules, or after the grammar rules, or in the mist of the 512*75897234Sdrhgrammar rules. It doesn't matter. The relative order of 513*75897234Sdrhdirectives used to assign precedence to terminals is important, but 514*75897234Sdrhother than that, the order of directives in Lemon is arbitrary.</p> 515*75897234Sdrh 516*75897234Sdrh<p>Lemon supports the following special directives: 517*75897234Sdrh<ul> 518*75897234Sdrh<li><tt>%destructor</tt> 519*75897234Sdrh<li><tt>%extra_argument</tt> 520*75897234Sdrh<li><tt>%include</tt> 521*75897234Sdrh<li><tt>%left</tt> 522*75897234Sdrh<li><tt>%name</tt> 523*75897234Sdrh<li><tt>%nonassoc</tt> 524*75897234Sdrh<li><tt>%parse_accept</tt> 525*75897234Sdrh<li><tt>%parse_failure </tt> 526*75897234Sdrh<li><tt>%right</tt> 527*75897234Sdrh<li><tt>%stack_overflow</tt> 528*75897234Sdrh<li><tt>%stack_size</tt> 529*75897234Sdrh<li><tt>%start_symbol</tt> 530*75897234Sdrh<li><tt>%syntax_error</tt> 531*75897234Sdrh<li><tt>%token_destructor</tt> 532*75897234Sdrh<li><tt>%token_prefix</tt> 533*75897234Sdrh<li><tt>%token_type</tt> 534*75897234Sdrh<li><tt>%type</tt> 535*75897234Sdrh</ul> 536*75897234SdrhEach of these directives will be described separately in the 537*75897234Sdrhfollowing sections:</p> 538*75897234Sdrh 539*75897234Sdrh<h4>The <tt>%destructor</tt> directive</h4> 540*75897234Sdrh 541*75897234Sdrh<p>The %destructor directive is used to specify a destructor for 542*75897234Sdrha non-terminal symbol. 543*75897234Sdrh(See also the %token_destructor directive which is used to 544*75897234Sdrhspecify a destructor for terminal symbols.)</p> 545*75897234Sdrh 546*75897234Sdrh<p>A non-terminal's destructor is called to dispose of the 547*75897234Sdrhnon-terminal's value whenever the non-terminal is popped from 548*75897234Sdrhthe stack. This includes all of the following circumstances: 549*75897234Sdrh<ul> 550*75897234Sdrh<li> When a rule reduces and the value of a non-terminal on 551*75897234Sdrh the right-hand side is not linked to C code. 552*75897234Sdrh<li> When the stack is popped during error processing. 553*75897234Sdrh<li> When the ParseFree() function runs. 554*75897234Sdrh</ul> 555*75897234SdrhThe destructor can do whatever it wants with the value of 556*75897234Sdrhthe non-terminal, but its design is to deallocate memory 557*75897234Sdrhor other resources held by that non-terminal.</p> 558*75897234Sdrh 559*75897234Sdrh<p>Consider an example: 560*75897234Sdrh<pre> 561*75897234Sdrh %type nt {void*} 562*75897234Sdrh %destructor nt { free($$); } 563*75897234Sdrh nt(A) ::= ID NUM. { A = malloc( 100 ); } 564*75897234Sdrh</pre> 565*75897234SdrhThis example is a bit contrived but it serves to illustrate how 566*75897234Sdrhdestructors work. The example shows a non-terminal named 567*75897234Sdrh``nt'' that holds values of type ``void*''. When the rule for 568*75897234Sdrhan ``nt'' reduces, it sets the value of the non-terminal to 569*75897234Sdrhspace obtained from malloc(). Later, when the nt non-terminal 570*75897234Sdrhis popped from the stack, the destructor will fire and call 571*75897234Sdrhfree() on this malloced space, thus avoiding a memory leak. 572*75897234Sdrh(Note that the symbol ``$$'' in the destructor code is replaced 573*75897234Sdrhby the value of the non-terminal.)</p> 574*75897234Sdrh 575*75897234Sdrh<p>It is important to note that the value of a non-terminal is passed 576*75897234Sdrhto the destructor whenever the non-terminal is removed from the 577*75897234Sdrhstack, unless the non-terminal is used in a C-code action. If 578*75897234Sdrhthe non-terminal is used by C-code, then it is assumed that the 579*75897234SdrhC-code will take care of destroying it if it should really 580*75897234Sdrhbe destroyed. More commonly, the value is used to build some 581*75897234Sdrhlarger structure and we don't want to destroy it, which is why 582*75897234Sdrhthe destructor is not called in this circumstance.</p> 583*75897234Sdrh 584*75897234Sdrh<p>By appropriate use of destructors, it is possible to 585*75897234Sdrhbuild a parser using Lemon that can be used within a long-running 586*75897234Sdrhprogram, such as a GUI, that will not leak memory or other resources. 587*75897234SdrhTo do the same using yacc or bison is much more difficult.</p> 588*75897234Sdrh 589*75897234Sdrh<h4>The <tt>%extra_argument</tt> directive</h4> 590*75897234Sdrh 591*75897234SdrhThe %extra_argument directive instructs Lemon to add a 4th parameter 592*75897234Sdrhto the parameter list of the Parse() function it generates. Lemon 593*75897234Sdrhdoesn't do anything itself with this extra argument, but it does 594*75897234Sdrhmake the argument available to C-code action routines, destructors, 595*75897234Sdrhand so forth. For example, if the grammar file contains:</p> 596*75897234Sdrh 597*75897234Sdrh<p><pre> 598*75897234Sdrh %extra_argument { MyStruct *pAbc } 599*75897234Sdrh</pre></p> 600*75897234Sdrh 601*75897234Sdrh<p>Then the Parse() function generated will have an 4th parameter 602*75897234Sdrhof type ``MyStruct*'' and all action routines will have access to 603*75897234Sdrha variable named ``pAbc'' that is the value of the 4th parameter 604*75897234Sdrhin the most recent call to Parse().</p> 605*75897234Sdrh 606*75897234Sdrh<h4>The <tt>%include</tt> directive</h4> 607*75897234Sdrh 608*75897234Sdrh<p>The %include directive specifies C code that is included at the 609*75897234Sdrhtop of the generated parser. You can include any text you want -- 610*75897234Sdrhthe Lemon parser generator copies to blindly. If you have multiple 611*75897234Sdrh%include directives in your grammar file, their values are concatenated 612*75897234Sdrhbefore being put at the beginning of the generated parser.</p> 613*75897234Sdrh 614*75897234Sdrh<p>The %include directive is very handy for getting some extra #include 615*75897234Sdrhpreprocessor statements at the beginning of the generated parser. 616*75897234SdrhFor example:</p> 617*75897234Sdrh 618*75897234Sdrh<p><pre> 619*75897234Sdrh %include {#include <unistd.h>} 620*75897234Sdrh</pre></p> 621*75897234Sdrh 622*75897234Sdrh<p>This might be needed, for example, if some of the C actions in the 623*75897234Sdrhgrammar call functions that are prototyed in unistd.h.</p> 624*75897234Sdrh 625*75897234Sdrh<h4>The <tt>%left</tt> directive</h4> 626*75897234Sdrh 627*75897234SdrhThe %left directive is used (along with the %right and 628*75897234Sdrh%nonassoc directives) to declare precedences of terminal 629*75897234Sdrhsymbols. Every terminal symbol whose name appears after 630*75897234Sdrha %left directive but before the next period (``.'') is 631*75897234Sdrhgiven the same left-associative precedence value. Subsequent 632*75897234Sdrh%left directives have higher precedence. For example:</p> 633*75897234Sdrh 634*75897234Sdrh<p><pre> 635*75897234Sdrh %left AND. 636*75897234Sdrh %left OR. 637*75897234Sdrh %nonassoc EQ NE GT GE LT LE. 638*75897234Sdrh %left PLUS MINUS. 639*75897234Sdrh %left TIMES DIVIDE MOD. 640*75897234Sdrh %right EXP NOT. 641*75897234Sdrh</pre></p> 642*75897234Sdrh 643*75897234Sdrh<p>Note the period that terminates each %left, %right or %nonassoc 644*75897234Sdrhdirective.</p> 645*75897234Sdrh 646*75897234Sdrh<p>LALR(1) grammars can get into a situation where they require 647*75897234Sdrha large amount of stack space if you make heavy use or right-associative 648*75897234Sdrhoperators. For this reason, it is recommended that you use %left 649*75897234Sdrhrather than %right whenever possible.</p> 650*75897234Sdrh 651*75897234Sdrh<h4>The <tt>%name</tt> directive</h4> 652*75897234Sdrh 653*75897234Sdrh<p>By default, the functions generated by Lemon all begin with the 654*75897234Sdrhfive-character string ``Parse''. You can change this string to something 655*75897234Sdrhdifferent using the %name directive. For instance:</p> 656*75897234Sdrh 657*75897234Sdrh<p><pre> 658*75897234Sdrh %name Abcde 659*75897234Sdrh</pre></p> 660*75897234Sdrh 661*75897234Sdrh<p>Putting this directive in the grammar file will cause Lemon to generate 662*75897234Sdrhfunctions named 663*75897234Sdrh<ul> 664*75897234Sdrh<li> AbcdeAlloc(), 665*75897234Sdrh<li> AbcdeFree(), 666*75897234Sdrh<li> AbcdeTrace(), and 667*75897234Sdrh<li> Abcde(). 668*75897234Sdrh</ul> 669*75897234SdrhThe %name directive allows you to generator two or more different 670*75897234Sdrhparsers and link them all into the same executable. 671*75897234Sdrh</p> 672*75897234Sdrh 673*75897234Sdrh<h4>The <tt>%nonassoc</tt> directive</h4> 674*75897234Sdrh 675*75897234Sdrh<p>This directive is used to assign non-associative precedence to 676*75897234Sdrhone or more terminal symbols. See the section on precedence rules 677*75897234Sdrhor on the %left directive for additional information.</p> 678*75897234Sdrh 679*75897234Sdrh<h4>The <tt>%parse_accept</tt> directive</h4> 680*75897234Sdrh 681*75897234Sdrh<p>The %parse_accept directive specifies a block of C code that is 682*75897234Sdrhexecuted whenever the parser accepts its input string. To ``accept'' 683*75897234Sdrhan input string means that the parser was able to process all tokens 684*75897234Sdrhwithout error.</p> 685*75897234Sdrh 686*75897234Sdrh<p>For example:</p> 687*75897234Sdrh 688*75897234Sdrh<p><pre> 689*75897234Sdrh %parse_accept { 690*75897234Sdrh printf("parsing complete!\n"); 691*75897234Sdrh } 692*75897234Sdrh</pre></p> 693*75897234Sdrh 694*75897234Sdrh 695*75897234Sdrh<h4>The <tt>%parse_failure</tt> directive</h4> 696*75897234Sdrh 697*75897234Sdrh<p>The %parse_failure directive specifies a block of C code that 698*75897234Sdrhis executed whenever the parser fails complete. This code is not 699*75897234Sdrhexecuted until the parser has tried and failed to resolve an input 700*75897234Sdrherror using is usual error recovery strategy. The routine is 701*75897234Sdrhonly invoked when parsing is unable to continue.</p> 702*75897234Sdrh 703*75897234Sdrh<p><pre> 704*75897234Sdrh %parse_failure { 705*75897234Sdrh fprintf(stderr,"Giving up. Parser is hopelessly lost...\n"); 706*75897234Sdrh } 707*75897234Sdrh</pre></p> 708*75897234Sdrh 709*75897234Sdrh<h4>The <tt>%right</tt> directive</h4> 710*75897234Sdrh 711*75897234Sdrh<p>This directive is used to assign right-associative precedence to 712*75897234Sdrhone or more terminal symbols. See the section on precedence rules 713*75897234Sdrhor on the %left directive for additional information.</p> 714*75897234Sdrh 715*75897234Sdrh<h4>The <tt>%stack_overflow</tt> directive</h4> 716*75897234Sdrh 717*75897234Sdrh<p>The %stack_overflow directive specifies a block of C code that 718*75897234Sdrhis executed if the parser's internal stack ever overflows. Typically 719*75897234Sdrhthis just prints an error message. After a stack overflow, the parser 720*75897234Sdrhwill be unable to continue and must be reset.</p> 721*75897234Sdrh 722*75897234Sdrh<p><pre> 723*75897234Sdrh %stack_overflow { 724*75897234Sdrh fprintf(stderr,"Giving up. Parser stack overflow\n"); 725*75897234Sdrh } 726*75897234Sdrh</pre></p> 727*75897234Sdrh 728*75897234Sdrh<p>You can help prevent parser stack overflows by avoiding the use 729*75897234Sdrhof right recursion and right-precedence operators in your grammar. 730*75897234SdrhUse left recursion and and left-precedence operators instead, to 731*75897234Sdrhencourage rules to reduce sooner and keep the stack size down. 732*75897234SdrhFor example, do rules like this: 733*75897234Sdrh<pre> 734*75897234Sdrh list ::= list element. // left-recursion. Good! 735*75897234Sdrh list ::= . 736*75897234Sdrh</pre> 737*75897234SdrhNot like this: 738*75897234Sdrh<pre> 739*75897234Sdrh list ::= element list. // right-recursion. Bad! 740*75897234Sdrh list ::= . 741*75897234Sdrh</pre> 742*75897234Sdrh 743*75897234Sdrh<h4>The <tt>%stack_size</tt> directive</h4> 744*75897234Sdrh 745*75897234Sdrh<p>If stack overflow is a problem and you can't resolve the trouble 746*75897234Sdrhby using left-recursion, then you might want to increase the size 747*75897234Sdrhof the parser's stack using this directive. Put an positive integer 748*75897234Sdrhafter the %stack_size directive and Lemon will generate a parse 749*75897234Sdrhwith a stack of the requested size. The default value is 100.</p> 750*75897234Sdrh 751*75897234Sdrh<p><pre> 752*75897234Sdrh %stack_size 2000 753*75897234Sdrh</pre></p> 754*75897234Sdrh 755*75897234Sdrh<h4>The <tt>%start_symbol</tt> directive</h4> 756*75897234Sdrh 757*75897234Sdrh<p>By default, the start-symbol for the grammar that Lemon generates 758*75897234Sdrhis the first non-terminal that appears in the grammar file. But you 759*75897234Sdrhcan choose a different start-symbol using the %start_symbol directive.</p> 760*75897234Sdrh 761*75897234Sdrh<p><pre> 762*75897234Sdrh %start_symbol prog 763*75897234Sdrh</pre></p> 764*75897234Sdrh 765*75897234Sdrh<h4>The <tt>%token_destructor</tt> directive</h4> 766*75897234Sdrh 767*75897234Sdrh<p>The %destructor directive assigns a destructor to a non-terminal 768*75897234Sdrhsymbol. (See the description of the %destructor directive above.) 769*75897234SdrhThis directive does the same thing for all terminal symbols.</p> 770*75897234Sdrh 771*75897234Sdrh<p>Unlike non-terminal symbols which may each have a different data type 772*75897234Sdrhfor their values, terminals all use the same data type (defined by 773*75897234Sdrhthe %token_type directive) and so they use a common destructor. Other 774*75897234Sdrhthan that, the token destructor works just like the non-terminal 775*75897234Sdrhdestructors.</p> 776*75897234Sdrh 777*75897234Sdrh<h4>The <tt>%token_prefix</tt> directive</h4> 778*75897234Sdrh 779*75897234Sdrh<p>Lemon generates #defines that assign small integer constants 780*75897234Sdrhto each terminal symbol in the grammar. If desired, Lemon will 781*75897234Sdrhadd a prefix specified by this directive 782*75897234Sdrhto each of the #defines it generates. 783*75897234SdrhSo if the default output of Lemon looked like this: 784*75897234Sdrh<pre> 785*75897234Sdrh #define AND 1 786*75897234Sdrh #define MINUS 2 787*75897234Sdrh #define OR 3 788*75897234Sdrh #define PLUS 4 789*75897234Sdrh</pre> 790*75897234SdrhYou can insert a statement into the grammar like this: 791*75897234Sdrh<pre> 792*75897234Sdrh %token_prefix TOKEN_ 793*75897234Sdrh</pre> 794*75897234Sdrhto cause Lemon to produce these symbols instead: 795*75897234Sdrh<pre> 796*75897234Sdrh #define TOKEN_AND 1 797*75897234Sdrh #define TOKEN_MINUS 2 798*75897234Sdrh #define TOKEN_OR 3 799*75897234Sdrh #define TOKEN_PLUS 4 800*75897234Sdrh</pre> 801*75897234Sdrh 802*75897234Sdrh<h4>The <tt>%token_type</tt> and <tt>%type</tt> directives</h4> 803*75897234Sdrh 804*75897234Sdrh<p>These directives are used to specify the data types for values 805*75897234Sdrhon the parser's stack associated with terminal and non-terminal 806*75897234Sdrhsymbols. The values of all terminal symbols must be of the same 807*75897234Sdrhtype. This turns out to be the same data type as the 3rd parameter 808*75897234Sdrhto the Parse() function generated by Lemon. Typically, you will 809*75897234Sdrhmake the value of a terminal symbol by a pointer to some kind of 810*75897234Sdrhtoken structure. Like this:</p> 811*75897234Sdrh 812*75897234Sdrh<p><pre> 813*75897234Sdrh %token_type {Token*} 814*75897234Sdrh</pre></p> 815*75897234Sdrh 816*75897234Sdrh<p>If the data type of terminals is not specified, the default value 817*75897234Sdrhis ``int''.</p> 818*75897234Sdrh 819*75897234Sdrh<p>Non-terminal symbols can each have their own data types. Typically 820*75897234Sdrhthe data type of a non-terminal is a pointer to the root of a parse-tree 821*75897234Sdrhstructure that contains all information about that non-terminal. 822*75897234SdrhFor example:</p> 823*75897234Sdrh 824*75897234Sdrh<p><pre> 825*75897234Sdrh %type expr {Expr*} 826*75897234Sdrh</pre></p> 827*75897234Sdrh 828*75897234Sdrh<p>Each entry on the parser's stack is actually a union containing 829*75897234Sdrhinstances of all data types for every non-terminal and terminal symbol. 830*75897234SdrhLemon will automatically use the correct element of this union depending 831*75897234Sdrhon what the corresponding non-terminal or terminal symbol is. But 832*75897234Sdrhthe grammar designer should keep in mind that the size of the union 833*75897234Sdrhwill be the size of its largest element. So if you have a single 834*75897234Sdrhnon-terminal whose data type requires 1K of storage, then your 100 835*75897234Sdrhentry parser stack will require 100K of heap space. If you are willing 836*75897234Sdrhand able to pay that price, fine. You just need to know.</p> 837*75897234Sdrh 838*75897234Sdrh<h3>Error Processing</h3> 839*75897234Sdrh 840*75897234Sdrh<p>After extensive experimentation over several years, it has been 841*75897234Sdrhdiscovered that the error recovery strategy used by yacc is about 842*75897234Sdrhas good as it gets. And so that is what Lemon uses.</p> 843*75897234Sdrh 844*75897234Sdrh<p>When a Lemon-generated parser encounters a syntax error, it 845*75897234Sdrhfirst invokes the code specified by the %syntax_error directive, if 846*75897234Sdrhany. It then enters its error recovery strategy. The error recovery 847*75897234Sdrhstrategy is to begin popping the parsers stack until it enters a 848*75897234Sdrhstate where it is permitted to shift a special non-terminal symbol 849*75897234Sdrhnamed ``error''. It then shifts this non-terminal and continues 850*75897234Sdrhparsing. But the %syntax_error routine will not be called again 851*75897234Sdrhuntil at least three new tokens have been successfully shifted.</p> 852*75897234Sdrh 853*75897234Sdrh<p>If the parser pops its stack until the stack is empty, and it still 854*75897234Sdrhis unable to shift the error symbol, then the %parse_failed routine 855*75897234Sdrhis invoked and the parser resets itself to its start state, ready 856*75897234Sdrhto begin parsing a new file. This is what will happen at the very 857*75897234Sdrhfirst syntax error, of course, if there are no instances of the 858*75897234Sdrh``error'' non-terminal in your grammar.</p> 859*75897234Sdrh 860*75897234Sdrh</body> 861*75897234Sdrh</html> 862