~ubuntu-branches/ubuntu/utopic/haskell-uulib/utopic

« back to all changes in this revision

Viewing changes to examples/parser/Example.hs

  • Committer: Bazaar Package Importer
  • Author(s): Marco Túlio Gontijo e Silva
  • Date: 2009-04-08 20:01:10 UTC
  • mfrom: (1.1.5 upstream)
  • Revision ID: james.westby@ubuntu.com-20090408200110-96hu9fr918e1wsr1
Tags: 0.9.10-0.1
* Non-maintainer upload.
* New upstream version.  Closes: #523214.
* Use new version of haskell-devscripts.
* debian/control:
  - Use new Standards-Version.
  - cpphs is not Indep.
* debian/patches: Remove directory.
* debian/haskell-uulib-doc.doc-base: haddock's file are stored now in
  /usr/share/libghc6-uulib-doc/html/.
* debian/haskell-uulib-doc.examples: Create file.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
module Main where
 
2
 
 
3
-- import the the library functions from uulib
 
4
import UU.Parsing
 
5
import UU.Scanner
 
6
 
 
7
-- import our custom made Alex-scanner
 
8
import Scanner
 
9
 
 
10
 
 
11
-- Some boilerplate code to use the parser
 
12
-- Give `parsetokens' your parser and a list of tokens, returned by the `scanTokens'
 
13
-- function exported by the Scanner module, then you get either a list of error
 
14
-- messages in case of a parse error, or the parse tree.
 
15
type TokenParser a = Parser Token a
 
16
 
 
17
parseTokens :: TokenParser a -> [Token] -> Either [String] a
 
18
parseTokens p tks
 
19
  = if null msgs
 
20
    then final `seq` Right v
 
21
    else Left (map show msgs)
 
22
  where
 
23
    steps = parse p tks
 
24
    msgs  = getMsgs steps
 
25
    (Pair v final) = evalSteps steps
 
26
 
 
27
 
 
28
-- define a parse tree
 
29
data Expr
 
30
  = Identifier String
 
31
  | Integer Int
 
32
  | String String
 
33
  | Plus Expr Expr
 
34
  | Times Expr Expr
 
35
  | Let String  -- variable
 
36
        Expr    --   = expr
 
37
        Expr    -- body
 
38
  deriving Show
 
39
 
 
40
 
 
41
-- write a parser for it
 
42
-- Note: * make sure that the parser is not left recursive
 
43
--           (to the left is never a pExpr, or always a terminal first)
 
44
--       * make sure that the parser is not ambiguous
 
45
--           (by introducing priority levels for the operators)
 
46
 
 
47
 
 
48
-- Term -> let var = Expr in Expr
 
49
pExpr :: TokenParser Expr
 
50
pExpr
 
51
  =   (\_ x _ e _ b -> Let x e b) <$> pKey "let" <*> pVarid <*> pKey "=" <*> pExpr <*> pKey "in" <*> pExpr
 
52
  <|> pMult
 
53
 
 
54
-- Expr -> Factor | Factor * Expr
 
55
pMult :: TokenParser Expr
 
56
pMult
 
57
  =   pFactor 
 
58
  <|> (\l _ r -> Times l r) <$> pFactor <*> pKey "*" <*> pExpr
 
59
 
 
60
-- Factor -> Term | Term * Factor
 
61
pFactor :: TokenParser Expr
 
62
pFactor
 
63
  =   pTerm
 
64
  <|> (\l _ r -> Plus l r) <$> pTerm <*> pKey "+" <*> pFactor
 
65
 
 
66
-- Term -> var
 
67
-- Term -> String
 
68
-- Term -> Int
 
69
-- Term -> (Expr)
 
70
pTerm :: TokenParser Expr
 
71
pTerm
 
72
  =   Identifier <$> pVarid
 
73
  <|> (Integer . read) <$> pInteger16
 
74
  <|> (String  . read) <$> pString
 
75
  <|> (\_ e _ -> e) <$> pKey "(" <*> pExpr <*> pKey ")"
 
76
 
 
77
 
 
78
-- test it
 
79
main :: IO ()
 
80
main
 
81
  = let res = parseTokens pExpr (tokenize "nofile" "let x = 3 in x+x")
 
82
    in case res of
 
83
         Left errs -> mapM_ putStrLn errs
 
84
         Right tree -> putStrLn $ show tree