Upgrade to Pro — share decks privately, control downloads, hide ads and more …

UP Lecture 08

UP Lecture 08

Compilers
Grammar II
(202402)

Javier Gonzalez-Sanchez

December 11, 2023
Tweet

More Decks by Javier Gonzalez-Sanchez

Other Decks in Programming

Transcript

  1. jgs Dr. Javier Gonzalez-Sanchez | Compilers | 3 5 /

    20 integer operator integer E ⇒ E OP E ⇒ integer OP E ⇒ integer / E ⇒ integer / integer Grammar | Derivation Derivation Tree Grammar 01
  2. jgs Dr. Javier Gonzalez-Sanchez | Compilers | 5 Grammar |

    Derivation Derivation Tree Grammar 01 5 * ( 7 + 20 ) Integer operator delimiter integer operator integer delimiter E ⇒ E OP E ⇒ integer OP E ⇒ integer * E ⇒ integer * (E) ⇒ integer * (E OP E) ⇒ integer * (integer OP E) ⇒ integer * (integer + E) ⇒ integer * (integer + integer)
  3. Dr. Javier Gonzalez-Sanchez | Compilers | 8 jgs Grammar |

    Definition A Grammar is a collection of four elements: 1. Set of nonterminal symbols (uppercase) 2. Set of terminal symbols (lowercase). Terminals can be tokens or specific words. 1. Set of production rules saying how each nonterminal can be converted by a string of terminals and nonterminals, 2. A start symbol
  4. jgs Dr. Javier Gonzalez-Sanchez | Compilers | 9 E à

    ( E ) E à E OP E E à integer OP à + | - | * | / Grammar | Definition
  5. Dr. Javier Gonzalez-Sanchez | Compilers | 10 jgs Parse Tree

    § A parse tree is a tree encoding the steps in a derivation. § Internal nodes represent nonterminal symbols used in the production. § Inorder walk of the leaves contains the generated string. § Encodes what productions are used, not the order in which those productions are applied.
  6. Dr. Javier Gonzalez-Sanchez | Compilers | 11 jgs Goal The

    g oal of syntax analysis: § Recover a parse tree for the given input (a series of tokens).
  7. Dr. Javier Gonzalez-Sanchez | Compilers | 13 jgs The problem

    5 * ( 7 + 20 ) Integer operator delimiter integer operator integer delimiter
  8. Dr. Javier Gonzalez-Sanchez | Compilers | 14 jgs The problem

    5 * 7 + 20 Integer operator integer operator integer
  9. Dr. Javier Gonzalez-Sanchez | Compilers | 16 jgs Ambiguity §

    A grammar is said to be ambiguous if there is at least one string with two or more parse trees. § Note that ambiguity is a property of grammars, not languages. We will review this topic in the next lecture
  10. Dr. Javier Gonzalez-Sanchez | Compilers | 17 jgs Solution §

    If a grammar can be made unambiguous at all, it is usually made unambiguous through layering. § Have exactly one way to build each piece of the string. § Have exactly one way of combining pieces back together. § Recursive constructions
  11. Dr. Javier Gonzalez-Sanchez | Compilers | 18 jgs Layering Rule

    1 Rule 2 Rule 3 Rule 4 Rule 5 . . . . . Leaf (Terminals, i.e., Tokens) Root Start symbol Layers
  12. Dr. Javier Gonzalez-Sanchez | Compilers | 19 jgs Layering 1

    2 4 5 3 inputs: § 1 + 2 + 3 < 4 * 5 § 1 * 2 + 3 + 4 < 5 § 1 < 2 + 3 + 4 * 5 § 1 + 2 < 3 * 4 + 5 § 1 + 2 * 3 < 4 + 5
  13. Dr. Javier Gonzalez-Sanchez | Compilers | 20 jgs Exercise Provide

    a Grammar that is Not ambiguous for arithmetic expressions 10 * 20 + 15 Precedence of operators and Associativity þ (10 * 20) + 15 ý 10 * (20 + 15)
  14. Dr. Javier Gonzalez-Sanchez | Compilers | 22 jgs Exercise 1

    Include rules for handling parenthesis into the previous grammar. The grammar should accept as correct the following expressions: 10 * 20 + 15 (10 * 20) + 15 10 * (20 + 15) (10) * (20) + (15) (10 * 20 + 15) 10 * (20) + 15
  15. Dr. Javier Gonzalez-Sanchez | Compilers | 24 jgs Exercise 2

    Include rules to accept variables names (identifiers) in expressions. The grammar should accept as correct the following expressions: A * 20 + time (x * y) + 15 10 * (ASU + cse340) (10) * (20) + (15) (hello * world + Arizona) 10 * (counter) + 15
  16. Dr. Javier Gonzalez-Sanchez | Compilers | 26 jgs Exercise 3

    Provide a Grammar that is Not ambiguous with Precedence of operators and Associativity for this: 10 + 20 > 15 & -10 != 1 | 20 / ( 10 + 1) < 5
  17. Dr. Javier Gonzalez-Sanchez | Compilers | 27 jgs Exercise 3

    | Note Precedence of operators | & ! < > == != <= >= + - * / - ( )
  18. jgs Compilers Javier Gonzalez-Sanchez, Ph.D. [email protected] Spring 2024 Copyright. These

    slides can only be used as study material for the Compilers course at Universidad Panamericana. They cannot be distributed or used for another purpose.