Download as pdf or txt
Download as pdf or txt
You are on page 1of 66

⇒ ⇒ω ω

Copyright 2013-2014
UCS702 Compiler Construction


*
rm
⇒ω
rm rm

Copyright 2013-2014
ω⇐ ⇐

UCS702 Compiler Construction


→ aaabb
→ aaAbb
→ aAbb ⇓
aABb
S
rm rm rm rm
⇒ ⇒ ⇒ ⇒

Copyright 2013-2014

UCS702 Compiler Construction

● γ ≡ αβω
→β γ
β
γ
*
⇒α ω ⇒
rmαβω rm

Copyright 2013-2014
● ω

UCS702 Compiler Construction


rm rm rm rm rm
γ ⇒γ ⇒γ ⇒ ⇒γ ⇒γ ω

● γ →β γ
β γ

Copyright 2013-2014
● →β γ
β γ

UCS702 Compiler Construction
→ id+id*id
→ ⇒ ⇒ ⇒ ⇒
→ ⇒ ⇒ ⇒ ⇒

id+id*id →
F+id*id →
T+id*id →
E+id*id →
E+F*id →
E+T*id →
E+T*F →

Copyright 2013-2014
E+T →
E
Handles

UCS702 Compiler Construction


Copyright 2013-2014


UCS702 Compiler Construction



Copyright 2013-2014


UCS702 Compiler Construction




Copyright 2013-2014

UCS702 Compiler Construction



Copyright 2013-2014

UCS702 Compiler Construction


Copyright 2013-2014
UCS702 Compiler Construction
input a1 ... ai ... an $
stack
Sm
Xm output
LR Parsing Algorithm
Sm-1
Xm-1
.
. Action Table Goto Table
terminals and $ non-terminal
S1 s s
t four different t each item is

Copyright 2013-2014
X1 a actions a a state number
t t
S0 e e
s s

UCS702 Compiler Construction


Copyright 2013-2014

UCS702 Compiler Construction


→β rn
● β

● →β

Copyright 2013-2014
UCS702 Compiler Construction
β
β

Copyright 2013-2014

UCS702 Compiler Construction
Action Table Goto Table
1) E → E+T state id + * ( ) $ E T F

2) E→T 0 s5 s4 1 2 3
1 s6 acc
3) T → T*F
2 r2 s7 r2 r2
4) T→F
3 r4 r4 r4 r4
5) F → (E)
4 s5 s4 8 2 3
6) F → id
5 r6 r6 r6 r6
•Si means shift
and stack state i 6 s5 s4 9 3
•rj means reduce 7 s5 s4 10
by production

Copyright 2013-2014
numbered j 8 s6 s11
•acc means accept state 9 r1 s7 r1 r1
•blank mean error
10 r3 r3 r3 r3

UCS702 Compiler Construction 11 r5 r5 r5 r5


→ →
→ →

→ →
→ →
→ →

Copyright 2013-2014
→ →
→ →
→ →

UCS702 Compiler Construction



→ →


Copyright 2013-2014

UCS702 Compiler Construction


→α β →γ
→γ

Copyright 2013-2014
UCS702 Compiler Construction
→ →
→ →
→ →
→ →
→ →
→ →
→ →

Copyright 2013-2014
UCS702 Compiler Construction
If I is a set of LR(0) items and X is a grammar symbol (terminal or non-terminal),
then goto(I,X) is defined as follows:
If A → α.Xβ in I then every item in closure({A → αX.β}) will be in goto(I,X).

Example:
I ={ E’ → .E, E → .E+T, E → .T,
T → .T*F, T → .F,
F → .(E), F → .id }
goto(I,E) = { E’ → E., E → E.+T }
goto(I,T) = { E → T., T → T.*F }
goto(I,F) = {T → F. }
goto(I,( ) = { F → (.E), E → .E+T, E → .T, T → .T*F, T → .F,
F → .(E), F → .id }

Copyright 2013-2014
goto(I,id) = { F → id. }

UCS702 Compiler Construction



Copyright 2013-2014
UCS702 Compiler Construction
Copyright 2013-2014
UCS702 Compiler Construction
E + T
I0 I1 I6 I9 * to I7
F
( to I3
T
id
to I4
I2 I7 to I5
F *
F
I3 I10
( to I4
I8 id
( I4 to I5
E to I2
) I11
id Iid
5 T to I3
F to I4 + to I6

Copyright 2013-2014
(

UCS702 Compiler Construction



→α β
→α →α

Copyright 2013-2014

UCS702 Compiler Construction


Action Table Goto Table
state id + * ( ) $ E T F
0 s5 s4 1 2 3
1 s6 acc
2 r2 s7 r2 r2
3 r4 r4 r4 r4
4 s5 s4 8 2 3
5 r6 r6 r6 r6
6 s5 s4 9 3
7 s5 s4 10

Copyright 2013-2014
8 s6 s11
9 r1 s7 r1 r1
10 r3 r3 r3 r3

UCS702 Compiler Construction 11 r5 r5 r5 r5


Copyright 2013-2014
UCS702 Compiler Construction
i j

Copyright 2013-2014
UCS702 Compiler Construction
→ → → → →
→ → →
→ → → →
→ → → →
→ →
→ →

→ →

→ →

Copyright 2013-2014

UCS702 Compiler Construction


→ →
→ →
→ε →
→ε →

→ε →ε

Copyright 2013-2014
→ε →ε

UCS702 Compiler Construction


→β1•β2 is valid for a viable prefix αβ1 if
there is derivation .
• The fact that →β1•β2 is valid for αβ1 tells us a lot
about whether to shift/reduce when we find αβ1 on
the parsing stack.
• if β2 is not an ε then, it suggests that we have not
shifted the handle onto stack, so shift is our move

Copyright 2013-2014
• if β2 is ε then it looks as if →β1 is the handle , and
we should reduce by this production.
UCS702 Compiler Construction
● →α

● →α

● β
βα

Copyright 2013-2014
→ ⇒ ⇒ ⇒ ⇒ ⇒ ⇒

→ε ⇒ε ⇒ε
→ε ⇒ ε ⇒ ε
UCS702 Compiler Construction
→α β

Copyright 2013-2014
UCS702 Compiler Construction
β →α β

β →α
→α

→α ⊆

Copyright 2013-2014
→α

UCS702 Compiler Construction


Copyright 2013-2014
→α β →γ
→γ
β
UCS702 Compiler Construction
→α β
→α β

Copyright 2013-2014
UCS702 Compiler Construction

Copyright 2013-2014
UCS702 Compiler Construction
→α β

→α β

Copyright 2013-2014
→α β

UCS702 Compiler Construction


S
→ → →
A
→ → a to I4
→ε → → B
→ε →
b to I5
→ →

A b
→ → →

→ B → →a

Copyright 2013-2014
UCS702 Compiler Construction
S’ → S I0:S’ → .S,$ I1:S’ → S.,$ I4:L → *.R,$/= R to I7
1) S → L=R S → .L=R,$ S * R → .L,$/= L
to I8
2) S → R S → .R,$ L I2:S → L.=R,$ to I6 L→ .*R,$/= *
R → L.,$ to I4
3) L→ *R L → .*R,$/= L → .id,$/= id
R to I5
4) L → id L → .id,$/= I3:S → R.,$ id
5) R → L R → .L,$ I5:L → id.,$/=

I9:S → L=R.,$
R I13:L → *R.,$
I6:S → L=.R,$ to I9
R → .L,$ L I10:R → L.,$
to I10
L → .*R,$ * I4 and I11
to I11 R
L → .id,$ I11:L → *.R,$ to I13
id R → .L,$ L I5 and I12
to I12 to I10

Copyright 2013-2014
L→ .*R,$ *
I7:L → *R.,$/= L → .id,$ to I11 I7 and I13
id
to I12
I8: R → L.,$/= I12:L → id.,$ I8 and I10
UCS702 Compiler Construction

→α β

→α →α ≠

Copyright 2013-2014
UCS702 Compiler Construction
id * = $ S L R
0 s5 s4 1 2 3
1 acc
2 s6 r5
3 r2
4 s5 s4 8 7 no shift/reduce or
5 r4 r4 no reduce/reduce conflict
6
7
s12 s11
r3 r3
10 9

so, it is a LR(1) grammar
8 r5 r5
9 r1

Copyright 2013-2014
10 r5
11 s12 s11 10 13
12 r4
13 Construction
UCS702 Compiler r3
Copyright 2013-2014
UCS702 Compiler Construction
S’ → S I0:S’ → .S,$ I1:S’ → S.,$ I4:L → *.R,$/= R to I7
1) S → L=R S → .L=R,$ S * R → .L,$/= L
to I8
2) S → R S → .R,$ L I2:S → L.=R,$ to I6 L→ .*R,$/= *
R → L.,$ to I4
3) L→ *R L → .*R,$/= L → .id,$/= id
R to I5
4) L → id L → .id,$/= I3:S → R.,$ id
5) R → L R → .L,$ I5:L → id.,$/=

I9:S → L=R.,$
R I13:L → *R.,$
I6:S → L=.R,$ to I9
R → .L,$ L I10:R → L.,$
to I10
L → .*R,$ * I4 and I11
to I11 R
L → .id,$ I11:L → *.R,$ to I13
id R → .L,$ L I5 and I12
to I12 to I10

Copyright 2013-2014
L→ .*R,$ *
I7:L → *R.,$/= L → .id,$ to I11 I7 and I13
id
to I12
I8: R → L.,$/= I12:L → id.,$ I8 and I10
UCS702 Compiler Construction

Copyright 2013-2014
UCS702 Compiler Construction

→ ➔ →

→ →

→ →

Copyright 2013-2014
➔ →


UCS702 Compiler Construction

➔ ≤

● ∪ ∪

Copyright 2013-2014


UCS702 Compiler Construction
R
S’ → S
1) S → L=R I0:S’ → S,$ . S .
I1:S’ → S *,$ .
I411:L → * R,$/= L
to I713

to I810
2) S → R
3) L→ *R
S → L=R,$. L
.
I2:S → L =R,$
to I6
.
R → L,$/=
*
id
to I411
4) L → id
S → R,$ . R
..
R → L ,$ id .
L→ *R,$/=
to I512
5) R → L
L → *R,$/= .
I3:S → R ,$
. .
I512:L → id ,$/=
L → id,$/=
R
.
I6:S → L= R,$ L → L
to I9
id,$/= .
to I810
I9:S → L=R ,$ . Same Cores
I4 and I11
.
R → L,$
*
R → L,$
id
.
to I411
I5 and I12
. to I512

Copyright 2013-2014
L → *R,$ I7 and I13
..
I713:L → *R ,$/=
L → id,$ I8 and I10
.
I810: R → L ,$/=
UCS702 Compiler Construction
id * = $ S L R
0 s5 s4 1 2 3
1 acc
2 s6 r5 no shift/reduce or
3 r2 no reduce/reduce conflict
4
5
s5 s4
r4 r4
8 7

so, it is a LALR(1) grammar
6 s12 s11 10 9
7 r3 r3

Copyright 2013-2014
8 r5 r5
9 r1

UCS702 Compiler Construction



→α →β γ

→α →β γ

Copyright 2013-2014
UCS702 Compiler Construction




→ →
→ →

Copyright 2013-2014
→ ➔

UCS702 Compiler Construction







Copyright 2013-2014


→ ➔ →

UCS702 Compiler Construction
E + E + I
.
I0: E’ → E I1: E’ → E. .
I4: E → E + E ( I7: E → E+E . *
I5
4

E → .E+E E → E .+E . .
I2
* E → E+E id E → E +E
I3
E → .E*E ( E → E .*E E → .E*E E → E.*E

E → .(E) E → .(E)
(
I : E → E *.E
E + I
( I : E → E*E. * 4
E → .id E → .id
5
.
I2: E → ( E)
E → .E+E
id
I3
I2 8

E → E.+E
I 5

E → .E+E
E → .E*E
E
E → E.*E
E → .E*Eid
id
E → .(E) )
I : E → (E.) I : E → (E).
E → .(E)
E → .id
6 + 9
* I4
E → E.+E

Copyright 2013-2014
I : E → id.
3 E → .id I5
E → E.*E

UCS702 Compiler Construction


FOLLOW(E) = { $,+,*,) }
State I7 has shift/reduce conflicts for symbols + and *.

E + E
I0 I1 I4 I7
when current token is +
shift ➔ + is right-associative
reduce ➔ + is left-associative

when current token is *


shift ➔ * has higher precedence than +

Copyright 2013-2014
reduce ➔ + has higher precedence than *

UCS702 Compiler Construction


FOLLOW(E) = { $,+,*,) }
State I8 has shift/reduce conflicts for symbols + and *.

E * E
I0 I1 I5 I7
when current token is *
shift ➔ * is right-associative
reduce ➔ * is left-associative

when current token is +


shift ➔ + has higher precedence than *

Copyright 2013-2014
reduce ➔ * has higher precedence than +

UCS702 Compiler Construction


Action Goto
id + * ( ) $ E
0 s3 s2 1
1 s4 s5 acc
2 s3 s2 6
3 r4 r4 r4 r4
4 s3 s2 7
5 s3 s2 8

Copyright 2013-2014
6 s4 s5 s9
7 r1 s5 r1 r1
8 r2 r2 r2 r2
UCS702 Compiler Construction


Copyright 2013-2014


UCS702 Compiler Construction
Copyright 2013-2014
UCS702 Compiler Construction
Copyright 2013-2014
UCS702 Compiler Construction
yacc
specification Yacc y.tab.c
yacc.y compiler

y.tab.c C a.out
compiler

Copyright 2013-2014
input output
stream a.out stream

UCS702 Compiler Construction


%{ %}
%%

%%

Copyright 2013-2014
UCS702 Compiler Construction
:
|

;

‘+’

%token

Copyright 2013-2014
UCS702 Compiler Construction

– $$
–$

factor : ‘(’ expr ‘)’ { $$=$2; }


factor.val=x
$$=$2

Copyright 2013-2014
( expr.val=x )

UCS702 Compiler Construction


%{ #include <ctype.h> %}
Also results in definition of
%token DIGIT #define DIGIT xxx
%%
line : expr ‘\n’ { printf(“%d\n”, $1); }
;
expr : expr ‘+’ term{ $$ = $1 + $3; }
| term { $$ = $1; }
;
term : term ‘*’ factor { $$ = $1 * $3; }
| factor { $$ = $1; }
;
factor : ‘(’ expr ‘)’ { $$ = $2; }
| DIGIT { $$ = $1; }
; Attribute of factor (child)
%% Attribute of
int yylex() term (parent) Attribute of token
{ int c = getchar(); (stored in yylval)
if (isdigit(c))

Copyright 2013-2014
{ yylval = c-’0’; Example of a very crude lexical
return DIGIT; analyzer invoked by the parser
}
return c;
UCS702
} Compiler Construction
yacc
specification Yacc or Bison y.tab.c
compiler y.tab.h
yacc.y

Lex specification
lex.l Lex or Flex
lex.yy.c
and token definitions compiler
y.tab.h

lex.yy.c C a.out
y.tab.c compiler

Copyright 2013-2014
input output
stream a.out stream
%{

%}

%%
lines : lines expr ‘\n’ { printf(“%g\n”, $2; }
| lines ‘\n’
| /* empty */
| error ‘\n’ { yyerror(“reenter last line: ”);
yyerrok;
}
;

Copyright 2013-2014
Error production:
Reset parser to normal mode
set error mode and
skip input until newline

You might also like