csci 3136 principles of programming languages
play

CSCI 3136 Principles of Programming Languages Syntactic Analysis - PowerPoint PPT Presentation

CSCI 3136 Principles of Programming Languages Syntactic Analysis and Context-Free Grammars - 2 Summer 2013 Faculty of Computer Science Dalhousie University 1 / 102 Language defined by a CFG L ( G ) = { w | S w } 2 / 102 Language


  1. Parse Trees Every derivation can be represented by a parse tree : ———————– S → P V P P → N P → A P A → big|green N → cheese|Jim V → ate ———————— • The root is S • The children of each node are the symbols (terminals and non-terminals) it is replaced with. • The internal nodes are non-terminals. The leaves–called the yield of the parse-tree–are terminals. 39 / 102

  2. Parse Trees Every derivation can be represented by a parse tree : ———————– • S ⇒ P V P S → P V P P → N P → A P A → big|green N → cheese|Jim V → ate ———————— • The root is S • The children of each node are the symbols (terminals and non-terminals) it is replaced with. • The internal nodes are non-terminals. The leaves–called the yield of the parse-tree–are terminals. 40 / 102

  3. Parse Trees Every derivation can be represented by a parse tree : ———————– • S ⇒ P V P S → P V P P → N P → A P S A → big|green P V P N → cheese|Jim V → ate ———————— • The root is S • The children of each node are the symbols (terminals and non-terminals) it is replaced with. • The internal nodes are non-terminals. The leaves–called the yield of the parse-tree–are terminals. 41 / 102

  4. Parse Trees Every derivation can be represented by a parse tree : ———————– • S ⇒ P V P S → P V P P → N P → A P S A → big|green P V P N → cheese|Jim V → ate ———————— • The root is S • The children of each node are the symbols (terminals and non-terminals) it is replaced with. • The internal nodes are non-terminals. The leaves–called the yield of the parse-tree–are terminals. 42 / 102

  5. Parse Trees Every derivation can be represented by a parse tree : ———————– • S ⇒ P V P ⇒ N V P S → P V P P → N P → A P S A → big|green P V P N → cheese|Jim V → ate N ———————— • The root is S • The children of each node are the symbols (terminals and non-terminals) it is replaced with. • The internal nodes are non-terminals. The leaves–called the yield of the parse-tree–are terminals. 43 / 102

  6. Parse Trees Every derivation can be represented by a parse tree : ———————– • S ⇒ P V P ⇒ N V P S → P V P P → N P → A P S A → big|green P V P N → cheese|Jim V → ate N ———————— • The root is S • The children of each node are the symbols (terminals and non-terminals) it is replaced with. • The internal nodes are non-terminals. The leaves–called the yield of the parse-tree–are terminals. 44 / 102

  7. Parse Trees Every derivation can be represented by a parse tree : ———————– • S ⇒ P V P ⇒ N V P S → P V P P → N P → A P S A → big|green P V P N → cheese|Jim V → ate N ———————— • The root is S • The children of each node are the symbols (terminals and non-terminals) it is replaced with. • The internal nodes are non-terminals. The leaves–called the yield of the parse-tree–are terminals. 45 / 102

  8. Parse Trees Every derivation can be represented by a parse tree : ———————– • S ⇒ P V P ⇒ N V P ⇒ N V N S → P V P P → N P → A P S A → big|green P V P N → cheese|Jim V → ate N N ———————— • The root is S • The children of each node are the symbols (terminals and non-terminals) it is replaced with. • The internal nodes are non-terminals. The leaves–called the yield of the parse-tree–are terminals. 46 / 102

  9. Parse Trees Every derivation can be represented by a parse tree : ———————– • S ⇒ P V P ⇒ N V P ⇒ N V N S → P V P P → N P → A P S A → big|green P V P N → cheese|Jim V → ate N N ———————— • The root is S • The children of each node are the symbols (terminals and non-terminals) it is replaced with. • The internal nodes are non-terminals. The leaves–called the yield of the parse-tree–are terminals. 47 / 102

  10. Parse Trees Every derivation can be represented by a parse tree : ———————– • S ⇒ P V P ⇒ N V P ⇒ N V N S → P V P P → N P → A P S A → big|green P V P N → cheese|Jim V → ate N N ———————— • The root is S • The children of each node are the symbols (terminals and non-terminals) it is replaced with. • The internal nodes are non-terminals. The leaves–called the yield of the parse-tree–are terminals. 48 / 102

  11. Parse Trees Every derivation can be represented by a parse tree : ———————– • S ⇒ P V P ⇒ N V P ⇒ N V N ⇒ Jim V N S → P V P P → N P → A P S A → big|green P V P N → cheese|Jim V → ate N N ———————— Jim • The root is S • The children of each node are the symbols (terminals and non-terminals) it is replaced with. • The internal nodes are non-terminals. The leaves–called the yield of the parse-tree–are terminals. 49 / 102

  12. Parse Trees Every derivation can be represented by a parse tree : ———————– • S ⇒ P V P ⇒ N V P ⇒ N V N ⇒ Jim V N S → P V P P → N P → A P S A → big|green P V P N → cheese|Jim V → ate N N ———————— Jim • The root is S • The children of each node are the symbols (terminals and non-terminals) it is replaced with. • The internal nodes are non-terminals. The leaves–called the yield of the parse-tree–are terminals. 50 / 102

  13. Parse Trees Every derivation can be represented by a parse tree : ———————– • S ⇒ P V P ⇒ N V P ⇒ N V N ⇒ Jim V N S → P V P P → N P → A P S A → big|green P V P N → cheese|Jim V → ate N N ———————— Jim • The root is S • The children of each node are the symbols (terminals and non-terminals) it is replaced with. • The internal nodes are non-terminals. The leaves–called the yield of the parse-tree–are terminals. 51 / 102

  14. Parse Trees Every derivation can be represented by a parse tree : ———————– • S ⇒ P V P ⇒ N V P ⇒ N V N ⇒ Jim V N S → P V P ⇒ Jim ate N P → N P → A P S A → big|green P V P N → cheese|Jim V → ate N N ate ———————— Jim • The root is S • The children of each node are the symbols (terminals and non-terminals) it is replaced with. • The internal nodes are non-terminals. The leaves–called the yield of the parse-tree–are terminals. 52 / 102

  15. Parse Trees Every derivation can be represented by a parse tree : ———————– • S ⇒ P V P ⇒ N V P ⇒ N V N ⇒ Jim V N S → P V P ⇒ Jim ate N P → N P → A P S A → big|green P V P N → cheese|Jim V → ate N N ate ———————— Jim • The root is S • The children of each node are the symbols (terminals and non-terminals) it is replaced with. • The internal nodes are non-terminals. The leaves–called the yield of the parse-tree–are terminals. 53 / 102

  16. Parse Trees Every derivation can be represented by a parse tree : ———————– • S ⇒ P V P ⇒ N V P ⇒ N V N ⇒ Jim V N S → P V P ⇒ Jim ate N P → N P → A P S A → big|green P V P N → cheese|Jim V → ate N N ate ———————— Jim • The root is S • The children of each node are the symbols (terminals and non-terminals) it is replaced with. • The internal nodes are non-terminals. The leaves–called the yield of the parse-tree–are terminals. 54 / 102

  17. Parse Trees Every derivation can be represented by a parse tree : ———————– • S ⇒ P V P ⇒ N V P ⇒ N V N ⇒ Jim V N S → P V P ⇒ Jim ate N ⇒ Jim ate cheese P → N P → A P S A → big|green P V P N → cheese|Jim V → ate N N ate ———————— Jim cheese • The root is S • The children of each node are the symbols (terminals and non-terminals) it is replaced with. • The internal nodes are non-terminals. The leaves–called the yield of the parse-tree–are terminals. 55 / 102

  18. Parse Trees Every derivation can be represented by a parse tree : ———————– • S ⇒ P V P ⇒ N V P ⇒ N V N ⇒ Jim V N S → P V P ⇒ Jim ate N ⇒ Jim ate cheese P → N P → A P S A → big|green P V P N → cheese|Jim V → ate N N ate ———————— Jim cheese • The root is S • The children of each node are the symbols (terminals and non-terminals) it is replaced with. • The internal nodes are non-terminals. The leaves–called the yield of the parse-tree–are terminals. 56 / 102

  19. Parse Tree (another example) 57 / 102

  20. Parse Tree (another example) ———————– S → P V P P → N P → A P A → big|green N → cheese|Jim V → ate ———————— 58 / 102

  21. Parse Tree (another example) ———————– • S ⇒ P V P S → P V P P → N P → A P A → big|green N → cheese|Jim V → ate ———————— 59 / 102

  22. Parse Tree (another example) ———————– • S ⇒ P V P S → P V P P → N P → A P A → big|green N → cheese|Jim V → ate S ———————— P V P 60 / 102

  23. Parse Tree (another example) ———————– • S ⇒ P V P S → P V P P → N P → A P A → big|green N → cheese|Jim V → ate S ———————— P V P 61 / 102

  24. Parse Tree (another example) ———————– • S ⇒ P V P ⇒ A P V P S → P V P P → N P → A P A → big|green N → cheese|Jim V → ate S ———————— P V P A P 62 / 102

  25. Parse Tree (another example) ———————– • S ⇒ P V P ⇒ A P V P S → P V P P → N P → A P A → big|green N → cheese|Jim V → ate S ———————— P V P A P 63 / 102

  26. Parse Tree (another example) ———————– • S ⇒ P V P ⇒ A P V P S → P V P P → N P → A P A → big|green N → cheese|Jim V → ate S ———————— P V P A P 64 / 102

  27. Parse Tree (another example) ———————– • S ⇒ P V P ⇒ A P V P ⇒ big P V P S → P V P P → N P → A P A → big|green N → cheese|Jim V → ate S ———————— P V P A P big 65 / 102

  28. Parse Tree (another example) ———————– • S ⇒ P V P ⇒ A P V P ⇒ big P V P S → P V P P → N P → A P A → big|green N → cheese|Jim V → ate S ———————— P V P A P big 66 / 102

  29. Parse Tree (another example) ———————– • S ⇒ P V P ⇒ A P V P ⇒ big P V P S → P V P P → N P → A P A → big|green N → cheese|Jim V → ate S ———————— P V P A P big 67 / 102

  30. Parse Tree (another example) ———————– • S ⇒ P V P ⇒ A P V P ⇒ big P V P ⇒ S → P V P big N V P P → N P → A P A → big|green N → cheese|Jim V → ate S ———————— P V P A P N big 68 / 102

  31. Parse Tree (another example) ———————– • S ⇒ P V P ⇒ A P V P ⇒ big P V P ⇒ S → P V P big N V P P → N P → A P A → big|green N → cheese|Jim V → ate S ———————— P V P A P N big 69 / 102

  32. Parse Tree (another example) ———————– • S ⇒ P V P ⇒ A P V P ⇒ big P V P ⇒ S → P V P big N V P P → N P → A P A → big|green N → cheese|Jim V → ate S ———————— P V P A P N big 70 / 102

  33. Parse Tree (another example) ———————– • S ⇒ P V P ⇒ A P V P ⇒ big P V P ⇒ S → P V P big N V P ⇒ big Jim V P P → N P → A P A → big|green N → cheese|Jim V → ate S ———————— P V P A P N big Jim 71 / 102

  34. Parse Tree (another example) ———————– • S ⇒ P V P ⇒ A P V P ⇒ big P V P ⇒ S → P V P big N V P ⇒ big Jim V P P → N P → A P A → big|green N → cheese|Jim V → ate S ———————— P V P A P N big Jim 72 / 102

  35. Parse Tree (another example) ———————– • S ⇒ P V P ⇒ A P V P ⇒ big P V P ⇒ S → P V P big N V P ⇒ big Jim V P P → N P → A P A → big|green N → cheese|Jim V → ate S ———————— P V P A P N big Jim 73 / 102

  36. Parse Tree (another example) ———————– • S ⇒ P V P ⇒ A P V P ⇒ big P V P ⇒ S → P V P big N V P ⇒ big Jim V P ⇒ big Jim ate P P → N P → A P A → big|green N → cheese|Jim V → ate S ———————— P V P A P ate N big Jim 74 / 102

  37. Parse Tree (another example) ———————– • S ⇒ P V P ⇒ A P V P ⇒ big P V P ⇒ S → P V P big N V P ⇒ big Jim V P ⇒ big Jim ate P P → N P → A P A → big|green N → cheese|Jim V → ate S ———————— P V P A P ate N big Jim 75 / 102

  38. Parse Tree (another example) ———————– • S ⇒ P V P ⇒ A P V P ⇒ big P V P ⇒ S → P V P big N V P ⇒ big Jim V P ⇒ big Jim ate P P → N P → A P A → big|green N → cheese|Jim V → ate S ———————— P V P A P ate N big Jim 76 / 102

  39. Parse Tree (another example) ———————– • S ⇒ P V P ⇒ A P V P ⇒ big P V P ⇒ S → P V P big N V P ⇒ big Jim V P ⇒ big Jim ate P P → N ⇒ big Jim ate A P P → A P A → big|green N → cheese|Jim V → ate S ———————— P V P A P ate A P N big Jim 77 / 102

  40. Parse Tree (another example) ———————– • S ⇒ P V P ⇒ A P V P ⇒ big P V P ⇒ S → P V P big N V P ⇒ big Jim V P ⇒ big Jim ate P P → N ⇒ big Jim ate A P P → A P A → big|green N → cheese|Jim V → ate S ———————— P V P A P ate A P N big Jim 78 / 102

  41. Parse Tree (another example) ———————– • S ⇒ P V P ⇒ A P V P ⇒ big P V P ⇒ S → P V P big N V P ⇒ big Jim V P ⇒ big Jim ate P P → N ⇒ big Jim ate A P P → A P A → big|green N → cheese|Jim V → ate S ———————— P V P A P ate A P N big Jim 79 / 102

  42. Parse Tree (another example) ———————– • S ⇒ P V P ⇒ A P V P ⇒ big P V P ⇒ S → P V P big N V P ⇒ big Jim V P ⇒ big Jim ate P P → N ⇒ big Jim ate A P ⇒ big Jim ate green P P → A P A → big|green N → cheese|Jim V → ate S ———————— P V P A P ate A P N big green Jim 80 / 102

  43. Parse Tree (another example) ———————– • S ⇒ P V P ⇒ A P V P ⇒ big P V P ⇒ S → P V P big N V P ⇒ big Jim V P ⇒ big Jim ate P P → N ⇒ big Jim ate A P ⇒ big Jim ate green P P → A P A → big|green N → cheese|Jim V → ate S ———————— P V P A P ate A P N big green Jim 81 / 102

  44. Parse Tree (another example) ———————– • S ⇒ P V P ⇒ A P V P ⇒ big P V P ⇒ S → P V P big N V P ⇒ big Jim V P ⇒ big Jim ate P P → N ⇒ big Jim ate A P ⇒ big Jim ate green P P → A P A → big|green N → cheese|Jim V → ate S ———————— P V P A P ate A P N big green Jim 82 / 102

  45. Parse Tree (another example) ———————– • S ⇒ P V P ⇒ A P V P ⇒ big P V P ⇒ S → P V P big N V P ⇒ big Jim V P ⇒ big Jim ate P P → N ⇒ big Jim ate A P ⇒ big Jim ate green P P → A P ⇒ big Jim ate green N A → big|green N → cheese|Jim V → ate S ———————— P V P A P ate A P N N big green Jim 83 / 102

  46. Parse Tree (another example) ———————– • S ⇒ P V P ⇒ A P V P ⇒ big P V P ⇒ S → P V P big N V P ⇒ big Jim V P ⇒ big Jim ate P P → N ⇒ big Jim ate A P ⇒ big Jim ate green P P → A P ⇒ big Jim ate green N A → big|green N → cheese|Jim V → ate S ———————— P V P A P ate A P N N big green Jim 84 / 102

  47. Parse Tree (another example) ———————– • S ⇒ P V P ⇒ A P V P ⇒ big P V P ⇒ S → P V P big N V P ⇒ big Jim V P ⇒ big Jim ate P P → N ⇒ big Jim ate A P ⇒ big Jim ate green P P → A P ⇒ big Jim ate green N A → big|green N → cheese|Jim V → ate S ———————— P V P A P ate A P N N big green Jim 85 / 102

  48. Parse Tree (another example) ———————– • S ⇒ P V P ⇒ A P V P ⇒ big P V P ⇒ S → P V P big N V P ⇒ big Jim V P ⇒ big Jim ate P P → N ⇒ big Jim ate A P ⇒ big Jim ate green P P → A P ⇒ big Jim ate green N A → big|green ⇒ big Jim ate green cheese N → cheese|Jim V → ate S ———————— P V P A P ate A P N N big green Jim cheese 86 / 102

  49. Parse Tree (another example) ———————– • S ⇒ P V P ⇒ A P V P ⇒ big P V P ⇒ S → P V P big N V P ⇒ big Jim V P ⇒ big Jim ate P P → N ⇒ big Jim ate A P ⇒ big Jim ate green P P → A P ⇒ big Jim ate green N A → big|green ⇒ big Jim ate green cheese N → cheese|Jim V → ate S ———————— P V P A P ate A P N N big green Jim cheese 87 / 102

  50. Ambiguity: Example 88 / 102

  51. Ambiguity: Example A CFG for arithmetic expressions ———————– E → n E → i E → E + E E → E − E E → E ∗ E E → E / E E → ( E ) ———————— 89 / 102

  52. Ambiguity: Example A CFG for 2 + 3 ∗ 4 arithmetic expressions ———————– E → n E → i E → E + E E → E − E E → E ∗ E E → E / E E → ( E ) ———————— 90 / 102

  53. Ambiguity: Example A CFG for 2 + 3 ∗ 4 arithmetic expressions E ———————– E → n E + E E → i E → E + E 2 E * E E → E − E E → E ∗ E 3 4 E → E / E E → ( E ) ———————— 91 / 102

  54. Ambiguity: Example A CFG for 2 + 3 ∗ 4 E arithmetic expressions E ———————– E * E E → n E + E E → i E + E 4 E → E + E 2 E * E E → E − E 2 3 E → E ∗ E 3 4 E → E / E E → ( E ) ———————— 92 / 102

  55. Ambiguity: Example A CFG for 2 + 3 ∗ 4 E arithmetic expressions E ———————– E * E E → n E + E E → i E + E 4 E → E + E 2 E * E E → E − E 2 3 E → E ∗ E 3 4 Violates precedence rules. E → E / E E → ( E ) ———————— 93 / 102

  56. Ambiguity 94 / 102

  57. Ambiguity • There may be more than one parse tree for the same sentence generated by a grammar G. If this is the case, we call G ambiguous . 95 / 102

  58. Ambiguity • There may be more than one parse tree for the same sentence generated by a grammar G. If this is the case, we call G ambiguous . • Problems of ambiguous grammar: one sentence has different interpretations. 96 / 102

  59. Some facts about multiple representations 97 / 102

  60. Some facts about multiple representations • There are infinitely many context-free grammars generating a given context-free language. 98 / 102

  61. Some facts about multiple representations • There are infinitely many context-free grammars generating a given context-free language. (How?) 99 / 102

  62. Some facts about multiple representations • There are infinitely many context-free grammars generating a given context-free language. (How?) • Just add arbitrary non-terminals to the right-hand sides of productions and then add ǫ -productions for these non-terminals. 100 / 102

Recommend


More recommend