ANTLR:从不同的语法调用规则
是否可以从不同的语法调用规则?
目的是在同一个文件中包含两种语言,第二种语言以 (begin ...) 开头,其中 ... 是第二种语言。该语法应该调用另一个语法来解析该第二种语言。
例如:
grammar A;
start_rule
: '(' 'begin' B.program ')' //or something like that
;
grammar B;
program
: something* EOF
;
something
: ...
;
is it possible to invoke a rule from a different grammar?
the purpose is to have two languages in the same file, the second language starting by an (begin ...) where ... is in the second language. the grammar should invoke another grammar to parse that second language.
for example:
grammar A;
start_rule
: '(' 'begin' B.program ')' //or something like that
;
grammar B;
program
: something* EOF
;
something
: ...
;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的问题可以(至少)两种方式解释:
我认为这是第一个,在这种情况下您可以导入语法。
选项 1 的演示:
file: Lg
file: Sub.g
file: Root.g
file: Main.java
运行演示:
它将打印:
到控制台。
更多信息,请参阅:http://www.antlr.org/wiki/display/ ANTLR3/Composite+Grammars
选项 2 的演示:
语言中的语言的一个很好的例子是正则表达式。您拥有带有元字符的“正常”正则表达式语言,但其中还有另一种:描述字符集(或字符类)的语言。
您可以简单地将字符集视为单个标记由
[
组成,然后是正则表达式语法中直到并包括]
的所有内容(其中可能包含\]
!)。当您在解析器规则之一中偶然发现CharSet
标记时,您将调用 CharSet 解析器。文件:Regex.g
文件:CharSet.g
文件:Main.java
如果运行主类,您将看到 DOT 输出,它是以下树:
神奇之处在于
atomRegex.g
语法code> 规则,其中我通过调用CharSetParser
类中的静态ast
方法在重写规则中插入树节点:请注意,在此类重写规则中,必须有 不是分号!因此,这是错误的:
{CharSetParser.ast($CharSet.text);}
。编辑
以下是如何为两种语法创建树遍历器:
file: RegexWalker.g
file: CharSetWalker.g
Main.java
要运行演示,请执行:
它将打印:
Your question could be interpreted in (at least) two ways:
I assume it's the first, in which case you can import grammars.
A demo for option 1:
file: L.g
file: Sub.g
file: Root.g
file: Main.java
Run the demo:
which will print:
to the console.
More info, see: http://www.antlr.org/wiki/display/ANTLR3/Composite+Grammars
A demo for option 2:
A nice example of a language inside a language is regex. You have the "normal" regex language with its meta characters, but there's another one in it: the language that describes a character set (or character class).
Instead of accounting for the meta characters of a character set (range
-
, negation^
, etc.) inside your regex-grammar, you could simply consider a character set as a single token consisting of a[
and then everything up to and including]
(with possibly\]
in it!) inside your regex-grammar. When you then stumble upon aCharSet
token in one of your parser rules, you invoke the CharSet-parser.file: Regex.g
file: CharSet.g
file: Main.java
And if you run the main class, you will see the DOT output for the regex
((xyz)*[^\\da-f])foo
which is the following tree:The magic is inside the
Regex.g
grammar in theatom
rule where I inserted a tree node in a rewrite rule by invoking the staticast
method from theCharSetParser
class:Note that inside such rewrite rules, there must not be a semi colon! So, this would be wrong:
{CharSetParser.ast($CharSet.text);}
.EDIT
And here's how to create tree walkers for both grammars:
file: RegexWalker.g
file: CharSetWalker.g
Main.java
To run the demo, do:
which will print: