compilers
play

Compilers Nikos Papaspyrou Kostis Sagonas nickie@softlab.ntua.gr - PowerPoint PPT Presentation

Compilers Nikos Papaspyrou Kostis Sagonas nickie@softlab.ntua.gr kostis@cs.ntua.gr National Technical University of Athens School of Electrical and Computer Engineering Software Engineering Laboratory Polytechnioupoli, 15780 Zografou,


  1. Compilers Nikos Papaspyrou Kostis Sagonas nickie@softlab.ntua.gr kostis@cs.ntua.gr National Technical University of Athens School of Electrical and Computer Engineering Software Engineering Laboratory Polytechnioupoli, 15780 Zografou, Athens, Greece. March 2017 Nikos Papaspyrou, Kostis Sagonas Compilers March 2017 1 / 216

  2. Chapter 4: Parsing Nikos Papaspyrou, Kostis Sagonas Compilers March 2017 55 / 216

  3. Parsing ▶ Parse tree ▶ It can be built in two ways: ▶ Top-down i.e., starting from the root and moving towards the leaves ▶ Bottom-up i.e., starting from the leaves and moving towards the root Nikos Papaspyrou, Kostis Sagonas Compilers March 2017 56 / 216

  4. Top-down and bottom-up από�πάνω�προς�τα�κάτω (top�down) � S � � a A �� B c �� � A a b �� �� � � c S B � ε a � � Nikos Papaspyrou, Kostis Sagonas Compilers March 2017 57 / 216

  5. Top-down and bottom-up από�πάνω�προς�τα�κάτω από�κάτω�προς�τα�πάνω (top�down) (bottom�up) �� � S S � � � � �� a A B c a A �� B c �� �� � A a b � A a b � � �� �� � c S B � � � c S B � � ε a � ε a � � Nikos Papaspyrou, Kostis Sagonas Compilers March 2017 57 / 216

  6. ▶ If 𝛽 ⇒ ∗ a 𝛾 then a ∈ FIRST (𝛽) ▶ If 𝛽 ⇒ ∗ 𝜗 then 𝜗 ∈ FIRST (𝛽) Auxiliaries (i) ▶ FIRST sets ▶ Let 𝛽 ∈ (𝑈 ∪ 𝑂) ∗ be a string ▶ The set FIRST (𝛽) ⊆ 𝑈 ∪ { 𝜗 } contains the terminal symbols from which all strings that are produced by 𝛽 are bound to start Nikos Papaspyrou, Kostis Sagonas Compilers March 2017 58 / 216

  7. Auxiliaries (i) ▶ FIRST sets ▶ Let 𝛽 ∈ (𝑈 ∪ 𝑂) ∗ be a string ▶ The set FIRST (𝛽) ⊆ 𝑈 ∪ { 𝜗 } contains the terminal symbols from which all strings that are produced by 𝛽 are bound to start ▶ If 𝛽 ⇒ ∗ a 𝛾 then a ∈ FIRST (𝛽) ▶ If 𝛽 ⇒ ∗ 𝜗 then 𝜗 ∈ FIRST (𝛽) Nikos Papaspyrou, Kostis Sagonas Compilers March 2017 58 / 216

  8. ▶ If 𝑇 ⇒ ∗ 𝛽𝐵 a 𝛾 then a ∈ FOLLOW (𝐵) ▶ If 𝑇 ⇒ ∗ 𝛽𝐵 then EOF ∈ FOLLOW (𝐵) Auxiliaries (ii) ▶ FOLLOW sets ▶ Let 𝐵 be a non-terminal symbol ▶ The set FOLLOW (𝐵) ⊆ 𝑈 ∪ { EOF } contains all terminal symbol that may follow 𝐵 during a production from the start symbol 𝑇 ▶ If 𝐵 can be the last symbol in a production, then EOF ∈ FOLLOW (𝐵) Nikos Papaspyrou, Kostis Sagonas Compilers March 2017 59 / 216

  9. Auxiliaries (ii) ▶ FOLLOW sets ▶ Let 𝐵 be a non-terminal symbol ▶ The set FOLLOW (𝐵) ⊆ 𝑈 ∪ { EOF } contains all terminal symbol that may follow 𝐵 during a production from the start symbol 𝑇 ▶ If 𝐵 can be the last symbol in a production, then EOF ∈ FOLLOW (𝐵) ▶ If 𝑇 ⇒ ∗ 𝛽𝐵 a 𝛾 then a ∈ FOLLOW (𝐵) ▶ If 𝑇 ⇒ ∗ 𝛽𝐵 then EOF ∈ FOLLOW (𝐵) Nikos Papaspyrou, Kostis Sagonas Compilers March 2017 59 / 216

  10. Calculating FIRST (i) ▶ FIRST (𝜗) = { 𝜗 } ▶ FIRST ( a 𝛾) = { a } ▶ if 𝜗 ∉ FIRST (𝐵) then FIRST (𝐵𝛾) = FIRST (𝐵) ▶ if 𝜗 ∈ FIRST (𝐵) then FIRST (𝐵𝛾) = ( FIRST (𝐵) − { 𝜗 }) ∪ FIRST (𝛾) ▶ for each rule 𝐵 → 𝛽 , it must be FIRST (𝛽) ⊆ FIRST (𝐵) Nikos Papaspyrou, Kostis Sagonas Compilers March 2017 60 / 216

  11. { id , ( } { id , ( } { id , ( } { + , 𝜗 } { * , 𝜗 } = = = Calculating FIRST (ii) E → T E’ E’ → 𝜗 ▶ Example E’ → + T E’ FIRST ( E ) FIRST ( T ) T → F T’ FIRST ( F ) T’ → 𝜗 FIRST ( E’ ) = T’ → * F T’ FIRST ( T’ ) = F → ( E ) F → id Nikos Papaspyrou, Kostis Sagonas Compilers March 2017 61 / 216

  12. { id , ( } { id , ( } { id , ( } { * , 𝜗 } = = = Calculating FIRST (ii) E → T E’ E’ → 𝜗 ▶ Example E’ → + T E’ FIRST ( E ) FIRST ( T ) T → F T’ FIRST ( F ) T’ → 𝜗 FIRST ( E’ ) = { + , 𝜗 } T’ → * F T’ FIRST ( T’ ) = F → ( E ) F → id Nikos Papaspyrou, Kostis Sagonas Compilers March 2017 61 / 216

  13. { id , ( } { id , ( } { id , ( } = = = Calculating FIRST (ii) E → T E’ E’ → 𝜗 ▶ Example E’ → + T E’ FIRST ( E ) FIRST ( T ) T → F T’ FIRST ( F ) T’ → 𝜗 FIRST ( E’ ) = { + , 𝜗 } T’ → * F T’ FIRST ( T’ ) = { * , 𝜗 } F → ( E ) F → id Nikos Papaspyrou, Kostis Sagonas Compilers March 2017 61 / 216

  14. { id , ( } { id , ( } = = = Calculating FIRST (ii) E → T E’ E’ → 𝜗 ▶ Example E’ → + T E’ FIRST ( E ) FIRST ( T ) T → F T’ FIRST ( F ) { id , ( } T’ → 𝜗 FIRST ( E’ ) = { + , 𝜗 } T’ → * F T’ FIRST ( T’ ) = { * , 𝜗 } F → ( E ) F → id Nikos Papaspyrou, Kostis Sagonas Compilers March 2017 61 / 216

  15. { id , ( } = = = Calculating FIRST (ii) E → T E’ E’ → 𝜗 ▶ Example E’ → + T E’ FIRST ( E ) FIRST ( T ) { id , ( } T → F T’ FIRST ( F ) { id , ( } T’ → 𝜗 FIRST ( E’ ) = { + , 𝜗 } T’ → * F T’ FIRST ( T’ ) = { * , 𝜗 } F → ( E ) F → id Nikos Papaspyrou, Kostis Sagonas Compilers March 2017 61 / 216

  16. = = = Calculating FIRST (ii) E → T E’ E’ → 𝜗 ▶ Example E’ → + T E’ FIRST ( E ) { id , ( } FIRST ( T ) { id , ( } T → F T’ FIRST ( F ) { id , ( } T’ → 𝜗 FIRST ( E’ ) = { + , 𝜗 } T’ → * F T’ FIRST ( T’ ) = { * , 𝜗 } F → ( E ) F → id Nikos Papaspyrou, Kostis Sagonas Compilers March 2017 61 / 216

  17. Calculating FOLLOW (i) ▶ EOF ∈ FOLLOW (𝑇) ▶ for each rule 𝐵 → 𝛽𝐶𝛾 ▶ ( FIRST (𝛾) − { 𝜗 }) ⊆ FOLLOW (𝐶) ▶ if 𝜗 ∈ FIRST (𝛾) then FOLLOW (𝐵) ⊆ FOLLOW (𝐶) Nikos Papaspyrou, Kostis Sagonas Compilers March 2017 62 / 216

  18. EOF , ) } + , EOF , ) } * , + , EOF , ) } EOF , ) } + , EOF , ) } = { = { = { Calculating FOLLOW (ii) E → T E’ ▶ Example E’ → 𝜗 FOLLOW ( E ) E’ → + T E’ FOLLOW ( T ) FOLLOW ( F ) T → F T’ FOLLOW ( E’ ) = { T’ → 𝜗 FOLLOW ( T’ ) = { T’ → * F T’ F → ( E ) FIRST ( E’ ) = { + , 𝜗 } FIRST ( T’ ) = { * , 𝜗 } F → id Nikos Papaspyrou, Kostis Sagonas Compilers March 2017 63 / 216

  19. , ) } + , EOF , ) } * , + , EOF , ) } EOF , ) } + , EOF , ) } = { = { Calculating FOLLOW (ii) E → T E’ ▶ Example E’ → 𝜗 FOLLOW ( E ) = { EOF E’ → + T E’ FOLLOW ( T ) FOLLOW ( F ) T → F T’ FOLLOW ( E’ ) = { T’ → 𝜗 FOLLOW ( T’ ) = { T’ → * F T’ F → ( E ) FIRST ( E’ ) = { + , 𝜗 } FIRST ( T’ ) = { * , 𝜗 } F → id Nikos Papaspyrou, Kostis Sagonas Compilers March 2017 63 / 216

  20. + , EOF , ) } * , + , EOF , ) } EOF , ) } + , EOF , ) } } = { = { Calculating FOLLOW (ii) E → T E’ ▶ Example E’ → 𝜗 FOLLOW ( E ) = { EOF , ) E’ → + T E’ FOLLOW ( T ) FOLLOW ( F ) T → F T’ FOLLOW ( E’ ) = { T’ → 𝜗 FOLLOW ( T’ ) = { T’ → * F T’ F → ( E ) FIRST ( E’ ) = { + , 𝜗 } FIRST ( T’ ) = { * , 𝜗 } F → id Nikos Papaspyrou, Kostis Sagonas Compilers March 2017 63 / 216

  21. + , EOF , ) } * , + , EOF , ) } EOF , ) } + , EOF , ) } = { = { Calculating FOLLOW (ii) E → T E’ ▶ Example E’ → 𝜗 FOLLOW ( E ) = { EOF , ) } E’ → + T E’ FOLLOW ( T ) FOLLOW ( F ) T → F T’ FOLLOW ( E’ ) = { T’ → 𝜗 FOLLOW ( T’ ) = { T’ → * F T’ F → ( E ) FIRST ( E’ ) = { + , 𝜗 } FIRST ( T’ ) = { * , 𝜗 } F → id Nikos Papaspyrou, Kostis Sagonas Compilers March 2017 63 / 216

  22. , EOF , ) } * , + , EOF , ) } EOF , ) } + , EOF , ) } = { Calculating FOLLOW (ii) E → T E’ ▶ Example E’ → 𝜗 FOLLOW ( E ) = { EOF , ) } E’ → + T E’ FOLLOW ( T ) = { + FOLLOW ( F ) T → F T’ FOLLOW ( E’ ) = { T’ → 𝜗 FOLLOW ( T’ ) = { T’ → * F T’ F → ( E ) FIRST ( E’ ) = { + , 𝜗 } FIRST ( T’ ) = { * , 𝜗 } F → id Nikos Papaspyrou, Kostis Sagonas Compilers March 2017 63 / 216

  23. * , + , EOF , ) } EOF , ) } + , EOF , ) } } = { Calculating FOLLOW (ii) E → T E’ ▶ Example E’ → 𝜗 FOLLOW ( E ) = { EOF , ) } E’ → + T E’ FOLLOW ( T ) = { + , EOF , ) FOLLOW ( F ) T → F T’ FOLLOW ( E’ ) = { T’ → 𝜗 FOLLOW ( T’ ) = { T’ → * F T’ F → ( E ) FIRST ( E’ ) = { + , 𝜗 } FIRST ( T’ ) = { * , 𝜗 } F → id Nikos Papaspyrou, Kostis Sagonas Compilers March 2017 63 / 216

  24. , + , EOF , ) } EOF , ) } + , EOF , ) } } Calculating FOLLOW (ii) E → T E’ ▶ Example E’ → 𝜗 FOLLOW ( E ) = { EOF , ) } E’ → + T E’ FOLLOW ( T ) = { + , EOF , ) FOLLOW ( F ) = { * T → F T’ FOLLOW ( E’ ) = { T’ → 𝜗 FOLLOW ( T’ ) = { T’ → * F T’ F → ( E ) FIRST ( E’ ) = { + , 𝜗 } FIRST ( T’ ) = { * , 𝜗 } F → id Nikos Papaspyrou, Kostis Sagonas Compilers March 2017 63 / 216

  25. EOF , ) } + , EOF , ) } } } Calculating FOLLOW (ii) E → T E’ ▶ Example E’ → 𝜗 FOLLOW ( E ) = { EOF , ) } E’ → + T E’ FOLLOW ( T ) = { + , EOF , ) FOLLOW ( F ) = { * , + , EOF , ) T → F T’ FOLLOW ( E’ ) = { T’ → 𝜗 FOLLOW ( T’ ) = { T’ → * F T’ F → ( E ) FIRST ( E’ ) = { + , 𝜗 } FIRST ( T’ ) = { * , 𝜗 } F → id Nikos Papaspyrou, Kostis Sagonas Compilers March 2017 63 / 216

Recommend


More recommend