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

UP Lecture 12

UP Lecture 12

Compilers
Precedence of Operators
(202503)

Javier Gonzalez-Sanchez

December 15, 2023
Tweet

More Decks by Javier Gonzalez-Sanchez

Other Decks in Programming

Transcript

  1. Dr. Javier Gonzalez-Sanchez | Compilers | 3 jgs Do not

    Forget ▪ Terminals: Represented in lowercase, enclosed in single or double quotes, or depicted as ovals in diagrams. ▪ Non-Terminals: Represented with an uppercase initial letter, enclosed in angle brackets (< >), or depicted as rectangles in diagrams.
  2. Dr. Javier Gonzalez-Sanchez | Compilers | 4 jgs Homework Syntax

    Diagrams for Java: - Class - Attributes - Methods - Parameters - Instruction (stop here) Do not forget static, final, public, private, data-types, abstract, extends, implements …
  3. Dr. Javier Gonzalez-Sanchez | Compilers | 6 jgs Grammar |

    Derivation 5 integer / operator 5 integer x ID = operator
  4. Dr. Javier Gonzalez-Sanchez | Compilers | 7 jgs Grammar |

    Derivation 5 integer / operator 5 integer x ID = operator Expression: Operator: Assignment:
  5. Dr. Javier Gonzalez-Sanchez | Compilers | 8 jgs Grammar |

    Derivation 5 integer / operator 20 integer x ID = operator Expression: Operator: Assignment:
  6. Dr. Javier Gonzalez-Sanchez | Compilers | 9 jgs Grammar |

    Derivation 1 integer + operator 2 integer x ID = operator / operator 3 integer 1 integer / operator 2 integer x ID = operator + operator 3 integer
  7. Dr. Javier Gonzalez-Sanchez | Compilers | 11 jgs Layering Rule

    1 Rule 2 Rule 3 Rule 4 Rule 5 . . . . . Leaf (Terminals, i.e., Tokens) Root Start symbol Layers
  8. Dr. Javier Gonzalez-Sanchez | Compilers | 12 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
  9. Dr. Javier Gonzalez-Sanchez | Compilers | 13 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)
  10. Dr. Javier Gonzalez-Sanchez | Compilers | 16 jgs Exercise 1

    | Precedence New Grammar: <E> → <__> + <__> | <__> < > → <__> - <__> | <__> < > → - <__> | <__> Original Grammar: E → E OP E | E E → integer OP→ + | - | * | /
  11. Dr. Javier Gonzalez-Sanchez | Compilers | 18 jgs Exercise |

    Precedence <E> → <A> | <A> {'+' <A>} | <A> {'-' <A>} <A> → <B> | <B> {'*' <B>} | <B> {'/' <B>} <B> → '-'<C> | <C> <C> → integer
  12. Dr. Javier Gonzalez-Sanchez | Compilers | 19 jgs Exercise 1

    | Precedence <E> → <A> {(’+'|'-’) <A>} <A> → <B> {('*'|'/') <B>} <B> → ['-'] <C> <C> → integer
  13. Dr. Javier Gonzalez-Sanchez | Compilers | 23 jgs Exercise 2

    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
  14. Dr. Javier Gonzalez-Sanchez | Compilers | 29 jgs Exercise 3

    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
  15. Dr. Javier Gonzalez-Sanchez | Compilers | 32 jgs Exercise 4

    Provide a Grammar with Precedence of operators and Associativity for this: 10 + 20 > 15 & -10 != 1 | 20 / ( 10 + 1) < 5
  16. Dr. Javier Gonzalez-Sanchez | Compilers | 33 jgs Precedence Precedence

    of operators | & ! < > == != <= >= + - * / - ( )
  17. Dr. Javier Gonzalez-Sanchez | Compilers | 37 jgs Homework |

    Derivation Tree x = ((3 + 5) * 2 > 10) && (8 / (4 + 1) == 1 || 7 - 3 < 2)
  18. jgs Compilers Javier Gonzalez-Sanchez, Ph.D. jgonzalezs@up.edu.mx Spring 2025 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.