10 3.1IntermediateCode11
10 3.1IntermediateCode11
Intermediate-Code Generation
Directed
(
using Syntax Translation)
Back-end and Front-end of A Compiler
Back-end and Front-end of A Compiler
m x n compilers can be built by writing just m front ends and nback ends
Back-end and Front-end of A Compiler
Includes:
• Type checking
•Any syntactic checks that remain after
parsing (e.g. ensure break statement is
enclosed within while-, for-, or switch
statements).
Intermediate Code
Data structure
data structure
£
data structure
Directed Acyclic
Graph
Postfix Code
②
②
⑧
Parse Tree too id , * id e tidy
Syntax Tree
• Similar to parse tree but
• Internal node represents operations
• Leaf node represents operands
• Semantic rules are only applied on reduction
Syntax Tree
Syntax Tree
+ *
a * - d
a - b c
b c
Syntax Tree
+ *
a * - d
a - b c
b c
Syntax Tree
+ *
a * - d
a - b c
b c
Syntax Tree
+
L 7
+ * - -
a * - d ✓
L ✓ L
a - b c L ✓
b c
=L --_
-
# → *
← → ← →
Is it
^
,
Etty n
✓ →
5÷→
E'
nd Jg
-
y
How to Generate DAG from
Syntax-Directed Definition?
All what is needed is that functions such as Node and Leaf above
check whether a node already exists. If such a node exists, a pointer
is returned to that node.
How to Generate DAG from
Syntax-Directed Definition?
Data Structure for Syntax
i. it
Tree/DAG: Array
10
( Fn
symbol
table)
Data Structure for Syntax Tree/DAG:
Array
Leaves
quite10
or
b t 10
Hash function = h(op, L, R)
Three-Address Code
• Another option for intermediate presentation
• Built from two concepts:
– addresses and instructions
• At most one operator apart from ’=’
• At most three operands (addresses)
at a * (b -
c ) t Cb -
c) Ad
Address
Can be one of the following:
• A name: source program name
• A constant
• Compiler-generated temporary
Instructions
OR
Choice of Operator Set
• Rich enough to implement the
operations of the source language
• Close enough to machine instructions to
simplify code generation
Data Structure
( three address code)
for
How to present these instructions in a
data structure?
– Quadruples
– Triples
– Indirect triples
Data Structure: Quadruples
If we use different names forX in true part and false part, then which name
shall we use in the assignment of y = x * a ?