Antlr3 解析器路径命令 shell

发布于 2024-08-19 18:57:15 字数 360 浏览 4 评论 0原文

我需要解析命令 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

彩扇题诗 2024-08-26 18:57:15

通过在 ANTLRWorks 中进行一些操作,我能够让它发挥作用:

commands
    :   command+ EOF;

command
    :   (CMD first=path second=path '\n') {System.out.println("Command found, first path:" + $first.text + ", and second path:" + $second.text + "\n");};

path : FILE {System.out.println("file is:" + $FILE.text);};

fragment
ID: ('A'..'Z'|'a'..'z')('A'..'Z'|'a'..'z'|'0'..'9')+;
CMD
    :   ID;
FILE 
    :   ('/' ID)+;
WS: (' '|'\t'|'\r'|'\n') {$channel = HIDDEN;};

请注意,我必须创建更多的词法分析器规则,然后开始测试不同的解析器规则。我使用了 java 目标,并且会让您使用您想要的任何目标。

哦,是的,由于命令规则中的 '\n',每个命令都必须位于单独的行上。

With a little playing around in ANTLRWorks I was able to get this to work:

commands
    :   command+ EOF;

command
    :   (CMD first=path second=path '\n') {System.out.println("Command found, first path:" + $first.text + ", and second path:" + $second.text + "\n");};

path : FILE {System.out.println("file is:" + $FILE.text);};

fragment
ID: ('A'..'Z'|'a'..'z')('A'..'Z'|'a'..'z'|'0'..'9')+;
CMD
    :   ID;
FILE 
    :   ('/' ID)+;
WS: (' '|'\t'|'\r'|'\n') {$channel = HIDDEN;};

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.

七分※倦醒 2024-08-26 18:57:15

好的,根据您的评论,像这样的怎么样:

commands
    :   command+ EOF;

command
    :   (ID ' ' (path)+ ' ' (path)+ '\n') {System.out.println("Command found:" + $command.text + "\n");};

path : 
    ('/' ID)+ {System.out.println("path is:" + $path.text);};

ID: ('A'..'Z'|'a'..'z')('A'..'Z'|'a'..'z'|'0'..'9')+;
WS: (' '|'\t'|'\r'|'\n') {$channel = HIDDEN;};

再次,我能够在 ANTLRWorks 中快速运行它,并且它似乎可以与上面列出的 cp 命令一起使用。但就我个人而言,我不太喜欢这个,因为你的路径是四个标记的列表,很快我就无法轻易分开。因此,您可能需要命令和路径之间的规则(因为我假设您的 shell 命令可能有一些命令处理文件,而其他命令则处理目录)。

我也希望 ID 和 WS 词法分析器规则是您想要的。

Ok, based on your comment, how about something like this:

commands
    :   command+ EOF;

command
    :   (ID ' ' (path)+ ' ' (path)+ '\n') {System.out.println("Command found:" + $command.text + "\n");};

path : 
    ('/' ID)+ {System.out.println("path is:" + $path.text);};

ID: ('A'..'Z'|'a'..'z')('A'..'Z'|'a'..'z'|'0'..'9')+;
WS: (' '|'\t'|'\r'|'\n') {$channel = HIDDEN;};

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文