Documents

Bison

Description
Bison The Yacc-compatible Parser Generator 23 October 2013, Bison Version 3.0.2 by Charles Donnelly and Richard Stallman This manual (23 October 2013) is for GNU Bison (version 3.0.2), the GNU parser generator. Copyright c 1988-1993, 1995, 1998-2013 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
Categories
Published
of 224
All materials on our website are shared by users. If you have any questions about copyright issues, please report us to resolve them. We are always happy to assist you.
Related Documents
Share
Transcript
  Bison The Yacc-compatible Parser Generator23 October 2013, Bison Version 3.0.2 by Charles Donnelly and Richard Stallman  This manual (23 October 2013) is for GNU Bison (version 3.0.2), the GNU parser generator.Copyright c   1988-1993, 1995, 1998-2013 Free Software Foundation, Inc.Permission is granted to copy, distribute and/or modify this document underthe terms of the GNU Free Documentation License, Version 1.3 or any laterversion published by the Free Software Foundation; with no Invariant Sections,with the Front-Cover texts being “A GNU Manual,” and with the Back-CoverTexts 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 modifythis GNU manual. Buying copies from the FSF supports it in developing GNUand promoting software freedom.”Published by the Free Software Foundation51 Franklin Street, Fifth FloorBoston, MA 02110-1301 USAPrinted copies are available from the Free Software Foundation.ISBN 1-882114-44-2Cover art by Etienne Suvasa.  i Table of Contents Introduction  . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  1Conditions for Using Bison . . . . . . . . . . . . . . . . . . . . . . . .  3GNU GENERAL PUBLIC LICENSE . . . . . . . . . . . .  51 The Concepts of Bison . . . . . . . . . . . . . . . . . . . . . . . .  17 1.1 Languages and Context-Free Grammars  . . . . . . . . . . . . . . . . . . . . . . .  171.2 From Formal Rules to Bison Input  . . . . . . . . . . . . . . . . . . . . . . . . . . . .  181.3 Semantic Values  . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  191.4 Semantic Actions  . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  201.5 Writing GLR Parsers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  201.5.1 Using GLR on Unambiguous Grammars  . . . . . . . . . . . . . . . . . .  211.5.2 Using GLR to Resolve Ambiguities  . . . . . . . . . . . . . . . . . . . . . . .  231.5.3 GLR Semantic Actions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  251.5.3.1 Deferred semantic actions  . . . . . . . . . . . . . . . . . . . . . . . . . . .  251.5.3.2 YYERROR  . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  261.5.3.3 Restrictions on semantic values and locations  . . . . . . . .  261.5.4 Controlling a Parse with Arbitrary Predicates  . . . . . . . . . . . .  261.5.5 Considerations when Compiling GLR Parsers . . . . . . . . . . . . .  271.6 Locations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  271.7 Bison Output: the Parser Implementation File . . . . . . . . . . . . . . . . .  281.8 Stages in Using Bison  . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  291.9 The Overall Layout of a Bison Grammar  . . . . . . . . . . . . . . . . . . . . . .  29 2 Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  31 2.1 Reverse Polish Notation Calculator . . . . . . . . . . . . . . . . . . . . . . . . . . . .  312.1.1 Declarations for  rpcalc  . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  312.1.2 Grammar Rules for  rpcalc . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  322.1.2.1 Explanation of   input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  322.1.2.2 Explanation of   line . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  332.1.2.3 Explanation of   expr . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  332.1.3 The  rpcalc  Lexical Analyzer . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  342.1.4 The Controlling Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  352.1.5 The Error Reporting Routine . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  362.1.6 Running Bison to Make the Parser  . . . . . . . . . . . . . . . . . . . . . . .  362.1.7 Compiling the Parser Implementation File . . . . . . . . . . . . . . . .  362.2 Infix Notation Calculator:  calc  . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  372.3 Simple Error Recovery  . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  382.4 Location Tracking Calculator:  ltcalc . . . . . . . . . . . . . . . . . . . . . . . . .  392.4.1 Declarations for  ltcalc  . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  392.4.2 Grammar Rules for  ltcalc . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  40  ii Bison 3.0.22.4.3 The  ltcalc  Lexical Analyzer.  . . . . . . . . . . . . . . . . . . . . . . . . . . . .  412.5 Multi-Function Calculator:  mfcalc . . . . . . . . . . . . . . . . . . . . . . . . . . . .  422.5.1 Declarations for  mfcalc  . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  432.5.2 Grammar Rules for  mfcalc . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  442.5.3 The  mfcalc  Symbol Table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  442.5.4 The  mfcalc  Lexer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  472.5.5 The  mfcalc  Main  . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  482.6 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  49 3 Bison Grammar Files  . . . . . . . . . . . . . . . . . . . . . . . . .  51 3.1 Outline of a Bison Grammar  . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  513.1.1 The prologue . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  513.1.2 Prologue Alternatives  . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  523.1.3 The Bison Declarations Section . . . . . . . . . . . . . . . . . . . . . . . . . . .  563.1.4 The Grammar Rules Section . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  563.1.5 The epilogue  . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  573.2 Symbols, Terminal and Nonterminal . . . . . . . . . . . . . . . . . . . . . . . . . . .  573.3 Grammar Rules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  593.3.1 Syntax of Grammar Rules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  593.3.2 Empty Rules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  603.3.3 Recursive Rules  . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  603.4 Defining Language Semantics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  613.4.1 Data Types of Semantic Values . . . . . . . . . . . . . . . . . . . . . . . . . . .  613.4.2 More Than One Value Type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  623.4.3 Generating the Semantic Value Type  . . . . . . . . . . . . . . . . . . . . .  623.4.4 The Union Declaration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  633.4.5 Providing a Structured Semantic Value Type  . . . . . . . . . . . . .  643.4.6 Actions  . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  643.4.7 Data Types of Values in Actions . . . . . . . . . . . . . . . . . . . . . . . . . .  653.4.8 Actions in Mid-Rule . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  663.4.8.1 Using Mid-Rule Actions  . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  663.4.8.2 Mid-Rule Action Translation  . . . . . . . . . . . . . . . . . . . . . . . .  683.4.8.3 Conflicts due to Mid-Rule Actions  . . . . . . . . . . . . . . . . . . .  693.5 Tracking Locations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  703.5.1 Data Type of Locations  . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  703.5.2 Actions and Locations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  713.5.3 Default Action for Locations  . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  723.6 Named References  . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  733.7 Bison Declarations  . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  743.7.1 Require a Version of Bison  . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  743.7.2 Token Type Names . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  753.7.3 Operator Precedence  . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  763.7.4 Nonterminal Symbols . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  763.7.5 Performing Actions before Parsing . . . . . . . . . . . . . . . . . . . . . . . .  773.7.6 Freeing Discarded Symbols  . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  773.7.7 Printing Semantic Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  793.7.8 Suppressing Conflict Warnings . . . . . . . . . . . . . . . . . . . . . . . . . . . .  803.7.9 The Start-Symbol . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  80
We Need Your Support
Thank you for visiting our website and your interest in our free products and services. We are nonprofit website to share and download documents. To the running of this website, we need your help to support us.

Thanks to everyone for your continued support.

No, Thanks
SAVE OUR EARTH

We need your sign to support Project to invent "SMART AND CONTROLLABLE REFLECTIVE BALLOONS" to cover the Sun and Save Our Earth.

More details...

Sign Now!

We are very appreciated for your Prompt Action!

x