1 / 48

Chapter 4 Top-Down Parsing

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.

pilar
Télécharger la présentation

Chapter 4 Top-Down Parsing

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 4 Top-Down Parsing LL(1) Parsing Example Gang S. Liu College of Computer Science & Technology Harbin Engineering University

  2. 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

  3. 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

  4. 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

  5. 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

  6. 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

  7. Constructing LL(1) Parsing Table Samuel2005@126.com

  8. 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

  9. Constructing LL(1) Parsing Table Samuel2005@126.com

  10. 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

  11. Constructing LL(1) Parsing Table Samuel2005@126.com

  12. 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

  13. Constructing LL(1) Parsing Table Samuel2005@126.com

  14. 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

  15. Constructing LL(1) Parsing Table Samuel2005@126.com

  16. 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

  17. Constructing LL(1) Parsing Table Samuel2005@126.com

  18. 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

  19. Constructing LL(1) Parsing Table Samuel2005@126.com

  20. 分析符号串i + i * i Samuel2005@126.com

  21. Constructing LL(1) Parsing Table Samuel2005@126.com

  22. 分析符号串i + i * i Samuel2005@126.com

  23. Constructing LL(1) Parsing Table Samuel2005@126.com

  24. 分析符号串i + i * i Samuel2005@126.com

  25. Constructing LL(1) Parsing Table Samuel2005@126.com

  26. 分析符号串i + i * i Samuel2005@126.com

  27. Constructing LL(1) Parsing Table Samuel2005@126.com

  28. 分析符号串i + i * i Samuel2005@126.com

  29. Constructing LL(1) Parsing Table Samuel2005@126.com

  30. 分析符号串i + i * i Samuel2005@126.com

  31. 分析符号串i + i * i Samuel2005@126.com

  32. Constructing LL(1) Parsing Table Samuel2005@126.com

  33. 分析符号串i + i * i Samuel2005@126.com

  34. Constructing LL(1) Parsing Table Samuel2005@126.com

  35. 分析符号串i + i * i Samuel2005@126.com

  36. Constructing LL(1) Parsing Table Samuel2005@126.com

  37. 分析符号串i + i * i Samuel2005@126.com

  38. 分析符号串i + i * i Samuel2005@126.com

  39. Constructing LL(1) Parsing Table Samuel2005@126.com

  40. 分析符号串i + i * i Samuel2005@126.com

  41. Constructing LL(1) Parsing Table Samuel2005@126.com

  42. 分析符号串i + i * i Samuel2005@126.com

  43. Constructing LL(1) Parsing Table Samuel2005@126.com

  44. 分析符号串i + i * i Samuel2005@126.com

  45. 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

  46. 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

  47. 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

  48. 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

More Related