02 Simple Sysntax Directed Translation
02 Simple Sysntax Directed Translation
Syntax Directed
Translation
Chapter 2
Faryal Shamsi
Lecturer Computer Science
Structure of
Compiler
Symbol Table
Analysis
• The analysis phase of a compiler breaks up a source program into
constituent pieces and produces an internal representation for it,
called intermediate code.
• Lexical Analysis,
• Syntax and Semantics Analysis,
• Intermediate Code Generation.
A Syntax Directed Translation
Tools and Techniques
Context Free Grammar
• A set of terminal symbols, may be referred to as “tokens”.
• A set of non-terminals, may be called “syntactic variables”.
• A designation of one of the non-terminals as the start symbol.
• A set of productions, where each production consists of a
1. Non-terminal, called the head or left-side of the production,
2. an arrow, and
3. a sequence of terminals and/or non-terminals , called the body or right
side of the production.
CFG Example
• lists of digits separated by plus or minus signs .
• OR
• And if it cannot be derived from the start symbol of the grammar , then
reporting syntax errors within the string.
Parse Trees
• A parse tree pictorially shows how the start symbol of a grammar derives a
string in the language.
• If non-terminal A has a production A XYZ, then a parse tree may have an
interior node labeled A with three children labeled X, Y, and Z, from left to
right:
Parse Trees
Parse Trees
• The process of finding a parse tree for a given string of terminals using a
predefined CFG, is called parsing.
• Such as 9+5-2
• Since a string with more than one parse trees usually has more than one
meaning,
• we need to design unambiguous grammars for compiling applications, or to use
ambiguous grammars with additional rules to resolve the ambiguities .
Consider a CFG -
E E+E
E E*E
E id
E E E E
+ *
E E E E id
id * +
id id id id
Precedence of Operators
• Precedence? 9+5*2
EE+T
TT*F
F id
Resolving Precedence Problem
Can you identify some problem here?
EE+T
TT*F
F id
Resolving Precedence Problem
EE+T
TT*F
F id
Try parsing:
• id+id*id*id
• id+id*id+id
Resolving Precedence Problem
EE+T
TT*F
F id
• Convert into
EE+T|T
TT*F|F
F id
Resolving Precedence Problem
E E+T | E-T | T
T T*F | T/F | F
F id | E
Associativity of Operators
• Associativity of 5? 9-5+2
• E E+E | id
• E E+id | id
• Also called left associative or left recursive
• E id+E | id
• Also called right associative or right recursive
Associativity of Operators
• Left Associativity
• Right Associativity
Associativity of Operators
4.3.4 – Left Factoring
AAα A β A’
Aβ A’ α A’ | ε
4.3.4 – Left Factoring
• A left recursive grammar –
• A left factored grammar –
AAα
Aβ A β A’
A’ α A’ | ε
EE+T
ET
4.3.4 – Left Factoring
• A left recursive grammar –
• A left factored grammar –
AAα
Aβ A β A’
A’ α A’ | ε
EE+T
ET E T E’
E’ +T E’ | ε
Solve
• S (L) | i • S Sa | Sb | c
L L,S | S
• S Aa | Bb
•SS0S1S A Ac | Ad | B
S 01 B Ba | Bc
4.3.4 – Left Factoring
• Left factoring is a grammar transformation that is useful for producing
a grammar suitable for predictive, or top-down, parsing.
•A*B+C/D
Convert into Postfix
• A*B+C/D
• A * (B + C) / D
Convert into Postfix
• A*B+C/D
• A * (B + C) / D
• A * (B + C / D)
Convert into Postfix
•A*B+C/D AB*CD/+
• A * (B + C) / D ABC+*D/
• A * (B + C / D) ABCD/+*
Intermediate Code Generation