Professional Documents
Culture Documents
P K Singh
MMMUT, Gorakhpur
Bootstrapping and
Porting of Compiler
P K Singh
MMMUT, Gorakhpur
T
H
BB
H
C
H
C
H
B
H
B
K
H
B
H
H
H
B
H
K
Process of Bootstrapping
Write a compiler in the same language
S
T
S
H
A
H
H
M
B
M
H
K
A
K
H
Original compiler
Compiler source code retargeted to K
Result in Cross Compiler
K
A
K
K
Cross compiler
Compiler source code retargeted to K
Result in Retargeted Compiler
Converting a RE Directly
to a DFA
P K Singh
MMMUT, Gorakhpur
14
P K Singh
MMMUT, Gorakhpur
15
firstpos(n)
The set of positions that can match the first symbol of a string
generated by the subtree at node n
lastpos(n)
The set of positions that can match the last symbol of a string
generated be the subtree at node n
followpos(i)
The set of positions that can follow position i in the tree
P K Singh
MMMUT, Gorakhpur
16
nullable(n)
firstpos(n)
lastpos(n)
A leaf labeled
by
true
A leaf with
position i
false
{i}
{i}
n = c 1 | c2
nullable(c1)
or
nullable(c2)
firstpos(c1) firstpos(c2)
lastpos(c1) lastpos(c2)
if ( nullable(c1) )
n = c 1 c2
nullable(c1)
and
nullable(c2)
firstpos(c1) firstpos(c2)
else firstpos(c1)
if ( nullable(c2) )
lastpos(c1) lastpos(c2)
else lastpos(c2)
n = c1*
true
firstpos(c1)
lastpos(c1)
P K Singh
MMMUT, Gorakhpur
17
Computing followpos
for (each node n in the tree)
{
//n is a cat-node with left child c1 and right child c2
if ( n == c1 c2)
for (each i in lastpos(c1) )
followpos(i) = followpos(i) firstpos(c2);
else if (n is a star-node)
for ( each i in lastpos(n) )
followpos(i) = followpos(i) firstpos(n);
}
P K Singh
MMMUT, Gorakhpur
18
MMMUT, Gorakhpur
19
Example
( a | b )* a b b #
a
3
*
|
a
1
P K Singh
b
4
b
2
MMMUT, Gorakhpur
b
5
n = ( a | b )* a
nullable(n) = false
firstpos(n) = { 1, 2, 3 }
lastpos(n) = { 3 }
followpos(1) = {1, 2, 3 }
20
Example
{1,2,3}
( a | b )* a b b #
{1,2,3}
{1,2,3}
{4}
{1,2}
{1,2}
{1}
P K Singh
a1 {1}
*
|
{3}
{5}
{4}
{3}
{1,2}
{6}
{5}
nullable
{1,2,3}
{6}
a3 {3}
#6 {6}
b5 {5}
b4 {4}
firstpos
lastpos
{1,2}
{2}
b2 {2}
MMMUT, Gorakhpur
21
Example
Node
followpos
{1, 2, 3}
{1, 2, 3}
{4}
{5}
{6}
( a | b )* a b b #
3
b
a
1,2,3
1,2,
3,4
1,2,
3,5
1,2,3,6
a
a
P K Singh
MMMUT, Gorakhpur
22
Where no
begins with an A
Example
Left-recursive grammar
AA
|
|
| A
AR
AR
AR
AR
Non-Immediate Left-Recursion
The Grammar
S Aa | b
A Ac | Sd |
Example
ABC|a
BCA|Ab
CAB|CC|a
i=1
nothing to do
i = 2, j = B C A | A b
1
BCA|BCb|ab
(imm) B C A BR | a b BR
BR C b BR |
i = 3, j = C A B | C C | a
1
CBCB|aB|CC|a
i = 3, j = C B C B | a B | C C | a
2
C C A BR C B | a b BR C B | a B | C C |
a
(imm)C a b BR C B CR | a B CR | a CR
CR A BR C B C R | C C R |
Exercise
The grammar
S Aa | b
A Ac | Sd |
Answer
AAc|Aad|bd|
Left Factoring
Left Factoring is a grammar transformation.
Predictive Parsing
Top-down Parsing
Replace productions
A 1 | 2 | | n |
with
A AR |
AR 1 | 2 | | n
Example
The Grammar
stmt if expr then stmt
| if expr then stmt else stmt
Replace with
stmt
stmts
Exercise
The following grammar
S iEtS | iEtSeS|a
E b
Answer
S i E t S S | a
S e S |
E b
Computing First
To compute First(X) for all grammar symbols X,
apply following rules until no more terminals or
can be added to any First set:
*
Computing follow
To compute First(A) for all nonterminals A, apply
following rules until nothing can be added to any
follow set:
1. Place $ in Follow(S) where S is the start
symbol
2. If there is a production A-> B then
everything in First() except is in Follow(B).
3. If there is a production A->B or a production
A->B where First() contains , then
everything in Follow(A) is in Follow(B)
Top-down Parsing
Give a Grammar G
EE+T|T
TT*F|F
F(E)|id
E T E
E + T E |
T F T
T * F T |
F ( E ) | id
Example
Give a Grammar G
E T E
E + T E |
T F T
T * F T |
F ( E ) | id
FIRST
E (
id
E +
T (
id
T *
F (
id
FOLLOW
E
E
T
T
F
)
+
*
$
$
+
+
)
)
$
$
)
)
input
stack
X
Y
Z
$
Predictiveparsing
program(driver)
Parsingtable
M
output
LL(1)
A grammar G is LL(1) if it is not left recursive and
for each collection of productions
A 1 | 2 | | n
for nonterminal A the following holds:
FIRST(i) FIRST(j) = for all i j
if i * then
j * for all i j
FIRST(j) FOLLOW(A) = for all i j
Example
Grammar
SSa|a
Left recursive
SaS|a
SaR|
RS|
For R: S * and *
SaRa
RS|
For R:
FIRST(S) FOLLOW(R)
Production
E->E1+T
Semantic Rule
E.code=E1.code||T.code||+
Inherited attributes
An inherited attribute at node N is defined only in terms of
attribute values at Ns parent, N itself and Ns siblings