-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmon_simple.py
More file actions
64 lines (47 loc) · 1.86 KB
/
Copy pathmon_simple.py
File metadata and controls
64 lines (47 loc) · 1.86 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
#!/usr/bin/env python3
"""
mon_simple.py -- Starter for the MON (Mission Object Notation) challenge.
Implement `canonicalize` below: parse a mission object from MON source
text and return its canonical form as a string. Raise MissionSyntaxError
(with a clear message) on any malformed input.
Full specification, grammar, examples, and error-message contract are in
README.md.
You may structure the implementation however you like -- tokenizer,
parser, single pass, whatever fits. There is no prescribed architecture.
The only hard constraints:
- no parser generators or parsing libraries (see README, "Constraints")
- the implementation must be written by hand
- `canonicalize(source: str) -> str` is the interface graders call;
keep its signature and behavior stable
Run it with:
python3 mon_simple.py input1.mon
"""
import sys
class MissionSyntaxError(Exception):
"""Raise with a clear message for any malformed MON input."""
def canonicalize(source):
"""
Parse `source` (MON text describing one mission object) and return its
canonical form as a string. See README.md, "Canonical Output".
Raise MissionSyntaxError on invalid input.
"""
# TODO: implement tokenizing, parsing, validation, and canonical
# formatting here, in whatever structure you prefer.
raise NotImplementedError("canonicalize is not implemented yet")
def main():
if len(sys.argv) != 2:
print("Usage: python3 mon_simple.py <file.mon>", file=sys.stderr)
sys.exit(1)
try:
with open(sys.argv[1], encoding="utf-8") as f:
source = f.read()
except OSError as exc:
print(f"Error: cannot read file: {exc}", file=sys.stderr)
sys.exit(1)
try:
print(canonicalize(source))
except MissionSyntaxError as exc:
print(f"Error: {exc}")
sys.exit(1)
if __name__ == "__main__":
main()