bison.info-3 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266
  1. This is Info file bison.info, produced by Makeinfo-1.55 from the input
  2. file bison.texinfo.
  3. This file documents the Bison parser generator.
  4. Copyright (C) 1988, 1989, 1990, 1991, 1992 Free Software Foundation,
  5. Inc. Modified (1993) from bison-1.22 by Wilfred J. Hansen
  6. (wjh+@cmu.edu), Andrew Consortium, Carnegie Mellon University
  7. CARNEGIE MELLON UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO
  8. THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  9. FITNESS. IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE FOR ANY
  10. SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
  11. RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
  12. CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  13. CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  14. Permission is granted to make and distribute verbatim copies of this
  15. manual provided the copyright notice and this permission notice are
  16. preserved on all copies.
  17. Permission is granted to copy and distribute modified versions of
  18. this manual under the conditions for verbatim copying, provided also
  19. that the sections entitled "GNU General Public License" and "Conditions
  20. for Using Bison" are included exactly as in the original, and provided
  21. that the entire resulting derived work is distributed under the terms
  22. of a permission notice identical to this one.
  23. Permission is granted to copy and distribute translations of this
  24. manual into another language, under the above conditions for modified
  25. versions, except that the sections entitled "GNU General Public
  26. License", "Conditions for Using Bison" and this permission notice may be
  27. included in translations approved by the Free Software Foundation
  28. instead of in the original English.
  29. 
  30. File: bison.info, Node: Mid-Rule Actions, Prev: Action Types, Up: Semantics
  31. Actions in Mid-Rule
  32. -------------------
  33. Occasionally it is useful to put an action in the middle of a rule.
  34. These actions are written just like usual end-of-rule actions, but they
  35. are executed before the parser even recognizes the following components.
  36. A mid-rule action may refer to the components preceding it using
  37. `$N', but it may not refer to subsequent components because it is run
  38. before they are parsed.
  39. The mid-rule action itself counts as one of the components of the
  40. rule. This makes a difference when there is another action later in
  41. the same rule (and usually there is another at the end): you have to
  42. count the actions along with the symbols when working out which number
  43. N to use in `$N'.
  44. The mid-rule action can also have a semantic value. The action can
  45. set its value with an assignment to `$$', and actions later in the rule
  46. can refer to the value using `$N'. Since there is no symbol to name
  47. the action, there is no way to declare a data type for the value in
  48. advance, so you must use the `$<...>' construct to specify a data type
  49. each time you refer to this value.
  50. There is no way to set the value of the entire rule with a mid-rule
  51. action, because assignments to `$$' do not have that effect. The only
  52. way to set the value for the entire rule is with an ordinary action at
  53. the end of the rule.
  54. Here is an example from a hypothetical compiler, handling a `let'
  55. statement that looks like `let (VARIABLE) STATEMENT' and serves to
  56. create a variable named VARIABLE temporarily for the duration of
  57. STATEMENT. To parse this construct, we must put VARIABLE into the
  58. symbol table while STATEMENT is parsed, then remove it afterward. Here
  59. is how it is done:
  60. stmt: LET '(' var ')'
  61. { $<context>$ = push_context ();
  62. declare_variable ($3); }
  63. stmt { $$ = $6;
  64. pop_context ($<context>5); }
  65. As soon as `let (VARIABLE)' has been recognized, the first action is
  66. run. It saves a copy of the current semantic context (the list of
  67. accessible variables) as its semantic value, using alternative
  68. `context' in the data-type union. Then it calls `declare_variable' to
  69. add the new variable to that list. Once the first action is finished,
  70. the embedded statement `stmt' can be parsed. Note that the mid-rule
  71. action is component number 5, so the `stmt' is component number 6.
  72. After the embedded statement is parsed, its semantic value becomes
  73. the value of the entire `let'-statement. Then the semantic value from
  74. the earlier action is used to restore the prior list of variables. This
  75. removes the temporary `let'-variable from the list so that it won't
  76. appear to exist while the rest of the program is parsed.
  77. Taking action before a rule is completely recognized often leads to
  78. conflicts since the parser must commit to a parse in order to execute
  79. the action. For example, the following two rules, without mid-rule
  80. actions, can coexist in a working parser because the parser can shift
  81. the open-brace token and look at what follows before deciding whether
  82. there is a declaration or not:
  83. compound: '{' declarations statements '}'
  84. | '{' statements '}'
  85. ;
  86. But when we add a mid-rule action as follows, the rules become
  87. nonfunctional:
  88. compound: { prepare_for_local_variables (); }
  89. '{' declarations statements '}'
  90. | '{' statements '}'
  91. ;
  92. Now the parser is forced to decide whether to run the mid-rule action
  93. when it has read no farther than the open-brace. In other words, it
  94. must commit to using one rule or the other, without sufficient
  95. information to do it correctly. (The open-brace token is what is called
  96. the "look-ahead" token at this time, since the parser is still deciding
  97. what to do about it. *Note Look-Ahead Tokens: Look-Ahead.)
  98. You might think that you could correct the problem by putting
  99. identical actions into the two rules, like this:
  100. compound: { prepare_for_local_variables (); }
  101. '{' declarations statements '}'
  102. | { prepare_for_local_variables (); }
  103. '{' statements '}'
  104. ;
  105. But this does not help, because Bison does not realize that the two
  106. actions are identical. (Bison never tries to understand the C code in
  107. an action.)
  108. If the grammar is such that a declaration can be distinguished from a
  109. statement by the first token (which is true in C), then one solution
  110. which does work is to put the action after the open-brace, like this:
  111. compound: '{' { prepare_for_local_variables (); }
  112. declarations statements '}'
  113. | '{' statements '}'
  114. ;
  115. Now the first token of the following declaration or statement, which
  116. would in any case tell Bison which rule to use, can still do so.
  117. Another solution is to bury the action inside a nonterminal symbol
  118. which serves as a subroutine:
  119. subroutine: /* empty */
  120. { prepare_for_local_variables (); }
  121. ;
  122. compound: subroutine
  123. '{' declarations statements '}'
  124. | subroutine
  125. '{' statements '}'
  126. ;
  127. Now Bison can execute the action in the rule for `subroutine' without
  128. deciding which rule for `compound' it will eventually use. Note that
  129. the action is now at the end of its rule. Any mid-rule action can be
  130. converted to an end-of-rule action in this way, and this is what Bison
  131. actually does to implement mid-rule actions.
  132. 
  133. File: bison.info, Node: Declarations, Next: Multiple Parsers, Prev: Semantics, Up: Grammar File
  134. Bison Declarations
  135. ==================
  136. The "Bison declarations" section of a Bison grammar defines the
  137. symbols used in formulating the grammar and the data types of semantic
  138. values. *Note Symbols - Terminal and Nonterminal: Symbols.
  139. All token type names (but not single-character literal tokens such as
  140. `'+'' and `'*'') must be declared. Nonterminal symbols must be
  141. declared if you need to specify which data type to use for the semantic
  142. value (*note More Than One Value Type: Multiple Types.).
  143. The first rule in the file also specifies the start symbol, by
  144. default. If you want some other symbol to be the start symbol, you
  145. must declare it explicitly (*note Languages and Context-Free Grammars:
  146. Language and Grammar.).
  147. * Menu:
  148. * Token Decl:: Declaring terminal symbols.
  149. * Thong Decl:: Declaring terminals for multi-character tokens.
  150. * Precedence Decl:: Declaring terminals with precedence and associativity.
  151. * Union Decl:: Declaring the set of all semantic value types.
  152. * Type Decl:: Declaring the choice of type for a nonterminal symbol.
  153. * Expect Decl:: Suppressing warnings about shift/reduce conflicts.
  154. * Start Decl:: Specifying the start symbol.
  155. * Pure Decl:: Requesting a reentrant parser.
  156. * Decl Summary:: Table of all Bison declarations.
  157. 
  158. File: bison.info, Node: Token Decl, Next: Thong Decl, Up: Declarations
  159. Token Type Names
  160. ----------------
  161. The basic way to declare a token type name (terminal symbol) is as
  162. follows:
  163. %token NAME
  164. Bison will convert this into a `#define' directive in the parser, so
  165. that the function `yylex' (if it is in this file) can use the name NAME
  166. to stand for this token type's code.
  167. Alternatively, you can use `%left', `%right', or `%nonassoc' instead
  168. of `%token', if you wish to specify precedence. *Note Operator
  169. Precedence: Precedence Decl.
  170. You can explicitly specify the numeric code for a token type by
  171. appending an integer value in the field immediately following the token
  172. name:
  173. %token NUM 300
  174. It is generally best, however, to let Bison choose the numeric codes for
  175. all token types. Bison will automatically select codes that don't
  176. conflict with each other or with ASCII characters.
  177. In the event that the stack type is a union, you must augment the
  178. `%token' or other token declaration to include the data type
  179. alternative delimited by angle-brackets (*note More Than One Value
  180. Type: Multiple Types.).
  181. For example:
  182. %union { /* define stack type */
  183. double val;
  184. symrec *tptr;
  185. }
  186. %token <val> NUM /* define token NUM and its type */
  187. 
  188. File: bison.info, Node: Thong Decl, Next: Precedence Decl, Prev: Token Decl, Up: Declarations
  189. Thong Declarations
  190. ------------------
  191. WARNING: If you use thong declarations, your grammar will NOT be
  192. compatible with Yacc.
  193. The `%thong' declaration associates a symbol with a
  194. multiple-character string literal token. The symbol will be entered in
  195. the #defines generated by the `-d' switch and may then be used to
  196. report the token by the lexical analyzer.
  197. The syntax of a thong declaration has four elements: the keyword,
  198. the type (optional), the token name, and a quoted string giving the
  199. contents of the thong. (Backslashes may be used as the usual escapes
  200. in the string.)
  201. For example, a C parser might specify
  202. %thong <operator> OR "||"
  203. %thong <operator> LE "<="
  204. %left OR "<="
  205. Either the symbolic or the string literal form may be used in further
  206. declarations or the grammar rules.
  207. Whether or not there is a `%thong' declaration, the symbolic name
  208. does not appear in the generated `yytname' table; only the string
  209. literal appears. Its corresponding token number is available as the
  210. index of its entry in the `yytname' table generated in response to the
  211. `-k' or `--token-table' switch.
  212. 
  213. File: bison.info, Node: Precedence Decl, Next: Union Decl, Prev: Thong Decl, Up: Declarations
  214. Operator Precedence
  215. -------------------
  216. Use the `%left', `%right' or `%nonassoc' declaration to declare a
  217. token and specify its precedence and associativity, all at once. These
  218. are called "precedence declarations". *Note Operator Precedence:
  219. Precedence, for general information on operator precedence.
  220. The syntax of a precedence declaration is the same as that of
  221. `%token': either
  222. %left SYMBOLS...
  223. or
  224. %left <TYPE> SYMBOLS...
  225. And indeed any of these declarations serves the purposes of `%token'.
  226. But in addition, they specify the associativity and relative precedence
  227. for all the SYMBOLS:
  228. * The associativity of an operator OP determines how repeated uses
  229. of the operator nest: whether `X OP Y OP Z' is parsed by grouping
  230. X with Y first or by grouping Y with Z first. `%left' specifies
  231. left-associativity (grouping X with Y first) and `%right'
  232. specifies right-associativity (grouping Y with Z first).
  233. `%nonassoc' specifies no associativity, which means that `X OP Y
  234. OP Z' is considered a syntax error.
  235. * The precedence of an operator determines how it nests with other
  236. operators. All the tokens declared in a single precedence
  237. declaration have equal precedence and nest together according to
  238. their associativity. When two tokens declared in different
  239. precedence declarations associate, the one declared later has the
  240. higher precedence and is grouped first.
  241. 
  242. File: bison.info, Node: Union Decl, Next: Type Decl, Prev: Precedence Decl, Up: Declarations
  243. The Collection of Value Types
  244. -----------------------------
  245. The `%union' declaration specifies the entire collection of possible
  246. data types for semantic values. The keyword `%union' is followed by a
  247. pair of braces containing the same thing that goes inside a `union' in
  248. C.
  249. For example:
  250. %union {
  251. double val;
  252. symrec *tptr;
  253. }
  254. This says that the two alternative types are `double' and `symrec *'.
  255. They are given names `val' and `tptr'; these names are used in the
  256. `%token' and `%type' declarations to pick one of the types for a
  257. terminal or nonterminal symbol (*note Nonterminal Symbols: Type Decl.).
  258. Note that, unlike making a `union' declaration in C, you do not write
  259. a semicolon after the closing brace.
  260. 
  261. File: bison.info, Node: Type Decl, Next: Expect Decl, Prev: Union Decl, Up: Declarations
  262. Nonterminal Symbols
  263. -------------------
  264. When you use `%union' to specify multiple value types, you must declare
  265. the value type of each nonterminal symbol for which values are used.
  266. This is done with a `%type' declaration, like this:
  267. %type <TYPE> NONTERMINAL...
  268. Here NONTERMINAL is the name of a nonterminal symbol, and TYPE is the
  269. name given in the `%union' to the alternative that you want (*note The
  270. Collection of Value Types: Union Decl.). You can give any number of
  271. nonterminal symbols in the same `%type' declaration, if they have the
  272. same value type. Use spaces to separate the symbol names.
  273. The value types of symbols may be declared by putting `<'TYPE`>'
  274. after any of these declarators: `%type', `%token', `%term', `%nterm',
  275. `%equiv', `%assoc', `%left', `%right', `%nonassoc', `%binary'.
  276. 
  277. File: bison.info, Node: Expect Decl, Next: Start Decl, Prev: Type Decl, Up: Declarations
  278. Suppressing Conflict Warnings
  279. -----------------------------
  280. Bison normally warns if there are any conflicts in the grammar
  281. (*note Shift/Reduce Conflicts: Shift/Reduce.), but most real grammars
  282. have harmless shift/reduce conflicts which are resolved in a
  283. predictable way and would be difficult to eliminate. It is desirable
  284. to suppress the warning about these conflicts unless the number of
  285. conflicts changes. You can do this with the `%expect' declaration.
  286. The declaration looks like this:
  287. %expect N
  288. Here N is a decimal integer. The declaration says there should be no
  289. warning if there are N shift/reduce conflicts and no reduce/reduce
  290. conflicts. The usual warning is given if there are either more or fewer
  291. conflicts, or if there are any reduce/reduce conflicts.
  292. In general, using `%expect' involves these steps:
  293. * Compile your grammar without `%expect'. Use the `-v' option to
  294. get a verbose list of where the conflicts occur. Bison will also
  295. print the number of conflicts.
  296. * Check each of the conflicts to make sure that Bison's default
  297. resolution is what you really want. If not, rewrite the grammar
  298. and go back to the beginning.
  299. * Add an `%expect' declaration, copying the number N from the number
  300. which Bison printed.
  301. Now Bison will stop annoying you about the conflicts you have
  302. checked, but it will warn you again if changes in the grammar result in
  303. additional conflicts.
  304. 
  305. File: bison.info, Node: Start Decl, Next: Pure Decl, Prev: Expect Decl, Up: Declarations
  306. The Start-Symbol
  307. ----------------
  308. Bison assumes by default that the start symbol for the grammar is
  309. the first nonterminal specified in the grammar specification section.
  310. The programmer may override this restriction with the `%start'
  311. declaration as follows:
  312. %start SYMBOL
  313. 
  314. File: bison.info, Node: Pure Decl, Next: Decl Summary, Prev: Start Decl, Up: Declarations
  315. A Pure (Reentrant) Parser
  316. -------------------------
  317. A "reentrant" program is one which does not alter in the course of
  318. execution; in other words, it consists entirely of "pure" (read-only)
  319. code. Reentrancy is important whenever asynchronous execution is
  320. possible; for example, a nonreentrant program may not be safe to call
  321. from a signal handler. In systems with multiple threads of control, a
  322. nonreentrant program must be called only within interlocks.
  323. The Bison parser is not normally a reentrant program, because it uses
  324. statically allocated variables for communication with `yylex'. These
  325. variables include `yylval' and `yylloc'.
  326. The Bison declaration `%pure_parser' says that you want the parser
  327. to be reentrant. It looks like this:
  328. %pure_parser
  329. The effect is that the two communication variables become local
  330. variables in `yyparse', and a different calling convention is used for
  331. the lexical analyzer function `yylex'. *Note Calling for Pure Parsers:
  332. Pure Calling, for the details of this. The variable `yynerrs' also
  333. becomes local in `yyparse' (*note The Error Reporting Function
  334. `yyerror': Error Reporting.). The convention for calling `yyparse'
  335. itself is unchanged.
  336. 
  337. File: bison.info, Node: Decl Summary, Prev: Pure Decl, Up: Declarations
  338. Bison Declaration Summary
  339. -------------------------
  340. Here is a summary of all Bison declarations:
  341. `%union'
  342. `%union'
  343. Declare the collection of data types that semantic values may have
  344. (*note The Collection of Value Types: Union Decl.).
  345. `%token'
  346. Declare a terminal symbol (token type name) with no precedence or
  347. associativity specified (*note Token Type Names: Token Decl.).
  348. `%thong'
  349. Declare a token name to refer to a multiple-character string
  350. literal token. (*note Thong Declarations: Thong Decl..)
  351. `%right'
  352. Declare a terminal symbol (token type name) that is
  353. right-associative (*note Operator Precedence: Precedence Decl.).
  354. `%left'
  355. Declare a terminal symbol (token type name) that is
  356. left-associative (*note Operator Precedence: Precedence Decl.).
  357. `%nonassoc'
  358. Declare a terminal symbol (token type name) that is nonassociative
  359. (using it in a way that would be associative is a syntax error)
  360. (*note Operator Precedence: Precedence Decl.).
  361. `%type'
  362. Declare the type of semantic values for a nonterminal symbol
  363. (*note Nonterminal Symbols: Type Decl.).
  364. `%start'
  365. Specify the grammar's start symbol (*note The Start-Symbol: Start
  366. Decl.).
  367. `%expect'
  368. Declare the expected number of shift-reduce conflicts (*note
  369. Suppressing Conflict Warnings: Expect Decl.).
  370. `%pure_parser'
  371. Request a pure (reentrant) parser program (*note A Pure
  372. (Reentrant) Parser: Pure Decl.).
  373. `%semantic_parser'
  374. This causes Bison to utilize a more sophisticated parsing machine.
  375. It is only appropriate if you have specified `%guard{...}'
  376. segments after one or more rules of the grammar.
  377. 
  378. File: bison.info, Node: Multiple Parsers, Prev: Declarations, Up: Grammar File
  379. Multiple Parsers in the Same Program
  380. ====================================
  381. Most programs that use Bison parse only one language and therefore
  382. contain only one Bison parser. But what if you want to parse more than
  383. one language with the same program? Then you need to avoid a name
  384. conflict between different definitions of `yyparse', `yylval', and so
  385. on.
  386. The easy way to do this is to use the option `-p PREFIX' (*note
  387. Invoking Bison: Invocation.). This renames the interface functions and
  388. variables of the Bison parser to start with PREFIX instead of `yy'.
  389. You can use this to give each parser distinct names that do not
  390. conflict.
  391. The precise list of symbols renamed is `yyparse', `yylex',
  392. `yyerror', `yylval', `yychar' and `yydebug'. For example, if you use
  393. `-p c', the names become `cparse', `clex', and so on.
  394. *All the other variables and macros associated with Bison are not
  395. renamed.* These others are not global; there is no conflict if the same
  396. name is used in different parsers. For example, `YYSTYPE' is not
  397. renamed, but defining this in different ways in different parsers causes
  398. no trouble (*note Data Types of Semantic Values: Value Type.).
  399. The `-p' option works by adding macro definitions to the beginning
  400. of the parser source file, defining `yyparse' as `PREFIXparse', and so
  401. on. This effectively substitutes one name for the other in the entire
  402. parser file.
  403. 
  404. File: bison.info, Node: Interface, Next: Algorithm, Prev: Grammar File, Up: Top
  405. Parser C-Language Interface
  406. ***************************
  407. The Bison parser is actually a C function named `yyparse'. Here we
  408. describe the interface conventions of `yyparse' and the other functions
  409. that it needs to use.
  410. Keep in mind that the parser uses many C identifiers starting with
  411. `yy' and `YY' for internal purposes. If you use such an identifier
  412. (aside from those in this manual) in an action or in additional C code
  413. in the grammar file, you are likely to run into trouble.
  414. * Menu:
  415. * Parser Function:: How to call `yyparse' and what it returns.
  416. * Lexical:: You must supply a function `yylex'
  417. which reads tokens.
  418. * Error Reporting:: You must supply a function `yyerror'.
  419. * Action Features:: Special features for use in actions.
  420. 
  421. File: bison.info, Node: Parser Function, Next: Lexical, Up: Interface
  422. The Parser Function `yyparse'
  423. =============================
  424. You call the function `yyparse' to cause parsing to occur. This
  425. function reads tokens, executes actions, and ultimately returns when it
  426. encounters end-of-input or an unrecoverable syntax error. You can also
  427. write an action which directs `yyparse' to return immediately without
  428. reading further.
  429. The value returned by `yyparse' is 0 if parsing was successful
  430. (return is due to end-of-input).
  431. The value is 1 if parsing failed (return is due to a syntax error).
  432. In an action, you can cause immediate return from `yyparse' by using
  433. these macros:
  434. `YYACCEPT'
  435. Return immediately with value 0 (to report success).
  436. `YYABORT'
  437. Return immediately with value 1 (to report failure).
  438. 
  439. File: bison.info, Node: Lexical, Next: Error Reporting, Prev: Parser Function, Up: Interface
  440. The Lexical Analyzer Function `yylex'
  441. =====================================
  442. The "lexical analyzer" function, `yylex', recognizes tokens from the
  443. input stream and returns them to the parser. Bison does not create
  444. this function automatically; you must write it so that `yyparse' can
  445. call it. The function is sometimes referred to as a lexical scanner.
  446. In simple programs, `yylex' is often defined at the end of the Bison
  447. grammar file. If `yylex' is defined in a separate source file, you
  448. need to arrange for the token-type macro definitions to be available
  449. there. To do this, use the `-d' option when you run Bison, so that it
  450. will write these macro definitions into a separate header file
  451. `NAME.tab.h' which you can include in the other source files that need
  452. it. *Note Invoking Bison: Invocation.
  453. * Menu:
  454. * Calling Convention:: How `yyparse' calls `yylex'.
  455. * Token Values:: How `yylex' must return the semantic value
  456. of the token it has read.
  457. * Token Positions:: How `yylex' must return the text position
  458. (line number, etc.) of the token, if the
  459. actions want that.
  460. * Pure Calling:: How the calling convention differs
  461. in a pure parser (*note A Pure (Reentrant) Parser: Pure Decl.).
  462. 
  463. File: bison.info, Node: Calling Convention, Next: Token Values, Up: Lexical
  464. Calling Convention for `yylex'
  465. ------------------------------
  466. The value that `yylex' returns must be the numeric code for the type
  467. of token it has just found, or 0 for end-of-input.
  468. When a token is referred to in the grammar rules by a name, that name
  469. in the parser file becomes a C macro whose definition is the proper
  470. numeric code for that token type. So `yylex' can use the name to
  471. indicate that type. *Note Symbols - Terminal and Nonterminal: Symbols.
  472. When a token is referred to in the grammar rules by a character
  473. literal, the numeric code for that character is also the code for the
  474. token type. So `yylex' can simply return that character code. The
  475. null character must not be used this way, because its code is zero and
  476. that is what signifies end-of-input.
  477. Here is an example showing these things:
  478. yylex ()
  479. {
  480. ...
  481. if (c == EOF) /* Detect end of file. */
  482. return 0;
  483. ...
  484. if (c == '+' || c == '-')
  485. return c; /* Assume token type for `+' is '+'. */
  486. ...
  487. return INT; /* Return the type of the token. */
  488. ...
  489. }
  490. This interface has been designed so that the output from the `lex'
  491. utility can be used without change as the definition of `yylex'.
  492. 
  493. File: bison.info, Node: Token Values, Next: Token Positions, Prev: Calling Convention, Up: Lexical
  494. Semantic Values of Tokens
  495. -------------------------
  496. In an ordinary (nonreentrant) parser, the semantic value of the
  497. token must be stored into the global variable `yylval'. When you are
  498. using just one data type for semantic values, `yylval' has that type.
  499. Thus, if the type is `int' (the default), you might write this in
  500. `yylex':
  501. ...
  502. yylval = value; /* Put value onto Bison stack. */
  503. return INT; /* Return the type of the token. */
  504. ...
  505. When you are using multiple data types, `yylval''s type is a union
  506. made from the `%union' declaration (*note The Collection of Value
  507. Types: Union Decl.). So when you store a token's value, you must use
  508. the proper member of the union. If the `%union' declaration looks like
  509. this:
  510. %union {
  511. int intval;
  512. double val;
  513. symrec *tptr;
  514. }
  515. then the code in `yylex' might look like this:
  516. ...
  517. yylval.intval = value; /* Put value onto Bison stack. */
  518. return INT; /* Return the type of the token. */
  519. ...
  520. 
  521. File: bison.info, Node: Token Positions, Next: Pure Calling, Prev: Token Values, Up: Lexical
  522. Textual Positions of Tokens
  523. ---------------------------
  524. If you are using the `@N'-feature (*note Special Features for Use in
  525. Actions: Action Features.) in actions to keep track of the textual
  526. locations of tokens and groupings, then you must provide this
  527. information in `yylex'. The function `yyparse' expects to find the
  528. textual location of a token just parsed in the global variable
  529. `yylloc'. So `yylex' must store the proper data in that variable. The
  530. value of `yylloc' is a structure and you need only initialize the
  531. members that are going to be used by the actions. The four members are
  532. called `first_line', `first_column', `last_line' and `last_column'.
  533. Note that the use of this feature makes the parser noticeably slower.
  534. The data type of `yylloc' has the name `YYLTYPE'.
  535. 
  536. File: bison.info, Node: Pure Calling, Prev: Token Positions, Up: Lexical
  537. Calling for Pure Parsers
  538. ------------------------
  539. When you use the Bison declaration `%pure_parser' to request a pure,
  540. reentrant parser, the global communication variables `yylval' and
  541. `yylloc' cannot be used. (*Note A Pure (Reentrant) Parser: Pure Decl.)
  542. In such parsers the two global variables are replaced by pointers
  543. passed as arguments to `yylex'. You must declare them as shown here,
  544. and pass the information back by storing it through those pointers.
  545. yylex (lvalp, llocp)
  546. YYSTYPE *lvalp;
  547. YYLTYPE *llocp;
  548. {
  549. ...
  550. *lvalp = value; /* Put value onto Bison stack. */
  551. return INT; /* Return the type of the token. */
  552. ...
  553. }
  554. If the grammar file does not use the `@' constructs to refer to
  555. textual positions, then the type `YYLTYPE' will not be defined. In
  556. this case, omit the second argument; `yylex' will be called with only
  557. one argument.
  558. 
  559. File: bison.info, Node: Error Reporting, Next: Action Features, Prev: Lexical, Up: Interface
  560. The Error Reporting Function `yyerror'
  561. ======================================
  562. The Bison parser detects a "parse error" or "syntax error" whenever
  563. it reads a token which cannot satisfy any syntax rule. A action in the
  564. grammar can also explicitly proclaim an error, using the macro
  565. `YYERROR' (*note Special Features for Use in Actions: Action Features.).
  566. The Bison parser expects to report the error by calling an error
  567. reporting function named `yyerror', which you must supply. It is
  568. called by `yyparse' whenever a syntax error is found, and it receives
  569. one argument. For a parse error, the string is normally
  570. `"parse error"'.
  571. If you define the macro `YYERROR_VERBOSE' in the Bison declarations
  572. section (*note The Bison Declarations Section: Bison Declarations.),
  573. then Bison provides a more verbose and specific error message string
  574. instead of just plain `"parse error"'. It doesn't matter what
  575. definition you use for `YYERROR_VERBOSE', just whether you define it.
  576. The parser can detect one other kind of error: stack overflow. This
  577. happens when the input contains constructions that are very deeply
  578. nested. It isn't likely you will encounter this, since the Bison
  579. parser extends its stack automatically up to a very large limit. But
  580. if overflow happens, `yyparse' calls `yyerror' in the usual fashion,
  581. except that the argument string is `"parser stack overflow"'.
  582. The following definition suffices in simple programs:
  583. yyerror (s)
  584. char *s;
  585. {
  586. fprintf (stderr, "%s\n", s);
  587. }
  588. After `yyerror' returns to `yyparse', the latter will attempt error
  589. recovery if you have written suitable error recovery grammar rules
  590. (*note Error Recovery::.). If recovery is impossible, `yyparse' will
  591. immediately return 1.
  592. The variable `yynerrs' contains the number of syntax errors
  593. encountered so far. Normally this variable is global; but if you
  594. request a pure parser (*note A Pure (Reentrant) Parser: Pure Decl.)
  595. then it is a local variable which only the actions can access.
  596. 
  597. File: bison.info, Node: Action Features, Prev: Error Reporting, Up: Interface
  598. Special Features for Use in Actions
  599. ===================================
  600. Here is a table of Bison constructs, variables and macros that are
  601. useful in actions.
  602. `$$'
  603. Acts like a variable that contains the semantic value for the
  604. grouping made by the current rule. *Note Actions::.
  605. `$N'
  606. Acts like a variable that contains the semantic value for the Nth
  607. component of the current rule. *Note Actions::.
  608. `$<TYPEALT>$'
  609. Like `$$' but specifies alternative TYPEALT in the union specified
  610. by the `%union' declaration. *Note Data Types of Values in
  611. Actions: Action Types.
  612. `$<TYPEALT>N'
  613. Like `$N' but specifies alternative TYPEALT in the union specified
  614. by the `%union' declaration. *Note Data Types of Values in
  615. Actions: Action Types.
  616. `YYABORT;'
  617. Return immediately from `yyparse', indicating failure. *Note The
  618. Parser Function `yyparse': Parser Function.
  619. `YYACCEPT;'
  620. Return immediately from `yyparse', indicating success. *Note The
  621. Parser Function `yyparse': Parser Function.
  622. `YYBACKUP (TOKEN, VALUE);'
  623. Unshift a token. This macro is allowed only for rules that reduce
  624. a single value, and only when there is no look-ahead token. It
  625. installs a look-ahead token with token type TOKEN and semantic
  626. value VALUE; then it discards the value that was going to be
  627. reduced by this rule.
  628. If the macro is used when it is not valid, such as when there is a
  629. look-ahead token already, then it reports a syntax error with a
  630. message `cannot back up' and performs ordinary error recovery.
  631. In either case, the rest of the action is not executed.
  632. `YYEMPTY'
  633. Value stored in `yychar' when there is no look-ahead token.
  634. `YYERROR;'
  635. Cause an immediate syntax error. This statement initiates error
  636. recovery just as if the parser itself had detected an error;
  637. however, it does not call `yyerror', and does not print any
  638. message. If you want to print an error message, call `yyerror'
  639. explicitly before the `YYERROR;' statement. *Note Error
  640. Recovery::.
  641. `YYRECOVERING'
  642. This macro stands for an expression that has the value 1 when the
  643. parser is recovering from a syntax error, and 0 the rest of the
  644. time. *Note Error Recovery::.
  645. `yychar'
  646. Variable containing the current look-ahead token. (In a pure
  647. parser, this is actually a local variable within `yyparse'.) When
  648. there is no look-ahead token, the value `YYEMPTY' is stored in the
  649. variable. *Note Look-Ahead Tokens: Look-Ahead.
  650. `yyclearin;'
  651. Discard the current look-ahead token. This is useful primarily in
  652. error rules. *Note Error Recovery::.
  653. `yyerrok;'
  654. Resume generating error messages immediately for subsequent syntax
  655. errors. This is useful primarily in error rules. *Note Error
  656. Recovery::.
  657. `@N'
  658. Acts like a structure variable containing information on the line
  659. numbers and column numbers of the Nth component of the current
  660. rule. The structure has four members, like this:
  661. struct {
  662. int first_line, last_line;
  663. int first_column, last_column;
  664. };
  665. Thus, to get the starting line number of the third component, use
  666. `@3.first_line'.
  667. In order for the members of this structure to contain valid
  668. information, you must make `yylex' supply this information about
  669. each token. If you need only certain members, then `yylex' need
  670. only fill in those members.
  671. The use of this feature makes the parser noticeably slower.
  672. 
  673. File: bison.info, Node: Algorithm, Next: Error Recovery, Prev: Interface, Up: Top
  674. The Bison Parser Algorithm
  675. **************************
  676. As Bison reads tokens, it pushes them onto a stack along with their
  677. semantic values. The stack is called the "parser stack". Pushing a
  678. token is traditionally called "shifting".
  679. For example, suppose the infix calculator has read `1 + 5 *', with a
  680. `3' to come. The stack will have four elements, one for each token
  681. that was shifted.
  682. But the stack does not always have an element for each token read.
  683. When the last N tokens and groupings shifted match the components of a
  684. grammar rule, they can be combined according to that rule. This is
  685. called "reduction". Those tokens and groupings are replaced on the
  686. stack by a single grouping whose symbol is the result (left hand side)
  687. of that rule. Running the rule's action is part of the process of
  688. reduction, because this is what computes the semantic value of the
  689. resulting grouping.
  690. For example, if the infix calculator's parser stack contains this:
  691. 1 + 5 * 3
  692. and the next input token is a newline character, then the last three
  693. elements can be reduced to 15 via the rule:
  694. expr: expr '*' expr;
  695. Then the stack contains just these three elements:
  696. 1 + 15
  697. At this point, another reduction can be made, resulting in the single
  698. value 16. Then the newline token can be shifted.
  699. The parser tries, by shifts and reductions, to reduce the entire
  700. input down to a single grouping whose symbol is the grammar's
  701. start-symbol (*note Languages and Context-Free Grammars: Language and
  702. Grammar.).
  703. This kind of parser is known in the literature as a bottom-up parser.
  704. * Menu:
  705. * Look-Ahead:: Parser looks one token ahead when deciding what to do.
  706. * Shift/Reduce:: Conflicts: when either shifting or reduction is valid.
  707. * Precedence:: Operator precedence works by resolving conflicts.
  708. * Contextual Precedence:: When an operator's precedence depends on context.
  709. * Parser States:: The parser is a finite-state-machine with stack.
  710. * Reduce/Reduce:: When two rules are applicable in the same situation.
  711. * Mystery Conflicts:: Reduce/reduce conflicts that look unjustified.
  712. * Stack Overflow:: What happens when stack gets full. How to avoid it.
  713. 
  714. File: bison.info, Node: Look-Ahead, Next: Shift/Reduce, Up: Algorithm
  715. Look-Ahead Tokens
  716. =================
  717. The Bison parser does *not* always reduce immediately as soon as the
  718. last N tokens and groupings match a rule. This is because such a
  719. simple strategy is inadequate to handle most languages. Instead, when a
  720. reduction is possible, the parser sometimes "looks ahead" at the next
  721. token in order to decide what to do.
  722. When a token is read, it is not immediately shifted; first it
  723. becomes the "look-ahead token", which is not on the stack. Now the
  724. parser can perform one or more reductions of tokens and groupings on
  725. the stack, while the look-ahead token remains off to the side. When no
  726. more reductions should take place, the look-ahead token is shifted onto
  727. the stack. This does not mean that all possible reductions have been
  728. done; depending on the token type of the look-ahead token, some rules
  729. may choose to delay their application.
  730. Here is a simple case where look-ahead is needed. These three rules
  731. define expressions which contain binary addition operators and postfix
  732. unary factorial operators (`!'), and allow parentheses for grouping.
  733. expr: term '+' expr
  734. | term
  735. ;
  736. term: '(' expr ')'
  737. | term '!'
  738. | NUMBER
  739. ;
  740. Suppose that the tokens `1 + 2' have been read and shifted; what
  741. should be done? If the following token is `)', then the first three
  742. tokens must be reduced to form an `expr'. This is the only valid
  743. course, because shifting the `)' would produce a sequence of symbols
  744. `term ')'', and no rule allows this.
  745. If the following token is `!', then it must be shifted immediately so
  746. that `2 !' can be reduced to make a `term'. If instead the parser were
  747. to reduce before shifting, `1 + 2' would become an `expr'. It would
  748. then be impossible to shift the `!' because doing so would produce on
  749. the stack the sequence of symbols `expr '!''. No rule allows that
  750. sequence.
  751. The current look-ahead token is stored in the variable `yychar'.
  752. *Note Special Features for Use in Actions: Action Features.
  753. 
  754. File: bison.info, Node: Shift/Reduce, Next: Precedence, Prev: Look-Ahead, Up: Algorithm
  755. Shift/Reduce Conflicts
  756. ======================
  757. Suppose we are parsing a language which has if-then and if-then-else
  758. statements, with a pair of rules like this:
  759. if_stmt:
  760. IF expr THEN stmt
  761. | IF expr THEN stmt ELSE stmt
  762. ;
  763. Here we assume that `IF', `THEN' and `ELSE' are terminal symbols for
  764. specific keyword tokens.
  765. When the `ELSE' token is read and becomes the look-ahead token, the
  766. contents of the stack (assuming the input is valid) are just right for
  767. reduction by the first rule. But it is also legitimate to shift the
  768. `ELSE', because that would lead to eventual reduction by the second
  769. rule.
  770. This situation, where either a shift or a reduction would be valid,
  771. is called a "shift/reduce conflict". Bison is designed to resolve
  772. these conflicts by choosing to shift, unless otherwise directed by
  773. operator precedence declarations. To see the reason for this, let's
  774. contrast it with the other alternative.
  775. Since the parser prefers to shift the `ELSE', the result is to attach
  776. the else-clause to the innermost if-statement, making these two inputs
  777. equivalent:
  778. if x then if y then win (); else lose;
  779. if x then do; if y then win (); else lose; end;
  780. But if the parser chose to reduce when possible rather than shift,
  781. the result would be to attach the else-clause to the outermost
  782. if-statement, making these two inputs equivalent:
  783. if x then if y then win (); else lose;
  784. if x then do; if y then win (); end; else lose;
  785. The conflict exists because the grammar as written is ambiguous:
  786. either parsing of the simple nested if-statement is legitimate. The
  787. established convention is that these ambiguities are resolved by
  788. attaching the else-clause to the innermost if-statement; this is what
  789. Bison accomplishes by choosing to shift rather than reduce. (It would
  790. ideally be cleaner to write an unambiguous grammar, but that is very
  791. hard to do in this case.) This particular ambiguity was first
  792. encountered in the specifications of Algol 60 and is called the
  793. "dangling `else'" ambiguity.
  794. To avoid warnings from Bison about predictable, legitimate
  795. shift/reduce conflicts, use the `%expect N' declaration. There will be
  796. no warning as long as the number of shift/reduce conflicts is exactly N.
  797. *Note Suppressing Conflict Warnings: Expect Decl.
  798. The definition of `if_stmt' above is solely to blame for the
  799. conflict, but the conflict does not actually appear without additional
  800. rules. Here is a complete Bison input file that actually manifests the
  801. conflict:
  802. %token IF THEN ELSE variable
  803. %%
  804. stmt: expr
  805. | if_stmt
  806. ;
  807. if_stmt:
  808. IF expr THEN stmt
  809. | IF expr THEN stmt ELSE stmt
  810. ;
  811. expr: variable
  812. ;
  813. 
  814. File: bison.info, Node: Precedence, Next: Contextual Precedence, Prev: Shift/Reduce, Up: Algorithm
  815. Operator Precedence
  816. ===================
  817. Another situation where shift/reduce conflicts appear is in
  818. arithmetic expressions. Here shifting is not always the preferred
  819. resolution; the Bison declarations for operator precedence allow you to
  820. specify when to shift and when to reduce.
  821. * Menu:
  822. * Why Precedence:: An example showing why precedence is needed.
  823. * Using Precedence:: How to specify precedence in Bison grammars.
  824. * Precedence Examples:: How these features are used in the previous example.
  825. * How Precedence:: How they work.
  826. 
  827. File: bison.info, Node: Why Precedence, Next: Using Precedence, Up: Precedence
  828. When Precedence is Needed
  829. -------------------------
  830. Consider the following ambiguous grammar fragment (ambiguous because
  831. the input `1 - 2 * 3' can be parsed in two different ways):
  832. expr: expr '-' expr
  833. | expr '*' expr
  834. | expr '<' expr
  835. | '(' expr ')'
  836. ...
  837. ;
  838. Suppose the parser has seen the tokens `1', `-' and `2'; should it
  839. reduce them via the rule for the addition operator? It depends on the
  840. next token. Of course, if the next token is `)', we must reduce;
  841. shifting is invalid because no single rule can reduce the token
  842. sequence `- 2 )' or anything starting with that. But if the next token
  843. is `*' or `<', we have a choice: either shifting or reduction would
  844. allow the parse to complete, but with different results.
  845. To decide which one Bison should do, we must consider the results.
  846. If the next operator token OP is shifted, then it must be reduced first
  847. in order to permit another opportunity to reduce the sum. The result
  848. is (in effect) `1 - (2 OP 3)'. On the other hand, if the subtraction
  849. is reduced before shifting OP, the result is `(1 - 2) OP 3'. Clearly,
  850. then, the choice of shift or reduce should depend on the relative
  851. precedence of the operators `-' and OP: `*' should be shifted first,
  852. but not `<'.
  853. What about input such as `1 - 2 - 5'; should this be `(1 - 2) - 5'
  854. or should it be `1 - (2 - 5)'? For most operators we prefer the
  855. former, which is called "left association". The latter alternative,
  856. "right association", is desirable for assignment operators. The choice
  857. of left or right association is a matter of whether the parser chooses
  858. to shift or reduce when the stack contains `1 - 2' and the look-ahead
  859. token is `-': shifting makes right-associativity.
  860. 
  861. File: bison.info, Node: Using Precedence, Next: Precedence Examples, Prev: Why Precedence, Up: Precedence
  862. Specifying Operator Precedence
  863. ------------------------------
  864. Bison allows you to specify these choices with the operator
  865. precedence declarations `%left' and `%right'. Each such declaration
  866. contains a list of tokens, which are operators whose precedence and
  867. associativity is being declared. The `%left' declaration makes all
  868. those operators left-associative and the `%right' declaration makes
  869. them right-associative. A third alternative is `%nonassoc', which
  870. declares that it is a syntax error to find the same operator twice "in a
  871. row".
  872. The relative precedence of different operators is controlled by the
  873. order in which they are declared. The first `%left' or `%right'
  874. declaration in the file declares the operators whose precedence is
  875. lowest, the next such declaration declares the operators whose
  876. precedence is a little higher, and so on.
  877. 
  878. File: bison.info, Node: Precedence Examples, Next: How Precedence, Prev: Using Precedence, Up: Precedence
  879. Precedence Examples
  880. -------------------
  881. In our example, we would want the following declarations:
  882. %left '<'
  883. %left '-'
  884. %left '*'
  885. In a more complete example, which supports other operators as well,
  886. we would declare them in groups of equal precedence. For example,
  887. `'+'' is declared with `'-'':
  888. %left '<' '>' '=' NE LE GE
  889. %left '+' '-'
  890. %left '*' '/'
  891. (Here `NE' and so on stand for the operators for "not equal" and so on.
  892. We assume that these tokens are more than one character long and
  893. therefore are represented by names, not character literals.)
  894. 
  895. File: bison.info, Node: How Precedence, Prev: Precedence Examples, Up: Precedence
  896. How Precedence Works
  897. --------------------
  898. The first effect of the precedence declarations is to assign
  899. precedence levels to the terminal symbols declared. The second effect
  900. is to assign precedence levels to certain rules: each rule gets its
  901. precedence from the last terminal symbol mentioned in the components.
  902. (You can also specify explicitly the precedence of a rule. *Note
  903. Context-Dependent Precedence: Contextual Precedence.)
  904. Finally, the resolution of conflicts works by comparing the
  905. precedence of the rule being considered with that of the look-ahead
  906. token. If the token's precedence is higher, the choice is to shift.
  907. If the rule's precedence is higher, the choice is to reduce. If they
  908. have equal precedence, the choice is made based on the associativity of
  909. that precedence level. The verbose output file made by `-v' (*note
  910. Invoking Bison: Invocation.) says how each conflict was resolved.
  911. Not all rules and not all tokens have precedence. If either the
  912. rule or the look-ahead token has no precedence, then the default is to
  913. shift.
  914. 
  915. File: bison.info, Node: Contextual Precedence, Next: Parser States, Prev: Precedence, Up: Algorithm
  916. Context-Dependent Precedence
  917. ============================
  918. Often the precedence of an operator depends on the context. This
  919. sounds outlandish at first, but it is really very common. For example,
  920. a minus sign typically has a very high precedence as a unary operator,
  921. and a somewhat lower precedence (lower than multiplication) as a binary
  922. operator.
  923. The Bison precedence declarations, `%left', `%right' and
  924. `%nonassoc', can only be used once for a given token; so a token has
  925. only one precedence declared in this way. For context-dependent
  926. precedence, you need to use an additional mechanism: the `%prec'
  927. modifier for rules.
  928. The `%prec' modifier declares the precedence of a particular rule by
  929. specifying a terminal symbol whose precedence should be used for that
  930. rule. It's not necessary for that symbol to appear otherwise in the
  931. rule. The modifier's syntax is:
  932. %prec TERMINAL-SYMBOL
  933. and it is written after the components of the rule. Its effect is to
  934. assign the rule the precedence of TERMINAL-SYMBOL, overriding the
  935. precedence that would be deduced for it in the ordinary way. The
  936. altered rule precedence then affects how conflicts involving that rule
  937. are resolved (*note Operator Precedence: Precedence.).
  938. Here is how `%prec' solves the problem of unary minus. First,
  939. declare a precedence for a fictitious terminal symbol named `UMINUS'.
  940. There are no tokens of this type, but the symbol serves to stand for its
  941. precedence:
  942. ...
  943. %left '+' '-'
  944. %left '*'
  945. %left UMINUS
  946. Now the precedence of `UMINUS' can be used in specific rules:
  947. exp: ...
  948. | exp '-' exp
  949. ...
  950. | '-' exp %prec UMINUS
  951. 
  952. File: bison.info, Node: Parser States, Next: Reduce/Reduce, Prev: Contextual Precedence, Up: Algorithm
  953. Parser States
  954. =============
  955. The function `yyparse' is implemented using a finite-state machine.
  956. The values pushed on the parser stack are not simply token type codes;
  957. they represent the entire sequence of terminal and nonterminal symbols
  958. at or near the top of the stack. The current state collects all the
  959. information about previous input which is relevant to deciding what to
  960. do next.
  961. Each time a look-ahead token is read, the current parser state
  962. together with the type of look-ahead token are looked up in a table.
  963. This table entry can say, "Shift the look-ahead token." In this case,
  964. it also specifies the new parser state, which is pushed onto the top of
  965. the parser stack. Or it can say, "Reduce using rule number N." This
  966. means that a certain number of tokens or groupings are taken off the
  967. top of the stack, and replaced by one grouping. In other words, that
  968. number of states are popped from the stack, and one new state is pushed.
  969. There is one other alternative: the table can say that the
  970. look-ahead token is erroneous in the current state. This causes error
  971. processing to begin (*note Error Recovery::.).