%tokentype { Token }
%token
- '=' { PT _ (TS "=") }
';' { PT _ (TS ";") }
'{' { PT _ (TS "{") }
'}' { PT _ (TS "}") }
+ '=' { PT _ (TS "=") }
'(' { PT _ (TS "(") }
')' { PT _ (TS ")") }
'++' { PT _ (TS "++") }
| 'false' { False }
+Type :: { Type }
+Type : 'int' { TInt }
+ | 'bool' { TBool }
+
+
+Stms :: { Stms }
+Stms : ListStm { Program (reverse $1) }
+
+
Stm :: { Stm }
-Stm : Type Ident '=' Exp ';' { SDecl $1 $2 $4 }
- | Type Ident ';' { decl_ $1 $2 }
- | Exp ';' { SExp $1 }
+Stm : Exp ';' { SExp $1 }
| '{' ListStm '}' { SBlock (reverse $2) }
- | 'if' '(' Exp ')' Stm { if_ $3 $5 }
- | 'if' '(' Exp ')' Stm 'else' Stm { SIf $3 $5 $7 }
+ | Type Ident '=' Exp ';' { SDecl $1 $2 $4 }
+ | Type Ident ';' { SDeclD $1 $2 }
| 'while' '(' Exp ')' Stm { SWhile $3 $5 }
+ | 'if' '(' Exp ')' Stm 'else' Stm { SIf $3 $5 $7 }
+ | 'if' '(' Exp ')' Stm { if_ $3 $5 }
| 'print' Exp ';' { SPrint $2 }
| Ident '--' { postDecr_ $1 }
| Ident { EVar $1 }
| Integer { EInt $1 }
+ | Bool { EBool $1 }
| '-' Exp3 { ENeg $2 }
| '!' Exp3 { ENot $2 }
- | Bool { EBool $1 }
| 'readInt' { EReadI }
| 'readBool' { EReadB }
| '(' Exp ')' { $2 }
| ListStm Stm { flip (:) $1 $2 }
-Stms :: { Stms }
-Stms : ListStm { Program (reverse $1) }
-
-
Op0 :: { Op }
Op0 : '<' { Lt }
| '<=' { ELt }
| Op0 { $1 }
-Type :: { Type }
-Type : 'int' { TInt }
- | 'bool' { TBool }
-
-
{
Bad $ "syntax error at " ++ tokenPos ts ++ if null ts then [] else (" before " ++ unwords (map prToken (take 4 ts)))
myLexer = tokens
-decl_ t_ v_ = SDecl t_ v_ EDefault
if_ e_ s_ = SIf e_ s_ SNoop
compExp_ e1_ o_ e2_ = BiOpExp e1_ o_ e2_
op1_ e1_ o_ e2_ = BiOpExp e1_ o_ e2_