Compiler Construction Week 04 Syntax Analysis I)
Compiler Construction Week 04 Syntax Analysis I)
Compiler Construction Week 04 Syntax Analysis I)
Week 04
Syntax Directed Translator
2
Syntax
Directed
Translato
r
3
Syntax Directed Translator
This section illustrates the compiling techniques
by developing a program that translates
representative programming language
statements into three-address code, an
intermediate representation.
We will focus on
Front end of a compiler
Lexical analysis
Parsing
Intermediate code generation.
4
Syntax Directed Translator
5
Three address code
6
Introduction
7
Introduction
8
Syntax Definition
9
Syntax Definition
10
What is
Context Free
Grammar?
11
Grammar
12
Expressions... Grammar
9-5+2, 5-4, 8...
Since a plus or minus sign must appear between two
digits, we refer to such expressions as lists of digits
separated by plus or minus signs.
The productions are
List -> list + digit P-
1
List -> list – digit P-
2
List -> digit P-
3
Digit->0 1 2 3 4 5 6 7 8 9 P-
4
13
Context Free Grammar
• Why we need CFGs?
14
Context
• Context-free grammar is used to specifyFree Grammar
the syntax of a
language
• A grammar naturally describes the hierarchical structure
for e.g.
16
A context-free grammar has four components:
Basic Terminologies of CFG
• Terminals: Sometimes referred to as "tokens." The
terminals are the elementary symbols of the language
defined by the grammar.
CFG (V, T, P, S)
• Terminals (T): Finite sets of end-nodes values
0, 1
• Non-terminals (V): S P
Finite sets of Variables
S, P
• Production (P): Substitution Rules
P 0P0
P 1P1
• Start symbol (S): Initiate Point P0
S
P1
P
18
Basic Terminologies of Lexical
Example (9-5+2, 3-1, or 7) Analysis
• Terminals:
• Non-terminals: list and digit
• Empty list: The string of zero terminals, written as
∊, is called the empty string
19
Derivation &
Parse Tree
20
Derivation & Parse Tree
Derivation:
21
Derivation & Parse Tree
Derivation:
22
Parse Tree:
Derivation & Parse Tree
• Parsing is the problem of taking a string of terminals
and figuring out how to derive it from the start
symbol of the grammar.
• If it cannot be derived from the start symbol of the
grammar, then reporting syntax errors within the
string.
• Given a context-free grammar, a parse tree according
to the grammar is a tree with the following
properties:
• The root is labeled by the start symbol.
• Each leaf is labeled by a terminal or by ε.
• Each interior node is labeled by a nonterminal
23
Derivation & Parse Tree
Parse Tree:
If A→ X1 X2 ... Xn is a production, then node A has immediate children X1, X2, ..., Xn
where Xi is a (non)terminal or 8.
24
Derivation & Parse Tree
Parse Tree:
25
Derivation & Parse Tree
Parse Tree:
26
Parse Tree:
Derivation & Parse Tree
27
Ambiguity
28
Ambiguity
• Ambiguity is problematic because meaning of the
programs can be incorrect
29
Ambiguity
Consider grammar
string string +
string
| string –
string
|0|1|…
|9
30
Tree
Terminology
31
Tree Terminology
32
Associativity
of Operators
33
Associativity of Operators
34
Operator
Precedence
35
Associativity of Operators
36
Associativity and Precedence Table
37
Operator Precedence
• Let's see an example of four common arithmetic
operators and a precedence table, showing the operators
in order of increasing precedence.
• left-associative: +
• left-associative: */
• Now we create two nonterminal expr and term for the
two levels of precedence, and an extra nonterminal factor
for generating basic units in expressions.
• The basic units in expressions are presently digits and
parenthesized expressions.
• factor->digit I (expr)
38
Operator Precedence
• Now consider the binary operators, and /, that have the
highest precedence and left associativity.
• term->term * factor | term / factor | factor
• Similarly, expr generates lists of terms separated by the
additive operators.
• expr -> expr+ term I expr-term I term
• Final grammar is
• expr -> expr+ term I expr-term I term
• term->term * factor | term / factor | factor factor> digit I
(expr)
39
Operator Precedence
40
Solve It
What am I?”
41