-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathParDeclaration.y
More file actions
185 lines (173 loc) · 5.84 KB
/
ParDeclaration.y
File metadata and controls
185 lines (173 loc) · 5.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
-- This Happy file was machine-generated by the BNF converter
{
{-# OPTIONS_GHC -fno-warn-incomplete-patterns -fno-warn-overlapping-patterns #-}
module ParDeclaration where
import AbsDeclaration
import LexDeclaration
import ErrM
}
%name pProgram Program
-- no lexer declaration
%monad { Err } { thenM } { returnM }
%tokentype {Token}
%token
'!' { PT _ (TS _ 1) }
'!=' { PT _ (TS _ 2) }
'%' { PT _ (TS _ 3) }
'&&' { PT _ (TS _ 4) }
'(' { PT _ (TS _ 5) }
')' { PT _ (TS _ 6) }
'*' { PT _ (TS _ 7) }
'+' { PT _ (TS _ 8) }
'++' { PT _ (TS _ 9) }
',' { PT _ (TS _ 10) }
'-' { PT _ (TS _ 11) }
'--' { PT _ (TS _ 12) }
'->' { PT _ (TS _ 13) }
'/' { PT _ (TS _ 14) }
':' { PT _ (TS _ 15) }
';' { PT _ (TS _ 16) }
'<' { PT _ (TS _ 17) }
'<=' { PT _ (TS _ 18) }
'=' { PT _ (TS _ 19) }
'==' { PT _ (TS _ 20) }
'>' { PT _ (TS _ 21) }
'>=' { PT _ (TS _ 22) }
'boolean' { PT _ (TS _ 23) }
'break' { PT _ (TS _ 24) }
'continue' { PT _ (TS _ 25) }
'else' { PT _ (TS _ 26) }
'false' { PT _ (TS _ 27) }
'final' { PT _ (TS _ 28) }
'for' { PT _ (TS _ 29) }
'fun' { PT _ (TS _ 30) }
'if' { PT _ (TS _ 31) }
'int' { PT _ (TS _ 32) }
'lambda' { PT _ (TS _ 33) }
'print' { PT _ (TS _ 34) }
'return' { PT _ (TS _ 35) }
'string' { PT _ (TS _ 36) }
'to' { PT _ (TS _ 37) }
'true' { PT _ (TS _ 38) }
'void' { PT _ (TS _ 39) }
'while' { PT _ (TS _ 40) }
'{' { PT _ (TS _ 41) }
'||' { PT _ (TS _ 42) }
'}' { PT _ (TS _ 43) }
L_ident { PT _ (TV $$) }
L_integ { PT _ (TI $$) }
L_quoted { PT _ (TL $$) }
%%
Ident :: { Ident } : L_ident { Ident $1 }
Integer :: { Integer } : L_integ { (read ( $1)) :: Integer }
String :: { String } : L_quoted { $1 }
Program :: { Program }
Program : ListTopDef { AbsDeclaration.Program $1 }
TopDef :: { TopDef }
TopDef : Type Ident '(' ListArg ')' Block { AbsDeclaration.FnDef $1 $2 $4 $6 }
| Type ListItem ';' { AbsDeclaration.GlobDecl $1 $2 }
| 'final' Type ListItem ';' { AbsDeclaration.GlobFinDecl $2 $3 }
ListTopDef :: { [TopDef] }
ListTopDef : TopDef { (:[]) $1 } | TopDef ListTopDef { (:) $1 $2 }
Arg :: { Arg }
Arg : Type Ident { AbsDeclaration.Arg $1 $2 }
ListArg :: { [Arg] }
ListArg : {- empty -} { [] }
| Arg { (:[]) $1 }
| Arg ',' ListArg { (:) $1 $3 }
Block :: { Block }
Block : '{' ListStmt '}' { AbsDeclaration.Block (reverse $2) }
ListStmt :: { [Stmt] }
ListStmt : {- empty -} { [] } | ListStmt Stmt { flip (:) $1 $2 }
Stmt :: { Stmt }
Stmt : ';' { AbsDeclaration.Empty }
| Block { AbsDeclaration.BStmt $1 }
| Type ListItem ';' { AbsDeclaration.Decl $1 $2 }
| 'final' Type ListItem ';' { AbsDeclaration.DeclFinal $2 $3 }
| Ident '=' Expr ';' { AbsDeclaration.Ass $1 $3 }
| Ident '++' ';' { AbsDeclaration.Incr $1 }
| Ident '--' ';' { AbsDeclaration.Decr $1 }
| 'return' Expr ';' { AbsDeclaration.Ret $2 }
| 'return' ';' { AbsDeclaration.VRet }
| 'if' '(' Expr ')' Stmt { AbsDeclaration.Cond $3 $5 }
| 'if' '(' Expr ')' Stmt 'else' Stmt { AbsDeclaration.CondElse $3 $5 $7 }
| 'while' '(' Expr ')' Stmt { AbsDeclaration.While $3 $5 }
| Expr ';' { AbsDeclaration.SExp $1 }
| Type Ident '(' ListArg ')' Block { AbsDeclaration.FnInDef $1 $2 $4 $6 }
| 'for' '(' Type Ident '=' Expr 'to' Expr ')' Stmt { AbsDeclaration.ConstFor $3 $4 $6 $8 $10 }
| 'break' ';' { AbsDeclaration.Break }
| 'continue' ';' { AbsDeclaration.Continue }
| 'print' '(' Expr ')' ';' { AbsDeclaration.Print $3 }
Item :: { Item }
Item : Ident { AbsDeclaration.NoInit $1 }
| Ident '=' Expr { AbsDeclaration.Init $1 $3 }
ListItem :: { [Item] }
ListItem : Item { (:[]) $1 } | Item ',' ListItem { (:) $1 $3 }
Type :: { Type }
Type : 'int' { AbsDeclaration.Int }
| 'string' { AbsDeclaration.Str }
| 'boolean' { AbsDeclaration.Bool }
| 'void' { AbsDeclaration.Void }
| 'fun' '(' ListType '->' Type ')' { AbsDeclaration.FuncType $3 $5 }
ListType :: { [Type] }
ListType : {- empty -} { [] }
| Type { (:[]) $1 }
| Type ',' ListType { (:) $1 $3 }
Expr6 :: { Expr }
Expr6 : Ident { AbsDeclaration.EVar $1 }
| Integer { AbsDeclaration.ELitInt $1 }
| 'true' { AbsDeclaration.ELitTrue }
| 'false' { AbsDeclaration.ELitFalse }
| Ident '(' ListExpr ')' { AbsDeclaration.EApp $1 $3 }
| String { AbsDeclaration.EString $1 }
| 'lambda' '(' ListArg ')' ':' Type Block { AbsDeclaration.ELambda $3 $6 $7 }
| '(' Expr ')' { $2 }
Expr5 :: { Expr }
Expr5 : '-' Expr6 { AbsDeclaration.Neg $2 }
| '!' Expr6 { AbsDeclaration.Not $2 }
| Expr6 { $1 }
Expr4 :: { Expr }
Expr4 : Expr4 MulOp Expr5 { AbsDeclaration.EMul $1 $2 $3 }
| Expr5 { $1 }
Expr3 :: { Expr }
Expr3 : Expr3 AddOp Expr4 { AbsDeclaration.EAdd $1 $2 $3 }
| Expr4 { $1 }
Expr2 :: { Expr }
Expr2 : Expr2 RelOp Expr3 { AbsDeclaration.ERel $1 $2 $3 }
| Expr3 { $1 }
Expr1 :: { Expr }
Expr1 : Expr2 '&&' Expr1 { AbsDeclaration.EAnd $1 $3 }
| Expr2 { $1 }
Expr :: { Expr }
Expr : Expr1 '||' Expr { AbsDeclaration.EOr $1 $3 } | Expr1 { $1 }
ListExpr :: { [Expr] }
ListExpr : {- empty -} { [] }
| Expr { (:[]) $1 }
| Expr ',' ListExpr { (:) $1 $3 }
AddOp :: { AddOp }
AddOp : '+' { AbsDeclaration.Plus } | '-' { AbsDeclaration.Minus }
MulOp :: { MulOp }
MulOp : '*' { AbsDeclaration.Times }
| '/' { AbsDeclaration.Div }
| '%' { AbsDeclaration.Mod }
RelOp :: { RelOp }
RelOp : '<' { AbsDeclaration.LTH }
| '<=' { AbsDeclaration.LE }
| '>' { AbsDeclaration.GTH }
| '>=' { AbsDeclaration.GE }
| '==' { AbsDeclaration.EQU }
| '!=' { AbsDeclaration.NE }
{
returnM :: a -> Err a
returnM = return
thenM :: Err a -> (a -> Err b) -> Err b
thenM = (>>=)
happyError :: [Token] -> Err a
happyError ts =
Bad $ "syntax error at " ++ tokenPos ts ++
case ts of
[] -> []
[Err _] -> " due to lexer error"
t:_ -> " before `" ++ id(prToken t) ++ "'"
myLexer = tokens
}