Hi:
Having completed enough serial driver code for a TMS320F2812 microcontroller to talk to a terminal, I am now trying different approaches to command interpretation.
I have a very simple command set consisting of several single letter commands which take no arguments. A few additional single letter commands take arguments:
Command language (somewhat generalized):
command argument(s) description
a do action_a b do action_b c do action_c ... n do action_n ... y [var_name] display variable(s) z var_name value set variable to value
where var_name is a variable identifier. The 'd' command lets one display the value of a variable identified by var_name. If no var_name is provide, this command will display all variables. The 'e' command lets one set the value of the variable identified by var_name to value.
Presently I am using the simplest yet least flexible approach to parse commands. I scan and lexically analyze on the fly, and use if, else if, else logic blocks to parse. (I know, pathetic.) This approach requires one if, else if, else block for each field of the command language.
Next I plan to split the lexical analysis to a first-pass, and create a list of tokens, with additional processing such as converting keywords and variable names to keys, and numeric text fields to machine numbers. This will eliminate the scanning/lexing on the fly to make the parsing code much more readable.
Then a second pass of parsing could still be done using if, else if, else it seems in the case that a language has a fixed and relatively small maximum number of fields per command.
The question is then, for a language similar to the above and considering these factors:
- need to implement in an embedded microcontroller in not more than a few K of program memory
- not a particularly high speed of parsing is required. Ccommands will be issued intermittently over a slow interface of perhaps 115.2kbps.
- ease of understanding and adapting the code to changes in the command set is very valuable. Most likely additions would be a command to tell the thing to accept a binary data stream representing a file/table to be stored in the uC RAM.
Is it common to hardcode parsing with if, else if, else blocks or at what point does one decide to use more generalized techniques such as formalizing the language description in BNR form, and using lexer/parser generation tools such as lex/yacc ?
I have always found the topic of parsers/interpreters/compilers fascinating, and would like to spend considerable time learning more about this. Yet I also have to "just get the thing to work." I feel like I will ultimately need to perform quite a few experiments with different parsing techniques to get a feel for the best method to choose for this and future applications.
Thanks for input.