Bison the Yacc-Compatible Parser Generator 10 September 2021, Bison Version 3.8.1
Total Page:16
File Type:pdf, Size:1020Kb
Bison The Yacc-compatible Parser Generator 10 September 2021, Bison Version 3.8.1 by Charles Donnelly and Richard Stallman This manual (10 September 2021) is for GNU Bison (version 3.8.1), the GNU parser gen- erator. Copyright c 1988{1993, 1995, 1998{2015, 2018{2021 Free Software Foundation, Inc. Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.3 or any later version published by the Free Software Foundation; with no Invariant Sections, with the Front-Cover texts being \A GNU Manual," and with the Back-Cover Texts as in (a) below. A copy of the license is included in the section entitled \GNU Free Documentation License." (a) The FSF's Back-Cover Text is: \You have the freedom to copy and modify this GNU manual. Buying copies from the FSF supports it in developing GNU and promoting software freedom." Published by the Free Software Foundation 51 Franklin Street, Fifth Floor Boston, MA 02110-1301 USA Printed copies are available from the Free Software Foundation. ISBN 1-882114-44-2 Cover art by Etienne Suvasa. i Table of Contents Introduction ::::::::::::::::::::::::::::::::::::::::: 1 Conditions for Using Bison ::::::::::::::::::::::::: 2 GNU GENERAL PUBLIC LICENSE ::::::::::::: 3 1 The Concepts of Bison::::::::::::::::::::::::: 14 1.1 Languages and Context-Free Grammars:::::::::::::::::::::::: 14 1.2 From Formal Rules to Bison Input ::::::::::::::::::::::::::::: 15 1.3 Semantic Values ::::::::::::::::::::::::::::::::::::::::::::::: 16 1.4 Semantic Actions :::::::::::::::::::::::::::::::::::::::::::::: 17 1.5 Writing GLR Parsers :::::::::::::::::::::::::::::::::::::::::: 17 1.5.1 Using GLR on Unambiguous Grammars::::::::::::::::::: 18 1.5.2 Using GLR to Resolve Ambiguities:::::::::::::::::::::::: 20 1.5.3 GLR Semantic Actions ::::::::::::::::::::::::::::::::::: 23 1.5.3.1 Deferred semantic actions :::::::::::::::::::::::::::: 23 1.5.3.2 YYERROR :::::::::::::::::::::::::::::::::::::::::: 23 1.5.3.3 Restrictions on semantic values and locations::::::::: 24 1.5.4 Controlling a Parse with Arbitrary Predicates ::::::::::::: 24 1.6 Locations ::::::::::::::::::::::::::::::::::::::::::::::::::::: 24 1.7 Bison Output: the Parser Implementation File ::::::::::::::::: 25 1.8 Stages in Using Bison:::::::::::::::::::::::::::::::::::::::::: 26 1.9 The Overall Layout of a Bison Grammar::::::::::::::::::::::: 26 2 Examples ::::::::::::::::::::::::::::::::::::::: 28 2.1 Reverse Polish Notation Calculator :::::::::::::::::::::::::::: 28 2.1.1 Declarations for rpcalc::::::::::::::::::::::::::::::::::: 28 2.1.2 Grammar Rules for rpcalc ::::::::::::::::::::::::::::::: 29 2.1.2.1 Explanation of input :::::::::::::::::::::::::::::::: 30 2.1.2.2 Explanation of line ::::::::::::::::::::::::::::::::: 30 2.1.2.3 Explanation of exp :::::::::::::::::::::::::::::::::: 31 2.1.3 The rpcalc Lexical Analyzer ::::::::::::::::::::::::::::: 31 2.1.4 The Controlling Function ::::::::::::::::::::::::::::::::: 33 2.1.5 The Error Reporting Routine ::::::::::::::::::::::::::::: 33 2.1.6 Running Bison to Make the Parser :::::::::::::::::::::::: 33 2.1.7 Compiling the Parser Implementation File :::::::::::::::: 34 2.2 Infix Notation Calculator: calc:::::::::::::::::::::::::::::::: 34 2.3 Simple Error Recovery::::::::::::::::::::::::::::::::::::::::: 36 2.4 Location Tracking Calculator: ltcalc ::::::::::::::::::::::::: 37 2.4.1 Declarations for ltcalc::::::::::::::::::::::::::::::::::: 37 2.4.2 Grammar Rules for ltcalc ::::::::::::::::::::::::::::::: 37 ii 2.4.3 The ltcalc Lexical Analyzer.::::::::::::::::::::::::::::: 38 2.5 Multi-Function Calculator: mfcalc :::::::::::::::::::::::::::: 40 2.5.1 Declarations for mfcalc::::::::::::::::::::::::::::::::::: 40 2.5.2 Grammar Rules for mfcalc ::::::::::::::::::::::::::::::: 41 2.5.3 The mfcalc Symbol Table :::::::::::::::::::::::::::::::: 42 2.5.4 The mfcalc Lexer :::::::::::::::::::::::::::::::::::::::: 44 2.5.5 The mfcalc Main::::::::::::::::::::::::::::::::::::::::: 46 2.6 Exercises :::::::::::::::::::::::::::::::::::::::::::::::::::::: 46 3 Bison Grammar Files :::::::::::::::::::::::::: 47 3.1 Outline of a Bison Grammar::::::::::::::::::::::::::::::::::: 47 3.1.1 The prologue ::::::::::::::::::::::::::::::::::::::::::::: 47 3.1.2 Prologue Alternatives ::::::::::::::::::::::::::::::::::::: 48 3.1.3 The Bison Declarations Section ::::::::::::::::::::::::::: 52 3.1.4 The Grammar Rules Section :::::::::::::::::::::::::::::: 52 3.1.5 The epilogue ::::::::::::::::::::::::::::::::::::::::::::: 53 3.2 Symbols, Terminal and Nonterminal ::::::::::::::::::::::::::: 53 3.3 Grammar Rules ::::::::::::::::::::::::::::::::::::::::::::::: 55 3.3.1 Syntax of Grammar Rules :::::::::::::::::::::::::::::::: 55 3.3.2 Empty Rules ::::::::::::::::::::::::::::::::::::::::::::: 56 3.3.3 Recursive Rules::::::::::::::::::::::::::::::::::::::::::: 56 3.4 Defining Language Semantics :::::::::::::::::::::::::::::::::: 57 3.4.1 Data Types of Semantic Values ::::::::::::::::::::::::::: 57 3.4.2 More Than One Value Type :::::::::::::::::::::::::::::: 58 3.4.3 Generating the Semantic Value Type:::::::::::::::::::::: 58 3.4.4 The Union Declaration ::::::::::::::::::::::::::::::::::: 59 3.4.5 Providing a Structured Semantic Value Type:::::::::::::: 60 3.4.6 Actions::::::::::::::::::::::::::::::::::::::::::::::::::: 60 3.4.7 Data Types of Values in Actions :::::::::::::::::::::::::: 62 3.4.8 Actions in Midrule:::::::::::::::::::::::::::::::::::::::: 62 3.4.8.1 Using Midrule Actions ::::::::::::::::::::::::::::::: 62 3.4.8.2 Typed Midrule Actions :::::::::::::::::::::::::::::: 64 3.4.8.3 Midrule Action Translation :::::::::::::::::::::::::: 64 3.4.8.4 Conflicts due to Midrule Actions ::::::::::::::::::::: 66 3.5 Tracking Locations :::::::::::::::::::::::::::::::::::::::::::: 67 3.5.1 Data Type of Locations::::::::::::::::::::::::::::::::::: 67 3.5.2 Actions and Locations :::::::::::::::::::::::::::::::::::: 68 3.5.3 Printing Locations :::::::::::::::::::::::::::::::::::::::: 69 3.5.4 Default Action for Locations :::::::::::::::::::::::::::::: 69 3.6 Named References ::::::::::::::::::::::::::::::::::::::::::::: 70 3.7 Bison Declarations :::::::::::::::::::::::::::::::::::::::::::: 71 3.7.1 Require a Version of Bison :::::::::::::::::::::::::::::::: 72 3.7.2 Token Kind Names ::::::::::::::::::::::::::::::::::::::: 72 3.7.3 Operator Precedence:::::::::::::::::::::::::::::::::::::: 73 3.7.4 Nonterminal Symbols ::::::::::::::::::::::::::::::::::::: 74 3.7.5 Syntax of Symbol Declarations:::::::::::::::::::::::::::: 74 3.7.6 Performing Actions before Parsing :::::::::::::::::::::::: 74 3.7.7 Freeing Discarded Symbols:::::::::::::::::::::::::::::::: 75 iii 3.7.8 Printing Semantic Values ::::::::::::::::::::::::::::::::: 76 3.7.9 Suppressing Conflict Warnings :::::::::::::::::::::::::::: 77 3.7.10 The Start-Symbol ::::::::::::::::::::::::::::::::::::::: 79 3.7.11 A Pure (Reentrant) Parser::::::::::::::::::::::::::::::: 79 3.7.12 A Push Parser::::::::::::::::::::::::::::::::::::::::::: 79 3.7.13 Bison Declaration Summary ::::::::::::::::::::::::::::: 81 3.7.14 %define Summary ::::::::::::::::::::::::::::::::::::::: 85 3.7.15 %code Summary :::::::::::::::::::::::::::::::::::::::: 95 3.8 Multiple Parsers in the Same Program ::::::::::::::::::::::::: 97 4 Parser C-Language Interface :::::::::::::::::: 99 4.1 The Parser Function yyparse:::::::::::::::::::::::::::::::::: 99 4.2 Push Parser Interface::::::::::::::::::::::::::::::::::::::::: 100 4.3 The Lexical Analyzer Function yylex::::::::::::::::::::::::: 101 4.3.1 Calling Convention for yylex :::::::::::::::::::::::::::: 101 4.3.2 Special Tokens :::::::::::::::::::::::::::::::::::::::::: 102 4.3.3 Finding Tokens by String Literals :::::::::::::::::::::::: 103 4.3.4 Semantic Values of Tokens ::::::::::::::::::::::::::::::: 103 4.3.5 Textual Locations of Tokens ::::::::::::::::::::::::::::: 103 4.3.6 Calling Conventions for Pure Parsers :::::::::::::::::::: 104 4.4 Error Reporting :::::::::::::::::::::::::::::::::::::::::::::: 105 4.4.1 The Error Reporting Function yyerror :::::::::::::::::: 105 4.4.2 The Syntax Error Reporting Function yyreport_syntax_error ::::::::::::::::::::::::::: 106 4.5 Special Features for Use in Actions ::::::::::::::::::::::::::: 108 4.6 Parser Internationalization ::::::::::::::::::::::::::::::::::: 110 4.6.1 Enabling Internationalization :::::::::::::::::::::::::::: 110 4.6.2 Token Internationalization ::::::::::::::::::::::::::::::: 111 5 The Bison Parser Algorithm ::::::::::::::::: 112 5.1 Lookahead Tokens :::::::::::::::::::::::::::::::::::::::::::: 112 5.2 Shift/Reduce Conflicts ::::::::::::::::::::::::::::::::::::::: 113 5.3 Operator Precedence ::::::::::::::::::::::::::::::::::::::::: 115 5.3.1 When Precedence is Needed ::::::::::::::::::::::::::::: 115 5.3.2 Specifying Operator Precedence:::::::::::::::::::::::::: 115 5.3.3 Specifying Precedence Only :::::::::::::::::::::::::::::: 116 5.3.4 Precedence Examples :::::::::::::::::::::::::::::::::::: 116 5.3.5 How Precedence Works :::::::::::::::::::::::::::::::::: 117 5.3.6 Using Precedence For Non Operators :::::::::::::::::::: 117 5.4 Context-Dependent Precedence ::::::::::::::::::::::::::::::: 117 5.5 Parser States ::::::::::::::::::::::::::::::::::::::::::::::::: 118 5.6 Reduce/Reduce Conflicts ::::::::::::::::::::::::::::::::::::: 118 5.7 Mysterious Conflicts:::::::::::::::::::::::::::::::::::::::::: 121 5.8 Tuning LR ::::::::::::::::::::::::::::::::::::::::::::::::::: 123 5.8.1 LR Table Construction :::::::::::::::::::::::::::::::::: 123 5.8.2