1 / 35

Part 1 : Programming and Abstraction

Part 1 : Programming and Abstraction. Lecture 2: Grammar and Parsing. Dr. Ir. I.S.W.B. Prasetya wishnu@cs.uu.nl A. Azurat S.Kom. ade@cs.uu.nl. Transformation. Transformation: Transforming one form of structured information to another form.

jaimin
Download Presentation

Part 1 : Programming and Abstraction

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. Part 1 : Programming and Abstraction Lecture 2: Grammar and Parsing Dr. Ir. I.S.W.B. Prasetya wishnu@cs.uu.nl A. Azurat S.Kom. ade@cs.uu.nl

  2. Transformation • Transformation:Transforming one form of structured information to another form. • Any kind of information processing is transformation.Information with complicated structure: program, specification, formula • Example of applications : • compiler, interpreter, translator • HTML tool • Y2K tool • Oracle code generator

  3. Describing structure • Example: 1.27601 e -10 • Grammar : SignedFloat Sign Float | Float Float Pdigit.PinteSignedExp SignedExpr  Sign Pint | Pint Pdigit  1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 Pint  Digit | Digit Pint Digit  0 | Pdigit

  4. Non-terminal symbol Terminal symbols Terminology Float Pdigit.PinteSignedExp This is called a production rule A grammar is a collection of a production rules.A grammar is context free (CGF) if the left hand side of all its rules consists of only one non-termimal.

  5. Deriving sentences P   | APA | BPB | CPC A  a B  b C  c with start P as the symbol. A sentence s is a sentence of G, if it can be derived from the start symbol of G.Example: P  P  APA  AA  ...  aa P  APA  ABPBA  ...  abba

  6. Language P   | APA | BPB | CPC A  a B  b C  c with start P as the symbol. L(G) = the set of all (terminal) sentences of G Example: for the above grammarL(G) = the set of all even length palindromes over {a,b,c}

  7. Bigger Example Program Identifier(ParameterList)Decl Body ParameterList  Parameter | Parameter,ParameterList Parameter  Identifier:Type ... Body  {Statement} Statement  skip | Assignment | ... Assignment  Identifier:=Expression ...

  8. Transformation transformation result sentence "abba" semantic function parser P parse tree A A P B P B a b b a

  9. Parse Tree Program ParameterList Decl Identifier Body Statement Assignment Identifier Expression x*y+1 } := poo { x ( x:int,y:int )

  10. Representing Parse Tree data Pal = Empty | A Pal | B Pal | C Pal Example:A (B (A Empty)) represents abaaba

  11. Representing Parse Tree Form  Form/\Form | Form \/Form | Form==>Form | ~ Form | Var | Const Var  ... Const  T | F

  12. Representing Parse Tree Expr Expr Expr Expr Expr Expr Expr Expr Var Var Var Var ~ a ==> b a \/ b ==>

  13. Representing Parse Tree data Form = Form `AND` Form | Form `OR` Form | Form `IMP` Form | NOT Form | Var String | Const Bool Example:(Var "a" `OR` Var "b") `IMP` Var "a" Notice also similarty between data type definition and CFG.

  14. Semantic Function • Example: simple tautology checker(a /\ T) \/ ~a  taulogy we won't do this (a /\ F) ==> ~a  taulogy • Represent result with Maybe Bool data Maybe a = Just a | nothing • staut :: Form -> Maybe Bool

  15. Semantic Function staut (Var a) = Nothing staut (Const c) = Just c staut (p `AND` q) = case (staut p, staut q) of (Just False , _) -> Just False (_ , Just False) -> Just False (Just True , Just True) -> Just True otherwise -> Nothing

  16. Semantic Function • Anopther example: (simple) simplifier(a \/ T) /\ ~a  ~a • simp :: Form -> Form

  17. Semantic Function simp (Var a) = Var a simp (Const c) = Const c simp (p `AND` q) = case (simp p, simp q) of (Const False , _) -> Const False (_ , Const False) -> Const False (Const True , q') -> q' (p' , Const True) -> p' otherwise -> Nothing

  18. Parser • Parser:Take a string, and tries to build a parse tree. • For our example grammar of formula:parser :: String -> Form • type Parser result = String -> result • type Parser result = String -> (result,String) • type Parser result = String -> [(result,String)] • type Parser sym res = [sym] -> [(res,[sym])]

  19. Several Primitive Parsers symbol :: Eq s => s -> Parser s s symbol a [ ] = [ ] symbol a (x : xs) | x == a = [(x,xs)] | otherwise = [ ] Example: symbol 'C' :: Parser Char Char symbol 'C' "CLASS" = [('C', "LASS")]

  20. Several Primitive Parsers satisfy :: (s -> Bool) -> Parser s s satisfy p [ ] = [ ] satisfy p (x : xs) | p x = [(x,xs)] | otherwise = [ ] Example: satisfy isDigit :: Parser Char Char satisfy isDigit "100" = [('1', "00")]

  21. Several Primitive Parsers token :: Eq s => [s] -> Parser s [s] Example: token "class" "class A { } " = [("class", " A { } ")] token "class" "cla <= 0" = [ ]

  22. Several Primitive Parsers failp :: Parser s a failp xs = [ ] succeed :: a -> Parser s a succeed r xs = [(r,xs)]

  23. Parser Combinators (<|>) :: Parser s a -> Parser s a -> Parser s a (p <|> q) xs = p xs ++ q xs Example: pSign = symbol '+' <|> symbol '-'

  24. Parser Combinators (<*>) :: Parser s (b -> a) -> Parser s b -> Parser s a (p <*> q) xs = [ (f x , zs) | (f ,ys) <- p xs , ( x, zs) <- q ys ] Example: (pSign <|> succeed '+') <*> satisfy isDigit <*> symbol '.' <*> pDigits

  25. Parser Combinators (<$>) :: (a -> b) -> Parser s a -> Parser s b (f <$> p) xs = [ (f y , ys) | (y , ys) <- p xs ] Example: p = f <*> satisfy isDigit where f :: String -> Int f c = read [c]

  26. f digit theRest = digit : theRest Priority and Associativity pDigits :: Parser Char String pDigits = pDigit <|> ( (f <$> pDigit) <*> pDigits) where f digit = (\theRest -> digit : theRest) or.... f is simply (:)

  27. Priority and Associativity infixl 7 <$> infixl 6 <*> infixr 4 <|> pDigits = pDigit <|> (:) <$> pDigit <*> pDigits pDigits = read <$> (pDigit <|> (:) <$> pDigit <*> pDigits)

  28. Example pIdentifier = (:) <$> satisfy isLower <*> pIdentifier <|> sing <$>satisfy isLower pVar :: Parser Char Form pVar = Var <$> pIdentifier

  29. Example pConst = mk_True <$> symbol 'T' <|> mk_False <$> token 'F' where mk_True s = Const True mk_False s = Const False

  30. Example pForm = mk_And <$> pForm <*> token "/\" <*> pForm <|> pVar <|> pConst <|> ... left recursive!!

  31. Removing Left Recursion Form  Atom/\Form | Atom \/Form | Atom==>Form | ~ Form Atom  | Var | Const Var  ... Const  T | F

  32. Example pAtom = pVar <I> pConst pForm = mk_And <$> pAtom <*> token "/\" <*> pForm <|> ... where mk_And a _ f = a `AND` f

  33. pDigits = pDigit <|> (:) <$> pDigit <*> pDigits Example: pDigit "123" = [("1","23"), ("12","3"), ("123", "") ] Greed pDigits = (:) <$> pDigit <*> pDigits <|> pDigit Example: pDigit "123" = [("123",""), ("12","3"), ("1", "23") ]

  34. list x s = x : s Many many :: Parser s a -> Parser s [a] many p = list <$> p <*> many p <|> succeed [ ] many1 :: Parser s a -> Parser s [a] many1 p = list <$> p <*> many p

  35. Greedy greedy, greedy1 :: Parser s b -> Parser s [b] greedy = first . many greedy1 = first . many1 Example: pDigits = many1 (satisfy isDigit) pDigits = greedy1 (satisfy isDigit)

More Related