1 / 22

Ontleden

Ontleden. Non-deterministisch m.b.v. Parser-combinators Deterministisch m.b.v. Stack-machine. type Parser a b = [a]  [ ( b , [a] ) ]. Mag ambigu zijn. type Parser a b = [a]  ( b , [a] ). Eén oplossing. Deterministisch ontleden. …m.b.v. Stack-machine LL -ontleden

Download Presentation

Ontleden

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Ontleden • Non-deterministischm.b.v. Parser-combinators • Deterministischm.b.v. Stack-machine type Parser ab = [a][ (b, [a]) ] Mag ambigu zijn type Parser ab = [a] (b, [a]) Eén oplossing

  2. Deterministisch ontleden …m.b.v. Stack-machine • LL-ontleden • Left-to-right input processing • Leftmost derivation • LR-ontleden • Left-to-right input processing • Rightmost derivation “Top-down” ontleden “Botton-up” ontleden

  3. Stackmachine voor LL-ontleden check :: String  Bool check input = run [’S’] input run :: Stack  String  Bool run [ ] [ ] = True run (a:as)(x:xs) | isT a && a==x = run as xs | isN a = run (rs++as) (x:xs) | otherwise = False where rs = select a x run _ _ = False

  4. Kiezen van de regelbij LL-ontleden select a x = snd ( hd ( filter ok ( prods gram))) • where • ok p@(n,_) = n==a • && x  lahP gram p lahP

  5. Grammatica voor expressies met + en * en () E T E T +E T F T F*T F N F ( E ) N 1 N 2 N 3 E T P P   P +E T F M M   M *T F N F ( E ) N 1 N 2 N 3 Linksfactoriseren (geen regels met gelijk beginstuk):

  6. nee ( 1 2 3 first(E) (1 2 3 nee ( 1 2 3 first(T) ja + follow(P) ) # + (1 2 3 nee ( 1 2 3 first(F) ja * follow(M) + ) # * nee ( 1 2 3 first(N) 1 2 3 ( nee 1 2 3 1 2 3 Bepalen van empty, first, lookahead S E # E T P P   P +E T F M M   M *T F N F ( E ) N 1 N 2 N 3 E T P P   P +E T F M M   M *T F N F ( E ) N 1 N 2 N 3

  7. Stackmachine voor LR-ontleden check :: String  Bool check input = run [ ] input run :: Stack  String  Bool run [’S’] [ ] = True run [’S’] _ = False run as (x:xs) = case aktie of Shift  run (x:as) xs Reduce a n  run (a:drop n as) (x:xs) Error  False where aktie = select’ as x

  8. Kiezen van de regelbij LR-ontleden select’ as x | null items = Error | null redItems = Shift | otherwise = Reduce a (length rs) • where • items = dfa as • redItems = filter red items • (a, rs, _) = hd redItems dfa red

  9. Bijvoorbeeld productieregel: F(E) F2 M …geeft bestaansrecht aan deze items: F• (E) F •2 M • F( •E) F 2 • F( E•) F( E) • Items • Een item is een productieregelmet daarin een “cursorpositie”

  10. …met toestandsovergangen: X A   X  A  X   • starttoestand:  Voor elke regelX  X   Z  S Toestandsautomaat • Constructie van een NFA: • toestanden zijn Items bij de grammatica A   

  11. S  E E T +E  E T+  E S E  E T +E T ( E )  T ( E) E T +E T (  E ) E  T E T  T N*T T (E ) T  N T N  T N*T T N* T T N*T  E T E T +E T N T N*T T ( E ) S E E T E T +E T N T N*T T ( E )

  12. Transformatie NFADFA • Nieuwe toestanden:verzamelingen van oude toestanden • Nieuwe overgangen:als er een overgang tussen een van de elementen uit de verzamelingen was

  13. + E S  E E  T E T +E T (E ) T N*T T  N T E T  E T +E E T+  E E  T E T +E T (E ) T N*T T  N E T+E  T T T N*T T N  N N N E * N T N* T T (E ) T N*T T  N S E  ( E ( T (  E ) E  T E T +E T (E ) T N*T T  N T (E ) ) T T (E )  T N*T ( (

  14. Kiezen van de regelbij LR-ontleden select’ as x null items = Error null redItems = Shift otherwise = Reduce a (length rs) • where • items = dfa as • redItems = filter red items • (a, rs, _) = hd redItems E T  E T +E dfa red red: Cursor aanhet eind • red (a,r,c) = c == length r • && x  follow a

  15. Stackmachine voor LR-ontleden run :: Stack  String  Bool run [’S’] [ ] = True run [’S’] _ = error run as (x:xs) = case aktie of Shift  run (x:as) xs Reduce a n  run (a:drop n as) (x:xs) Error  error where aktie = select’ as x select’ as x = …… where items = dfa as …… Zonde van het werkom dfa steeds opnieuwuit te rekenen

  16. Optimalisatieuitrekenen DFA • DFA wordt steeds uitgerekendop de symbolen in de stack • Bewaar die waarde op de stack erbij

  17. + E S  E E  T E  T + E T ( E ) T  N * T T  N S  E E  T E T +E T (E ) T N*T T  N T E  T  E  T + E E T  E T +E 5 E  T +  E E  T E  T + E T ( E ) T  N * T T  N E T+  E E  T E T +E T (E ) T N*T T  N 1 E  T + E  E T+E  7 T T  N * T T  N  T N*T T N  N N 3 6 N E * N T  N *  T T ( E ) T  N * T T  N T N* T T (E ) T N*T T  N S E  S E  2 ( E ( T (  E ) E  T E  T + E T ( E ) T  N * T T  N T (  E ) E  T E T +E T (E ) T N*T T  N T (E ) T ( E ) 9 11 ) T T (E )  T ( E )  10 T  N * T  T N*T 4 ( ( 8 T T * N ( * N T * N ( * N @ 4 11 3 8 11 3 1

  18. Optimalisatieuitrekenen DFA • DFA wordt steeds uitgerekendop de symbolen in de stack • Bewaar die waarde op de stack erbij • Bereken eenmalig twee tabellen: • goto :: (Int, Nonterm)  Int • action :: (Int, Term)  Actiontype Action = Shift Int | Reduce Nonterm Int | Error | Accept

  19. X i j a  a  xb j i • action(i,x) = Shift j a  r i • action(i,x) = Reduce a |r| && x  follow a S  N i • action(i,EOF) = Accept otherwise • action(i,x) = Error Goto en Action tabellen • goto(i,X) = j

  20. a  r i • action(i,x) = Reduce a |r| && x  follow a Soorten LR ontleden Zoals beschreven is alleen nog maar… • SLR: Simpel LR Te royaal:followset onafhankelijkvan de context • SLR reduceert (iets) te vaak

  21. LR item:idem, plus bijbehorende follow-set T (E ) +) Van SLR naar LR • SLR item:Regel uit grammatica met cursor T (E )

  22. Van LR naar LALR • SLR (Simpel LR)nadeel: reduceert te vaak • LR: met contextafhankelijke followsetsnadeel: DFA krijgt erg veel toestanden • LALR (Lookahead LR) compromis: • Merge DFA-toestanden waarvan de itemsalleen qua lookahead verschillen LALR Wordt in de praktijk het meest gebruikt (yacc, Bison, …)

More Related