Documente Academic
Documente Profesional
Documente Cultură
! Parsare predictiv"!
! FIRST, FOLLOW! ! Gramatici LL(1)! ! Parsare recursiv descendent"! ! Tabela de parsare LL(1)! ! Parsare predictiv" ne-recursiv"!
! Parsare Bottom-Up
!!
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 " ** "!
8 !
9 !
10 !
output!
11 !
12 !
Exemplu!
E % T ER$ ER % + T ER | " " T % F TR$ TR % * F TR | " " F % ( E ) | id!
+! ER % + T ER$
*!
TR % "!
13 !
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!
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!
21 !
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!
23 !
0!
A! a!
B! 2! a! 3!
4!
5!
Gramatica:$ S % C$ C % A B" A % a$ B % a!
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 !
25 !
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 !
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 !
S % C" C % A B" A % a$ B % 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 !
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 !
S % C" C % A B" A % a$ B % 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)!