《递归下降分析 (40).ppt》由会员分享,可在线阅读,更多相关《递归下降分析 (40).ppt(4页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、CompilerS1Attribute GrammarsIn syntax-directed semantics,attributes are associated directly with the grammar symbols of the language.If X is a grammar symbol,and a is an attribute associated to X,then we write X.a for the value of a associated to X.For each grammar rule X0X1 X2Xn,the values of attri
2、butes Xi.aj of each grammar symbol Xi are related.Attribute equation or semantic rule:Xi.aj=fij(X0.a1,X0.ak,X1.a1,X1.ak,Xn.a1,Xn.ak)Attribute grammar for the attributes a1,a2,ak is the collection of such equations for all grammar rules of the language.CompilerS2Example 6.1number number digit|digitdi
3、git 0|1|2|3|4|5|6|7|8|9 The most significant attribute of a number is its value.Each digit has a value computable from the digit it represents.digit 0 implies that digit has value 0 digit.val=0 If a number is derived using the rule number digit,then number.val=digit.val If a number contains more dig
4、its number number digit,then number1.val=number2.val*10+digit.valnumber1 number2 digitCompilerS3Example 6.2exp exp+term|exp term|termterm term*factor|factorfactor (exp)|number factor.val=number.val factor number factor.val=exp.val factor (exp)term.val=factor.val term factor term1.val=term2.val*facto
5、r.val term1 term2*factor exp.val=term.val exp term exp1.val=exp2.val -term.val exp1 exp2 term exp1.val=exp2.val +term.val exp1 exp2+termSemantic RulesGrammar RuleCompilerS4Example 6.3decl type var-listtype int|floatvar-list id,var-list|id id.type=var-list.dtype var-list id id.type=var-list1.dtype var-list2.dtype=var-list1.dtype var-list1 id,var-list2 type.dtype=float type float type.dtype=integer type int var-list.dtype=type.dtype decl type var-listSemantic RulesGrammar Rule
限制150内