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

📄 yacc.html

📁 unix 下的C开发手册,还用详细的例程。
💻 HTML
📖 第 1 页 / 共 4 页
字号:
yyorYYsince the<i>yacc</i>parser uses such names.Many of the names appear in the final output of<i>yacc</i>,and thus they should be chosen to conform with any additional rulescreated by the C compiler to be used.In particular they will appearin<b>#define</b>statements.<p>A literal consists of a single character enclosed in single-quotes(').All of the escape sequences supported for character constantsby the ISO&nbsp;C standard are supported by<i>yacc</i>.<p>The relationship with the lexical analyser is discussed in detail below.<p>The NUL character must not be used in grammar rules or literals.<h5><a name = "tag_001_014_2954_003">&nbsp;</a>Declarations Section</h5><xref type="5" name="yaccdecl"></xref>The declarations section is used to define the symbols used to definethe target language and their relationship with each other.In particular,much of the additional information required to resolve ambiguities inthe context-free grammar for thetarget language is provided here.<p>Usually<i>yacc</i>assigns the relationship between the symbolic names it generates andtheir underlying numeric value.The declarations section makes itpossible to control the assignment of these values.<p>It is also possible to keep semantic information associated withthe tokens currently on the parse stack in a user-defined C-language<b>union</b>,if the members of the union are associated with the various namesin the grammar.The declarations section provides for this as well.<p>The first group of declarators below all take a list of names as arguments.That list can optionally be preceded bythe name of a C union member (called a<i>tag</i>below) appearing within "&lt;" and "&gt;".(As an exception to the typographical conventionsof the rest of this specification, in this case&lt;<i>tag</i>&gt;does not represent a metavariable,but the literal angle bracket characters surrounding a symbol.)The use of<i>tag</i>specifies that the tokens named on this lineare to be of the same C type as theunion member referenced by<i>tag</i>.This is discussed in more detail below.<p>For lists used to define tokens, the first appearance ofa given token can be followed by apositive integer (as a string of decimal digits).If this is done, the underlying value assigned to it for lexical purposeswill be taken to be that number.<dl compact><dt>%token <b>[</b>&lt;<i>tag</i>&gt;<b>] </b><i>name </i><b>[</b><i>number</i><b>] [</b><i>name </i><b>[</b><i>number</i><b>]]</b>...<dd>Declares<i>name</i>sto be a token.If<i>tag</i>is present, the C type for all tokens on this line will be declaredto be the type referenced by<i>tag</i>.If a positive integer,<i>number</i>,follows a<i>name</i>,that value will be assigned to the token.<dt>%left <b>[</b>&lt;<i>tag</i>&gt;<b>] </b><i>name </i><b>[</b><i>number</i><b>] [</b><i>name </i><b>[</b><i>number</i><b>]]</b>...<dd><dt>%right <b>[</b>&lt;<i>tag</i>&gt;<b>] </b><i>name </i><b>[</b><i>number</i><b>] [</b><i>name </i><b>[</b><i>number</i><b>]]</b>...<dd>Declares<i>name</i>to be a token, and assigns precedence to it.One or more lines, each beginning with one of these symbols,can appear in this section.All tokens on the same line have the same precedence level andassociativity; the lines are in order of increasing precedence or bindingstrength.<b>%left</b>denotes that the operators on that line are left associative, and<b>%right</b>similarly denotes right associative operators.If<i>tag</i>is present, it declares a C type for<i>name</i>sas described for<b>%token</b>.<dt>%nonassoc <b>[</b>&lt;<i>tag</i>&gt;<b>] </b><i>name </i><b>[</b><i>number</i><b>] [</b><i>name </i><b>[</b><i>number</i><b>]]</b>...<dd>Declares<i>name</i>to be a token, and indicates that thiscannot be used associatively.If the parser encounters associative use of this tokenit will report an error.If<i>tag</i>is present, it declares a C type for<i>name</i>sas described for<b>%token</b>.<dt>%type <b>[</b>&lt;<i>tag</i>&gt;<b>] </b><i>name</i>...<dd>Declares that union member<i>name</i>sare non-terminals,and thus it is required to have a<i>tag</i>field at its beginning.Because it deals with non-terminals only,assigning a token number or using a literal is also prohibited.If this construct is present,<i>yacc</i>will perform type checking;if this construct is not present, the parse stack will hold only the<b>int</b>type.</dl><p>Every name used in<i>grammar</i>undefined by a<b>%token</b>,<b>%left</b>,<b>%right</b>or<b>%nonassoc</b>declaration is assumed to represent a non-terminal symbol.The<i>yacc</i>utility will report an error for any non-terminal symbol thatdoes not appear on the left side of at least one grammar rule.<p>Once the type, precedence or token number of a name is specified,it will not be changed.If the first declaration of a token does notassign a token number,<i>yacc</i>will assign a token number.Once this assignment is made, the token number will not be changedby explicit assignment.<p>The following declarators do not follow the previous pattern.<dl compact><dt>%start<dd>Declares the non-terminal<i>name</i>to be the<i>start symbol ,</i>which represents the largest, most general structuredescribed by the grammar rules.By default, it is the left-hand side of the first grammar rule; thisdefault can be overridden with this declaration.<dt>%union {<i> body of union (in C) </i>}<dd>Declares the<i>yacc</i>value stack to be a union of the various typesof values desired.By default, the values returned by actions (see below) and the lexicalanalyser will be integers.The<i>yacc</i>utility keeps track of types, and will insert corresponding union membernames in order to perform strict type checking of the resulting parser.Alternatively,given that at least one <b>&lt;</b><i>tag</i><b>&gt;</b>construct is used,the union can be declared in a header file(which will be included in the declarations section by usingan#includeconstruct within<b>%{</b>and<b>%}</b>),and a<b>typedef</b>used to define the symbolYYSTYPEto represent this union.The effect of<b>%union</b>is to provide the declaration ofYYSTYPEdirectly from theinput.<dt>%{ ... %}<dd>C-language declarations and definitions can appear in thedeclarations section,enclosed by these marks.These statements will be copied into thecode file, and have global scopewithin it so that they can be usedin the rules and program sections.</dl><p>The declarations section must be terminated by the token<b>%%</b>.<h5><a name = "tag_001_014_2954_004">&nbsp;</a>Grammar Rules in yacc</h5><xref type="5" name="yaccgram"></xref>The rules section defines the context-free grammar to be accepted bythe function<i>yacc</i>generates, and associates with those rules C-language actions andadditional precedence information.The grammar is described below,and a formal definition follows.<p>The rules section is comprised of one or more grammar rules.A grammar rule has the form:<pre><code>A : BODY ;</code></pre><p>The symbolArepresents a non-terminal name, and<b>BODY</b>represents a sequence of zero or more<i>name</i>s,<i>literal</i>sand<i>semantic action</i>sthat can then befollowed by optional<i>precedence rule</i>s.Only the names and literalsparticipate in the formation of the grammar; the semantic actions andprecedence rules are used in other ways.The colon and the semicolon are<i>yacc</i>punctuation.If there are several successive grammar rules with the same left-hand side,the vertical bar "|"can be used to avoid rewriting the left-hand side;in this case the semicolon appears only after the last rule.TheBODYpart can be empty (or empty of names and literals) to indicate that thenon-terminal symbol matchesthe empty string.<p>The<i>yacc</i>utility assigns a unique number to each rule.Rules using the vertical bar notation are distinct rules.The number assigned to the rule appears in the description file.<p>The elements comprising a BODY are:<dl compact><dt><i>name</i><dd><dt><i>literal</i><dd>These form the rules of the grammar:<i>name</i>is either a<i>token</i>or a<i>non-terminal</i>;<i>literal</i>stands for itself (less the lexically required quotation marks).<dt><i>semantic action</i><dd>With each grammar rule, the user can associate actions to be performedeach time the rule is recognised in the input process.(Note that the word &quot;action&quot; can also refer to the actions of theparsershift, reduce, and so on.)These actions can return values and can obtain the values returned byprevious actions.These values will be kept in objects of type YYSTYPE (see<b>%union</b>).The result value of the action will be kept on the parse stack with theleft-hand side of the rule, to be accessed by other reductionsas part of their right-hand side.By using the&lt;<i>tag</i>&gt;information provided in the declarations section, the code generated by<i>yacc</i>can be strictly type checked and contain arbitrary information.In addition, the lexical analyser can provide the same kinds ofvalues for tokens, if desired.An action is an arbitrary C statement and as such can do input or output,call subprograms and alter external variables.An action is one or more C statements enclosed in curly braces"{"and"}".Certain pseudo-variables can be used in the action.These aremacros for access to data structures known internally to<i>yacc</i>.<dl compact><dt><b>$$</b><dd>The value of the action can be set by assigning it to$$.If type checking is enabled andthe type of the value to be assigned cannot be determined,a diagnostic message may be generated.<dt><b>$</b><i>number</i><dd>This refers to the value returned by the component specified bythe token<i>number</i>in the right side of a rule, reading from left to right;<i>number</i>can be zero or negative.If it is, it refers to the dataassociated with the nameon the parser's stack preceding the leftmost symbol of the current rule.(That is,$0refers to the name immediately preceding the leftmost name in thecurrent rule, to be found on the parser's stack and$-1refers to thesymbol to<i>its</i>left.)If<i>number</i>refers to an element past the current point in the rule, or beyond thebottom of the stack, the result is undefined.If type checking is enabled andthe type of the value to be assigned cannot be determined,a diagnostic message may be generated.<dt><b>$</b><i>&lt;tag&gt;number</i><dd>These correspond exactly to the corresponding symbols without the<i>tag</i>inclusion, but allow for strict type checking (and preclude unwantedtype conversions).The effect is that the macro isexpanded to use<i>tag</i>to select an element from theYYSTYPEunion (using<i>dataname.tag</i>).This is particularly useful if<i>number</i>is not positive.<dt><b>$</b><i>&lt;tag&gt;</i><b>$</b><dd>This imposes on the reference the type of the union memberreferenced by<i>tag</i>.This construction is applicable whena reference to a left context value occurs in the grammar,and provides<i>yacc</i>with a means for selecting a type.</dl><p>Actions can occur in the middle of a rule as well as atthe end; an action can access values returned by actionsto its left, and in turn the value it returns can be accessedby actions to its right.An action appearing in the middleof a rule will be equivalent to replacing the action with a new

⌨️ 快捷键说明

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