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

📄 bison_4.htm

📁 Lex和Yacc的Manual
💻 HTM
📖 第 1 页 / 共 2 页
字号:
except their types.</P><P>The semantic value has all the rest of the information about themeaning of the token, such as the value of an integer, or the name of anidentifier.  (A token such as <CODE>','</CODE> which is just punctuation doesn'tneed to have any semantic value.)</P><P>For example, an input token might be classified as token type<CODE>INTEGER</CODE> and have the semantic value 4.  Another input token mighthave the same token type <CODE>INTEGER</CODE> but value 3989.  When a grammarrule says that <CODE>INTEGER</CODE> is allowed, either of these tokens isacceptable because each is an <CODE>INTEGER</CODE>.  When the parser accepts thetoken, it keeps track of the token's semantic value.</P><P>Each grouping can also have a semantic value as well as its nonterminalsymbol.  For example, in a calculator, an expression typically has asemantic value that is a number.  In a compiler for a programminglanguage, an expression typically has a semantic value that is a treestructure describing the meaning of the expression.</P><H2><A NAME="SEC11" HREF="index.html#SEC11">Semantic Actions</A></H2><P><A NAME="IDX16"></A><A NAME="IDX17"></A></P><P>In 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 <STRONG>action</STRONG> made up of C statements.  Each time theparser recognizes a match for that rule, the action is executed.See section <A HREF="bison_6.html#SEC46">Actions</A>.        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.</P><P>For example, here is a rule that says an expression can be the sum oftwo subexpressions:</P><PRE>expr: expr '+' expr   { $$ = $1 + $3; }        ;</PRE><P>The action says how to produce the semantic value of the sum expressionfrom the values of the two subexpressions.</P><H2><A NAME="SEC12" HREF="index.html#SEC12">Bison Output: the Parser File</A></H2><P><A NAME="IDX18"></A><A NAME="IDX19"></A><A NAME="IDX20"></A><A NAME="IDX21"></A></P><P>When 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 <STRONG>Bison parser</STRONG>.  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.</P><P>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.</P><P>The tokens come from a function called the <STRONG>lexical analyzer</STRONG> 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.  See section <A HREF="bison_7.html#SEC61">The Lexical Analyzer Function <CODE>yylex</CODE></A>.</P><P>The Bison parser file is C code which defines a function named<CODE>yyparse</CODE> 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</CODE>; you have to provide this, andarrange for it to call <CODE>yyparse</CODE> or the parser will never run.See section <A HREF="bison_7.html#SEC59">Parser C-Language Interface</A>.</P><P>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'</SAMP> or <SAMP>`YY'</SAMP>.  This includes interface functionssuch as the lexical analyzer function <CODE>yylex</CODE>, the error reportingfunction <CODE>yyerror</CODE> and the parser function <CODE>yyparse</CODE> itself.This also includes numerous identifiers used for internal purposes.Therefore, you should avoid using C identifiers starting with <SAMP>`yy'</SAMP>or <SAMP>`YY'</SAMP> in the Bison grammar file except for the ones defined inthis manual.</P><H2><A NAME="SEC13" HREF="index.html#SEC13">Stages in Using Bison</A></H2><P><A NAME="IDX22"></A><A NAME="IDX23"></A></P><P>The actual language-design process using Bison, from grammar specificationto a working compiler or interpreter, has these parts:</P><OL><LI>Formally specify the grammar in a form recognized by Bison(see section <A HREF="bison_6.html#SEC34">Bison Grammar Files</A>).  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.<LI>Write a lexical analyzer to process input and pass tokens to theparser.  The lexical analyzer may be written by hand in C(see section <A HREF="bison_7.html#SEC61">The Lexical Analyzer Function <CODE>yylex</CODE></A>).  It could also be produced using Lex, but the useof Lex is not discussed in this manual.<LI>Write a controlling function that calls the Bison-produced parser.<LI>Write error-reporting routines.</OL><P>To turn this source code as written into a runnable program, youmust follow these steps:</P><OL><LI>Run Bison on the grammar to produce the parser.<LI>Compile the code output by Bison, as well as any other source files.<LI>Link the object files to produce the finished product.</OL><H2><A NAME="SEC14" HREF="index.html#SEC14">The Overall Layout of a Bison Grammar</A></H2><P><A NAME="IDX24"></A><A NAME="IDX25"></A><A NAME="IDX26"></A><A NAME="IDX27"></A></P><P>The input file for the Bison utility is a <STRONG>Bison grammar file</STRONG>.  Thegeneral form of a Bison grammar file is as follows:</P><PRE>%{<VAR>C declarations</VAR>%}<VAR>Bison declarations</VAR>%%<VAR>Grammar rules</VAR>%%<VAR>Additional C code</VAR></PRE><P>The <SAMP>`%%'</SAMP>, <SAMP>`%{'</SAMP> and <SAMP>`%}'</SAMP> are punctuation that appearsin every Bison grammar file to separate the sections.</P><P>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</CODE> to include header files that do any of these things.</P><P>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.</P><P>The grammar rules define how to construct each nonterminal symbol from itsparts.</P><P>The additional C code can contain any C code you want to use.  Often thedefinition of the lexical analyzer <CODE>yylex</CODE> goes here, plus subroutinescalled by the actions in the grammar rules.  In a simple program, all therest of the program can go here.</P><HR>Go to the <A HREF="bison_1.html">first</A>, <A HREF="bison_3.html">previous</A>, <A HREF="bison_5.html">next</A>, <A HREF="bison_15.html">last</A> section, <A HREF="index.html">table of contents</A>.</BODY></HTML>

⌨️ 快捷键说明

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