~ubuntu-branches/ubuntu/trusty/happy/trusty-proposed

« back to all changes in this revision

Viewing changes to tests/AttrGrammar002.y

  • Committer: Package Import Robot
  • Author(s): Joachim Breitner
  • Date: 2013-09-18 19:11:12 UTC
  • mfrom: (1.2.8)
  • Revision ID: package-import@ubuntu.com-20130918191112-h5he0u2g5tqnh90m
Tags: 1.19.0-1
* Fix Vcs-Darcs url: http://darcs.debian.org/ instead of
  http://darcs.debian.org/darcs/
* New upstream release

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
%tokentype { Char }
 
3
 
 
4
%token minus { '-' }
 
5
%token plus  { '+' }
 
6
%token one   { '1' }
 
7
%token zero  { '0' }
 
8
 
 
9
%attributetype { Attrs }
 
10
%attribute value { Integer }
 
11
%attribute pos   { Int }
 
12
 
 
13
%name parse start
 
14
 
 
15
%monad { Maybe }
 
16
 
 
17
%%
 
18
 
 
19
start 
 
20
   : num { $$ = $1 }
 
21
 
 
22
num 
 
23
   : bits        { $$ = $1       ; $1.pos = 0 }
 
24
   | plus bits   { $$ = $2       ; $2.pos = 0 }
 
25
   | minus bits  { $$ = negate $2; $2.pos = 0 }
 
26
 
 
27
bits
 
28
   : bit         { $$ = $1
 
29
                 ; $1.pos = $$.pos 
 
30
                 }
 
31
 
 
32
   | bits bit    { $$ = $1 + $2
 
33
                 ; $1.pos = $$.pos + 1
 
34
                 ; $2.pos = $$.pos
 
35
                 }
 
36
 
 
37
bit
 
38
   : zero        { $$ = 0 }
 
39
   | one         { $$ = 2^($$.pos) }
 
40
 
 
41
 
 
42
{
 
43
happyError msg = fail $ "parse error: "++msg
 
44
 
 
45
main = case parse ""      of { Nothing ->
 
46
       case parse "abc"   of { Nothing ->
 
47
       case parse "0"     of { Just 0  ->
 
48
       case parse "1"     of { Just 1  ->
 
49
       case parse "101"   of { Just 5  ->
 
50
       case parse "111"   of { Just 7  ->
 
51
       case parse "10001" of { Just 17 ->
 
52
       putStrLn "Test worked";
 
53
       _ -> quit }; _ -> quit }; _ -> quit };
 
54
       _ -> quit }; _ -> quit }; _ -> quit };
 
55
       _ -> quit }
 
56
 
 
57
quit = putStrLn "Test Failed"
 
58
}