]>
Commit | Line | Data |
---|---|---|
1e24cc5b | 1 | This is bison.info, produced by makeinfo version 4.0 from bison.texinfo. |
705db0b5 AD |
2 | |
3 | START-INFO-DIR-ENTRY | |
4 | * bison: (bison). GNU Project parser generator (yacc replacement). | |
5 | END-INFO-DIR-ENTRY | |
6 | ||
7 | This file documents the Bison parser generator. | |
8 | ||
9 | Copyright (C) 1988, 1989, 1990, 1991, 1992, 1993, 1995, 1998, 1999, | |
10 | 2000 Free Software Foundation, Inc. | |
11 | ||
12 | Permission is granted to make and distribute verbatim copies of this | |
13 | manual provided the copyright notice and this permission notice are | |
14 | preserved on all copies. | |
15 | ||
16 | Permission is granted to copy and distribute modified versions of | |
17 | this manual under the conditions for verbatim copying, provided also | |
18 | that the sections entitled "GNU General Public License" and "Conditions | |
19 | for Using Bison" are included exactly as in the original, and provided | |
20 | that the entire resulting derived work is distributed under the terms | |
21 | of a permission notice identical to this one. | |
22 | ||
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 | \1f | |
31 | File: bison.info, Node: Top, Next: Introduction, Prev: (dir), Up: (dir) | |
32 | ||
33 | This manual documents version 1.28a of Bison. | |
34 | ||
35 | * Menu: | |
36 | ||
37 | * Introduction:: | |
38 | * Conditions:: | |
39 | * Copying:: The GNU General Public License says | |
40 | how you can copy and share Bison | |
41 | ||
42 | Tutorial sections: | |
43 | * Concepts:: Basic concepts for understanding Bison. | |
44 | * Examples:: Three simple explained examples of using Bison. | |
45 | ||
46 | Reference sections: | |
47 | * Grammar File:: Writing Bison declarations and rules. | |
48 | * Interface:: C-language interface to the parser function `yyparse'. | |
49 | * Algorithm:: How the Bison parser works at run-time. | |
50 | * Error Recovery:: Writing rules for error recovery. | |
51 | * Context Dependency:: What to do if your language syntax is too | |
52 | messy for Bison to handle straightforwardly. | |
53 | * Debugging:: Debugging Bison parsers that parse wrong. | |
54 | * Invocation:: How to run Bison (to produce the parser source file). | |
55 | * Table of Symbols:: All the keywords of the Bison language are explained. | |
56 | * Glossary:: Basic concepts are explained. | |
57 | * Index:: Cross-references to the text. | |
58 | ||
59 | --- The Detailed Node Listing --- | |
60 | ||
61 | The Concepts of Bison | |
62 | ||
63 | * Language and Grammar:: Languages and context-free grammars, | |
64 | as mathematical ideas. | |
65 | * Grammar in Bison:: How we represent grammars for Bison's sake. | |
66 | * Semantic Values:: Each token or syntactic grouping can have | |
67 | a semantic value (the value of an integer, | |
68 | the name of an identifier, etc.). | |
69 | * Semantic Actions:: Each rule can have an action containing C code. | |
70 | * Bison Parser:: What are Bison's input and output, | |
71 | how is the output used? | |
72 | * Stages:: Stages in writing and running Bison grammars. | |
73 | * Grammar Layout:: Overall structure of a Bison grammar file. | |
74 | ||
75 | Examples | |
76 | ||
77 | * RPN Calc:: Reverse polish notation calculator; | |
78 | a first example with no operator precedence. | |
79 | * Infix Calc:: Infix (algebraic) notation calculator. | |
80 | Operator precedence is introduced. | |
81 | * Simple Error Recovery:: Continuing after syntax errors. | |
82 | * Multi-function Calc:: Calculator with memory and trig functions. | |
83 | It uses multiple data-types for semantic values. | |
84 | * Exercises:: Ideas for improving the multi-function calculator. | |
85 | ||
86 | Reverse Polish Notation Calculator | |
87 | ||
88 | * Decls: Rpcalc Decls. Bison and C declarations for rpcalc. | |
89 | * Rules: Rpcalc Rules. Grammar Rules for rpcalc, with explanation. | |
90 | * Lexer: Rpcalc Lexer. The lexical analyzer. | |
91 | * Main: Rpcalc Main. The controlling function. | |
92 | * Error: Rpcalc Error. The error reporting function. | |
93 | * Gen: Rpcalc Gen. Running Bison on the grammar file. | |
94 | * Comp: Rpcalc Compile. Run the C compiler on the output code. | |
95 | ||
96 | Grammar Rules for `rpcalc' | |
97 | ||
98 | * Rpcalc Input:: | |
99 | * Rpcalc Line:: | |
100 | * Rpcalc Expr:: | |
101 | ||
102 | Multi-Function Calculator: `mfcalc' | |
103 | ||
104 | * Decl: Mfcalc Decl. Bison declarations for multi-function calculator. | |
105 | * Rules: Mfcalc Rules. Grammar rules for the calculator. | |
106 | * Symtab: Mfcalc Symtab. Symbol table management subroutines. | |
107 | ||
108 | Bison Grammar Files | |
109 | ||
110 | * Grammar Outline:: Overall layout of the grammar file. | |
111 | * Symbols:: Terminal and nonterminal symbols. | |
112 | * Rules:: How to write grammar rules. | |
113 | * Recursion:: Writing recursive rules. | |
114 | * Semantics:: Semantic values and actions. | |
115 | * Declarations:: All kinds of Bison declarations are described here. | |
116 | * Multiple Parsers:: Putting more than one Bison parser in one program. | |
117 | ||
118 | Outline of a Bison Grammar | |
119 | ||
120 | * C Declarations:: Syntax and usage of the C declarations section. | |
121 | * Bison Declarations:: Syntax and usage of the Bison declarations section. | |
122 | * Grammar Rules:: Syntax and usage of the grammar rules section. | |
123 | * C Code:: Syntax and usage of the additional C code section. | |
124 | ||
125 | Defining Language Semantics | |
126 | ||
127 | * Value Type:: Specifying one data type for all semantic values. | |
128 | * Multiple Types:: Specifying several alternative data types. | |
129 | * Actions:: An action is the semantic definition of a grammar rule. | |
130 | * Action Types:: Specifying data types for actions to operate on. | |
131 | * Mid-Rule Actions:: Most actions go at the end of a rule. | |
132 | This says when, why and how to use the exceptional | |
133 | action in the middle of a rule. | |
134 | ||
135 | Bison Declarations | |
136 | ||
137 | * Token Decl:: Declaring terminal symbols. | |
138 | * Precedence Decl:: Declaring terminals with precedence and associativity. | |
139 | * Union Decl:: Declaring the set of all semantic value types. | |
140 | * Type Decl:: Declaring the choice of type for a nonterminal symbol. | |
141 | * Expect Decl:: Suppressing warnings about shift/reduce conflicts. | |
142 | * Start Decl:: Specifying the start symbol. | |
143 | * Pure Decl:: Requesting a reentrant parser. | |
144 | * Decl Summary:: Table of all Bison declarations. | |
145 | ||
146 | Parser C-Language Interface | |
147 | ||
148 | * Parser Function:: How to call `yyparse' and what it returns. | |
149 | * Lexical:: You must supply a function `yylex' | |
150 | which reads tokens. | |
151 | * Error Reporting:: You must supply a function `yyerror'. | |
152 | * Action Features:: Special features for use in actions. | |
153 | ||
154 | The Lexical Analyzer Function `yylex' | |
155 | ||
156 | * Calling Convention:: How `yyparse' calls `yylex'. | |
157 | * Token Values:: How `yylex' must return the semantic value | |
158 | of the token it has read. | |
159 | * Token Positions:: How `yylex' must return the text position | |
160 | (line number, etc.) of the token, if the | |
161 | actions want that. | |
162 | * Pure Calling:: How the calling convention differs | |
163 | in a pure parser (*note A Pure (Reentrant) Parser: Pure Decl.). | |
164 | ||
165 | The Bison Parser Algorithm | |
166 | ||
167 | * Look-Ahead:: Parser looks one token ahead when deciding what to do. | |
168 | * Shift/Reduce:: Conflicts: when either shifting or reduction is valid. | |
169 | * Precedence:: Operator precedence works by resolving conflicts. | |
170 | * Contextual Precedence:: When an operator's precedence depends on context. | |
171 | * Parser States:: The parser is a finite-state-machine with stack. | |
172 | * Reduce/Reduce:: When two rules are applicable in the same situation. | |
173 | * Mystery Conflicts:: Reduce/reduce conflicts that look unjustified. | |
174 | * Stack Overflow:: What happens when stack gets full. How to avoid it. | |
175 | ||
176 | Operator Precedence | |
177 | ||
178 | * Why Precedence:: An example showing why precedence is needed. | |
179 | * Using Precedence:: How to specify precedence in Bison grammars. | |
180 | * Precedence Examples:: How these features are used in the previous example. | |
181 | * How Precedence:: How they work. | |
182 | ||
183 | Handling Context Dependencies | |
184 | ||
185 | * Semantic Tokens:: Token parsing can depend on the semantic context. | |
186 | * Lexical Tie-ins:: Token parsing can depend on the syntactic context. | |
187 | * Tie-in Recovery:: Lexical tie-ins have implications for how | |
188 | error recovery rules must be written. | |
189 | ||
190 | Invoking Bison | |
191 | ||
192 | * Bison Options:: All the options described in detail, | |
193 | in alphabetical order by short options. | |
194 | * Option Cross Key:: Alphabetical list of long options. | |
195 | * VMS Invocation:: Bison command syntax on VMS. | |
196 | ||
197 | \1f | |
198 | File: bison.info, Node: Introduction, Next: Conditions, Prev: Top, Up: Top | |
199 | ||
200 | Introduction | |
201 | ************ | |
202 | ||
203 | "Bison" is a general-purpose parser generator that converts a | |
204 | grammar description for an LALR(1) context-free grammar into a C | |
205 | program to parse that grammar. Once you are proficient with Bison, you | |
206 | may use it to develop a wide range of language parsers, from those used | |
207 | in simple desk calculators to complex programming languages. | |
208 | ||
209 | Bison is upward compatible with Yacc: all properly-written Yacc | |
210 | grammars ought to work with Bison with no change. Anyone familiar with | |
211 | Yacc should be able to use Bison with little trouble. You need to be | |
212 | fluent in C programming in order to use Bison or to understand this | |
213 | manual. | |
214 | ||
215 | We begin with tutorial chapters that explain the basic concepts of | |
216 | using Bison and show three explained examples, each building on the | |
217 | last. If you don't know Bison or Yacc, start by reading these | |
218 | chapters. Reference chapters follow which describe specific aspects of | |
219 | Bison in detail. | |
220 | ||
221 | Bison was written primarily by Robert Corbett; Richard Stallman made | |
222 | it Yacc-compatible. Wilfred Hansen of Carnegie Mellon University added | |
223 | multi-character string literals and other features. | |
224 | ||
225 | This edition corresponds to version 1.28a of Bison. | |
226 | ||
227 | \1f | |
228 | File: bison.info, Node: Conditions, Next: Copying, Prev: Introduction, Up: Top | |
229 | ||
230 | Conditions for Using Bison | |
231 | ************************** | |
232 | ||
233 | As of Bison version 1.24, we have changed the distribution terms for | |
234 | `yyparse' to permit using Bison's output in nonfree programs. | |
235 | Formerly, Bison parsers could be used only in programs that were free | |
236 | software. | |
237 | ||
238 | The other GNU programming tools, such as the GNU C compiler, have | |
239 | never had such a requirement. They could always be used for nonfree | |
240 | software. The reason Bison was different was not due to a special | |
241 | policy decision; it resulted from applying the usual General Public | |
242 | License to all of the Bison source code. | |
243 | ||
244 | The output of the Bison utility--the Bison parser file--contains a | |
245 | verbatim copy of a sizable piece of Bison, which is the code for the | |
246 | `yyparse' function. (The actions from your grammar are inserted into | |
247 | this function at one point, but the rest of the function is not | |
248 | changed.) When we applied the GPL terms to the code for `yyparse', the | |
249 | effect was to restrict the use of Bison output to free software. | |
250 | ||
251 | We didn't change the terms because of sympathy for people who want to | |
252 | make software proprietary. *Software should be free.* But we | |
253 | concluded that limiting Bison's use to free software was doing little to | |
254 | encourage people to make other software free. So we decided to make the | |
255 | practical conditions for using Bison match the practical conditions for | |
256 | using the other GNU tools. | |
257 | ||
258 | \1f | |
259 | File: bison.info, Node: Copying, Next: Concepts, Prev: Conditions, Up: Top | |
260 | ||
261 | GNU GENERAL PUBLIC LICENSE | |
262 | ************************** | |
263 | ||
264 | Version 2, June 1991 | |
265 | ||
266 | Copyright (C) 1989, 1991 Free Software Foundation, Inc. | |
267 | 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA | |
268 | ||
269 | Everyone is permitted to copy and distribute verbatim copies | |
270 | of this license document, but changing it is not allowed. | |
271 | ||
272 | Preamble | |
273 | ======== | |
274 | ||
275 | The licenses for most software are designed to take away your | |
276 | freedom to share and change it. By contrast, the GNU General Public | |
277 | License is intended to guarantee your freedom to share and change free | |
278 | software--to make sure the software is free for all its users. This | |
279 | General Public License applies to most of the Free Software | |
280 | Foundation's software and to any other program whose authors commit to | |
281 | using it. (Some other Free Software Foundation software is covered by | |
282 | the GNU Library General Public License instead.) You can apply it to | |
283 | your programs, too. | |
284 | ||
285 | When we speak of free software, we are referring to freedom, not | |
286 | price. Our General Public Licenses are designed to make sure that you | |
287 | have the freedom to distribute copies of free software (and charge for | |
288 | this service if you wish), that you receive source code or can get it | |
289 | if you want it, that you can change the software or use pieces of it in | |
290 | new free programs; and that you know you can do these things. | |
291 | ||
292 | To protect your rights, we need to make restrictions that forbid | |
293 | anyone to deny you these rights or to ask you to surrender the rights. | |
294 | These restrictions translate to certain responsibilities for you if you | |
295 | distribute copies of the software, or if you modify it. | |
296 | ||
297 | For example, if you distribute copies of such a program, whether | |
298 | gratis or for a fee, you must give the recipients all the rights that | |
299 | you have. You must make sure that they, too, receive or can get the | |
300 | source code. And you must show them these terms so they know their | |
301 | rights. | |
302 | ||
303 | We protect your rights with two steps: (1) copyright the software, | |
304 | and (2) offer you this license which gives you legal permission to copy, | |
305 | distribute and/or modify the software. | |
306 | ||
307 | Also, for each author's protection and ours, we want to make certain | |
308 | that everyone understands that there is no warranty for this free | |
309 | software. If the software is modified by someone else and passed on, we | |
310 | want its recipients to know that what they have is not the original, so | |
311 | that any problems introduced by others will not reflect on the original | |
312 | authors' reputations. | |
313 | ||
314 | Finally, any free program is threatened constantly by software | |
315 | patents. We wish to avoid the danger that redistributors of a free | |
316 | program will individually obtain patent licenses, in effect making the | |
317 | program proprietary. To prevent this, we have made it clear that any | |
318 | patent must be licensed for everyone's free use or not licensed at all. | |
319 | ||
320 | The precise terms and conditions for copying, distribution and | |
321 | modification follow. | |
322 | ||
323 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION | |
324 | ||
325 | 0. This License applies to any program or other work which contains a | |
326 | notice placed by the copyright holder saying it may be distributed | |
327 | under the terms of this General Public License. The "Program", | |
328 | below, refers to any such program or work, and a "work based on | |
329 | the Program" means either the Program or any derivative work under | |
330 | copyright law: that is to say, a work containing the Program or a | |
331 | portion of it, either verbatim or with modifications and/or | |
332 | translated into another language. (Hereinafter, translation is | |
333 | included without limitation in the term "modification".) Each | |
334 | licensee is addressed as "you". | |
335 | ||
336 | Activities other than copying, distribution and modification are | |
337 | not covered by this License; they are outside its scope. The act | |
338 | of running the Program is not restricted, and the output from the | |
339 | Program is covered only if its contents constitute a work based on | |
340 | the Program (independent of having been made by running the | |
341 | Program). Whether that is true depends on what the Program does. | |
342 | ||
343 | 1. You may copy and distribute verbatim copies of the Program's | |
344 | source code as you receive it, in any medium, provided that you | |
345 | conspicuously and appropriately publish on each copy an appropriate | |
346 | copyright notice and disclaimer of warranty; keep intact all the | |
347 | notices that refer to this License and to the absence of any | |
348 | warranty; and give any other recipients of the Program a copy of | |
349 | this License along with the Program. | |
350 | ||
351 | You may charge a fee for the physical act of transferring a copy, | |
352 | and you may at your option offer warranty protection in exchange | |
353 | for a fee. | |
354 | ||
355 | 2. You may modify your copy or copies of the Program or any portion | |
356 | of it, thus forming a work based on the Program, and copy and | |
357 | distribute such modifications or work under the terms of Section 1 | |
358 | above, provided that you also meet all of these conditions: | |
359 | ||
360 | a. You must cause the modified files to carry prominent notices | |
361 | stating that you changed the files and the date of any change. | |
362 | ||
363 | b. You must cause any work that you distribute or publish, that | |
364 | in whole or in part contains or is derived from the Program | |
365 | or any part thereof, to be licensed as a whole at no charge | |
366 | to all third parties under the terms of this License. | |
367 | ||
368 | c. If the modified program normally reads commands interactively | |
369 | when run, you must cause it, when started running for such | |
370 | interactive use in the most ordinary way, to print or display | |
371 | an announcement including an appropriate copyright notice and | |
372 | a notice that there is no warranty (or else, saying that you | |
373 | provide a warranty) and that users may redistribute the | |
374 | program under these conditions, and telling the user how to | |
375 | view a copy of this License. (Exception: if the Program | |
376 | itself is interactive but does not normally print such an | |
377 | announcement, your work based on the Program is not required | |
378 | to print an announcement.) | |
379 | ||
380 | These requirements apply to the modified work as a whole. If | |
381 | identifiable sections of that work are not derived from the | |
382 | Program, and can be reasonably considered independent and separate | |
383 | works in themselves, then this License, and its terms, do not | |
384 | apply to those sections when you distribute them as separate | |
385 | works. But when you distribute the same sections as part of a | |
386 | whole which is a work based on the Program, the distribution of | |
387 | the whole must be on the terms of this License, whose permissions | |
388 | for other licensees extend to the entire whole, and thus to each | |
389 | and every part regardless of who wrote it. | |
390 | ||
391 | Thus, it is not the intent of this section to claim rights or | |
392 | contest your rights to work written entirely by you; rather, the | |
393 | intent is to exercise the right to control the distribution of | |
394 | derivative or collective works based on the Program. | |
395 | ||
396 | In addition, mere aggregation of another work not based on the | |
397 | Program with the Program (or with a work based on the Program) on | |
398 | a volume of a storage or distribution medium does not bring the | |
399 | other work under the scope of this License. | |
400 | ||
401 | 3. You may copy and distribute the Program (or a work based on it, | |
402 | under Section 2) in object code or executable form under the terms | |
403 | of Sections 1 and 2 above provided that you also do one of the | |
404 | following: | |
405 | ||
406 | a. Accompany it with the complete corresponding machine-readable | |
407 | source code, which must be distributed under the terms of | |
408 | Sections 1 and 2 above on a medium customarily used for | |
409 | software interchange; or, | |
410 | ||
411 | b. Accompany it with a written offer, valid for at least three | |
412 | years, to give any third party, for a charge no more than your | |
413 | cost of physically performing source distribution, a complete | |
414 | machine-readable copy of the corresponding source code, to be | |
415 | distributed under the terms of Sections 1 and 2 above on a | |
416 | medium customarily used for software interchange; or, | |
417 | ||
418 | c. Accompany it with the information you received as to the offer | |
419 | to distribute corresponding source code. (This alternative is | |
420 | allowed only for noncommercial distribution and only if you | |
421 | received the program in object code or executable form with | |
422 | such an offer, in accord with Subsection b above.) | |
423 | ||
424 | The source code for a work means the preferred form of the work for | |
425 | making modifications to it. For an executable work, complete | |
426 | source code means all the source code for all modules it contains, | |
427 | plus any associated interface definition files, plus the scripts | |
428 | used to control compilation and installation of the executable. | |
429 | However, as a special exception, the source code distributed need | |
430 | not include anything that is normally distributed (in either | |
431 | source or binary form) with the major components (compiler, | |
432 | kernel, and so on) of the operating system on which the executable | |
433 | runs, unless that component itself accompanies the executable. | |
434 | ||
435 | If distribution of executable or object code is made by offering | |
436 | access to copy from a designated place, then offering equivalent | |
437 | access to copy the source code from the same place counts as | |
438 | distribution of the source code, even though third parties are not | |
439 | compelled to copy the source along with the object code. | |
440 | ||
441 | 4. You may not copy, modify, sublicense, or distribute the Program | |
442 | except as expressly provided under this License. Any attempt | |
443 | otherwise to copy, modify, sublicense or distribute the Program is | |
444 | void, and will automatically terminate your rights under this | |
445 | License. However, parties who have received copies, or rights, | |
446 | from you under this License will not have their licenses | |
447 | terminated so long as such parties remain in full compliance. | |
448 | ||
449 | 5. You are not required to accept this License, since you have not | |
450 | signed it. However, nothing else grants you permission to modify | |
451 | or distribute the Program or its derivative works. These actions | |
452 | are prohibited by law if you do not accept this License. | |
453 | Therefore, by modifying or distributing the Program (or any work | |
454 | based on the Program), you indicate your acceptance of this | |
455 | License to do so, and all its terms and conditions for copying, | |
456 | distributing or modifying the Program or works based on it. | |
457 | ||
458 | 6. Each time you redistribute the Program (or any work based on the | |
459 | Program), the recipient automatically receives a license from the | |
460 | original licensor to copy, distribute or modify the Program | |
461 | subject to these terms and conditions. You may not impose any | |
462 | further restrictions on the recipients' exercise of the rights | |
463 | granted herein. You are not responsible for enforcing compliance | |
464 | by third parties to this License. | |
465 | ||
466 | 7. If, as a consequence of a court judgment or allegation of patent | |
467 | infringement or for any other reason (not limited to patent | |
468 | issues), conditions are imposed on you (whether by court order, | |
469 | agreement or otherwise) that contradict the conditions of this | |
470 | License, they do not excuse you from the conditions of this | |
471 | License. If you cannot distribute so as to satisfy simultaneously | |
472 | your obligations under this License and any other pertinent | |
473 | obligations, then as a consequence you may not distribute the | |
474 | Program at all. For example, if a patent license would not permit | |
475 | royalty-free redistribution of the Program by all those who | |
476 | receive copies directly or indirectly through you, then the only | |
477 | way you could satisfy both it and this License would be to refrain | |
478 | entirely from distribution of the Program. | |
479 | ||
480 | If any portion of this section is held invalid or unenforceable | |
481 | under any particular circumstance, the balance of the section is | |
482 | intended to apply and the section as a whole is intended to apply | |
483 | in other circumstances. | |
484 | ||
485 | It is not the purpose of this section to induce you to infringe any | |
486 | patents or other property right claims or to contest validity of | |
487 | any such claims; this section has the sole purpose of protecting | |
488 | the integrity of the free software distribution system, which is | |
489 | implemented by public license practices. Many people have made | |
490 | generous contributions to the wide range of software distributed | |
491 | through that system in reliance on consistent application of that | |
492 | system; it is up to the author/donor to decide if he or she is | |
493 | willing to distribute software through any other system and a | |
494 | licensee cannot impose that choice. | |
495 | ||
496 | This section is intended to make thoroughly clear what is believed | |
497 | to be a consequence of the rest of this License. | |
498 | ||
499 | 8. If the distribution and/or use of the Program is restricted in | |
500 | certain countries either by patents or by copyrighted interfaces, | |
501 | the original copyright holder who places the Program under this | |
502 | License may add an explicit geographical distribution limitation | |
503 | excluding those countries, so that distribution is permitted only | |
504 | in or among countries not thus excluded. In such case, this | |
505 | License incorporates the limitation as if written in the body of | |
506 | this License. | |
507 | ||
508 | 9. The Free Software Foundation may publish revised and/or new | |
509 | versions of the General Public License from time to time. Such | |
510 | new versions will be similar in spirit to the present version, but | |
511 | may differ in detail to address new problems or concerns. | |
512 | ||
513 | Each version is given a distinguishing version number. If the | |
514 | Program specifies a version number of this License which applies | |
515 | to it and "any later version", you have the option of following | |
516 | the terms and conditions either of that version or of any later | |
517 | version published by the Free Software Foundation. If the Program | |
518 | does not specify a version number of this License, you may choose | |
519 | any version ever published by the Free Software Foundation. | |
520 | ||
521 | 10. If you wish to incorporate parts of the Program into other free | |
522 | programs whose distribution conditions are different, write to the | |
523 | author to ask for permission. For software which is copyrighted | |
524 | by the Free Software Foundation, write to the Free Software | |
525 | Foundation; we sometimes make exceptions for this. Our decision | |
526 | will be guided by the two goals of preserving the free status of | |
527 | all derivatives of our free software and of promoting the sharing | |
528 | and reuse of software generally. | |
529 | ||
530 | NO WARRANTY | |
531 | ||
532 | 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO | |
533 | WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE | |
534 | LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT | |
535 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT | |
536 | WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT | |
537 | NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND | |
538 | FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE | |
539 | QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE | |
540 | PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY | |
541 | SERVICING, REPAIR OR CORRECTION. | |
542 | ||
543 | 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN | |
544 | WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY | |
545 | MODIFY AND/OR REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE | |
546 | LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, | |
547 | INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR | |
548 | INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF | |
549 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU | |
550 | OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY | |
551 | OTHER PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN | |
552 | ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. | |
553 | ||
554 | END OF TERMS AND CONDITIONS | |
555 | ||
556 | How to Apply These Terms to Your New Programs | |
557 | ============================================= | |
558 | ||
559 | If you develop a new program, and you want it to be of the greatest | |
560 | possible use to the public, the best way to achieve this is to make it | |
561 | free software which everyone can redistribute and change under these | |
562 | terms. | |
563 | ||
564 | To do so, attach the following notices to the program. It is safest | |
565 | to attach them to the start of each source file to most effectively | |
566 | convey the exclusion of warranty; and each file should have at least | |
567 | the "copyright" line and a pointer to where the full notice is found. | |
568 | ||
569 | ONE LINE TO GIVE THE PROGRAM'S NAME AND A BRIEF IDEA OF WHAT IT DOES. | |
570 | Copyright (C) 19YY NAME OF AUTHOR | |
571 | ||
572 | This program is free software; you can redistribute it and/or modify | |
573 | it under the terms of the GNU General Public License as published by | |
574 | the Free Software Foundation; either version 2 of the License, or | |
575 | (at your option) any later version. | |
576 | ||
577 | This program is distributed in the hope that it will be useful, | |
578 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
579 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
580 | GNU General Public License for more details. | |
581 | ||
582 | You should have received a copy of the GNU General Public License | |
583 | along with this program; if not, write to the Free Software | |
584 | Foundation, Inc., 59 Temple Place - Suite 330, | |
585 | Boston, MA 02111-1307, USA. | |
586 | ||
587 | Also add information on how to contact you by electronic and paper | |
588 | mail. | |
589 | ||
590 | If the program is interactive, make it output a short notice like | |
591 | this when it starts in an interactive mode: | |
592 | ||
593 | Gnomovision version 69, Copyright (C) 19YY NAME OF AUTHOR | |
594 | Gnomovision comes with ABSOLUTELY NO WARRANTY; for details | |
595 | type `show w'. | |
596 | This is free software, and you are welcome to redistribute it | |
597 | under certain conditions; type `show c' for details. | |
598 | ||
599 | The hypothetical commands `show w' and `show c' should show the | |
600 | appropriate parts of the General Public License. Of course, the | |
601 | commands you use may be called something other than `show w' and `show | |
602 | c'; they could even be mouse-clicks or menu items--whatever suits your | |
603 | program. | |
604 | ||
605 | You should also get your employer (if you work as a programmer) or | |
606 | your school, if any, to sign a "copyright disclaimer" for the program, | |
607 | if necessary. Here is a sample; alter the names: | |
608 | ||
609 | Yoyodyne, Inc., hereby disclaims all copyright interest in the program | |
610 | `Gnomovision' (which makes passes at compilers) written by James Hacker. | |
611 | ||
612 | SIGNATURE OF TY COON, 1 April 1989 | |
613 | Ty Coon, President of Vice | |
614 | ||
615 | This General Public License does not permit incorporating your | |
616 | program into proprietary programs. If your program is a subroutine | |
617 | library, you may consider it more useful to permit linking proprietary | |
618 | applications with the library. If this is what you want to do, use the | |
619 | GNU Library General Public License instead of this License. | |
620 | ||
621 | \1f | |
622 | File: bison.info, Node: Concepts, Next: Examples, Prev: Copying, Up: Top | |
623 | ||
624 | The Concepts of Bison | |
625 | ********************* | |
626 | ||
627 | This chapter introduces many of the basic concepts without which the | |
628 | details of Bison will not make sense. If you do not already know how to | |
629 | use Bison or Yacc, we suggest you start by reading this chapter | |
630 | carefully. | |
631 | ||
632 | * Menu: | |
633 | ||
634 | * Language and Grammar:: Languages and context-free grammars, | |
635 | as mathematical ideas. | |
636 | * Grammar in Bison:: How we represent grammars for Bison's sake. | |
637 | * Semantic Values:: Each token or syntactic grouping can have | |
638 | a semantic value (the value of an integer, | |
639 | the name of an identifier, etc.). | |
640 | * Semantic Actions:: Each rule can have an action containing C code. | |
641 | * Bison Parser:: What are Bison's input and output, | |
642 | how is the output used? | |
643 | * Stages:: Stages in writing and running Bison grammars. | |
644 | * Grammar Layout:: Overall structure of a Bison grammar file. | |
645 | ||
646 | \1f | |
647 | File: bison.info, Node: Language and Grammar, Next: Grammar in Bison, Up: Concepts | |
648 | ||
649 | Languages and Context-Free Grammars | |
650 | =================================== | |
651 | ||
652 | In order for Bison to parse a language, it must be described by a | |
653 | "context-free grammar". This means that you specify one or more | |
654 | "syntactic groupings" and give rules for constructing them from their | |
655 | parts. For example, in the C language, one kind of grouping is called | |
656 | an `expression'. One rule for making an expression might be, "An | |
657 | expression can be made of a minus sign and another expression". | |
658 | Another would be, "An expression can be an integer". As you can see, | |
659 | rules are often recursive, but there must be at least one rule which | |
660 | leads out of the recursion. | |
661 | ||
662 | The most common formal system for presenting such rules for humans | |
663 | to read is "Backus-Naur Form" or "BNF", which was developed in order to | |
664 | specify the language Algol 60. Any grammar expressed in BNF is a | |
665 | context-free grammar. The input to Bison is essentially | |
666 | machine-readable BNF. | |
667 | ||
668 | Not all context-free languages can be handled by Bison, only those | |
669 | that are LALR(1). In brief, this means that it must be possible to | |
670 | tell how to parse any portion of an input string with just a single | |
671 | token of look-ahead. Strictly speaking, that is a description of an | |
672 | LR(1) grammar, and LALR(1) involves additional restrictions that are | |
673 | hard to explain simply; but it is rare in actual practice to find an | |
674 | LR(1) grammar that fails to be LALR(1). *Note Mysterious Reduce/Reduce | |
675 | Conflicts: Mystery Conflicts, for more information on this. | |
676 | ||
677 | In the formal grammatical rules for a language, each kind of | |
678 | syntactic unit or grouping is named by a "symbol". Those which are | |
679 | built by grouping smaller constructs according to grammatical rules are | |
680 | called "nonterminal symbols"; those which can't be subdivided are called | |
681 | "terminal symbols" or "token types". We call a piece of input | |
682 | corresponding to a single terminal symbol a "token", and a piece | |
683 | corresponding to a single nonterminal symbol a "grouping". | |
684 | ||
685 | We can use the C language as an example of what symbols, terminal and | |
686 | nonterminal, mean. The tokens of C are identifiers, constants (numeric | |
687 | and string), and the various keywords, arithmetic operators and | |
688 | punctuation marks. So the terminal symbols of a grammar for C include | |
689 | `identifier', `number', `string', plus one symbol for each keyword, | |
690 | operator or punctuation mark: `if', `return', `const', `static', `int', | |
691 | `char', `plus-sign', `open-brace', `close-brace', `comma' and many | |
692 | more. (These tokens can be subdivided into characters, but that is a | |
693 | matter of lexicography, not grammar.) | |
694 | ||
695 | Here is a simple C function subdivided into tokens: | |
696 | ||
697 | int /* keyword `int' */ | |
698 | square (x) /* identifier, open-paren, */ | |
699 | /* identifier, close-paren */ | |
700 | int x; /* keyword `int', identifier, semicolon */ | |
701 | { /* open-brace */ | |
702 | return x * x; /* keyword `return', identifier, */ | |
703 | /* asterisk, identifier, semicolon */ | |
704 | } /* close-brace */ | |
705 | ||
706 | The syntactic groupings of C include the expression, the statement, | |
707 | the declaration, and the function definition. These are represented in | |
708 | the grammar of C by nonterminal symbols `expression', `statement', | |
709 | `declaration' and `function definition'. The full grammar uses dozens | |
710 | of additional language constructs, each with its own nonterminal | |
711 | symbol, in order to express the meanings of these four. The example | |
712 | above is a function definition; it contains one declaration, and one | |
713 | statement. In the statement, each `x' is an expression and so is `x * | |
714 | x'. | |
715 | ||
716 | Each nonterminal symbol must have grammatical rules showing how it | |
717 | is made out of simpler constructs. For example, one kind of C | |
718 | statement is the `return' statement; this would be described with a | |
719 | grammar rule which reads informally as follows: | |
720 | ||
721 | A `statement' can be made of a `return' keyword, an `expression' | |
722 | and a `semicolon'. | |
723 | ||
724 | There would be many other rules for `statement', one for each kind of | |
725 | statement in C. | |
726 | ||
727 | One nonterminal symbol must be distinguished as the special one which | |
728 | defines a complete utterance in the language. It is called the "start | |
729 | symbol". In a compiler, this means a complete input program. In the C | |
730 | language, the nonterminal symbol `sequence of definitions and | |
731 | declarations' plays this role. | |
732 | ||
733 | For example, `1 + 2' is a valid C expression--a valid part of a C | |
734 | program--but it is not valid as an _entire_ C program. In the | |
735 | context-free grammar of C, this follows from the fact that `expression' | |
736 | is not the start symbol. | |
737 | ||
738 | The Bison parser reads a sequence of tokens as its input, and groups | |
739 | the tokens using the grammar rules. If the input is valid, the end | |
740 | result is that the entire token sequence reduces to a single grouping | |
741 | whose symbol is the grammar's start symbol. If we use a grammar for C, | |
742 | the entire input must be a `sequence of definitions and declarations'. | |
743 | If not, the parser reports a syntax error. | |
744 | ||
745 | \1f | |
746 | File: bison.info, Node: Grammar in Bison, Next: Semantic Values, Prev: Language and Grammar, Up: Concepts | |
747 | ||
748 | From Formal Rules to Bison Input | |
749 | ================================ | |
750 | ||
751 | A formal grammar is a mathematical construct. To define the language | |
752 | for Bison, you must write a file expressing the grammar in Bison syntax: | |
753 | a "Bison grammar" file. *Note Bison Grammar Files: Grammar File. | |
754 | ||
755 | A nonterminal symbol in the formal grammar is represented in Bison | |
756 | input as an identifier, like an identifier in C. By convention, it | |
757 | should be in lower case, such as `expr', `stmt' or `declaration'. | |
758 | ||
759 | The Bison representation for a terminal symbol is also called a | |
760 | "token type". Token types as well can be represented as C-like | |
761 | identifiers. By convention, these identifiers should be upper case to | |
762 | distinguish them from nonterminals: for example, `INTEGER', | |
763 | `IDENTIFIER', `IF' or `RETURN'. A terminal symbol that stands for a | |
764 | particular keyword in the language should be named after that keyword | |
765 | converted to upper case. The terminal symbol `error' is reserved for | |
766 | error recovery. *Note Symbols::. | |
767 | ||
768 | A terminal symbol can also be represented as a character literal, | |
769 | just like a C character constant. You should do this whenever a token | |
770 | is just a single character (parenthesis, plus-sign, etc.): use that | |
771 | same character in a literal as the terminal symbol for that token. | |
772 | ||
773 | A third way to represent a terminal symbol is with a C string | |
774 | constant containing several characters. *Note Symbols::, for more | |
775 | information. | |
776 | ||
777 | The grammar rules also have an expression in Bison syntax. For | |
778 | example, here is the Bison rule for a C `return' statement. The | |
779 | semicolon in quotes is a literal character token, representing part of | |
780 | the C syntax for the statement; the naked semicolon, and the colon, are | |
781 | Bison punctuation used in every rule. | |
782 | ||
783 | stmt: RETURN expr ';' | |
784 | ; | |
785 | ||
786 | *Note Syntax of Grammar Rules: Rules. | |
787 | ||
788 | \1f | |
789 | File: bison.info, Node: Semantic Values, Next: Semantic Actions, Prev: Grammar in Bison, Up: Concepts | |
790 | ||
791 | Semantic Values | |
792 | =============== | |
793 | ||
794 | A formal grammar selects tokens only by their classifications: for | |
795 | example, if a rule mentions the terminal symbol `integer constant', it | |
796 | means that _any_ integer constant is grammatically valid in that | |
797 | position. The precise value of the constant is irrelevant to how to | |
798 | parse the input: if `x+4' is grammatical then `x+1' or `x+3989' is | |
799 | equally grammatical. | |
800 | ||
801 | But the precise value is very important for what the input means | |
802 | once it is parsed. A compiler is useless if it fails to distinguish | |
803 | between 4, 1 and 3989 as constants in the program! Therefore, each | |
804 | token in a Bison grammar has both a token type and a "semantic value". | |
805 | *Note Defining Language Semantics: Semantics, for details. | |
806 | ||
807 | The token type is a terminal symbol defined in the grammar, such as | |
808 | `INTEGER', `IDENTIFIER' or `',''. It tells everything you need to know | |
809 | to decide where the token may validly appear and how to group it with | |
810 | other tokens. The grammar rules know nothing about tokens except their | |
811 | types. | |
812 | ||
813 | The semantic value has all the rest of the information about the | |
814 | meaning of the token, such as the value of an integer, or the name of an | |
815 | identifier. (A token such as `','' which is just punctuation doesn't | |
816 | need to have any semantic value.) | |
817 | ||
818 | For example, an input token might be classified as token type | |
819 | `INTEGER' and have the semantic value 4. Another input token might | |
820 | have the same token type `INTEGER' but value 3989. When a grammar rule | |
821 | says that `INTEGER' is allowed, either of these tokens is acceptable | |
822 | because each is an `INTEGER'. When the parser accepts the token, it | |
823 | keeps track of the token's semantic value. | |
824 | ||
825 | Each grouping can also have a semantic value as well as its | |
826 | nonterminal symbol. For example, in a calculator, an expression | |
827 | typically has a semantic value that is a number. In a compiler for a | |
828 | programming language, an expression typically has a semantic value that | |
829 | is a tree structure describing the meaning of the expression. | |
830 | ||
831 | \1f | |
832 | File: bison.info, Node: Semantic Actions, Next: Bison Parser, Prev: Semantic Values, Up: Concepts | |
833 | ||
834 | Semantic Actions | |
835 | ================ | |
836 | ||
837 | In order to be useful, a program must do more than parse input; it | |
838 | must also produce some output based on the input. In a Bison grammar, | |
839 | a grammar rule can have an "action" made up of C statements. Each time | |
840 | the parser recognizes a match for that rule, the action is executed. | |
841 | *Note Actions::. | |
842 | ||
843 | Most of the time, the purpose of an action is to compute the | |
844 | semantic value of the whole construct from the semantic values of its | |
845 | parts. For example, suppose we have a rule which says an expression | |
846 | can be the sum of two expressions. When the parser recognizes such a | |
847 | sum, each of the subexpressions has a semantic value which describes | |
848 | how it was built up. The action for this rule should create a similar | |
849 | sort of value for the newly recognized larger expression. | |
850 | ||
851 | For example, here is a rule that says an expression can be the sum of | |
852 | two subexpressions: | |
853 | ||
854 | expr: expr '+' expr { $$ = $1 + $3; } | |
855 | ; | |
856 | ||
857 | The action says how to produce the semantic value of the sum expression | |
858 | from the values of the two subexpressions. | |
859 | ||
860 | \1f | |
861 | File: bison.info, Node: Bison Parser, Next: Stages, Prev: Semantic Actions, Up: Concepts | |
862 | ||
863 | Bison Output: the Parser File | |
864 | ============================= | |
865 | ||
866 | When you run Bison, you give it a Bison grammar file as input. The | |
867 | output is a C source file that parses the language described by the | |
868 | grammar. This file is called a "Bison parser". Keep in mind that the | |
869 | Bison utility and the Bison parser are two distinct programs: the Bison | |
870 | utility is a program whose output is the Bison parser that becomes part | |
871 | of your program. | |
872 | ||
873 | The job of the Bison parser is to group tokens into groupings | |
874 | according to the grammar rules--for example, to build identifiers and | |
875 | operators into expressions. As it does this, it runs the actions for | |
876 | the grammar rules it uses. | |
877 | ||
878 | The tokens come from a function called the "lexical analyzer" that | |
879 | you must supply in some fashion (such as by writing it in C). The | |
880 | Bison parser calls the lexical analyzer each time it wants a new token. | |
881 | It doesn't know what is "inside" the tokens (though their semantic | |
882 | values may reflect this). Typically the lexical analyzer makes the | |
883 | tokens by parsing characters of text, but Bison does not depend on | |
884 | this. *Note The Lexical Analyzer Function `yylex': Lexical. | |
885 | ||
886 | The Bison parser file is C code which defines a function named | |
887 | `yyparse' which implements that grammar. This function does not make a | |
888 | complete C program: you must supply some additional functions. One is | |
889 | the lexical analyzer. Another is an error-reporting function which the | |
890 | parser calls to report an error. In addition, a complete C program must | |
891 | start with a function called `main'; you have to provide this, and | |
892 | arrange for it to call `yyparse' or the parser will never run. *Note | |
893 | Parser C-Language Interface: Interface. | |
894 | ||
895 | Aside from the token type names and the symbols in the actions you | |
896 | write, all variable and function names used in the Bison parser file | |
897 | begin with `yy' or `YY'. This includes interface functions such as the | |
898 | lexical analyzer function `yylex', the error reporting function | |
899 | `yyerror' and the parser function `yyparse' itself. This also includes | |
900 | numerous identifiers used for internal purposes. Therefore, you should | |
901 | avoid using C identifiers starting with `yy' or `YY' in the Bison | |
902 | grammar file except for the ones defined in this manual. | |
903 | ||
904 | \1f | |
905 | File: bison.info, Node: Stages, Next: Grammar Layout, Prev: Bison Parser, Up: Concepts | |
906 | ||
907 | Stages in Using Bison | |
908 | ===================== | |
909 | ||
910 | The actual language-design process using Bison, from grammar | |
911 | specification to a working compiler or interpreter, has these parts: | |
912 | ||
913 | 1. Formally specify the grammar in a form recognized by Bison (*note | |
914 | Bison Grammar Files: Grammar File.). For each grammatical rule in | |
915 | the language, describe the action that is to be taken when an | |
916 | instance of that rule is recognized. The action is described by a | |
917 | sequence of C statements. | |
918 | ||
919 | 2. Write a lexical analyzer to process input and pass tokens to the | |
920 | parser. The lexical analyzer may be written by hand in C (*note | |
921 | The Lexical Analyzer Function `yylex': Lexical.). It could also | |
922 | be produced using Lex, but the use of Lex is not discussed in this | |
923 | manual. | |
924 | ||
925 | 3. Write a controlling function that calls the Bison-produced parser. | |
926 | ||
927 | 4. Write error-reporting routines. | |
928 | ||
929 | To turn this source code as written into a runnable program, you | |
930 | must follow these steps: | |
931 | ||
932 | 1. Run Bison on the grammar to produce the parser. | |
933 | ||
934 | 2. Compile the code output by Bison, as well as any other source | |
935 | files. | |
936 | ||
937 | 3. Link the object files to produce the finished product. | |
938 | ||
939 | \1f | |
940 | File: bison.info, Node: Grammar Layout, Prev: Stages, Up: Concepts | |
941 | ||
942 | The Overall Layout of a Bison Grammar | |
943 | ===================================== | |
944 | ||
945 | The input file for the Bison utility is a "Bison grammar file". The | |
946 | general form of a Bison grammar file is as follows: | |
947 | ||
948 | %{ | |
949 | C DECLARATIONS | |
950 | %} | |
951 | ||
952 | BISON DECLARATIONS | |
953 | ||
954 | %% | |
955 | GRAMMAR RULES | |
956 | %% | |
957 | ADDITIONAL C CODE | |
958 | ||
959 | The `%%', `%{' and `%}' are punctuation that appears in every Bison | |
960 | grammar file to separate the sections. | |
961 | ||
962 | The C declarations may define types and variables used in the | |
963 | actions. You can also use preprocessor commands to define macros used | |
964 | there, and use `#include' to include header files that do any of these | |
965 | things. | |
966 | ||
967 | The Bison declarations declare the names of the terminal and | |
968 | nonterminal symbols, and may also describe operator precedence and the | |
969 | data types of semantic values of various symbols. | |
970 | ||
971 | The grammar rules define how to construct each nonterminal symbol | |
972 | from its parts. | |
973 | ||
974 | The additional C code can contain any C code you want to use. Often | |
975 | the definition of the lexical analyzer `yylex' goes here, plus | |
976 | subroutines called by the actions in the grammar rules. In a simple | |
977 | program, all the rest of the program can go here. | |
978 | ||
979 | \1f | |
980 | File: bison.info, Node: Examples, Next: Grammar File, Prev: Concepts, Up: Top | |
981 | ||
982 | Examples | |
983 | ******** | |
984 | ||
985 | Now we show and explain three sample programs written using Bison: a | |
986 | reverse polish notation calculator, an algebraic (infix) notation | |
987 | calculator, and a multi-function calculator. All three have been tested | |
988 | under BSD Unix 4.3; each produces a usable, though limited, interactive | |
989 | desk-top calculator. | |
990 | ||
991 | These examples are simple, but Bison grammars for real programming | |
992 | languages are written the same way. You can copy these examples out of | |
993 | the Info file and into a source file to try them. | |
994 | ||
995 | * Menu: | |
996 | ||
997 | * RPN Calc:: Reverse polish notation calculator; | |
998 | a first example with no operator precedence. | |
999 | * Infix Calc:: Infix (algebraic) notation calculator. | |
1000 | Operator precedence is introduced. | |
1001 | * Simple Error Recovery:: Continuing after syntax errors. | |
1002 | * Multi-function Calc:: Calculator with memory and trig functions. | |
1003 | It uses multiple data-types for semantic values. | |
1004 | * Exercises:: Ideas for improving the multi-function calculator. | |
1005 | ||
1006 | \1f | |
1007 | File: bison.info, Node: RPN Calc, Next: Infix Calc, Up: Examples | |
1008 | ||
1009 | Reverse Polish Notation Calculator | |
1010 | ================================== | |
1011 | ||
1012 | The first example is that of a simple double-precision "reverse | |
1013 | polish notation" calculator (a calculator using postfix operators). | |
1014 | This example provides a good starting point, since operator precedence | |
1015 | is not an issue. The second example will illustrate how operator | |
1016 | precedence is handled. | |
1017 | ||
1018 | The source code for this calculator is named `rpcalc.y'. The `.y' | |
1019 | extension is a convention used for Bison input files. | |
1020 | ||
1021 | * Menu: | |
1022 | ||
1023 | * Decls: Rpcalc Decls. Bison and C declarations for rpcalc. | |
1024 | * Rules: Rpcalc Rules. Grammar Rules for rpcalc, with explanation. | |
1025 | * Lexer: Rpcalc Lexer. The lexical analyzer. | |
1026 | * Main: Rpcalc Main. The controlling function. | |
1027 | * Error: Rpcalc Error. The error reporting function. | |
1028 | * Gen: Rpcalc Gen. Running Bison on the grammar file. | |
1029 | * Comp: Rpcalc Compile. Run the C compiler on the output code. | |
1030 | ||
1031 | \1f | |
1032 | File: bison.info, Node: Rpcalc Decls, Next: Rpcalc Rules, Up: RPN Calc | |
1033 | ||
1034 | Declarations for `rpcalc' | |
1035 | ------------------------- | |
1036 | ||
1037 | Here are the C and Bison declarations for the reverse polish notation | |
1038 | calculator. As in C, comments are placed between `/*...*/'. | |
1039 | ||
1040 | /* Reverse polish notation calculator. */ | |
1041 | ||
1042 | %{ | |
1043 | #define YYSTYPE double | |
1044 | #include <math.h> | |
1045 | %} | |
1046 | ||
1047 | %token NUM | |
1048 | ||
1049 | %% /* Grammar rules and actions follow */ | |
1050 | ||
1051 | The C declarations section (*note The C Declarations Section: C | |
1052 | Declarations.) contains two preprocessor directives. | |
1053 | ||
1054 | The `#define' directive defines the macro `YYSTYPE', thus specifying | |
1055 | the C data type for semantic values of both tokens and groupings (*note | |
1056 | Data Types of Semantic Values: Value Type.). The Bison parser will use | |
1057 | whatever type `YYSTYPE' is defined as; if you don't define it, `int' is | |
1058 | the default. Because we specify `double', each token and each | |
1059 | expression has an associated value, which is a floating point number. | |
1060 | ||
1061 | The `#include' directive is used to declare the exponentiation | |
1062 | function `pow'. | |
1063 | ||
1064 | The second section, Bison declarations, provides information to | |
1065 | Bison about the token types (*note The Bison Declarations Section: | |
1066 | Bison Declarations.). Each terminal symbol that is not a | |
1067 | single-character literal must be declared here. (Single-character | |
1068 | literals normally don't need to be declared.) In this example, all the | |
1069 | arithmetic operators are designated by single-character literals, so the | |
1070 | only terminal symbol that needs to be declared is `NUM', the token type | |
1071 | for numeric constants. | |
1072 |