-
Notifications
You must be signed in to change notification settings - Fork 0
/
Parshl.y
253 lines (190 loc) · 5.24 KB
/
Parshl.y
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
-- This Happy file was machine-generated by the BNF converter
{
{-# OPTIONS_GHC -fno-warn-incomplete-patterns -fno-warn-overlapping-patterns #-}
module Parshl where
import Absshl
import Lexshl
import ErrM
}
%name pProg Prog
%name pBlk Blk
%name pStm Stm
%name pListStm ListStm
%name pAssM AssM
%name pAssE AssE
%name pListAssM ListAssM
%name pListExp ListExp
%name pDec Dec
%name pFArg FArg
%name pListFArg ListFArg
%name pListDec ListDec
%name pTyp Typ
%name pExp Exp
%name pExp2 Exp2
%name pExp3 Exp3
%name pExp4 Exp4
%name pExp5 Exp5
%name pExp1 Exp1
%name pExp6 Exp6
%name pConstraint Constraint
%name pBoolT BoolT
%name pIParam IParam
%name pListIParam ListIParam
-- 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) }
'Boolean' { PT _ (TS _ 19) }
'CYA' { PT _ (TS _ 20) }
'DO' { PT _ (TS _ 21) }
'DONE' { PT _ (TS _ 22) }
'ELSE' { PT _ (TS _ 23) }
'FI' { PT _ (TS _ 24) }
'FOR' { PT _ (TS _ 25) }
'False' { PT _ (TS _ 26) }
'IF' { PT _ (TS _ 27) }
'IN' { PT _ (TS _ 28) }
'Integer' { PT _ (TS _ 29) }
'LAMBDA' { PT _ (TS _ 30) }
'PRINT' { PT _ (TS _ 31) }
'REF' { PT _ (TS _ 32) }
'RETURN' { PT _ (TS _ 33) }
'RETURNED' { PT _ (TS _ 34) }
'SOLUTION' { PT _ (TS _ 35) }
'String' { PT _ (TS _ 36) }
'THEN' { PT _ (TS _ 37) }
'True' { PT _ (TS _ 38) }
'[' { PT _ (TS _ 39) }
'[]' { PT _ (TS _ 40) }
']' { PT _ (TS _ 41) }
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 }
Prog :: { Prog }
Prog : 'SOLUTION' Blk 'CYA' { Program $2 }
Blk :: { Blk }
Blk : ListDec ListStm { Block (reverse $1) (reverse $2) }
Stm :: { Stm }
Stm : 'FOR' Ident 'IN' Exp 'DO' Blk 'DONE' { ForLoop $2 $4 $6 }
| 'IF' Exp 'THEN' Blk 'FI' { IfStmt $2 $4 }
| 'IF' Exp 'THEN' Blk 'ELSE' Blk 'FI' { IfElseStmt $2 $4 $6 }
| 'RETURN' Exp ';' { ReturnStmt $2 }
| 'PRINT' Exp ';' { PrintStmt $2 }
| Exp ';' { ExpStmt $1 }
| ListAssM '=' ListExp ';' { AssignMultiple $1 $3 }
ListStm :: { [Stm] }
ListStm : {- empty -} { [] }
| ListStm Stm { flip (:) $1 $2 }
AssM :: { AssM }
AssM : Ident { AssignVar $1 }
| Ident '[' Exp ']' { AssignArr $1 $3 }
AssE :: { AssE }
AssE : Exp { AssignExp $1 }
ListAssM :: { [AssM] }
ListAssM : AssM { (:[]) $1 }
| AssM ',' ListAssM { (:) $1 $3 }
ListExp :: { [Exp] }
ListExp : Exp { (:[]) $1 }
| Exp ',' ListExp { (:) $1 $3 }
Dec :: { Dec }
Dec : Typ Ident ';' { Declaration $1 $2 }
| Typ Ident '=' Exp ';' { DeclarationAssing $1 $2 $4 }
| Typ Ident '(' ListFArg ')' 'DO' Blk 'RETURNED' { DeclarationFunc $1 $2 $4 $7 }
| Typ Ident '[' Exp ']' ';' { DeclarationArray $1 $2 $4 }
FArg :: { FArg }
FArg : Typ Ident { FArgument $1 $2 }
| Typ Ident '=' Exp { FArgumentAssing $1 $2 $4 }
| Typ Ident '(' ListFArg ')' { FArgumentFunc $1 $2 $4 }
| 'REF' Typ Ident { FArgumentRef $2 $3 }
| Typ Ident '[]' { FArgumentArr $1 $2 }
ListFArg :: { [FArg] }
ListFArg : {- empty -} { [] }
| FArg { (:[]) $1 }
| FArg ',' ListFArg { (:) $1 $3 }
ListDec :: { [Dec] }
ListDec : {- empty -} { [] }
| ListDec Dec { flip (:) $1 $2 }
Typ :: { Typ }
Typ : 'Integer' { TInt }
| 'Boolean' { TBool }
| 'String' { TString }
Exp :: { Exp }
Exp : Exp '==' Exp2 { Eeq $1 $3 }
| Exp '!=' Exp2 { Eneq $1 $3 }
| Exp1 { $1 }
Exp2 :: { Exp }
Exp2 : Exp2 '<' Exp3 { Elthen $1 $3 }
| Exp2 '>' Exp3 { Egrthen $1 $3 }
| Exp2 '<=' Exp3 { Ele $1 $3 }
| Exp2 '>=' Exp3 { Ege $1 $3 }
| Exp3 { $1 }
Exp3 :: { Exp }
Exp3 : Exp3 '+' Exp4 { Eplus $1 $3 }
| Exp3 '-' Exp4 { Eminus $1 $3 }
| Exp4 { $1 }
Exp4 :: { Exp }
Exp4 : Exp4 '*' Exp5 { Etimes $1 $3 }
| Exp4 '/' Exp5 { Ediv $1 $3 }
| Exp5 { $1 }
Exp5 :: { Exp }
Exp5 : Ident '++' { Einc $1 }
| Ident '--' { Edec $1 }
| Ident '(' ListIParam ')' { Einvok $1 $3 }
| Ident { Evar $1 }
| Constraint { Econst $1 }
| 'LAMBDA' ListFArg ':' Exp { Elmb $2 $4 }
| Ident '[' Exp ']' { Earr $1 $3 }
| Exp6 { $1 }
Exp1 :: { Exp }
Exp1 : Exp2 { $1 }
Exp6 :: { Exp }
Exp6 : '(' Exp ')' { $2 }
Constraint :: { Constraint }
Constraint : Integer { Eint $1 }
| BoolT { Ebool $1 }
| String { Estring $1 }
BoolT :: { BoolT }
BoolT : 'True' { Constraint_True }
| 'False' { Constraint_False }
IParam :: { IParam }
IParam : Exp { InvokeParamater $1 }
ListIParam :: { [IParam] }
ListIParam : {- empty -} { [] }
| IParam { (:[]) $1 }
| IParam ',' ListIParam { (:) $1 $3 }
{
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"
_ -> " before " ++ unwords (map (id . prToken) (take 4 ts))
myLexer = tokens
}