Main Site Documentation

.NET Command Parser


#1

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

Any idea’s or suggestions on where to start?


#2

I will be reading commands in over either USB or TCP/IP and need to parse out the command and options so my program can move a motor as described in the command. Move in my example

So using my MOVE example
MOVE TO 5000

This might set some variables such as

CMD=MOVE
PARM_MOTION_MODE=ABSOLUTE
PARM_POSITION=5000
PARM_MOTION_TYPE=STEP
PARM_COMPENSATION=NO
PARM_SPEED=SLOW

So now I can move the motor and know how it should be moved

very basic example but is what I would be after


#3

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.

Like I said, WAY beyond the scope of this forum.


#4

Is there a good tutorial out there that could get me going on creating a DSL.

when I look at possibly 20 commands each with multiple keywords and parms doing a string.split() would seem to be massive and prone to errors

I took a look at gold and irony now but need some pointers to get me up to speed

not looking for that here but hopefully someone knows some good online tutorials that might help?

TIA


#5

the classical way to build a command line processor is YACC/LEX. Google “yacc lex c#”.

you define the language and then code is generated.


#6

Perhaps Skewworks’ SBASIC project may be helpful to review.

https://www.ghielectronics.com/community/codeshare/entry/603

He built a Basic interpreter application.

From the codeshare:

He also build an OS with command line interpreter:


#7

Thanks Mike
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.


#8

What you are trying to do is not a simple task. You could check out some textbooks on compiler design, but without a computer science background, they might be difficult to read.

You might have to use a brute force solution.


#9

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.


#10

Try this book:


#11

@ 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.

-Danny


#12

@ smgvbest -
Also, is the command syntax something of your own design (and can be tweaked to simplify processing) or is the syntax defined by someone else (and immutable)?

-Danny