480 likes | 888 Views
Chapter 4 Top-Down Parsing. LL(1) Parsing Example. Gang S. Liu College of Computer Science & Technology Harbin Engineering University. Example. E → E + T | T T → T * F | F F → ( E ) | i. 分析符号串 i + i * i. E → TE’ E’ → +TE’ | ε T → FT’ T’ → *FT’ | ε F → ( E ) | i.
E N D
Chapter 4 Top-Down Parsing LL(1) Parsing Example Gang S. Liu College of Computer Science & Technology Harbin Engineering University
Example E → E + T | T T → T * F | F F → ( E ) | i 分析符号串i + i * i E → TE’ E’ → +TE’ | ε T → FT’ T’ → *FT’ | ε F →(E) | i Left Recursion Removal Samuel2005@126.com
Example E → TE’ E’ → +TE’ | ε T → FT’ T’ → *FT’ | ε F →(E) | i • E → TE’ • E’ → +TE’ • E’ → ε • T → FT’ • T’ → *FT’ • T’ → ε • F →(E) • F →i Samuel2005@126.com
Example • E → TE’ • E’ → +TE’ • E’ → ε • T → FT’ • T’ → *FT’ • T’ → ε • F →(E) • F →i First(E) = { } First(E’) = { } First(T) = { } First(T’) = { } First(F) = { } ( , i + , ε ( , i * , ε ( , i Samuel2005@126.com
Example 2 • E → TE’ • E’ → +TE’ • E’ → ε • T → FT’ • T’ → *FT’ • T’ → ε • F →(E) • F →i First(E) = { ( , i } First(E’) = { + , ε } First(T) = { ( , i } First(T’) = { * , ε } First(F) = { ( , i } Follow(E) = { } Follow(E’) = { } Follow(T) = { } Follow(T’) = { } Follow(F) = { } $ , ) $ , ) + , $ , ) +, $ , ) ,+, $ , ) * Samuel2005@126.com
Example • E → TE’ • E’ → +TE’ • E’ → ε • T → FT’ • T’ → *FT’ • T’ → ε • F →(E) • F →i First(E) = { ( , i } First(E’) = { + , ε } First(T) = { ( , i } First(T’) = { * , ε } First(F) = { ( , i } Follow(E) = { $ , ) } Follow(E’) = { $ , ) } Follow(T) = { $ , + , ) } Follow(T’) = { $ , + , ) } Follow(F) = { $ , + , * , ) } Samuel2005@126.com
Constructing LL(1) Parsing Table Samuel2005@126.com
Example • E → TE’ • E’ → +TE’ • E’ → ε • T → FT’ • T’ → *FT’ • T’ → ε • F →(E) • F →i First(E) = { ( , i } First(E’) = { + , ε } First(T) = { ( , i } First(T’) = { * , ε } First(F) = { ( , i } Follow(E) = { $ , ) } Follow(E’) = { $ , ) } Follow(T) = { $ , + , ) } Follow(T’) = { $ , + , ) } Follow(F) = { $ , + , * , ) } Samuel2005@126.com
Constructing LL(1) Parsing Table Samuel2005@126.com
Example • E → TE’ • E’ → +TE’ • E’ → ε • T → FT’ • T’ → *FT’ • T’ → ε • F →(E) • F →i First(E) = { ( , i } First(E’) = { + , ε } First(T) = { ( , i } First(T’) = { * , ε } First(F) = { ( , i } Follow(E) = { $ , ) } Follow(E’) = { $ , ) } Follow(T) = { $ , + , ) } Follow(T’) = { $ , + , ) } Follow(F) = { $ , + , * , ) } Samuel2005@126.com
Constructing LL(1) Parsing Table Samuel2005@126.com
Example • E → TE’ • E’ → +TE’ • E’ → ε • T → FT’ • T’ → *FT’ • T’ → ε • F →(E) • F →i First(E) = { ( , i } First(E’) = { + , ε } First(T) = { ( , i } First(T’) = { * , ε } First(F) = { ( , i } Follow(E) = { $ , ) } Follow(E’) = { $ , ) } Follow(T) = { $ , + , ) } Follow(T’) = { $ , + , ) } Follow(F) = { $ , + , * , ) } Samuel2005@126.com
Constructing LL(1) Parsing Table Samuel2005@126.com
Example • E → TE’ • E’ → +TE’ • E’ → ε • T → FT’ • T’ → *FT’ • T’ → ε • F →(E) • F →i First(E) = { ( , i } First(E’) = { + , ε } First(T) = { ( , i } First(T’) = { * , ε } First(F) = { ( , i } Follow(E) = { $ , ) } Follow(E’) = { $ , ) } Follow(T) = { $ , + , ) } Follow(T’) = { $ , + , ) } Follow(F) = { $ , + , * , ) } Samuel2005@126.com
Constructing LL(1) Parsing Table Samuel2005@126.com
Example • E → TE’ • E’ → +TE’ • E’ → ε • T → FT’ • T’ → *FT’ • T’ → ε • F →(E) • F →i First(E) = { ( , i } First(E’) = { + , ε } First(T) = { ( , i } First(T’) = { * , ε } First(F) = { ( , i } Follow(E) = { $ , ) } Follow(E’) = { $ , ) } Follow(T) = { $ , + , ) } Follow(T’) = { $ , + , ) } Follow(F) = { $ , + , * , ) } Samuel2005@126.com
Constructing LL(1) Parsing Table Samuel2005@126.com
Example • E → TE’ • E’ → +TE’ • E’ → ε • T → FT’ • T’ → *FT’ • T’ → ε • F →(E) • F →i First(E) = { ( , i } First(E’) = { + , ε } First(T) = { ( , i } First(T’) = { * , ε } First(F) = { ( , i } Follow(E) = { $ , ) } Follow(E’) = { $ , ) } Follow(T) = { $ , + , ) } Follow(T’) = { $ , + , ) } Follow(F) = { $ , + , * , ) } Samuel2005@126.com
Constructing LL(1) Parsing Table Samuel2005@126.com
分析符号串i + i * i Samuel2005@126.com
Constructing LL(1) Parsing Table Samuel2005@126.com
分析符号串i + i * i Samuel2005@126.com
Constructing LL(1) Parsing Table Samuel2005@126.com
分析符号串i + i * i Samuel2005@126.com
Constructing LL(1) Parsing Table Samuel2005@126.com
分析符号串i + i * i Samuel2005@126.com
Constructing LL(1) Parsing Table Samuel2005@126.com
分析符号串i + i * i Samuel2005@126.com
Constructing LL(1) Parsing Table Samuel2005@126.com
分析符号串i + i * i Samuel2005@126.com
分析符号串i + i * i Samuel2005@126.com
Constructing LL(1) Parsing Table Samuel2005@126.com
分析符号串i + i * i Samuel2005@126.com
Constructing LL(1) Parsing Table Samuel2005@126.com
分析符号串i + i * i Samuel2005@126.com
Constructing LL(1) Parsing Table Samuel2005@126.com
分析符号串i + i * i Samuel2005@126.com
分析符号串i + i * i Samuel2005@126.com
Constructing LL(1) Parsing Table Samuel2005@126.com
分析符号串i + i * i Samuel2005@126.com
Constructing LL(1) Parsing Table Samuel2005@126.com
分析符号串i + i * i Samuel2005@126.com
Constructing LL(1) Parsing Table Samuel2005@126.com
分析符号串i + i * i Samuel2005@126.com
Homework • 4.5Show the actions of an LL(1) parser that uses Table 4.4(Page 163) to recognize the following arithmetic expressions: • a. 3 + 4 * 5 - 6 • b. 3 * ( 4 – 5 + 6 ) • c. 3 - ( 4 + 5 * 6 ) exp → term exp’ exp’ → addop term exp’ | ε addop →+ | - term → factor term’ term’ →mulop factor term’ | ε mulop →* factor →(exp) | number Samuel2005@126.com
Homework • 4.7Given the grammar A → ( A ) A | ε, • a. Construct First and Follow sets for the nonterminal A. • b. Show this grammar is LL(1). Samuel2005@126.com
Homework • 4.8Consider the grammar • a. Remove the left recursion. • b. Construct First and Follow sets for the nonterminals of the resulting grammar. • c. Show that the resulting grammar is LL(1). lexp → atom | list atom →number | identifier list →(lexp-seq) lexp-seq → lexp-seq lexp | lexp Samuel2005@126.com
Homework • d. Construct the LL(1) parsing table for the resulting grammar. • e. Show the actions of the corresponding LL(1) parser, given the input string • ( a ( b ( 2 ) ) ( c ) ). lexp → atom | list atom →number | identifier list →(lexp-seq) lexp-seq → lexp-seq lexp | lexp Samuel2005@126.com