Sunteți pe pagina 1din 29

Curs 10 - plan!

! Parsare predictiv"!
! FIRST, FOLLOW! ! Gramatici LL(1)! ! Parsare recursiv descendent"! ! Tabela de parsare LL(1)! ! Parsare predictiv" ne-recursiv"!

! Parsare Bottom-Up

!!

! Parsare general" shift - reduce! ! Conicte!

2 !

Parsare predictiv"!
! Se elimin" recursia stng" (dac" este cazul)! ! Se factorizeaz" stnga (dac" este cazul)! ! Se determin" FIRST #i FOLLOW! ! Se alege una din variante:!
! Recursiv" (recursive calls)! ! Non-recursiv" (table - driven)!

3 !

FIRST!
! FIRST(!) = { mul!imea terminalilor ce ncep cuvintele derivate din ! (incluznd ")}$ FIRST(a) = {a} ! ! ! !if a # T" FIRST(") = {"}$ FIRST(A) = $A%! FIRST(!) ! !for A%! # P$ FIRST(X1X2Xk) =$ !if for all j = 1, , i-1 : " # FIRST(Xj) then$ ! !add non-" in FIRST(Xi) to FIRST(X1X2Xk)$ !if for all j = 1, , k : " # FIRST(Xj) then$ ! !add " to FIRST(X1X2Xk)!

4 !

FOLLOW!
! FOLLOW(A) = { mul!imea terminalilor ce pot urma imediat dup# neterminalul A n formele propozi!ionale }" FOLLOW(A) =$ !for all (B % ! A &) # P do" $ $add FIRST(&)\{"} to FOLLOW(A)! ! ! if A is the start symbol S then$ ! !add $ to FOLLOW(A) $ !for all (B % ! A &) # P and " # FIRST(&) do" $ $add FOLLOW(B) to FOLLOW(A)$ !for all (B % ! A) # P do" $ $add FOLLOW(B) to FOLLOW(A)$

5 !

Gramatici LL(1)!
! O gramatic" G este LL(1) dac" nu este stng recursiv" #i pentru orice colec%ie de produc%ii:$ !A % !1 | !2 | | !n$ pentru neterminalul A au loc:$ 1. !FIRST(!i) ' FIRST(!j) = ( for all i ) j$ 2. !if !i ** " then$ !2.a. !!j ** " for all i ) j" $2.b. !FIRST(!j) ' FOLLOW(A) = ($ ! ! ! for all i ) j$ echivalent cu: !
FIRST(!i FOLLOW(A)) ' FIRST(!j FOLLOW(A)) = ( for all i ) j!

6 !

Exemple!
S % aSa | bSb | c ! S%E|B! ! ! B % a | begin SC end ! S % ABC !A % aA |"! ! ! !B % bB |" ! ! ! !C % cC |"! !E % "! !C % " | ; SC ! !!

7 !

Exemple Non-LL(1)!
Gramatica! S % S a | a! S % a S | a! S % a R | "$ R % S | "! S % a R a$ R % S | "! Nu este LL(1) deoarece:! Este stng recursiv"! FIRST(a S) ' FIRST(a) ) (! R: S ** " and " ** "!

R:$ FIRST(S) ' FOLLOW(R) ) ( !

8 !

Parsare recursiv descendent"!


! Gramatica trebuie s" e LL(1)! ! Pentru ecare neterminal se construie#te o procedur" (eventual recursiv") care realizeaz" parsarea categoriei sintactice corespunz"toare acelui neterminal! ! Cnd un neterminal are produc%ii multiple, ecare produc%ie este implementat" ntr-o ramur" a instruc%iunii de selectare, corespunz"toare informa%iilor din intrare (look-ahead information)!

9 !

Utilizare FIRST #i FOLLOW!


expr % term rest" rest % + term rest" | - term rest" | "$ term % id!
procedure rest();$ begin" if lookahead in FIRST(+ term rest) then" match(+); term(); rest()$ else if lookahead in FIRST(- term rest) then" match(-); term(); rest()$ else if lookahead in FOLLOW(rest) then" return" else error()" end;!

FIRST(+ term rest) = { + }$ FIRST(- term rest) = { - }$ FOLLOW(rest) = { $ }!

Parsare predictiv" ne-recursiv" (bazat" pe tabela de parsare)!


! Dat" o gramatic" LL(1) G = (N, T, P, S) se construie#te o tabel" M[A,a] pentru ecare A # N, a # T #i un driver program cu o stiv#:!
input! stack! X$ Y$ Z$ $$ a! +! b! $! Predictive parsing$ program (driver)! Parsing table$ M!

10 !

output!

11 !

Construirea Tabelei de parsare LL(1)!


for each production A % ! do" !for each a # FIRST(!) do // a ) "$ ! !add A % ! to M[A,a]$ !enddo$ !if " # FIRST(!) then$ ! !for each b # FOLLOW(A) do$ ! ! !add A % ! to M[A,b]$ ! !enddo $ !endif" enddo" Mark each undened entry in M error!

12 !

Exemplu!
E % T ER$ ER % + T ER | " " T % F TR$ TR % * F TR | " " F % ( E ) | id!

A % !! E % T ER$ ER % + T ER$ ER % "! T % F TR$ TR % * F TR$ TR % "! F % ( E )! F % id!

FIRST(!)! FOLLOW(A)! ( id! +! "! ( id! *! "! (! id! (! E % T ER$ )! $ )! $ )! $ )! + $ )! + $ )! + $ )! * + $ )! * + $ )! $!

id! E$ ER! T! TR! F! F % id! T % F TR$ E % T ER$

+! ER % + T ER$

*!

ER % "! ER % "! T % F TR$ TR % * F TR$ F % ( E )! TR % "! TR % "!

TR % "!

13 !

LL(1) vs. ambiguitate!


Gramatic" ambigu"" S % i E t S SR | a$ SR % e S | " " E % b!
A % !! S % i E t S SR$ S % a$ SR % e S! SR % "! E % b$ e! SR % "$ SR % e S$ E % b! i! S % i E t S SR$ SR % "! FIRST(!)! FOLLOW(A)! i! a! e! "! b! t! e $! e $! e $! e $! t! $!

Error: duplicate table entry!


a! S$ SR! E! S % a$ b!

Parsare predictiv": Programul (Driver)!

14 !

push($)$ push(S)$ a := lookahead! repeat$ !X := pop()$ !if X is a terminal or X = $ then$ ! !match(X) // moves to next token and a := lookahead$ !else if M[X, a] = X % Y1Y2Yk then$ ! !push(Yk, Yk-1, , Y2, Y1) // such that Y1 is on top" $ $ invoke actions and/or produce output $ !else !error()$ !endif" until X = $!

15 !

Exemplu!
Stack! $E! $ERT" $ERTRF" $ERTRid" $ERTR" $ER" $ERT+" $ERT" $ERTRF" $ERTRid" $ERTR$ $ERTRF*! $ERTRF" $ERTRid" $ERTR" $ER" $! Input! Production applied" id+id*id$" E % T ER$ id+id*id$" T % F TR" id+id*id$" F % id" id+id*id$" +id*id$" TR % "$ +id*id$" ER % + T ER" +id*id$" id*id$" T % F TR$ id*id$" F % id" id*id$" *id$" TR % * F TR" *id$" id$" F % id" id$" $" TR % "$ $" ER % "! $!

16 !

Parsare Bottom-Up!
! Metoda LR (Left-to-right, Rightmost derivation)!
! SLR, LR, LALR!

! Alte metode:!
! Shift-reduce parsing! ! Operator-precedence parsing!

17 !

Parsare Shift-Reduce!
Gramatica:$ S % a A B e" A % A b c | b$ B % d! P"r%i drepte ! ale produc%iilor! Reducere:$ a b b c d e" a A b c d e" a A d e" a A B e" S! Derivarea extrem! dreapt" corespunz"toare:$ S *rm a A B e" *rm a A d e" *rm a A b c d e" *rm a b b c d e! S! A! A! A! A! A! B! A! A! B!

a b b c d e! a b b c d e! a b b c d e! a b b c d e!

18 !

Handles!
Un handle este un sub#ir n forma propozi!ional# dreapta$ care se potrive#te cu partea dreapt" a unei produc%ii!

Gramatica:$ S % a A B e" A % A b c | b$ B % d!

a b b c d e" a A b c d e" Handle$ a A d e" a A B e" S! a b b c d e" a A b c d e" Uneori procedeul duce la blocaj!! a A A c d e" ?!

19 !

Implementare Shift-Reduce!

Gramatica:$ E % E + E" E % E * E$ E % ( E )$ E % id! Se determin"! un handle!

Stack! Input! $" id+id*id$" $id! +id*id$" $E" +id*id$" $E+" id*id$" $E+id" *id$" $E+E" *id$" $E+E*" id$" $E+E*id" $" $E+E*E" $" $E+E" $" $E! $!

Action$ shift$ reduce E % id$ shift$ shift$ reduce E % id" shift (or reduce?)$ shift$ reduce E % id" reduce E % E * E" reduce E % E + E" accept$

Conict!!

20 !

Conicte!
! Conicte shift-reduce #i reduce-reduce$ ! Cauze!
! Limitele pars"rii LR! ! Ambiguitatea gramaticii!

! Uneori se pot rezolva n favoarea unei ac%iuni!

21 !

Conict shift - reduce!

Stack! $" $if E then S!

Input! Action$ $" $ else$! shift or reduce?!

Gramatic" ambigu"$ S % if E then S" | if E then S else S$ | other! Rezolvare n favoarea ! lui shift; else se potrive#te ! cu cel mai apropiat if!

22 !

Conict reduce-reduce!

Stack! $" $a!

Input! Action$ aa$" shift$ a$! reduce A % a or B % a ?!

Gramatica:$ C % A B" A % a$ B % a! Rezolvare n favoarea$ reducerii A % a," altfel se blocheaz"!!

23 !

Parser LR(k) : DFA pentru decizii!


1! C!
start!

0!

A! a!

B! 2! a! 3!

4!

5!

Gramatica:$ S % C$ C % A B" A % a$ B % a!

State I1:$ goto(I0,C)! S % C!

State I4:$ C % A B!

State I0:$ goto(I2,B)! S % C $ goto(I0,A)! State I2:$ C % AB" C % A B" B % a! goto(I2,a)! A % a! goto(I0,a)! State I :$ 3 A % a! reduce A % a! State I5:$ B % a !

24 !

DFA decide shift/reduce!


St"rile DFA sunt parcurse (shift) pn" la una ! ce con%ine punctul la sfr#it (reduce)!
Grammar:$ S % C" C % A B" A % a$ B % a! goto(I0,a)! State I0:$ S % C $ State I3:$ C % A B" A % a! A % a!
Stack! $ 0" $ 0" $ 0 a 3" $ 0 A 2" $ 0 A 2 a 5" $ 0 A 2 B 4" $ 0 C 1$ Input! aa$" aa$" a$" a$" $" $" $! Action$ start in state 0$ shift (and goto state 3)$ reduce A % a (goto 2)" shift (goto 5)$ reduce B % a (goto 4)" reduce C % AB (goto 1)" accept (S % C)!

25 !

DFA decide shift/reduce!

S % C" C % A B" A % a$ B % a! goto(I0,A)! State I0:$ State I2:$ S % C $ C % AB" C % A B" B % a! A % a!

Stack! $ 0" $ 0" $ 0 a 3" $ 0 A 2" $ 0 A 2 a 5" $ 0 A 2 B 4" $ 0 C 1$

Input! aa$" aa$" a$" a$" $" $" $!

Action$ start in state 0$ shift (and goto state 3)$ reduce A % a (goto 2)" shift (goto 5)$ reduce B % a (goto 4)" reduce C % AB (goto 1)" accept (S % C) !

26 !

DFA decide shift/reduce!


Grammar:$ S % C" C % A B" A % a$ B % a!

State I2:$ C % AB" B % a!

goto(I2,a)! State I5:$ B % a!

Stack! $ 0" $ 0" $ 0 a 3" $ 0 A 2" $ 0 A 2 a 5" $ 0 A 2 B 4" $ 0 C 1$

Input! aa$" aa$" a$" a$" $" $" $!

Action$ start in state 0$ shift (and goto state 3)$ reduce A % a (goto 2)" shift (goto 5)$ reduce B % a (goto 4)" reduce C % AB (goto 1)" accept (S % C)!

27 !

DFA decide shift/reduce!

S % C" C % A B" A % a$ B % a!

State I2:$ C % AB" B % a!

goto(I2,B)! State I4:$ C % A B!

Stack! $ 0" $ 0" $ 0 a 3" $ 0 A 2" $ 0 A 2 a 5" $ 0 A 2 B 4" $ 0 C 1$

Input! aa$" aa$" a$" a$" $" $" $!

Action$ start in state 0$ shift (and goto state 3)$ reduce A % a (goto 2)" shift (goto 5)$ reduce B % a (goto 4)" reduce C % AB (goto 1)" accept (S % C)!

28 !

DFA decide shift/reduce!


Grammar:$ S % C" C % A B" A % a$ B % a!

State I0:$ S % C $ C % A B" A % a!

goto(I0,C)! State I1:$ S % C!

Stack! $ 0" $ 0" $ 0 a 3" $ 0 A 2" $ 0 A 2 a 5" $ 0 A 2 B 4" $ 0 C 1$

Input! aa$" aa$" a$" a$" $" $" $!

Action$ start in state 0$ shift (and goto state 3)$ reduce A % a (goto 2)" shift (goto 5)$ reduce B % a (goto 4)" reduce C % AB (goto 1)" accept (S % C)!

29 !

DFA decide shift/reduce!

S % C" C % A B" A % a$ B % a!

State I0:$ S % C $ C % A B" A % a!

goto(I0,C)! State I1:$ S % C!

Stack! $ 0" $ 0" $ 0 a 3" $ 0 A 2" $ 0 A 2 a 5" $ 0 A 2 B 4" $ 0 C 1$

Input! aa$" aa$" a$" a$" $" $" $!

Action$ start in state 0$ shift (and goto state 3)$ reduce A % a (goto 2)" shift (goto 5)$ reduce B % a (goto 4)" reduce C % AB (goto 1)" accept (S % C)!

S-ar putea să vă placă și