?? ss2
字號:
.SH2: Actions.PPWith each grammar rule, the user may associate actions to be performed each timethe rule is recognized in the input process.These actions may return values, and may obtain the values returned by previousactions.Moreover, the lexical analyzer can return valuesfor tokens, if desired..PPAn action is an arbitrary C statement, and as such can doinput and output, call subprograms, and alterexternal vectors and variables.An action is specified byone or more statements, enclosed in curly braces ``{'' and ``}''.For example,.DSA : \'(\' B \')\' { hello( 1, "abc" ); }.DEand.DSXXX : YYY ZZZ { printf("a message\en"); flag = 25; }.DEare grammar rules with actions..PPTo facilitate easy communication between the actions and the parser, the action statements are alteredslightly.The symbol ``dollar sign'' ``$'' is used as a signal to Yacc in this context..PPTo return a value, the action normally sets thepseudo-variable ``$$'' to some value.For example, an action that does nothing but return the value 1 is.DS { $$ = 1; }.DE.PPTo obtain the values returned by previous actions and the lexical analyzer, theaction may use the pseudo-variables $1, $2, . . .,which refer to the values returned by thecomponents of the right side of a rule, reading from left to right.Thus, if the rule is.DSA : B C D ;.DEfor example, then $2 has the value returned by C, and $3 the value returned by D..PPAs a more concrete example, consider the rule.DSexpr : \'(\' expr \')\' ;.DEThe value returned by this rule is usually the value of the.I exprin parentheses.This can be indicated by.DSexpr : \'(\' expr \')\' { $$ = $2 ; }.DE.PPBy default, the value of a rule is the value of the first element in it ($1).Thus, grammar rules of the form.DSA : B ;.DEfrequently need not have an explicit action..PPIn the examples above, all the actions came at the end of their rules.Sometimes, it is desirable to get control before a rule is fully parsed.Yacc permits an action to be written in the middle of a rule as wellas at the end.This rule is assumed to return a value, accessiblethrough the usual \$ mechanism by the actions tothe right of it.In turn, it may access the valuesreturned by the symbols to its left.Thus, in the rule.DSA : B { $$ = 1; } C { x = $2; y = $3; } ;.DEthe effect is to set.I xto 1, and.I yto the value returned by C..PPActions that do not terminate a rule are actuallyhandled by Yacc by manufacturing a new nonterminalsymbol name, and a new rule matching thisname to the empty string.The interior action is the action triggered off by recognizingthis added rule.Yacc actually treats the above example as ifit had been written:.DS$ACT : /* empty */ { $$ = 1; } ;A : B $ACT C { x = $2; y = $3; } ;.DE.PPIn many applications, output is not done directly by the actions;rather, a data structure, such as a parse tree, is constructed in memory,and transformations are applied to it before output is generated.Parse trees are particularly easy toconstruct, given routines to build and maintain the treestructure desired.For example, suppose there is a C function.I node ,written so that the call.DSnode( L, n1, n2 ).DEcreates a node with label L, and descendants n1 and n2, and returns the index ofthe newly created node.Then parse tree can be built by supplying actions such as:.DSexpr : expr \'+\' expr { $$ = node( \'+\', $1, $3 ); }.DEin the specification..PPThe user may define other variables to be used by the actions.Declarations and definitions can appear inthe declarations section,enclosed in the marks ``%{'' and ``%}''.These declarations and definitions have global scope, so they are known to the action statements and the lexical analyzer.For example,.DS%{ int variable = 0; %}.DEcould be placed in the declarations section,making.I variableaccessible to all of the actions.The Yacc parser uses only names beginning in ``yy'';the user should avoid such names..PPIn these examples, all the values are integers: a discussion ofvalues of other types will be found in Section 10.
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -