哈工大编译原理51.1.pptVIP

  • 29
  • 0
  • 约1.51万字
  • 约 70页
  • 2016-12-29 发布于北京
  • 举报
第五章 语法制导翻译 例2: 一个简单的翻译模式(中缀变后缀) E→TR R→addop T {print(addop.lexeme)}R1|ε T→num{print(num.val)} Evaluation only. Created with Aspose.Slides for .NET 3.5 Client Profile 5.2.0.0. Copyright 2004-2011 Aspose Pty Ltd. 3+5的语义翻译过程 E R T Pr’3’ 3 T + Pr’+’ 5 R Pr’5’ ε 结果:35+ Evaluation only. Created with Aspose.Slides for .NET 3.5 Client Profile 5.2.0.0. Copyright 2004-2011 Aspose Pty Ltd. 翻译方案不仅要考虑“做什么”,还要考虑“怎么做” 某种意义上说,语法制导定义类似于算法,而翻译方案更象程序 Evaluation only. Created with Aspose.Slides for .NET 3.5 Client Profile 5.2.0.0. Copyright 2004-2011 Aspose Pty Ltd.  带有继承属性L.in的翻译方案 D?T{ L?in:=T ? type }L T ?int { T ? type :=integer } T ?real { T ? type :=real } L ?{L1 ?in :=L ?in} L1,id{addtype(id ?entry,L ?in) } L ?id { addtype(id ?entry,L ?in) } 例5 . 3 变量说明的类型定义 int a,b,c Evaluation only. Created with Aspose.Slides for .NET 3.5 Client Profile 5.2.0.0. Copyright 2004-2011 Aspose Pty Ltd. D L.in=t.type L real L1.in=L.in , id3 L1.in=L.in , id2 id1 句子real id1,id2,id3的带继承属性的分析树 T T.type=real L L Add(L.in) Add(L.in) Add(L.in) Evaluation only. Created with Aspose.Slides for .NET 3.5 Client Profile 5.2.0.0. Copyright 2004-2011 Aspose Pty Ltd. 例:文法G的产生式如下: S→(L) S →a L →L,S L →S 1.试写出一个语法制导定义,输出配对括号个数 2.写一个翻译方案,打印每个a的嵌套深度 Evaluation only. Created with Aspose.Slides for .NET 3.5 Client Profile 5.2.0.0. Copyright 2004-2011 Aspose Pty Ltd. 解:1.为S,L引入属性h 产生式 语法制导定义 S→(L) S.h=L.h+1 S →a S.h=0 L →L1,S L.h=L1.h+S.h L →S L.h=s.h S’ →S print(S.h) Evaluation only. Created with Aspose.Slides for .NET 3.5 Client Profile 5.2.0.0. Copyright 2004-2011 Aspose Pty Ltd. ( a S S.h=0 L , ( a L.h=0 S S.h=0 L L.h=0 ) S S.h=1 L L.h=1 ) S S.h=2 (a,(a))的分析过程 Evaluation only. Created with Aspose.Slides for .NET 3.5 Client Profile 5.2.0.0. Copyright 2004-2011 Aspose Pty Ltd. 2.为S,L引入属性d,翻译方案如下 S’ →{S.d=0} S S→( {L.d=S.d+1} L) S →a {print(s.d)} L →{L1.d=L.d} L1, {S.d=L

文档评论(0)

1亿VIP精品文档

相关文档