definitie ll(1)
DESCRIPTION
Definitie LL(1). Een grammatica is LL(1) Als je op grond van het eerstvolgende input-symbool kunt kiezen uit alternatieven. Formeel: Als de lookahead-sets van de alternatieven van elke nonterminal onderling disjunct zijn. S. N. . . x . - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Definitie LL(1)](https://reader031.vdocuments.pub/reader031/viewer/2022033103/56814cb4550346895db9bd3e/html5/thumbnails/1.jpg)
Definitie LL(1)
Een grammatica is LL(1) Als je op grond van het eerstvolgende
input-symbool kunt kiezen uit alternatieven
Formeel: Als de lookahead-sets van de
alternatieven van elke nonterminalonderling disjunct zijn
![Page 2: Definitie LL(1)](https://reader031.vdocuments.pub/reader031/viewer/2022033103/56814cb4550346895db9bd3e/html5/thumbnails/2.jpg)
Definitie Lookaheadset van alternatief N
S
x
N
{ x | S * N * x }
Lah(Nx) = {x}Lah(NP) = ……
![Page 3: Definitie LL(1)](https://reader031.vdocuments.pub/reader031/viewer/2022033103/56814cb4550346895db9bd3e/html5/thumbnails/3.jpg)
Eigenschappen vanNonterminals
… nodig om Lookahead-sets te bepalen
Empty(N) First(N)
Follow(N)
N *
S
x
N
x
N
![Page 4: Definitie LL(1)](https://reader031.vdocuments.pub/reader031/viewer/2022033103/56814cb4550346895db9bd3e/html5/thumbnails/4.jpg)
LL1 ontleden
Hoofdstuk 3: Parser Combinators
type Parser a b = [a] [ (b, [a]) ]
Hoofdstuk 10: LL1 Parsers
type Parser a b = [a] (b, [a])
Mag ambigu zijn
Eén oplossing
![Page 5: Definitie LL(1)](https://reader031.vdocuments.pub/reader031/viewer/2022033103/56814cb4550346895db9bd3e/html5/thumbnails/5.jpg)
LL1 ontleden
Hoofdstuk 3: Parser Combinators
parseHaakjes :: Parser Char HaakjesparseExpr :: Parser Char ExprparseHaskTp :: Parser Char HaskTp
symbol :: a Parser a a( <|> ) :: Parser a b Parser a b Parser a b
Hoofdstuk 10: LL1 Parsers
parse :: Gram s Parser s (Boom s)
Universele functie Universeel boomtype
![Page 6: Definitie LL(1)](https://reader031.vdocuments.pub/reader031/viewer/2022033103/56814cb4550346895db9bd3e/html5/thumbnails/6.jpg)
Universeel ontleden
Wat hebben we nodig? type Gram type Boom Functies op Gram
parse :: Gram s Parser s (Boom s)
gen :: Gram s Parser s (Boom s)s
Gegeneraliseerde ontleedfunctie
termsnontermsstartsymprods
lookaheademptyfirstfollow
[ s ] [ ]
startsymbool
![Page 7: Definitie LL(1)](https://reader031.vdocuments.pub/reader031/viewer/2022033103/56814cb4550346895db9bd3e/html5/thumbnails/7.jpg)
Type voor Grammatica’s
S A a S | B | C BA S C | B A | bC DD d
S A a S S B S C BA S CA B AB bC DD d
[ ( ‘S’ , “A a S”), ( ‘S’ , “B” ), ( ‘S’ , “C B” ), ( ‘A’ , “S C” ), ( ‘A’ , “” ), ( ‘B’ , “A” ), ( ‘B’ , “b” ), ( ‘C’ , “D” ), ( ‘D’ , “d” )]
( ‘S’ ,
)
type Prod s = (s, [s]) type Gram s = ( s, [Prod s] )
![Page 8: Definitie LL(1)](https://reader031.vdocuments.pub/reader031/viewer/2022033103/56814cb4550346895db9bd3e/html5/thumbnails/8.jpg)
Type voor Ontleedbomen
Binaire bomendata Tree a = Bin (Tree a) (Tree a)
| Leaf a
Gelabelde Binaire bomendata Tree a = Bin a (Tree a) (Tree a)
| Leaf a
Gelabelde Multi-splitsende bomendata Tree a = Node a [ Tree a ]
| Leaf a
![Page 9: Definitie LL(1)](https://reader031.vdocuments.pub/reader031/viewer/2022033103/56814cb4550346895db9bd3e/html5/thumbnails/9.jpg)
Functies op grammatica’s
( ‘S’ , [ ( ‘S’ , “A a S”) , ( ‘S’ , “B” ) , ( ‘S’ , “C B” ) , ( ‘A’ , “S C” ) , ( ‘A’ , “” ) , ( ‘B’ , “A” ) , ( ‘B’ , “b” ) , ( ‘C’ , “D” ) , ( ‘D’ , “d” ) ])
start :: Gram s sstart = fst
prods :: Gram s [Prod s]prods = snd
nonts :: Gram s [s]nonts = nub . map fst . prods
terms :: Gram s [s]terms = nub . filter isT . concat
. map snd . prods
![Page 10: Definitie LL(1)](https://reader031.vdocuments.pub/reader031/viewer/2022033103/56814cb4550346895db9bd3e/html5/thumbnails/10.jpg)
Functies voor eigenschappen van een grammatica
Is de grammatica LL1 ?
isLL1 :: Gram s Bool
Kan een NT epsilon genereren?
empty :: Gram s s Bool
x
N Waar kan een zin mee beginnen?
firsts :: Gram s s [s]firstsTab :: Gram s [(s , [s])]
![Page 11: Definitie LL(1)](https://reader031.vdocuments.pub/reader031/viewer/2022033103/56814cb4550346895db9bd3e/html5/thumbnails/11.jpg)
Functies voor eigenschappen van een grammatica
Wat zijn de lookahead-setsvan de producties ?
lahP :: Gram s Prod s [s]S
x
N
![Page 12: Definitie LL(1)](https://reader031.vdocuments.pub/reader031/viewer/2022033103/56814cb4550346895db9bd3e/html5/thumbnails/12.jpg)
De ontleed-functie
S
parse gram
=
( , )
![Page 13: Definitie LL(1)](https://reader031.vdocuments.pub/reader031/viewer/2022033103/56814cb4550346895db9bd3e/html5/thumbnails/13.jpg)
De ontleed-functie
[ , , , ]
AB
x
A
[ , , , ]xA B A
genParse gram
=
( , )
parse gram input | isLL1 gram = (t, rest)
where ([t],rest) = genParse gram [start gram] input
| otherwise = error
![Page 14: Definitie LL(1)](https://reader031.vdocuments.pub/reader031/viewer/2022033103/56814cb4550346895db9bd3e/html5/thumbnails/14.jpg)
De ontleed-functie
[ , , , ]
AB
x
A
[ , , , ]xA B A
genParse gram
=
( , )
genParse gram [ ] in = ( [ ], in )genParse gram (a:as) in@(x:xs) | isT a && a==x = (t:ts, uit)where t = Node a [ ]
(ts,uit) = genParse gram as xs
![Page 15: Definitie LL(1)](https://reader031.vdocuments.pub/reader031/viewer/2022033103/56814cb4550346895db9bd3e/html5/thumbnails/15.jpg)
De ontleed-functie
[ , , , ]
AB
x
A
[ , , , ]xA B A
genParse gram
=
( , )
genParse gram (a:as) in@(x:xs) | isN a = (t:ts, uit)where t = Node a ks
(ks,door) = genParse gram rs in (ts,uit) = genParse gram as door
rs
A
rs
![Page 16: Definitie LL(1)](https://reader031.vdocuments.pub/reader031/viewer/2022033103/56814cb4550346895db9bd3e/html5/thumbnails/16.jpg)
Welke productie kiezen?
wherers = snd ( hd ( filter ok (prods gram)))
genParse gram (a:as) in@(x:xs) | isN a = (t:ts, uit)where t = Node a ks
(ks,door) = genParse gram rs in (ts,uit) = genParse gram as door
rs
ok p@(n,_) = n==a && x lahP gram p
![Page 17: Definitie LL(1)](https://reader031.vdocuments.pub/reader031/viewer/2022033103/56814cb4550346895db9bd3e/html5/thumbnails/17.jpg)
Bepalen van lookahead-setvan een productie
S A a S S B S C BA S CA B AB bC DD d lahP (Dd) = {d}
lahP (CD) = firsts D
lahP (SAaS) = firsts A {a}
lahP (ASC) = firsts S firsts C
lahP (BA) = firsts A follow B
empty A
empty S
![Page 18: Definitie LL(1)](https://reader031.vdocuments.pub/reader031/viewer/2022033103/56814cb4550346895db9bd3e/html5/thumbnails/18.jpg)
Bepalen van lookahead-setvan een productie
Lookahead-set van een productie: Verenig de firsts van de rechterkant… …en ga door zolang ze empty kunnen zijn Bereik je het eind:
neem dan ook de follow van jezelf
lahP (SABCDE) = firsts A firsts B firsts C firsts D firsts E follow S
![Page 19: Definitie LL(1)](https://reader031.vdocuments.pub/reader031/viewer/2022033103/56814cb4550346895db9bd3e/html5/thumbnails/19.jpg)
Bepalen van lookahead-setvan een productie
Lookahead-set van een productie: Verenig de firsts van de rechterkant… …en ga door zolang ze empty kunnen zijn Bereik je het eind:
neem dan ook de follow van jezelf
lahP (n,ys) = foldr f eind yswhere f y r =
eind =
firsts y
follow n if empty y then r else [ ]
![Page 20: Definitie LL(1)](https://reader031.vdocuments.pub/reader031/viewer/2022033103/56814cb4550346895db9bd3e/html5/thumbnails/20.jpg)
Empty: kan een nonterminalepsilon genereren?
S A a S S B S C BA S CA B AB bC DD d
In 0 stappen: A In 1 stap:
A B In 2 stappen:
A B S In 3 stappen:
A B S In 4 stappen: Verandert niet meer, stop met zoeken
![Page 21: Definitie LL(1)](https://reader031.vdocuments.pub/reader031/viewer/2022033103/56814cb4550346895db9bd3e/html5/thumbnails/21.jpg)
Empty: kan een nonterminalepsilon genereren?
S A a SS B S C BA S CA B AB bC DD d
Herhaal zolang er iets verandertfixed f xs | xs==ys = xs
| otherwise = fixed f yswhere ys = f xs
Begin met lege set
empty gram = fixed (stap gram) [ ]
stap gram xs =
(prods gram)))(filter (\(n,rs) all (xs) rs)
(map fstnub
![Page 22: Definitie LL(1)](https://reader031.vdocuments.pub/reader031/viewer/2022033103/56814cb4550346895db9bd3e/html5/thumbnails/22.jpg)
Firsts: wat kan een symboollinksonder genereren?
S A a SS B S C BA S CA B AB bC DD d
x
N
In 0stap
:SABCDabd
ABCSAbDd
In 1stap
:SABCASBAbCDDdabd
In 0/1stap:
SABCDbSABCSAbDdd
In 1/2stap:
SABCDabd
SABCDbSABCSABbCDdDdabd
In 0/1/2 stap:
Herhaal zolang er iets verandert!
![Page 23: Definitie LL(1)](https://reader031.vdocuments.pub/reader031/viewer/2022033103/56814cb4550346895db9bd3e/html5/thumbnails/23.jpg)
Firsts: wat kan een symboollinksonder genereren? Herhaal zolang er iets
verandertfirsts gram = fixed (stap gram) (single gram)
stap gram ben =
single gram =(symbols gram)map (\x (x,[x]))
(first1 gram)(compose ben )(single gram)combine
first1 = …; compose = …; combine = …;
![Page 24: Definitie LL(1)](https://reader031.vdocuments.pub/reader031/viewer/2022033103/56814cb4550346895db9bd3e/html5/thumbnails/24.jpg)
Eigenschappen vanNonterminals
… nodig om Lookahead-sets te bepalen
Empty(N) First(N)
Follow(N)
N *
S
x
N
x
N
![Page 25: Definitie LL(1)](https://reader031.vdocuments.pub/reader031/viewer/2022033103/56814cb4550346895db9bd3e/html5/thumbnails/25.jpg)
Follow: wat kan er volgenop een nonterminal?
S A a SS B S C BA S CA B AB bC DD d
S A a S
S C BA S C
A a
A
C
S
S
a S B
C
Kan beginnen
met terminaldba
a
dba
d
Kan eindigen met non
terminal
-
ADC
DC
SABCD