⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 bison.texinfo

📁 bison源代码.bison 是替代yacc的语法分析程序生成器. yacc是 Yet Another Compiler Compiler的缩写. bison又是什么呐 是一个生成可以分析文本文件结构
💻 TEXINFO
📖 第 1 页 / 共 5 页
字号:
language, an expression typically has a semantic value that is a treestructure describing the meaning of the expression.@node Semantic Actions, Bison Parser, Semantic Values, Concepts@section Semantic Actions@cindex semantic actions@cindex actions, semanticIn order to be useful, a program must do more than parse input; it mustalso produce some output based on the input.  In a Bison grammar, a grammarrule can have an @dfn{action} made up of C statements.  Each time theparser recognizes a match for that rule, the action is executed.@xref{Actions}.        Most of the time, the purpose of an action is to compute the semantic valueof the whole construct from the semantic values of its parts.  For example,suppose we have a rule which says an expression can be the sum of twoexpressions.  When the parser recognizes such a sum, each of thesubexpressions has a semantic value which describes how it was built up.The action for this rule should create a similar sort of value for thenewly recognized larger expression.For example, here is a rule that says an expression can be the sum oftwo subexpressions:@exampleexpr: expr '+' expr   @{ $$ = $1 + $3; @}        ;@end example@noindentThe action says how to produce the semantic value of the sum expressionfrom the values of the two subexpressions.@node Bison Parser, Stages, Semantic Actions, Concepts@section Bison Output: the Parser File@cindex Bison parser@cindex Bison utility@cindex lexical analyzer, purpose@cindex parserWhen you run Bison, you give it a Bison grammar file as input.  The outputis a C source file that parses the language described by the grammar.This file is called a @dfn{Bison parser}.  Keep in mind that the Bisonutility and the Bison parser are two distinct programs: the Bison utilityis a program whose output is the Bison parser that becomes part of yourprogram.The job of the Bison parser is to group tokens into groupings according tothe grammar rules---for example, to build identifiers and operators intoexpressions.  As it does this, it runs the actions for the grammar rules ituses.The tokens come from a function called the @dfn{lexical analyzer} that youmust supply in some fashion (such as by writing it in C).  The Bison parsercalls the lexical analyzer each time it wants a new token.  It doesn't knowwhat is ``inside'' the tokens (though their semantic values may reflectthis).  Typically the lexical analyzer makes the tokens by parsingcharacters of text, but Bison does not depend on this.  @xref{Lexical, ,The Lexical Analyzer Function @code{yylex}}.The Bison parser file is C code which defines a function named@code{yyparse} which implements that grammar.  This function does not makea complete C program: you must supply some additional functions.  One isthe lexical analyzer.  Another is an error-reporting function which theparser calls to report an error.  In addition, a complete C program muststart with a function called @code{main}; you have to provide this, andarrange for it to call @code{yyparse} or the parser will never run.@xref{Interface, ,Parser C-Language Interface}.Aside from the token type names and the symbols in the actions youwrite, all variable and function names used in the Bison parser filebegin with @samp{yy} or @samp{YY}.  This includes interface functionssuch as the lexical analyzer function @code{yylex}, the error reportingfunction @code{yyerror} and the parser function @code{yyparse} itself.This also includes numerous identifiers used for internal purposes.Therefore, you should avoid using C identifiers starting with @samp{yy}or @samp{YY} in the Bison grammar file except for the ones defined inthis manual.@node Stages, Grammar Layout, Bison Parser, Concepts@section Stages in Using Bison@cindex stages in using Bison@cindex using BisonThe actual language-design process using Bison, from grammar specificationto a working compiler or interpreter, has these parts:@enumerate@itemFormally specify the grammar in a form recognized by Bison(@pxref{Grammar File, ,Bison Grammar Files}).  For each grammatical rule in the language,describe the action that is to be taken when an instance of that ruleis recognized.  The action is described by a sequence of C statements.@itemWrite a lexical analyzer to process input and pass tokens to theparser.  The lexical analyzer may be written by hand in C(@pxref{Lexical, ,The Lexical Analyzer Function @code{yylex}}).  It could also be produced using Lex, but the useof Lex is not discussed in this manual.@itemWrite a controlling function that calls the Bison-produced parser.@itemWrite error-reporting routines.@end enumerateTo turn this source code as written into a runnable program, youmust follow these steps:@enumerate@itemRun Bison on the grammar to produce the parser.@itemCompile the code output by Bison, as well as any other source files.@itemLink the object files to produce the finished product.@end enumerate@node Grammar Layout,  , Stages, Concepts@section The Overall Layout of a Bison Grammar@cindex grammar file@cindex file format@cindex format of grammar file@cindex layout of Bison grammarThe input file for the Bison utility is a @dfn{Bison grammar file}.  Thegeneral form of a Bison grammar file is as follows:@example%@{@var{C declarations}%@}@var{Bison declarations}%%@var{Grammar rules}%%@var{Additional C code}@end example@noindentThe @samp{%%}, @samp{%@{} and @samp{%@}} are punctuation that appearsin every Bison grammar file to separate the sections.The C declarations may define types and variables used in the actions.You can also use preprocessor commands to define macros used there, and use@code{#include} to include header files that do any of these things.The Bison declarations declare the names of the terminal and nonterminalsymbols, and may also describe operator precedence and the data types ofsemantic values of various symbols.The grammar rules define how to construct each nonterminal symbol from itsparts.The additional C code can contain any C code you want to use.  Often thedefinition of the lexical analyzer @code{yylex} goes here, plus subroutinescalled by the actions in the grammar rules.  In a simple program, all therest of the program can go here.@node Examples, Grammar File, Concepts, Top@chapter Examples@cindex simple examples@cindex examples, simpleNow we show and explain three sample programs written using Bison: areverse polish notation calculator, an algebraic (infix) notationcalculator, and a multi-function calculator.  All three have been testedunder BSD Unix 4.3; each produces a usable, though limited, interactivedesk-top calculator.These examples are simple, but Bison grammars for real programminglanguages are written the same way.@ifinfoYou can copy these examples out of the Info file and into a source fileto try them.@end ifinfo@menu* RPN Calc::          Reverse polish notation calculator;                        a first example with no operator precedence.* Infix Calc::        Infix (algebraic) notation calculator.                        Operator precedence is introduced.* Simple Error Recovery::  Continuing after syntax errors.* Multi-function Calc::  Calculator with memory and trig functions.                           It uses multiple data-types for semantic values.* Exercises::         Ideas for improving the multi-function calculator.@end menu@node RPN Calc, Infix Calc,  , Examples@section Reverse Polish Notation Calculator@cindex reverse polish notation@cindex polish notation calculator@cindex @code{rpcalc}@cindex calculator, simpleThe first example is that of a simple double-precision @dfn{reverse polishnotation} calculator (a calculator using postfix operators).  This exampleprovides a good starting point, since operator precedence is not an issue.The second example will illustrate how operator precedence is handled.The source code for this calculator is named @file{rpcalc.y}.  The@samp{.y} extension is a convention used for Bison input files.@menu* Decls: Rpcalc Decls.  Bison and C declarations for rpcalc.* Rules: Rpcalc Rules.  Grammar Rules for rpcalc, with explanation.* Lexer: Rpcalc Lexer.  The lexical analyzer.* Main: Rpcalc Main.    The controlling function.* Error: Rpcalc Error.  The error reporting function.* Gen: Rpcalc Gen.      Running Bison on the grammar file.* Comp: Rpcalc Compile. Run the C compiler on the output code.@end menu@node Rpcalc Decls, Rpcalc Rules,  , RPN Calc@subsection Declarations for @code{rpcalc}Here are the C and Bison declarations for the reverse polish notationcalculator.  As in C, comments are placed between @samp{/*@dots{}*/}.@example/* Reverse polish notation calculator. */%@{#define YYSTYPE double#include <math.h>%@}%token NUM%% /* Grammar rules and actions follow */@end exampleThe C declarations section (@pxref{C Declarations, ,The C Declarations Section}) contains twopreprocessor directives.The @code{#define} directive defines the macro @code{YYSTYPE}, thusspecifying the C data type for semantic values of both tokens and groupings(@pxref{Value Type, ,Data Types of Semantic Values}).  The Bison parser will use whatever type@code{YYSTYPE} is defined as; if you don't define it, @code{int} is thedefault.  Because we specify @code{double}, each token and each expressionhas an associated value, which is a floating point number.The @code{#include} directive is used to declare the exponentiationfunction @code{pow}.The second section, Bison declarations, provides information to Bison aboutthe token types (@pxref{Bison Declarations, ,The Bison Declarations Section}).  Each terminal symbol that isnot a single-character literal must be declared here.  (Single-characterliterals normally don't need to be declared.)  In this example, all thearithmetic operators are designated by single-character literals, so theonly terminal symbol that needs to be declared is @code{NUM}, the tokentype for numeric constants.@node Rpcalc Rules, Rpcalc Lexer, Rpcalc Decls, RPN Calc@subsection Grammar Rules for @code{rpcalc}Here are the grammar rules for the reverse polish notation calculator.@exampleinput:    /* empty */        | input line;line:     '\n'        | exp '\n'  @{ printf ("\t%.10g\n", $1); @};exp:      NUM             @{ $$ = $1;         @}        | exp exp '+'     @{ $$ = $1 + $2;    @}        | exp exp '-'     @{ $$ = $1 - $2;    @}        | exp exp '*'     @{ $$ = $1 * $2;    @}        | exp exp '/'     @{ $$ = $1 / $2;    @}      /* Exponentiation */        | exp exp '^'     @{ $$ = pow ($1, $2); @}      /* Unary minus    */        | exp 'n'         @{ $$ = -$1;        @};%%@end exampleThe groupings of the rpcalc ``language'' defined here are the expression(given the name @code{exp}), the line of input (@code{line}), and thecomplete input transcript (@code{input}).  Each of these nonterminalsymbols has several alternate rules, joined by the @samp{|} punctuatorwhich is read as ``or''.  The following sections explain what these rulesmean.The semantics of the language is determined by the actions taken when agrouping is recognized.  The actions are the C code that appears insidebraces.  @xref{Actions}.You must specify these actions in C, but Bison provides the means forpassing semantic values between the rules.  In each action, thepseudo-variable @code{$$} stands for the semantic value for the groupingthat the rule is going to construct.  Assigning a value to @code{$$} is themain job of most actions.  The semantic values of the components of therule are referred to as @code{$1}, @code{$2}, and so on.@menu* Rpcalc Input::      * Rpcalc Line::       * Rpcalc Expr::       @end menu@node Rpcalc Input, Rpcalc Line,  , Rpcalc Rules@subsubsection Explanation of @code{input}Consider the definition of @code{input}:@exampleinput:    /* empty */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -