-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathParser.y
482 lines (389 loc) · 14.1 KB
/
Parser.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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
{
module Parser where
import Lexer
}
%name parser
%tokentype { Token }
%error { parseError }
%token
'number' { NumberToken $$ }
'integer' { IntegerToken $$ }
'identification' { IdentificationToken }
'data' { DataToken }
'procedure' { ProcedureToken }
'end' { EndToken}
'division' { DivisionToken }
'id' { IdToken }
'program' { ProgramToken }
'working' { WorkingToken }
'storage' { StorageToken }
'section' { SectionToken }
'pic' { PicToken }
's9' { NumericToken }
'a' { AlphaNumericToken }
'value' { ValueToken }
'exit' { ExitToken }
'move' { MoveToken }
'to' { ToToken }
'add' { AddToken }
'giving' { GivingToken }
'subtract' { SubtractToken }
'from' { FromToken }
'multiply' { MultiplyToken }
'by' { ByToken }
'divide' { DivideToken }
'into' { IntoToken }
'remainder' { RemainderToken }
'compute' { ComputeToken }
'if' { IfToken }
'then' { ThenToken }
'else' { ElseToken }
'perform' { PerformToken }
'until' { UntilToken }
'varying' { VaryingToken }
'function' { FunctionToken }
'display' { DisplayToken }
'or' { OrToken }
'and' { AndToken }
'not' { NotToken }
'equals' { EqualsToken }
'=>' { ArrowToken }
'<=' { LessOrEqualToken }
'<' { LessToken }
'>=' { GreaterEqualToken }
'>' { GreaterToken }
'+' { PlusToken }
'-' { MinusToken }
'*' { TimesToken }
'/' { DividesToken }
'.' { DotToken }
',' { CommaToken }
'=' { AssignToken }
'(' { LeftPToken }
')' { RightPToken }
'[' { LeftBToken }
']' { RightBToken }
'?' { QuestionToken }
':' { CollonToken }
'string' { StringToken $$ }
'var' { IdentifierToken $$ }
%%
Program
: IdentificationDivision DataDivision ProcedureDivision EndStatement { TypedProgram $1 $2 $3 $4 }
| IdentificationDivision ProcedureDivision EndStatement { NoneTypeProgram $1 $2 $3 }
IdentificationDivision
: 'identification' 'division' '.' 'program' '-' 'id' '.' 'var' { IdentificationDivision $8 }
DataDivision
: 'data' 'division' '.' DataSection { DataDivision $4 }
ProcedureDivision
: 'procedure' 'division' '.' ProcedureSection { ProcedureDivision $4 }
EndStatement
: 'end' 'program' '.' 'var' { HasEnd $4 }
| {- empty -} { NoEnd }
DataSection
: 'working' '-' 'storage' 'section' '.' DataDeclarations { DataSection $6 }
ProcedureSection
: Paragraph { SingleParagraph $1 }
| ProcedureSection Paragraph { MultiParagraph $1 $2 }
DataDeclarations
: DataDeclarations VariableDeclaration { Declarations $1 $2 }
| {- empty -} { NoDeclaration }
Paragraph
: 'var' '.' Statements { Paragraph $1 $3 }
VariableDeclaration
: 'integer' 'var' 'pic' DataType DefaultValue '.' { VarDeclaration $1 $2 $4 $5 }
DataType
: 'a' { AlphanumericString }
| 's9' { SignedNumber }
DefaultValue
: 'value' Expression { HasDefault $2 }
| {- empty -} { NoDefault }
Value
: 'string' { StringValue $1 }
| 'number' { NumberValue $1 }
| 'integer' { NumberValue (fromIntegral $1) }
Statements
: Statements Statement '.' { MultiStatement $1 $2 }
| Statement '.' { SingleStatement $1 }
Statement
: MoveStmt { MoveStatement $1 }
| ArithmeticStmt { ArithmeticStatement $1 }
| ComputeStmt { CompStatement $1 }
| IfStmt { IfStatement $1 }
| PerformStmt { PerformStatement $1 }
| FunctionStmt { DeclareFunction $1 }
| DisplayStmt { ShowStatement $1 }
| 'exit' { ExitStatement }
MoveStmt
: 'move' Expression 'to' Variables { Move $2 $4 }
ArithmeticStmt
: AddStmt { AddStatement $1 }
| SubStmt { SubtractStatement $1 }
| MultStmt { MultiplyStatement $1 }
| DivStmt { DivisionStatement $1 }
Variable
: 'var' { NormalVariable $1 }
| 'var' '[' Expression ']' { ListVariable $1 $3 }
AddStmt
: 'add' Expression 'to' Variable { SingleAdd $2 $4 }
| 'add' Expressions 'giving' Variable { MultiAdd $2 $4 }
SubStmt
: 'subtract' Expressions 'from' Variable { MultiSubtract $2 $4 }
| 'subtract' Expressions 'from' Expression 'giving' Variable { SingleSubtract $2 $4 $6 }
MultStmt
: 'multiply' Expression 'by' Variable { MultiplyTo $2 $4 }
| 'multiply' Expressions 'giving' Variable { MultiplyGiving $2 $4 }
DivStmt
: 'divide' Expression 'into' Variable { DivideInto $2 $4 }
| 'divide' Expression ',' Expression 'giving' Variable { DivideGiving $2 $4 $6 }
| 'divide' Expression 'into' Expression 'remainder' Variable { DivideRemainder $2 $4 $6 }
ComputeStmt
: 'compute' Variable '=' Expression { ComputeStatement $2 $4 }
IfStmt
: 'if' Disjunction 'then' Statements { IfOnlyStatement $2 $4 }
| 'if' Disjunction 'then' Statements 'else' Statements 'end' '-' 'if' { IfElseStatement $2 $4 $6 }
PerformStmt
: 'perform' Statements 'until' Disjunction { ConditionalPerform $4 $2 }
| 'perform' 'until' Disjunction Statements 'end' '-' 'perform' { ConditionalPerform $3 $4 }
| 'perform' 'varying' 'var' 'from' Expression 'to' Expression Step Statements 'end' '-' 'perform' { RangePerform $3 $5 $7 $8 $9 }
| 'perform' 'var' { ParagraphPerform $2 }
Step
: 'by' Expression { HasStep $2 }
| {- empty -} { HasNoStep }
FunctionStmt
: 'function' 'var' '=' Variables '=>' Expression { FunctionStatement $2 $4 $6 }
DisplayStmt
: 'display' Expression { DisplayStatement $2 }
Variables
: Variables ',' Variable { MultiVariables $1 $3 }
| Variable { SingleVariable $1 }
Expressions
: Expressions ',' Expression { MultiExpressions $1 $3 }
| Expression { SingleExpression $1 }
Disjunction
: Conjunction { HasNoOr $1 }
| Disjunction 'or' Conjunction { HasOr $1 $3 }
Conjunction
: Inversion { HasNoAnd $1 }
| Conjunction 'and' Inversion { HasAnd $1 $3 }
Inversion
: 'not' Inversion { HasInversion $2 }
| Comparison { HasNoInversion $1 }
Comparison
: Eq { EqualComparison $1 }
| NotEq { NotEqualComparison $1 }
| Lt { LessThanComparison $1 }
| Lte { LessThanOrEqualComparison $1 }
| Gt { GreaterThanComparison $1 }
| Gte { GreaterThanOrEqualComparison $1 }
Eq
: Expression 'equals' Expression { IsEq $1 $3 }
NotEq
: Expression 'not' 'equals' Expression { IsNotEq $1 $4 }
Lt
: Expression '<' Expression { IsLessThan $1 $3 }
Lte
: Expression '<=' Expression { IsLessThanOrEqual $1 $3 }
Gt
: Expression '>' Expression { IsGreaterThan $1 $3 }
Gte
: Expression '>=' Expression { IsGreaterThanOrEqual $1 $3 }
Expression
: '(' Disjunction ')' '?' Expression ':' Expression { ConditionalExpression $2 $5 $7 }
| Sum { EmptySum $1 }
Sum
: Sum '+' Term { AddSum $1 $3 }
| Sum '-' Term { SubtractSum $1 $3 }
| Term { EmptyTerm $1 }
Term
: Term '*' Factor { MultiplyTerm $1 $3 }
| Term '/' Factor { DivideTerm $1 $3 }
| Factor { EmptyFactor $1 }
Factor
: '+' Factor { PositiveFactor $2 }
| '-' Factor { NegativeFactor $2 }
| '(' Expression ')' { ParenthesesExpression $2 }
| Element { EmptyElement $1 }
Element
: Variable { VarElement $1 }
| 'var' '(' Expressions ')' { FunctionElement $1 $3 }
| Value { ValueElement $1 }
{
data Program
= TypedProgram IdentificationDivision DataDivision ProcedureDivision EndStatement
| NoneTypeProgram IdentificationDivision ProcedureDivision EndStatement
deriving (Show, Eq, Ord);
data IdentificationDivision
= IdentificationDivision String
deriving (Show, Eq, Ord);
data DataDivision
= DataDivision DataSection
deriving (Show, Eq, Ord);
data ProcedureDivision
= ProcedureDivision ProcedureSection
deriving (Show, Eq, Ord);
data EndStatement
= HasEnd String
| NoEnd
deriving (Show, Eq, Ord);
data DataSection
= DataSection DataDeclarations
deriving (Show, Eq, Ord);
data ProcedureSection
= SingleParagraph Paragraph
| MultiParagraph ProcedureSection Paragraph
deriving (Show, Eq, Ord);
data DataDeclarations
= Declarations DataDeclarations VariableDeclaration
| NoDeclaration
deriving (Show, Eq, Ord);
data Paragraph
= Paragraph String Statements
deriving (Show, Eq, Ord);
data VariableDeclaration
= VarDeclaration Integer String DataType DefaultValue
deriving (Show, Eq, Ord);
data DataType
= SignedNumber
| AlphanumericString
deriving (Show, Eq, Ord);
data DefaultValue
= HasDefault Expression
| NoDefault
deriving (Show, Eq, Ord);
data DataValue
= StringValue String
| NumberValue Float
deriving (Show, Eq, Ord);
data Statements
= SingleStatement Statement
| MultiStatement Statements Statement
deriving (Show, Eq, Ord);
data Statement
= MoveStatement MoveStatement
| ArithmeticStatement ArithmeticStatement
| CompStatement ComputeStatement
| IfStatement IfStatement
| PerformStatement PerformStatement
| DeclareFunction FunctionStatement
| ShowStatement DisplayStatement
| ExitStatement
deriving (Show, Eq, Ord);
data MoveStatement
= Move Expression Variables
deriving (Show, Eq, Ord);
data ArithmeticStatement
= AddStatement AddStatement
| SubtractStatement SubStatement
| MultiplyStatement MultStatement
| DivisionStatement DivStatement
deriving (Show, Eq, Ord);
data Variable
= NormalVariable String
| ListVariable String Expression
deriving (Show, Eq, Ord);
data AddStatement
= SingleAdd Expression Variable
| MultiAdd Expressions Variable
deriving (Show, Eq, Ord);
data SubStatement
= SingleSubtract Expressions Expression Variable
| MultiSubtract Expressions Variable
deriving (Show, Eq, Ord);
data MultStatement
= MultiplyTo Expression Variable
| MultiplyGiving Expressions Variable
deriving (Show, Eq, Ord);
data DivStatement
= DivideInto Expression Variable
| DivideGiving Expression Expression Variable
| DivideRemainder Expression Expression Variable
deriving (Show, Eq, Ord);
data ComputeStatement
= ComputeStatement Variable Expression
deriving (Show, Eq, Ord);
data IfStatement
= IfOnlyStatement Disjunction Statements
| IfElseStatement Disjunction Statements Statements
deriving (Show, Eq, Ord);
data PerformStatement
= ConditionalPerform Disjunction Statements
| RangePerform String Expression Expression Step Statements
| ParagraphPerform String
deriving (Show, Eq, Ord);
data Step
= HasStep Expression
| HasNoStep
deriving (Show, Eq, Ord);
data FunctionStatement
= FunctionStatement String Variables Expression
deriving (Show, Eq, Ord);
data DisplayStatement
= DisplayStatement Expression
deriving (Show, Eq, Ord);
data Variables
= SingleVariable Variable
| MultiVariables Variables Variable
deriving (Show, Eq, Ord);
data Expressions
= SingleExpression Expression
| MultiExpressions Expressions Expression
deriving (Show, Eq, Ord);
data Disjunction
= HasNoOr Conjunction
| HasOr Disjunction Conjunction
deriving (Show, Eq, Ord);
data Conjunction
= HasNoAnd Inversion
| HasAnd Conjunction Inversion
deriving (Show, Eq, Ord);
data Inversion
= HasNoInversion Comparison
| HasInversion Inversion
deriving (Show, Eq, Ord);
data Comparison
= EqualComparison Equality
| NotEqualComparison Equality
| LessThanComparison Unequality
| LessThanOrEqualComparison Unequality
| GreaterThanComparison Unequality
| GreaterThanOrEqualComparison Unequality
deriving (Show, Eq, Ord);
data Equality
= IsEq Expression Expression
| IsNotEq Expression Expression
deriving (Show, Eq, Ord);
data Unequality
= IsLessThan Expression Expression
| IsLessThanOrEqual Expression Expression
| IsGreaterThan Expression Expression
| IsGreaterThanOrEqual Expression Expression
deriving (Show, Eq, Ord);
data Expression
= ConditionalExpression Disjunction Expression Expression
| EmptySum Sum
deriving (Show, Eq, Ord);
data Sum
= AddSum Sum Term
| SubtractSum Sum Term
| EmptyTerm Term
deriving (Show, Eq, Ord);
data Term
= MultiplyTerm Term Factor
| DivideTerm Term Factor
| EmptyFactor Factor
deriving (Show, Eq, Ord);
data Factor
= PositiveFactor Factor
| NegativeFactor Factor
| ParenthesesExpression Expression
| EmptyElement Element
deriving (Show, Eq, Ord);
data Element
= VarElement Variable
| FunctionElement String Expressions
| ValueElement DataValue
deriving (Show, Eq, Ord);
parseError :: [Token] -> a;
parseError _ = error "Parse error";
}