tags:

views:

370

answers:

1

I see a few sample main() for C floating about, e.g. http://www.antlr.org/wiki/display/ANTLR3/Five+minute+introduction+to+ANTLR+3 and http://www.antlr.org/api/C/index.html

The dereference seems to be AST. I don't know what that is , and - please excuse me - don't want to if I can avoid it.

I woudl like to just define the lexer & grammar (for modem AT commands) and have the main() auto-generated, or cut/pasted from somewhere.

Ons slight twist is that most examples seem to read from a file, whereas I will be getting a series of inputs (AT commands) as strings, passed as parameters.

Can anyone please point me at a simple main() which I can adapt (and maybe suggest hwo to adapt it?) A lot to ask I know, sorry.

+1  A: 

An AST is an "Abstract Syntax Tree", and for AT commands you do not need one.

In fact, ANTLR is overkill for parsing AT commands. Why not just check for the "AT", and then parse the commands by hand? The modem command set is probably sufficiently simple that it will be simpler to do that than to get ANTLR going if you have never done it before.

If you are implementing a full AT command set, including the "+++" escape sequence, timing is also a factor which you will not be able to implement using ANTLR alone.

Response to comments:

Seeing ANTLR as a tool to reduce bugs is reasonable. In answer to your questions "Would you rather read that or the had-crafted code? And which would you rather maintain?", the answer is "it depends." I use both ANTLR and hand-coded parsers depending on the context; this a context where I would choose a hand-coded parser. Some reasons: probably an embedded application, relatively straightforward (many commands does not equal complexity), a comms protocol. You have a different context and clearly have your own reasons which include "trying it out".

So, to answer your other question about a main: Yes, you can use the one on the ANTLR wiki. To read from a piece of memory use antlr3NewAsciiStringInPlaceStream() or antlr3NewAsciiStringCopyStream() (as appropriate) instead of antlr3AsciiFileStreamNew().

janm
Thanks for clarifying AST ;-)Btw, can you point me at a sample main()? Can I just use that at http://www.antlr.org/wiki/display/ANTLR3/Five+minute+introduction+to+ANTLR+3 ?
Mawg
I think that you "misunderestimate" the complexity of AT commands. In general, for any non-trivial grammar, I would much prefer a parser, than hand-crafted. For one thing, AntlrWorks visual representation of the grammar has already helped me spot a few mistakes, code reading and unit testing might not get all bugs out of a hand-crafted parser, I see Antlr as another tool to help reduce the number of bugs.I am doing this by way of a hobby after seeing a disasterous hand-crafted parser in a previous job.27.007 and, I think, 25.007 (SMS) have a few hundred commands.
Mawg
The trick bit is that some have optional sub-parts, and, oh, I just think that it is easier to read and maintain the lexer/parser than hand-written code.I will post details of the Dial comamnd, which is one of the most complex, in several comments (due to size limit) to show what I mean ...
Mawg
V.250 dialling digits1 2 3 4 5 6 7 8 9 0 * # + A B C (implementation of these characters is mandatory for GSM/UMTS)D (implementation of this character is optional for GSM/UMTS, and it is ignored)V.250 modifier characters, (implementation of this character is mandatory for GSM/UMTS, but it may be ignored)T P (implementation of these characters is mandatory for GSM/UMTS, but they are ignored)! W @ (implementation of these characters is optional for GSM/UMTS, and they are ignored)
Mawg
GSM/UMTS modifier characters> (refer subclause "Direct dialling from phonebooks")I or i (override the CLIR supplementary service subscription default value for this call; I = invocation (restrict CLIpresentation) and i = suppression (allow CLI presentation); refer subclause "Calling line identification restriction+CLIR")G or g (control the CUG supplementary service information for this call; uses index and info values set with command+CCUG; refer subclause "Closed user group +CCUG")
Mawg
Direct dialling from phonebooks1. D><str>[I][G][;] originate call to phone number which corresponding alphanumeric field is <str> (if possible, all available memories should be searched for the correct entry).2. D>mem<n>[I][G][;] originate call to phone number in memory mem entry location <n> (available memories may be queried with Select Phonebook Storage test command +CPBS=?; mem could be e.g. ME).
Mawg
3. D><n>[I][G][;] originate call to phone number in entry location <n> (it is manufacturer specific which memory storage of MT, SIM/UICC and TA is used; command Select Phonebook Memory Storage +CPBS setting is recommended to be used)
Mawg
Sorry about posting so much text, but the point I want to make is that that woudl be a nightmare to code in C or C++, but I have reduced the parser (I won't bore you with the tokens) to
Mawg
ATD_dial : AT_cmd_dial CLIR_override? V250_dialing_digits END_OF_DIAL? ;ATD_dial_from_phone_book : AT_cmd_dial_from_phone_book Decimal_number CLIR_override? GUG_token? END_OF_DIAL?| AT_cmd_dial_from_phone_book PB_mem_type Decimal_number CLIR_override? GUG_token? END_OF_DIAL?| AT_cmd_dial_from_phone_book Alpha_numeric_string CLIR_override? GUG_token? END_OF_DIAL?; Would you rather read that or the had-crafted code? And which would you rather maintain?
Mawg
Mawg