《编译原理》课程教学资源:第二章(2-4-1)One parse tree only

Removing Ambiguity
Removing Ambiguity

One parse tree only The role of the grammar a distinguish between syntactically legal and illegal programs a But that' s not enough it must also define a parse tree a the parse tree conveys the meaning of the program What if a string can be parsed with multiple parse trees? a we say the grammar is ambiguous a must fix the grammar ( the problem is not in the parser a Note: often a string can be derived in more than one way D ie, with more than one derivation sequence a this does not mean the grammar is ambiguous
One parse tree only! ◼ The role of the grammar distinguish between syntactically legal and illegal programs ◼ But that’s not enough: it must also define a parse tree the parse tree conveys the meaning of the program ◼ What if a string can be parsed with multiple parse trees? we say the grammar is ambiguous must fix the grammar (the problem is not in the parser) ◼ Note: often a string can be derived in more than one way ie, with more than one derivation sequence this does not mean the grammar is ambiguous 2

Ambiguity: EXample ■ Grammar EE+EeE(E)int ■ Strings nt int int nt x int int
3 Ambiguity: Example ◼ Grammar E E + E | E * E | ( E ) | int ◼ Strings int + int + int int * int + int

Ambiguity. EXample This string has two parse trees E E E E t E E +E int int e+ E int int int int +is left-associative
4 Ambiguity. Example This string has two parse trees E E E E + E int + int int E E E E + E + int int int + is left-associative

Ambiguity. EXample This string has two parse trees E E E E E E *E int int e+ E int int int int has higher precedence than
5 Ambiguity. Example This string has two parse trees E E E E * E int + int int E E E E + E int * int int * has higher precedence than +

Ambiguity( Cont) aA grammar is ambiguous D if it has more than one parse tree for any string ■ Ambiguity is bad lEaves meaning of some programs ill-defined Ambiguity is common in programming languages D Arithmetic expressions 口|F-THEN-ELSE
6 Ambiguity (Cont.) ◼ A grammar is ambiguous if it has more than one parse tree for any string ◼ Ambiguity is bad Leaves meaning of some programs ill-defined ◼ Ambiguity is common in programming languages Arithmetic expressions IF-THEN-ELSE

Dealing with Ambiguity There are two ways to remove ambiguity 1)Rewrite the grammar into a unambiguous grammar EE+T T TT*int int I(E) 口 Enforces precedence(优先级)of*over+ 口 Enforces left- associativity(左结合)of+and* 2) Declarations: instruct parser to pick desired parse trees a using %left, %right, %prec declarations
7 Dealing with Ambiguity There are two ways to remove ambiguity: 1) Rewrite the grammar into a unambiguous grammar E E + T | T T T * int | int | ( E ) Enforces precedence(优先级) of * over + Enforces left-associativity(左结合) of + and * 2) Declarations: instruct parser to pick desired parse trees using %left, %right, %prec declarations

Ambiguity. EXample The int* int+ int has on one parse tree now E+ T E t e nt int int int
8 Ambiguity. Example The int * int + int has ony one parse tree now E E E E * E int + int int E T T int + T int * E int

Rewriting the grammar: what's the trick? Trick 1: Fixing precedence( computed before + EE+E IE*E I id/ a In the parse tree for id+ id *id, we want id*id to a Create a new nonterminal(m//t by be subtree of E+E. How to accomplisk-that rewriting make it deriⅳ eid*id, T|7 ensureS trees are nested in Es of E+E I ■ New grammar
Rewriting the grammar: what’s the trick? Trick 1: Fixing precedence (* computed before +) E E + E | E * E | id ◼ In the parse tree for id + id * id, we want id*id to be subtree of E+E. How to accomplish that by rewriting? ◼ Create a new nonterminal (T) make it derive id*id, … ensure T’s trees are nested in E’s of E+E ◼ New grammar: 9

Rewriting the grammar: what's the trick?(part 2) Trick 2: Fixing associativity(+, associate to the left) EE+ET TT*T id a In the parse tree for id+ id id, we want tha left id+id to be subtree of the right E+idE TSafhe for id"id "id ■ Use left! recursion a it will ensure that + *associate to theeftid +id a New grammar (a simple change)
Rewriting the grammar: what’s the trick? (part 2) Trick 2: Fixing associativity (+, *, associate to the left) E E + E | T T T * T | id ◼ In the parse tree for id + id + id, we want the left id+id to be subtree of the right E+id. Same for id*id*id. ◼ Use left recursion it will ensure that +, * associate to the left ◼ New grammar (a simple change): 10
按次数下载不扣除下载券;
注册用户24小时内重复下载只扣除一次;
顺序:VIP每日次数-->可用次数-->下载券;
- 《编译原理》课程教学资源:第二章(2-4)语法分析一自上而下分析.ppt
- 《JavaScript》权威指南简介.ppt
- 《编译原理》课程教学资源:第三章 正则表达式常应用于文本匹配:.ppt
- 《编译原理》课程教学资源:第二章(2-3-1)对于词法分析器的要求.ppt
- 《编译原理》课程教学资源:第二章 词法分析 2.6 利用Lex自动生成扫描程序.ppt
- 《编译原理》课程教学资源:第二章 语言描述与实现 Language Description and Implementation 2.1 程序语言的语法描述.ppt
- 《编译原理》课程教学资源:第一章(1-2)编译简介.ppt
- 《编译原理》课程教学资源:语义分析和中间代码产生.ppt
- 《编译原理》课程教学资源:Chapter 5 Procedure Activations.ppt
- 《互联网软件应用与开发》综合复习材料.doc
- 《计算机电路基础》课程教学资源(PPT课件讲稿)第四章 门电路.ppt
- 《计算机电路基础》课程教学资源(PPT课件讲稿)第六章 时序逻辑电路.ppt
- 《计算机电路基础》课程教学资源(PPT课件讲稿)第五章 组合逻辑电路.ppt
- 《计算机电路基础》课程教学资源(PPT课件讲稿)第二章 半导体基本器件.ppt
- 《计算机电路基础》课程教学资源(PPT课件讲稿)第三章 开关理论基础.ppt
- 《计算机电路基础》课程教学资源(PPT课件讲稿)第七章 存储器和可编程逻辑器件.ppt
- 《计算机电路基础》课程教学资源(PPT课件讲稿)第一章 课程简介.ppt
- 清华大学:《数据通信原理》课程教学资源(学习讲义)软件无线电体系结构的新趋势.doc
- 清华大学:《数据通信原理》课程教学资源(学习讲义)超宽带无线通信技术及发展.doc
- 清华大学:《数据通信原理》课程教学资源(学习讲义)第12讲 无线系统及网络.ppt
- 《编译原理》课程教学资源:第五章 YACC.ppt
- 《编译原理》课程教学资源:第四章 对象和环境.ppt
- 《编译原理》课程教学资源:第八章 符号表.ppt
- 《编译原理》课程教学资源:第六章 属性文法.ppt
- 《编译原理》课程教学资源:第五章(5-2)过程激活.ppt
- 《编译原理》课程教学资源:第二章 语言描述与实现 Language Description and Implementation 2.5 语法分析——自下而上分析.ppt
- 《编译原理》课程教学资源:教学计划.doc
- 《编译原理》课程教学资源:第十章 优化.ppt
- 《编译原理》课程教学资源:属性文法.ppt
- 《体系结构》第二章 计算机指令集结构设计.doc
- 《体系结构》第三章 流水线技术.doc
- 《体系结构》第五章 存储层次.doc
- 《体系结构》第六章 输入输出系统.doc
- 《体系结构》第一章 计算机体系结构的基本概念.doc
- USB系统研究(学位论文)USB System Study.pdf
- 《微型计算机原理与接口技术》第10章 串行通信接口.ppt
- 《微型计算机原理与接口技术》第11章 人机交互接口技术.ppt
- 《微型计算机原理与接口技术》第12章 模拟量输入输出接口技术.ppt
- 《微型计算机原理与接口技术》第1章 微型计算机基础知识.ppt
- 《微型计算机原理与接口技术》第2章 典型微处理器.ppt