]> ruin.nu Git - proglang.git/blob - Parsyntax.y
removed NoType and EDefault and added assert
[proglang.git] / Parsyntax.y
1 -- This Happy file was machine-generated by the BNF converter
2 {
3 module Parsyntax where
4 import Abssyntax
5 import Lexsyntax
6 import ErrM
7 }
8
9 %name pStms Stms
10 %name pExp Exp
11
12 -- no lexer declaration
13 %monad { Err } { thenM } { returnM }
14 %tokentype { Token }
15
16 %token 
17  ';' { PT _ (TS ";") }
18  '{' { PT _ (TS "{") }
19  '}' { PT _ (TS "}") }
20  '=' { PT _ (TS "=") }
21  '(' { PT _ (TS "(") }
22  ')' { PT _ (TS ")") }
23  '++' { PT _ (TS "++") }
24  '--' { PT _ (TS "--") }
25  '-' { PT _ (TS "-") }
26  '!' { PT _ (TS "!") }
27  '<' { PT _ (TS "<") }
28  '<=' { PT _ (TS "<=") }
29  '>' { PT _ (TS ">") }
30  '>=' { PT _ (TS ">=") }
31  '==' { PT _ (TS "==") }
32  '!=' { PT _ (TS "!=") }
33  '+' { PT _ (TS "+") }
34  '*' { PT _ (TS "*") }
35  '/' { PT _ (TS "/") }
36  'bool' { PT _ (TS "bool") }
37  'else' { PT _ (TS "else") }
38  'false' { PT _ (TS "false") }
39  'if' { PT _ (TS "if") }
40  'int' { PT _ (TS "int") }
41  'print' { PT _ (TS "print") }
42  'readBool' { PT _ (TS "readBool") }
43  'readInt' { PT _ (TS "readInt") }
44  'true' { PT _ (TS "true") }
45  'while' { PT _ (TS "while") }
46
47 L_ident  { PT _ (TV $$) }
48 L_integ  { PT _ (TI $$) }
49 L_err    { _ }
50
51
52 %%
53
54 Ident   :: { Ident }   : L_ident  { Ident $1 }
55 Integer :: { Integer } : L_integ  { (read $1) :: Integer }
56
57 Bool :: { Bool }
58 Bool : 'true' { True } 
59   | 'false' { False }
60
61
62 Type :: { Type }
63 Type : 'int' { TInt } 
64   | 'bool' { TBool }
65
66
67 Stms :: { Stms }
68 Stms : ListStm { Program (reverse $1) } 
69
70
71 Stm :: { Stm }
72 Stm : Exp ';' { SExp $1 } 
73   | '{' ListStm '}' { SBlock (reverse $2) }
74   | 'int' Ident '=' Exp ';' { declIntE_ $2 $4 }
75   | 'bool' Ident '=' Exp ';' { declBoolE_ $2 $4 }
76   | 'int' Ident ';' { declInt_ $2 }
77   | 'bool' Ident ';' { declBool_ $2 }
78   | 'while' '(' Exp ')' Stm { SWhile $3 $5 }
79   | 'if' '(' Exp ')' Stm 'else' Stm { SIf $3 $5 $7 }
80   | 'if' '(' Exp ')' Stm { if_ $3 $5 }
81   | 'print' Exp ';' { SPrint $2 }
82
83
84 Exp :: { Exp }
85 Exp : Ident '=' Exp { EAss $1 $3 } 
86   | Exp1 Op0 Exp1 { compExp_ $1 $2 $3 }
87   | Exp1 { $1 }
88
89
90 Exp1 :: { Exp }
91 Exp1 : Exp1 Op1 Exp2 { op1_ $1 $2 $3 } 
92   | Exp2 { $1 }
93
94
95 Exp2 :: { Exp }
96 Exp2 : Exp2 Op2 Exp3 { op2_ $1 $2 $3 } 
97   | Exp3 { $1 }
98
99
100 Exp3 :: { Exp }
101 Exp3 : Ident '++' { postIncr_ $1 } 
102   | Ident '--' { postDecr_ $1 }
103   | Ident { EVar $1 }
104   | Integer { EInt $1 }
105   | Bool { EBool $1 }
106   | '-' Exp3 { ENeg $2 }
107   | '!' Exp3 { ENot $2 }
108   | 'readInt' { EReadI }
109   | 'readBool' { EReadB }
110   | '(' Exp ')' { $2 }
111
112
113 ListStm :: { [Stm] }
114 ListStm : {- empty -} { [] } 
115   | ListStm Stm { flip (:) $1 $2 }
116
117
118 Op0 :: { Op }
119 Op0 : '<' { Lt } 
120   | '<=' { ELt }
121   | '>' { Gt }
122   | '>=' { EGt }
123   | '==' { Eq }
124   | '!=' { NEq }
125
126
127 Op1 :: { Op }
128 Op1 : '+' { Plus } 
129   | '-' { Minus }
130
131
132 Op2 :: { Op }
133 Op2 : '*' { Times } 
134   | '/' { Div }
135
136
137 Op :: { Op }
138 Op : Op1 { $1 } 
139   | Op2 { $1 }
140   | Op0 { $1 }
141
142
143
144 {
145
146 returnM :: a -> Err a
147 returnM = return
148
149 thenM :: Err a -> (a -> Err b) -> Err b
150 thenM = (>>=)
151
152 happyError :: [Token] -> Err a
153 happyError ts =
154   Bad $ "syntax error at " ++ tokenPos ts ++ if null ts then [] else (" before " ++ unwords (map prToken (take 4 ts)))
155
156 myLexer = tokens
157 declIntE_ x_ e_ = SDecl TInt x_ e_
158 declBoolE_ x_ e_ = SDecl TBool x_ e_
159 declInt_ x_ = SDecl TInt x_ (EInt 0)
160 declBool_ x_ = SDecl TBool x_ (EBool False)
161 if_ e_ s_ = SIf e_ s_ SNoop
162 compExp_ e1_ o_ e2_ = BiOpExp e1_ o_ e2_
163 op1_ e1_ o_ e2_ = BiOpExp e1_ o_ e2_
164 op2_ e1_ o_ e2_ = BiOpExp e1_ o_ e2_
165 postIncr_ i_ = EPost i_ Plus
166 postDecr_ i_ = EPost i_ Minus
167 }
168