Share
Embed
UP Lecture 07
Compilers
Fundamentals of Syntactic Analysis
(202602)
Javier Gonzalez-Sanchez PRO
December 10, 2023
Transcript
26 slides
-
Dr. Javier Gonzalez-Sanchez | Compilers | 2 jgs Next Step
Lexical Analysis ☐ Syntactic Analysis -
Dr. Javier Gonzalez-Sanchez | Compilers | 3 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 -
Dr. Javier Gonzalez-Sanchez | Compilers | 4 jgs Big Picture
5 integer / operator 5 integer x ID = operator -
Dr. Javier Gonzalez-Sanchez | Compilers | 5 jgs Why we
need trees? 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); } } -
Dr. Javier Gonzalez-Sanchez | Compilers | 6 jgs Goal Now,
we want to: ▪ Review the structure defined by the given series of words (tokens), i.e., whether the order of words is correct. ▪ Report errors if the tokens do not correctly represent a valid structure. -
Dr. Javier Gonzalez-Sanchez | Compilers | 7 jgs Goal ▪
The goal of syntax analysis is to construct a parse tree for a given input (a sequence of tokens). -
Dr. Javier Gonzalez-Sanchez | Compilers | 8 jgs Outline Language
Lexical Analysis (Lexer) Rules Symbols Token Tools Regular Expression DFA Syntactic Analysis (Parser) Grammar (Rules) Tree -
Dr. Javier Gonzalez-Sanchez | Compilers | 10 jgs Back to
Elementary School Parse Trees Grammar -
Dr. Javier Gonzalez-Sanchez | Compilers | 11 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 Terminal (leaf) Non-Terminal (node) Start (root) -
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. Terminal (leaf) Non-Terminal (node) Start (root) -
Dr. Javier Gonzalez-Sanchez | Compilers | 14 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 -
Dr. Javier Gonzalez-Sanchez | Compilers | 15 jgs A Simple
Calculator | Parse Tree Assignment ⇒ id = Expression ⇒ x = Expression ⇒ x = integer / integer ⇒ x = 5 / integer ⇒ x = 5 / integer ⇒ x = 5 / 5 5 integer / operator 5 integer x ID = operator -
Dr. Javier Gonzalez-Sanchez | Compilers | 16 jgs Grammar |
Derivation 5 integer / operator 5 integer x ID = operator -
Dr. Javier Gonzalez-Sanchez | Compilers | 17 jgs Grammar |
Derivation 5 integer { delimiter 5 integer x ID = operator -
Dr. Javier Gonzalez-Sanchez | Compilers | 18 jgs Grammar |
Derivation - operator 5 integer - operator x ID = operator - operator 20 integer -
Dr. Javier Gonzalez-Sanchez | Compilers | 19 jgs Grammar |
Derivation - operator 5 integer - operator x ID = operator * operator 20 integer -
Dr. Javier Gonzalez-Sanchez | Compilers | 20 jgs Grammar |
Derivation * operator 5 integer ( delimiter x ID = operator ) delimiter 20 integer + operator 20 integer -
Dr. Javier Gonzalez-Sanchez | Compilers | 21 jgs Grammar |
Derivation * operator 5 integer ( delimiter x ID = operator ; delimiter 20 integer + operator 20 integer -
Dr. Javier Gonzalez-Sanchez | Compilers | 22 jgs 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 Outline -
jgs Compilers Javier Gonzalez-Sanchez, Ph.D. [email protected] 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.