1 / 13

C H A P T E R T W O

C H A P T E R T W O. Syntactic Analysis. Programming Languages – Principles and Paradigms by Allen Tucker, Robert Noonan. Syntactic Analysis:. Lexical Analysis  Token Stream Remove comments and white space Classify tokens by type Token Stream  Syntactic Analysis

trevet
Download Presentation

C H A P T E R T W O

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. C H A P T E R T W O Syntactic Analysis Programming Languages – Principles and Paradigms by Allen Tucker, Robert Noonan

  2. Syntactic Analysis: • Lexical Analysis  Token Stream • Remove comments and white space • Classify tokens by type • Token Stream  Syntactic Analysis • Define the structure of the elements of a program: • Expressions • Assignments • Loops, conditionals and other blocks. • The whole enchilada • Detect errors • Create a parse

  3. A Concrete Syntax for Assignments and Expressions Figure 2.7 Note that Expressions can be recursively defined Expression and Term establish precedence for operators

  4. Parse Tree for the Expression x+2*y Figure 2.8 Traversing the parse tree in an in-order sequence defines the order of evaluation. An Expression in parentheses becomes a Factor, changing the order of evaluation.

  5. Sketch of a Parse Tree for a Complete Program Figure 2.9

  6. Ambiguity: A grammar is ambiguous if it permits a string to be parsed into two or more different parse trees. AmbExp  Integer | AmbExp – AmbExp Consider the program fragment 2 – 3 – 4

  7. Two Different Parse Trees for the AmbExp 2 – 3 – 4 Figure 2.10 (2 – 3) - 4 2 – (3 – 4)

  8. An Ambiguous If Statement Figure 2.11 if (x<0) if (y<0) y = y – 1; else y = 0;

  9. The “Dangling Else” Grammatical Ambiguity Figure 2.12

  10. Extended BNF: EBNF simplifies the specification of recursion in grammar rules. Expression  Term | Expression + Term | Expression – Term Term  Factor | Term * Factor | Term / Factor Expression  Term { [ + | - ] Term } * Term  Factor { [‘*’ | / ] Factor } *

  11. EBNF-Based Parse Tree for the Expression x+2*y Figure 2.13

  12. Extended BNF: EBNF adds optional symbols on the right hand side of rules. Conditional  if ( Expression ) Statement { else Statement } opt

  13. Next time… Linking Syntax And Semantics

More Related