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
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
Definitie Lookaheadset van alternatief N
S
x
N
{ x | S * N * x }
Lah(Nx) = {x}Lah(NP) = ……
Eigenschappen vanNonterminals
… nodig om Lookahead-sets te bepalen
Empty(N) First(N)
Follow(N)
N *
S
x
N
x
N
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
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
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
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] )
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
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
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])]
Functies voor eigenschappen van een grammatica
Wat zijn de lookahead-setsvan de producties ?
lahP :: Gram s Prod s [s]S
x
N
De ontleed-functie
S
parse gram
=
( , )
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
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
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
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
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
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
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 [ ]
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
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
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!
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 = …;
Eigenschappen vanNonterminals
… nodig om Lookahead-sets te bepalen
Empty(N) First(N)
Follow(N)
N *
S
x
N
x
N
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