Slide 1

Slide 1 text

jgs Compilers Lecture 09: Fundamentals of Syntactic Analysis Dr. Javier Gonzalez-Sanchez [email protected]

Slide 2

Slide 2 text

jgs Previously…

Slide 3

Slide 3 text

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); } }

Slide 4

Slide 4 text

Dr. Javier Gonzalez-Sanchez | Compilers | 4 jgs Next Step  Lexical Analysis ☐ Syntactic Analysis

Slide 5

Slide 5 text

jgs Syntactic Analysis

Slide 6

Slide 6 text

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

Slide 7

Slide 7 text

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.

Slide 8

Slide 8 text

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

Slide 9

Slide 9 text

jgs Definitions

Slide 10

Slide 10 text

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

Slide 11

Slide 11 text

Dr. Javier Gonzalez-Sanchez | Compilers | 11 jgs Back to Elementary School Parse Trees Grammar

Slide 12

Slide 12 text

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.

Slide 13

Slide 13 text

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).

Slide 14

Slide 14 text

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

Slide 15

Slide 15 text

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

Slide 16

Slide 16 text

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

Slide 17

Slide 17 text

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

Slide 18

Slide 18 text

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

Slide 19

Slide 19 text

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

Slide 20

Slide 20 text

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

Slide 21

Slide 21 text

jgs What is Next

Slide 22

Slide 22 text

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

Slide 23

Slide 23 text

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

Slide 24

Slide 24 text

Dr. Javier Gonzalez-Sanchez | Compilers | 24 jgs Homework Review Recursion

Slide 25

Slide 25 text

Dr. Javier Gonzalez-Sanchez | Compilers | 25 jgs Homework Finish Your Lexer (Try the new test cases)

Slide 26

Slide 26 text

Dr. Javier Gonzalez-Sanchez | Compilers | 26 jgs Questions

Slide 27

Slide 27 text

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.