Antlr3 解析器路径命令 shell
我需要解析命令 shell,例如:
cp /home/test /home/test2
我的问题在于正确的路径解析。
我定义了一个规则(我不能使用令牌作为路径,但我需要在解析器中定义它):
path : ('/' ID)+;
我
ID: (A.. Z | a.. z) +;
WS: (' ') {$channel = HIDDEN;};
需要隐藏令牌 WS,但这给我带来了一个问题,即本示例中的 2 个路径被视为一条路径。
我该如何解决这个问题?
谢谢
I need to parse the command shell such as:
cp /home/test /home/test2
My problem is in the correct path parsing.
I defined a rule (I can not use a token as path but I need to define it in the parser):
path : ('/' ID)+;
with
ID: (A.. Z | a.. z) +;
WS: (' ') {$channel = HIDDEN;};
I need to keep the token WS hidden, but this gives me the problem that the 2 paths in this example are considered as a single path.
How can I solve this problem?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
通过在 ANTLRWorks 中进行一些操作,我能够让它发挥作用:
请注意,我必须创建更多的词法分析器规则,然后开始测试不同的解析器规则。我使用了 java 目标,并且会让您使用您想要的任何目标。
哦,是的,由于命令规则中的 '\n',每个命令都必须位于单独的行上。
With a little playing around in ANTLRWorks I was able to get this to work:
Please notice that I had to create a few more lexer rules and then start putting different parser rules to test. I used a java target and will let you use what ever target you want.
Oh yeah, each command has to be on a separate line because of the '\n' in the command rule.
好的,根据您的评论,像这样的怎么样:
再次,我能够在 ANTLRWorks 中快速运行它,并且它似乎可以与上面列出的 cp 命令一起使用。但就我个人而言,我不太喜欢这个,因为你的路径是四个标记的列表,很快我就无法轻易分开。因此,您可能需要命令和路径之间的规则(因为我假设您的 shell 命令可能有一些命令处理文件,而其他命令则处理目录)。
我也希望 ID 和 WS 词法分析器规则是您想要的。
Ok, based on your comment, how about something like this:
Again, I was able to get this working in ANTLRWorks quickly and it appears to work with the cp command listed above. But pesonally I don't like this as much since your path is a list of four tokens and quickly I could not split out easily. So, you might require a rule between command and path (since I would assume your shell command might have some commands that work with files while others work on directories).
I am also hoping the ID and WS lexer rules are what you want.