Professional Documents
Culture Documents
Lecture#7 - Chap#2 (Syntax Directed Translator (Part-III) )
Lecture#7 - Chap#2 (Syntax Directed Translator (Part-III) )
Compiler Construction
A Simple Syntax-Directed Translator,
(Part-III)
by Safdar Hussain
Topics
• Parsing Types (Top-down parsing, Bottom-up parsing)
• Recursive descent parsing, Predictive Parsing, Predictive Parser (Pseudo Code)
• Tracing the input string using Predictive Parser
Overview
• This chapter contains introductory material
• Building a simple compiler
– Syntax Definition
– Syntax-Directed Translation
– Parsing
– A Translator for Simple Expressions
– The Lexical Analyzer
Lookahead
symbol, at
initial/start
match(‘array’)
lookahead 15
Example Predictive Parser (Execution Step 2) Grammar
type simple
| ^ id
| array [ simple ] of type
simple integer
| char
| num dotdot num
type()
match(‘array’) match(‘[’)
lookahead 16
Example Predictive Parser (Execution Step 3) Grammar
type simple
| ^ id
| array [ simple ] of type
simple integer
| char
| num dotdot num
type()
match(‘num’)
lookahead 17
Example Predictive Parser (Execution Step 4) Grammar
type simple
| ^ id
| array [ simple ] of type
simple integer
| char
| num dotdot num
type()
match(‘num’) match(‘dotdot’)
lookahead 18
Example Predictive Parser (Execution Step 5) Grammar
type simple
| ^ id
| array [ simple ] of type
simple integer
| char
| num dotdot num
type()
lookahead 19
Example Predictive Parser (Execution Step 6) Grammar
type simple
| ^ id
| array [ simple ] of type
simple integer
| char
| num dotdot num
type()
lookahead 20
Example Predictive Parser (Execution Step 7) Grammar
type simple
| ^ id
| array [ simple ] of type
simple integer
| char
| num dotdot num
type()
lookahead 21
Example Predictive Parser (Execution Step 8) Grammar
type simple
| ^ id
| array [ simple ] of type
simple integer
| char
| num dotdot num
type()
match(‘integer’)
Input: array [ num dotdot num ] of integer
lookahead
22
Limitations
Q: Can we apply the previous technique to every grammar?
A: NO:
type simple
| array [ simple ] of type
simple integer
| array digit
digit 0|1|2|3|4|5|6|7|8|9
• S -> + S S | - S S | a
26