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

UP Lecture 09

UP Lecture 09

Compilers
Syntactical Analysis
(202502)

Javier Gonzalez-Sanchez

December 12, 2023
Tweet

More Decks by Javier Gonzalez-Sanchez

Other Decks in Programming

Transcript

  1. Dr. Javier Gonzalez-Sanchez | Compilers | 3 jgs Where are

    we now? After lexical analysis, we have a series of tokens. But we can not: I. define a DFA matching all expressions with properly balanced parentheses. II. i.e., define a DFA matching all functions with properly nested block structure. void a () { b (c); for (;;) {a=(-(1+2)+5); } }
  2. Dr. Javier Gonzalez-Sanchez | Compilers | 4 jgs Next Step

     Lexical Analysis ☐ Syntactic Analysis
  3. Dr. Javier Gonzalez-Sanchez | Compilers | 6 jgs High-Level Languages

    X,E,G,O,O #e1,I,I,0,7 @ OPR 19, AX STO x, AX LIT 5, AX OPR 21, AX LOD #e1,AX CAL 1, AX OPR 0, AX 5 Virtual Machine (interpreter) // sorce code int x; int foo () { read (x); print (5); } main () { foo (); } Lexer Parser Semantic Analyzer Code Generation 01001010101000010 01010100101010010 10100100000011011 11010010110101111 00010010101010010 10101001010101011 Assembler compilation execution
  4. Dr. Javier Gonzalez-Sanchez | Compilers | 7 jgs Where are

    we now? Now, we want to: ▪ Review the structure defined by the given series of tokens, i.e., whether the order of words is correct. ▪ Report errors if the tokens do not correctly represent a valid structure.
  5. Dr. Javier Gonzalez-Sanchez | Compilers | 8 jgs Outline Language

    Lexical Analysis (Lexer) Rules Symbols Token Tools Regular Expression DFA Syntactic Analysis (Parser) Grammar (Rules)
  6. Dr. Javier Gonzalez-Sanchez | Compilers | 10 jgs Grammar |

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

    Elementary School Parse Trees Grammar
  8. Dr. Javier Gonzalez-Sanchez | Compilers | 12 jgs Parse Tree

    ▪ A parse tree is a tree encoding the steps in a derivation. ▪ Internal nodes represent nonterminal symbols used in the production. ▪ Leaves represent terminals ▪ In-order Traversal describes a well-formed input ▪ Encodes what rules are used, not the order in which those rules are applied.
  9. Dr. Javier Gonzalez-Sanchez | Compilers | 13 jgs Goal ▪

    The goal of syntax analysis is to construct a parse tree for a given input (a sequence of tokens).
  10. Dr. Javier Gonzalez-Sanchez | Compilers | 14 jgs Grammar |

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

    Derivation 5 integer { delimiter 5 integer x ID = operator
  12. Dr. Javier Gonzalez-Sanchez | Compilers | 16 jgs Grammar |

    Derivation - operator 5 integer - operator x ID = operator - operator 20 integer
  13. Dr. Javier Gonzalez-Sanchez | Compilers | 17 jgs Grammar |

    Derivation - operator 5 integer - operator x ID = operator * operator 20 integer
  14. Dr. Javier Gonzalez-Sanchez | Compilers | 18 jgs Grammar |

    Derivation * operator 5 integer ( delimiter x ID = operator ) delimiter 20 integer + operator 20 integer
  15. Dr. Javier Gonzalez-Sanchez | Compilers | 19 jgs Grammar |

    Derivation * operator 5 integer ( delimiter x ID = operator ; delimiter 20 integer + operator 20 integer
  16. Dr. Javier Gonzalez-Sanchez | Compilers | 20 jgs Outline Language

    Lexical Analysis (Lexer) Rules Symbols Token Tools Regular Expression DFA Syntactic Analysis (Parser) Grammar (Rules) Terminal Non-terminal Tools BNF (Backus-Naur Form) Syntax Diagrams
  17. Dr. Javier Gonzalez-Sanchez | Compilers | 22 jgs A Simple

    Calculator | Grammar 5 integer / operator 5 integer Non-Terminals Terminals ::= 'integer' ( '+' | '-' | '*' | '/' ) 'integer' Expression Rules Start Symbol (root) Assignment ::= 'id' '=' Expression x ID = operator Assignment Expression Assignment
  18. Dr. Javier Gonzalez-Sanchez | Compilers | 23 jgs A Simple

    Calculator | Parse Tree Assignment ⇒ id = Expression ⇒ x = Expression ⇒ x = integer / integer ⇒ x = 5 / integer ⇒ x = 5 / integer ⇒ x = 5 / 5
  19. 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.