递归下降分析 (42).ppt

上传人:刘静 文档编号:84102148 上传时间:2023-04-01 格式:PPT 页数:11 大小:82.49KB
返回 下载 相关 举报
递归下降分析 (42).ppt_第1页
第1页 / 共11页
递归下降分析 (42).ppt_第2页
第2页 / 共11页
点击查看更多>>
资源描述

《递归下降分析 (42).ppt》由会员分享,可在线阅读,更多相关《递归下降分析 (42).ppt(11页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。

1、CompilerS1Synthesized AttributesAn attribute is synthesized if all its dependencies point from child to parent in the parse tree.Equivalently,an attribute a is synthesized if,given a grammar rule A X1 X2,Xn the only associated attribute equation with an a on the left-hand side is of the form A.a=f(X

2、1.a1,X1.ak,Xn.a1,Xn.ak)An attribute grammar in which all the attributes are synthesized is called an S-attributed grammar.CompilerS2Attribute Values of S-Attributed GrammarGiven a parse tree or a syntax tree constructed by a parser,the attribute values of S-attributed grammar can be computed by a si

3、ngle bottom-up,or postorder,traversal of the tree.procedure PostEval(T:treenode);for each child C of T do PostEval(C);compute all synthesized attributes of TCompilerS3Examplenumber1.valnumber2.valdigit.valnumber.valdigit.valCompilerS4Inherited AttributeAn attribute that is not synthesized is called

4、an inherited attribute.Example:decltypevar-listid(x),var-listid(y)floatdtypedtypedtypedtypedtypeCompilerS5Inherited Attribute(cont)Dependencies flow either from parent to children or from sibling to sibling.Inherited attributes can be computed by a preorder traversal,or combined preorder/inorder tra

5、versal of the syntax tree.a Aa Ba Ca Aa Ba Cprocedure PreEval(T:treenode);for each child C of T do compute all inherited attributes of C PreEval(C);CompilerS6Example 6.12decl type var-listtype int|floatvar-list id,var-list|iddecltypevar-listid(x),var-listid(y)floatdtypedtypedtypedtypedtype12345Compi

6、lerS7Attribute Computation and SyntaxProperties of attributes depend on the structure of the grammar.Modifications to the grammar that do not change the legal strings of the language can make the computation of attributes simpler or more complex.Theorem:Given an attribute grammar,all inherited attri

7、butes can be changed into synthesized attributes by suitable modification of the grammar,without changing the language of the grammar.KnuthCompilerS8Example 6.18decl type var-listtype int|floatvar-list id,var-list|idGrammar RuleSemantic Rules decl type var-list var-list.dtype=type.dtype type int typ

8、e.dtype=integer type float type.dtype=float var-list1 id,var-list2 id.dtype=var-list1.dtype var-list2.dtype=var-list1.dtype var-list1 id id.dtype=var-list1.dtypedtype attribute is inherited.CompilerS9Example 6.7(cont)String:float x,ydecltypevar-listid(x),var-listid(y)floatdtypedtypedtypedtypedtypedt

9、ype attribute is inherited.CompilerS10Example 6.18(cont)decl type var-listtype int|floatvar-list id,var-list|iddecl var-list idvar-list var-list id,|typetype int|floatGrammar RuleSemantic Rules decl var-list id id.dtype=var-list.dtype var-list1 var-list2 id,var-list1.dtype=var-list2.dtype id.dtype=v

10、ar-list1.dtype var-list type var-list.dtype=type.dtype type int type.dtype=integer type float type.dtype=floatCompilerS11Example 6.18(cont)float x,ydeclvar-list dtype=realid dtype=real(y)var-list dtype=realid dtype=real(x),type dtype=realfloat The two dependencies that are drawn as dashed lines appear to be inherited attributes.These dependencies are always to the leaves(not recursive)and not viewed as inherited.

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 教育专区 > 大学资料

本站为文档C TO C交易模式,本站只提供存储空间、用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。本站仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知淘文阁网,我们立即给予删除!客服QQ:136780468 微信:18945177775 电话:18904686070

工信部备案号:黑ICP备15003705号© 2020-2023 www.taowenge.com 淘文阁