The Final Piece: Command Parser
Last modified
Command Parser
- read a line
- split the line
- use first word in line to route to a particular API function, passing remaining words as arguments
Design Rule Discussion
- Rule of Modularity: Write simple parts connected by clean interfaces.
- Rule of Composition: Design programs to be connected with other programs.
-
Rule of Representation: Fold knowledge into data, so program logic can be stupid and robust.
- What would we need to do if we wanted to handle more path directions (e.g. ‘northeast’, or ‘up’ and ‘down’?)
- There is redundant information in our data file: we must specify that Room A leads to Room B in the north and that Room B leads to Room A in the south. Is this a problem? If we wanted to be more DRY, what could we do?
- Could we follow a filter interface pattern like
bc
(implement an interactive mode and a batch mode)
More Complex Parsing
What if you wanted to handle commands such as
> throw ball at window
> fight monster with magic sward
> drop 5 candles
- regular expressions
- flex/bison (and Python’s C bindings)