I’ve looked through codeshare for a command parser that would do what I need to achieve and don’t think I see any that will do what I want. I have very little (actually no ) experience writing command parsers.
I basically need it to read in a command with 1 or more optional parms to build the actual call to the correct module in my code.
Here is a sample command
MOVE [*ABSOLUTE|RELATIVE] TO [+|-] NNNNNNN.NN IN [*STEPS|MICRONS|INCHES|MILLIMETERS] [[WITH|*WITHOUT] COMPENSATION] AT [*SPEED *SLOW|MEDIUM|FAST|NNN]
* are defaults for the command
Some sample commands would be
• MOVE TO 5000 // uses defaults ABSOLUTE,STEPS,WITHOUT COMPENSATION and SPEED SLOW
• MOVE ABSOLUTE TO 5000 IN STEPS WITH COMPENSATION AT SPEED 10 // MOVES to positio 5000 steps with compensation on at 10 step per steps
• MOVE RELATIVE TO -254 IN MICRONS WITHOUT COMPENSATION // MOVES -254 MICRONS from the current position without compensation at a slow speed
This would result in call to move( struct move_parms ) or similar call
I can use a massive number of if/then/else or case statements but that can get messy and this is only one of the commands I’ll be coding.
So I am looking at some kind of command parser.
I’ve looked at various ones out there including some command parse generators like Irony but the language is very new to me and not yet sure how to implement it.
So I am looking for something simple that allows me to build command structure with about 20 commands and even more parms
What you’re talking about is called a “DSL” or Domain Specific Language. Essentially, you’re creating a programming language.
Your best bet for parsing one of these, depending on how complex, is either a simple series of string.Split() calls, or a more complex parsing engine.
If you want to go the more complex route, you begin by defining a grammar, then build a lexer and a parser that operate on the grammar. This is all non-trivial, and is WAY WAY beyond the scope of this forum. You might want to start with some books about compilers.
Some I’ve used in the past are Gold Parser Builder and ANTLR. Depending on whether you want to build, say, an LALR (for example) or LL(k) (also for example) grammar, your choices will be limited to tools that support that type of grammar.
Others are Grammatica, MinosseCC, Irony (which you looked at), and Coco/R.
What I need though is a good tutuorial on how you define the language. Never done that before and everything I find so far seem like it assumes you’ve attended college and taken some sort of intro classes. i.e. they jump in the middle and don’t explain the basics well.
so no matter if its YACC/Lex or Gold or Irony I am trying to find something to get me those basics and am having no luck so far.
I’m actually a Mainframe Systems Programmer by trade so I understand allot of this but I haven’t found a good tutorial on BRN for example. I’ve found examples yes but not a real explanation to what the samples are doing.
@ smgvbest -
What does your command syntax look like? In your earlier post, you mentioned “move to 5000”. Is that the input, and the multiple lines that follow are the action that is to be taken?
Building a full on parser always requires a non trivial chunk of code, whether you use a generator like lex/yacc or you write it by hand. However, a simple command syntax doesn’t have to require a huge parser. A switch statement with a dozen case blocks may be sufficient.
Tell me more about the command syntax you have in mind. I may be able to help you avoid disappearing down a rat hole.