Bison - The Yacc-compatible Parser Generator

9 years 8 months ago
Bison - The Yacc-compatible Parser Generator
"Bison is a general-purpose parser generator that converts an annotated context-free grammar into an LALR(1) or GLR parser for that grammar. Once you are proficient with Bison, you can use it to develop a wide range of language parsers, from those used in simple desk calculators to complex programming languages. Bison is upward compatible with Yacc: all properly-written Yacc grammars ought to work with Bison with no change. Anyone familiar with Yacc should be able to use Bison with little trouble. You need to be fluent in C or C++ programming in order to use Bison or to understand this manual."
Charles Donnelly and Richard Stallman
Added 21 Apr 2009
Updated 21 Apr 2009
Authors Charles Donnelly and Richard Stallman
 Table of Contents
Conditions for Using Bison
1 The Concepts of Bison
1.1 Languages and Context-Free Grammars  
1.2 From Formal Rules to Bison Input  
1.3 Semantic Values  
1.4 Semantic Actions  
1.5 Writing GLR Parsers  
1.5.1 Using GLR on Unambiguous Grammars
1.5.2 Using GLR to Resolve Ambiguities
1.5.3 GLR Semantic Actions  
1.5.4 Considerations when Compiling GLR Parsers  
1.6 Locations  
1.7 Bison Output the Parser File  
1.8 Stages in Using Bison  
1.9 The Overall Layout of a Bison Grammar  
2 Examples
2.1 Reverse Polish Notation Calculator  
2.1.1 Declarations for rpcalc  
2.1.2 Grammar Rules for rpcalc Explanation of input Explanation of line Explanation of expr  
2.1.3 The rpcalc Lexical Analyzer  
2.1.4 The Controlling Function
2.1.5 The Error Reporting Routine  
2.1.6 Running Bison to Make the Parser  
2.1.7 Compiling the Parser File  
2.2 Infix Notation Calculator calc  
2.3 Simple Error Recovery  
2.4 Location Tracking Calculator ltcalc  
2.4.1 Declarations for ltcalc  
2.4.2 Grammar Rules for ltcalc  
2.4.3 The ltcalc Lexical Analyzer.  
2.5 Multi-Function Calculator mfcalc  
2.5.1 Declarations for mfcalc  
2.5.2 Grammar Rules for mfcalc  
2.5.3 The mfcalc Symbol Table  
2.6 Exercises
3 Bison Grammar Files
3.1 Outline of a Bison Grammar  
3.1.1 The prologue  
3.1.2 Prologue Alternatives  
3.1.3 The Bison Declarations Section  
3.1.4 The Grammar Rules Section
3.1.5 The epilogue  
3.2 Symbols, Terminal and Nonterminal
3.3 Syntax of Grammar Rules  
3.4 Recursive Rules  
3.5 Defining Language Semantics  
3.5.1 Data Types of Semantic Values  
3.5.2 More Than One Value Type  
3.5.3 Actions  
3.5.4 Data Types of Values in Actions
3.5.5 Actions in Mid-Rule  
3.6 Tracking Locations  
3.6.1 Data Type of Locations  
3.6.2 Actions and Locations  
3.6.3 Default Action for Locations  
3.7 Bison Declarations  
3.7.1 Require a Version of Bison  
3.7.2 Token Type Names
3.7.3 Operator Precedence  
3.7.4 The Collection of Value Types  
3.7.5 Nonterminal Symbols
3.7.6 Performing Actions before Parsing  
3.7.7 Freeing Discarded Symbols  
3.7.8 Suppressing Conflict Warnings
3.7.9 The Start-Symbol  
3.7.10 A Pure (Reentrant) Parser  
3.7.11 A Push Parser  
3.7.12 Bison Declaration Summary  
3.8 Multiple Parsers in the Same Program
4 Parser C-Language Interface  
4.1 The Parser Function yyparse  
4.2 The Push Parser Function yypush_parse
4.3 The Pull Parser Function yypull_parse
4.4 The Parser Create Function yystate_new  
4.5 The Parser Delete Function yystate_delete  
4.6 The Lexical Analyzer Function yylex  
4.6.1 Calling Convention for yylex  
4.6.2 Semantic Values of Tokens  
4.6.3 Textual Locations of Tokens  
4.6.4 Calling Conventions for Pure Parsers  
4.7 The Error Reporting Function yyerror
4.8 Special Features for Use in Actions  
4.9 Parser Internationalization  
5 The Bison Parser Algorithm
5.1 Lookahead Tokens  
5.2 Shift/Reduce Conflicts  
5.3 Operator Precedence
5.3.1 When Precedence is Needed  
5.3.2 Specifying Operator Precedence  
5.3.3 Precedence Examples
5.3.4 How Precedence Works  
5.4 Context-Dependent Precedence
5.5 Parser States
5.6 Reduce/Reduce Conflicts
5.7 Mysterious Reduce/Reduce Conflicts  
5.8 Generalized LR (GLR) Parsing  
5.9 Memory Management, and How to Avoid Memory Exhaustion
6 Error Recovery
7 Handling Context Dependencies
7.1 Semantic Info in Token Types  
7.2 Lexical Tie-ins  
7.3 Lexical Tie-ins and Error Recovery  
8 Debugging Your Parser  
8.1 Understanding Your Parser  
8.2 Tracing Your Parser  
9 Invoking Bison  
9.1 Bison Options  
9.2 Option Cross Key
9.3 Yacc Library  
10 Parsers Written In Other Languages
10.1 C++ Parsers  
10.1.1 C++ Bison Interface  
10.1.2 C++ Semantic Values
10.1.3 C++ Location Values
10.1.4 C++ Parser Interface  
10.1.5 C++ Scanner Interface
10.1.6 A Complete C++ Example Calc++ — C++ Calculator Calc++ Parsing Driver Calc++ Parser Calc++ Scanner Calc++ Top Level
10.2 Java Parsers
10.2.1 Java Bison Interface
10.2.2 Java Semantic Values
10.2.3 Java Location Values
10.2.4 Java Parser Interface
10.2.5 Java Scanner Interface
10.2.6 Special Features for Use in Java Actions
10.2.7 Differences between C/C++ and Java Grammars
10.2.8 Java Declarations Summary
11 Frequently Asked Questions
11.1 Memory Exhausted  
11.2 How Can I Reset the Parser
11.3 Strings are Destroyed  
11.4 Implementing Gotos/Loops  
11.5 Multiple start-symbols  
11.6 Secure? Conform?  
11.7 I can’t build Bison  
11.8 Where can I find help?  
11.9 Bug Reports  
11.10 More Languages  
11.11 Beta Testing  
11.12 Mailing Lists  
Appendix A Bison Symbols  
Appendix B Glossary 
Comments (0)