Looking for Antlr 3 / C sample main()

让人想犯罪 __ 提交于 2019-12-23 01:41:45

问题


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:


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().



来源:https://stackoverflow.com/questions/1831025/looking-for-antlr-3-c-sample-main

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!