-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.fs
More file actions
65 lines (57 loc) · 1.55 KB
/
Copy pathparser.fs
File metadata and controls
65 lines (57 loc) · 1.55 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
//
// 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 string denoting
// success or failure.
//
// Returns: the string "success" if the input program is
// legal, otherwise the string "syntax_error: ..." is
// returned denoting an invalid simple C program.
//
// <<YOUR NAME>>
//
// Original author:
// Prof. Joe Hummel
// U. of Illinois, Chicago
// CS 341, Spring 2022
//
namespace compiler
module parser =
//
// NOTE: all functions in the module must be indented.
//
//
// matchToken
//
let private matchToken expected_token tokens =
//
// if the next 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
// at the first error.
//
let next_token = List.head tokens
if expected_token = next_token then
List.tail tokens
else
failwith ("expecting " + expected_token + ", but found " + next_token)
//
// simpleC
//
let private simpleC tokens =
matchToken "$" tokens
//
// parse tokens
//
// Given a list of tokens, parses the list and determines
// if the list represents a valid simple C program. Returns
// the string "success" if valid, otherwise returns a
// string of the form "syntax_error:...".
//
let parse tokens =
try
let result = simpleC tokens
"success"
with
| ex -> "syntax_error: " + ex.Message