-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.fs
359 lines (306 loc) · 11.4 KB
/
parser.fs
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
//
// Parser for simple C programs. This component checks
// the input program to see if it meets the syntax rules
// of simple C. The parser returns a tuple containing
// 3 values:
//
// (result, msg, program)
//
// where result is true or false (legal or not legal),
// msg is a success or syntax error message, and program
// is a list of instructions if parsing was successful.
//
// Mohmed Hira
// U. of Illinois, Chicago
// CS 341, Spring 2019
// Project #05
//
#light
namespace compiler
module parser =
//
// NOTE: all functions in the module must be indented.
//
//
// These are debug routines that output the tokens, or
// program, respectively. They are written so you can
// inject these into a pipeline to output the current
// state of the tokens or program.
//
let private __outputTokens (tokens, program) =
printfn "Tokens: %A" tokens
(tokens, program)
let private __outputProgram (tokens, program) =
printfn "Program: %A" program
(tokens, program)
//
// matchToken
//
let private matchToken expected_token (tokens, program) =
let (token, _) = List.head tokens
//
// if the token matches the expected token, keep parsing by
// returning the rest of the tokens. Otherwise throw an
// exception because there's a syntax error, effectively
// stopping compilation:
//
if expected_token = token then
(List.tail tokens, program)
else
failwith ("expecting " + (string expected_token) + ", but found " + (string token))
let rec checkForOp x L =
match L with
| [] -> 0
| hd::tl -> if hd = x then 1 + checkForOp x tl else 0 + checkForOp x tl
let expr_op (tokens,program) =
let(nextToken,p) = List.head tokens
if nextToken = lexer.Tokens.EQ then
let(T1,P1) = matchToken lexer.Tokens.EQ (tokens,program)
(T1,P1)
else if nextToken = lexer.Tokens.Minus then
let(T1,P1) = matchToken lexer.Tokens.Minus (tokens,program)
(T1,P1)
else if nextToken = lexer.Tokens.Plus then
let(T1,P1) = matchToken lexer.Tokens.Plus (tokens,program)
(T1,P1)
else if nextToken = lexer.Tokens.Times then
let(T1,P1) = matchToken lexer.Tokens.Times (tokens,program)
(T1,P1)
else if nextToken = lexer.Tokens.Divide then
let(T1,P1) = matchToken lexer.Tokens.Divide (tokens,program)
(T1,P1)
else if nextToken = lexer.Tokens.LT then
let(T1,P1) = matchToken lexer.Tokens.LT (tokens,program)
(T1,P1)
else if nextToken = lexer.Tokens.LTE then
let(T1,P1) = matchToken lexer.Tokens.LTE (tokens,program)
(T1,P1)
else if nextToken = lexer.Tokens.GT then
let(T1,P1) = matchToken lexer.Tokens.GT (tokens,program)
(T1,P1)
else if nextToken = lexer.Tokens.GTE then
let(T1,P1) = matchToken lexer.Tokens.GTE (tokens,program)
(T1,P1)
else if nextToken = lexer.Tokens.NE then
let(T1,P1) = matchToken lexer.Tokens.NE (tokens,program)
(T1,P1)
else if nextToken = lexer.Tokens.Power then
let(T1,P1) = matchToken lexer.Tokens.Power (tokens,program)
(T1,P1)
else
failwith ("Invalid Token")
let expr_value (tokens,program) =
let(nextToken,_) = List.head tokens
if nextToken = lexer.Tokens.ID then
let (T1,P1) = matchToken lexer.Tokens.ID (tokens,program)
(T1,P1)
else if nextToken = lexer.Tokens.Int_Literal then
let (T1,P1) = matchToken lexer.Tokens.Int_Literal (tokens,program)
(T1,P1)
else if nextToken = lexer.Tokens.Str_Literal then
let (T1,P1) = matchToken lexer.Tokens.Str_Literal (tokens,program)
(T1,P1)
else if nextToken = lexer.Tokens.Bool_Literal then
let (T1,P1) = matchToken lexer.Tokens.Bool_Literal (tokens,program)
(T1,P1)
else
failwith ("expecting identifier or literal but found " + (string nextToken))
let expr (tokens,program) =
let(T1,P1) = expr_value(tokens,program)
let(nextToken,_) = List.head (List.tail tokens)
let a = int nextToken
let n = checkForOp a ([7..17]@[25])
if n = 1 then
let(T2,P2) = expr_op (T1,P1)
let(T3,P3) = expr_value (T2,P2)
(T3,P3)
else
(T1,P1)
let vardecl (tokens,program) =
let(nextToken,nextProgram) = List.head tokens
if nextToken = lexer.Tokens.ID then
let(T1,P1) = matchToken lexer.Tokens.ID (tokens,["$DECL"; nextProgram;]::program)
let(T2,P2) = matchToken lexer.Tokens.Semicolon (T1,P1)
(T2,P2)
else
failwith ("Invalid vardecl")
let input (tokens,program) =
let(nextToken,nextProgram) = List.head tokens
let(pp,p) = List.head (List.tail tokens)
if nextToken = lexer.Tokens.Input then
let(T1,P1) = matchToken lexer.Tokens.Input (tokens,program)
let(T2,P2) = matchToken lexer.Tokens.ID (T1,["$INPUT"; p]::P1)
let(T3,P3) = matchToken lexer.Tokens.Semicolon (T2,P2)
(T3,P3)
else
failwith ("Invalid Input")
let output_value (tokens,program) =
let(nextToken,_) = List.head tokens
if nextToken = lexer.Tokens.Endl then
let(T1,P1) = matchToken lexer.Tokens.Endl (tokens,program)
(T1,P1)
else if nextToken = lexer.Tokens.ID then
let(T1,P1) = expr_value (tokens,program)
(T1,P1)
else if nextToken = lexer.Tokens.Str_Literal then
let(T1,P1) = expr_value (tokens,program)
(T1,P1)
else if nextToken = lexer.Tokens.Bool_Literal then
let(T1,P1) = expr_value (tokens,program)
(T1,P1)
else if nextToken = lexer.Tokens.Int_Literal then
let(T1,P1) = expr_value (tokens,program)
(T1,P1)
else
failwith ("expecting identifier or literal or endl but found " + (string nextToken))
let output (tokens,program) =
let(nextToken,_) = List.head tokens
let (t,t1) = List.head(List.tail tokens)
if nextToken = lexer.Tokens.Output then
let(T1,P1) = matchToken lexer.Tokens.Output (tokens,program)
let(T2,P2) = output_value (T1,["$OUTPUT";(string t) ;t1]::P1)
let(T3,P3) = matchToken lexer.Tokens.Semicolon (T2,P2)
(T3,P3)
else
failwith ("Invalid Output")
let assignment b (tokens,program) =
let(nextToken,_) = List.head tokens
let (t,t1) = List.head(List.tail tokens)
let (t2,t3) = List.head(List.tail (List.tail tokens) )
let a = int t2
let n = checkForOp a ([7..17]@[25])
if n = 0 then
let l = ["$ASSIGN";b;(string t);t1;]
if nextToken = lexer.Tokens.Assign then
let(T1,P1) = matchToken lexer.Tokens.Assign (tokens,program)
let(T2,P2) = expr (T1,P1)
let(T3,P3) = matchToken lexer.Tokens.Semicolon (T2,l::P2)
(T3,P3)
else
failwith ("Invalid assignment")
else
let (t4,t5) = List.head(List.tail (List.tail (List.tail tokens) ) )
let l = ["$ASSIGN"; b;(string t);t1;t3;(string t4);t5]
if nextToken = lexer.Tokens.Assign then
let(T1,P1) = matchToken lexer.Tokens.Assign (tokens,program)
let(T2,P2) = expr (T1,P1)
let(T3,P3) = matchToken lexer.Tokens.Semicolon (T2,l::P2)
(T3,P3)
else
failwith ("Invalid assignment")
//START Of the Statemnts
let rec stmt (tokens, program) =
let(nextToken,test) = List.head tokens
if nextToken = lexer.Tokens.If then
let (T1,P1) = matchToken lexer.Tokens.If (tokens,program)
let (T2,P2) = ifstmt (T1,P1)
(T2,P2)
else if nextToken = lexer.Tokens.Cout then
let (T1,P1) = matchToken lexer.Tokens.Cout (tokens,program)
let (T2,P2) = output (T1,P1)
(T2,P2)
else if nextToken = lexer.Tokens.Semicolon then
let (T1,P1) = matchToken lexer.Tokens.Semicolon (tokens,["$EMPTY"]::program)
(T1,P1)
else if nextToken = lexer.Tokens.Int then
let (T1,P1) = matchToken lexer.Tokens.Int (tokens,program)
let (T2,P2) = vardecl (T1,P1)
(T2,P2)
else if nextToken = lexer.Tokens.ID then
let (T1,P1) = matchToken lexer.Tokens.ID (tokens,program)
let (T2,P2) = assignment test (T1,P1)
(T2,P2)
else if nextToken = lexer.Tokens.Cin then
let (T1,P1) = matchToken lexer.Tokens.Cin (tokens,program)
let (T2,P2) = input (T1,P1)
(T2,P2)
else
failwith("expected statement, but found " + (string tokens))
and private condition (tokens,program) =
let(T1,P1) = expr(tokens,program)
(T1,P1)
and private then_part (tokens,program) =
let(T1,P1) = stmt (tokens,program)
(T1,P1)
and private else_part (tokens,program) =
let(nextToken,_) = List.head tokens
if nextToken = lexer.Tokens.Else then
let (T1,P1) = matchToken lexer.Tokens.Else (tokens,program)
let (T2,P2) = stmt (T1,P1)
(T2,P2)
else
(tokens, ["$EMPTY"]::program)
and private ifstmt (tokens,program) =
let(nextToken,_) = List.head tokens
let (t,t1) = List.head(List.tail tokens)
let (t2,t3) = List.head(List.tail (List.tail tokens) )
let (t4,t5) = List.head(List.tail (List.tail (List.tail tokens) ) )
//Match Open Paren
let (T1,P1) = matchToken lexer.Tokens.OpenParen (tokens,program)
//Match Condition
let (T2,P2) = condition (T1,P1)
if(t3 = ")") then
let (T3,P3) = matchToken lexer.Tokens.CloseParen (T2,["$IF";(string t); t1;]::P2)
let (T4,P4) = then_part (T3,P3)
let (T5,P5) = else_part (T4,P4)
(T5,P5)
else
//Match Close Paren
let (T3,P3) = matchToken lexer.Tokens.CloseParen (T2,["$IF";(string t); t1; t3;(string t4); t5 ]::P2)
//Match Then Part
let (T4,P4) = then_part (T3,P3)
//Match Else Part
let (T5,P5) = else_part (T4,P4)
(T5,P5)
let rec morestmts (tokens,program) =
let(nextToken,_) = List.head tokens
if nextToken = lexer.Tokens.If then
morestmts(stmt (tokens,program))
else if nextToken = lexer.Tokens.Cout then
morestmts(stmt (tokens,program))
else if nextToken = lexer.Tokens.Int then
morestmts(stmt (tokens,program))
else if nextToken = lexer.Tokens.Cin then
morestmts(stmt (tokens,program))
else if nextToken = lexer.Tokens.ID then
morestmts(stmt (tokens,program))
else if nextToken = lexer.Tokens.Semicolon then
morestmts(stmt (tokens,program))
else
(tokens,program)
let rec stmts (tokens,program) =
let (T1,P1) = stmt (tokens, program)
let (T2,P2) = morestmts(T1,P1)
(T2,P2)
//
// simpleC
//
let private simpleC (tokens, program) =
let (T1, P1) = matchToken lexer.Tokens.Void (tokens, program)
let (T2, P2) = matchToken lexer.Tokens.Main (T1, P1)
let (T3, P3) = matchToken lexer.Tokens.OpenParen (T2, P2)
let (T4, P4) = matchToken lexer.Tokens.CloseParen (T3, P3)
let (T5, P5) = matchToken lexer.Tokens.OpenBrace (T4, P4)
let (T6, P6) = stmts (T5,P5)
let (T7, P7) = matchToken lexer.Tokens.CloseBrace (T6, P6)
let (T8, P8) = matchToken lexer.Tokens.EOF (T7, P7)
(T8, P8)
//
// parse tokens
//
// Given a list of tokens, parses the list and determines
// if the list represents a valid simple C program. Returns
// a tuple containing 3 values:
//
// (result, msg, program)
//
// where result is true or false (legal or not legal),
// msg is a success or syntax error message, and program
// is a list of instructions if parsing was successful.
//
let parse tokens =
try
let (_, program) = simpleC (tokens, [])
(true, "success", List.rev program)
with
| ex -> (false, ex.Message, [])