问题描述
我想将解析器操作与基本文件io(Java)一起使用. G. ANTLR语法中的PrintWriter.我必须使用superClass选项还是可以使用@header?在这两种情况下,如何声明PrintWriter对象以及如何处理异常?
I want to use parser actions with basic file io (Java), e. g. PrintWriter in an ANTLR grammar. Must I use the superClass option or can I use @header? In both cases how can I declare the PrintWriter-object and how must I handle the exceptions?
推荐答案
选项superClass=...
用于让您的Parser
扩展自定义类.所以,我不认为那是你追求的.
The option superClass=...
is used to let your Parser
extend a custom class. So, I don't think that is what you're after.
@header
部分中的所有内容都将放置在Parser
类的开头.这用于导入类:
Everything inside the @header
section will be placed at the start of your Parser
class. This is used to import classes:
@header {
import java.io.PrintWriter;
}
请注意,@header {...}
是@parser::header {...}
的缩写.您还可以定义:@lexer::header {...}
用于词法分析器.
Note that @header {...}
is short for @parser::header {...}
. You can also define: @lexer::header {...}
for your lexer.
在@member {...}
(或@parser::member {...}
,@lexer::member {...}
)部分中,您可以添加可在Parser
或Lexer
中使用的实例变量和方法:
And inside @member {...}
(or: @parser::member {...}
, @lexer::member {...}
) sections, you can add instance variables and methods that can be used inside either the Parser
or Lexer
:
@header {
import java.io.PrintWriter;
}
@members {
PrintWriter writer;
}
一个小语法演示,其解析器会将解析后的数字写入特定的编写器:
A small demo of a grammar whose parser will write the parsed numbers to a specific writer:
grammar T;
@header {
import java.io.PrintWriter;
}
@members {
PrintWriter writer;
public TParser(TokenStream input, String fileName) {
super(input);
try {
writer = new PrintWriter(fileName);
} catch(Exception e) {
e.printStackTrace();
}
}
}
parse
: numbers EOF
;
numbers
: (NUM {
writer.println("parsed: " + $NUM.text);
writer.flush();
}
)+
;
NUM : '0'..'9'+;
WS : ' ' {skip();};
可以通过以下方式进行测试:
which can be tested with:
import java.io.File;
import org.antlr.runtime.*;
public class Main {
public static void main(String[] args) throws Exception {
String source = "42 500000000 666";
TLexer lexer = new TLexer(new ANTLRStringStream(source));
TParser parser = new TParser(new CommonTokenStream(lexer), "log.txt");
parser.parse();
}
}
如果运行上述类,则将创建一个名为log.txt
的文件,其中包含:
If you run the class above, a file called log.txt
has been created containing:
parsed: 42
parsed: 500000000
parsed: 666
请注意,所有这些@...
和options {...}
等实例都有严格的顺序:
Note that there is a strict order of all these @...
and options {...}
etc. instances:
-
grammar
定义 -
options
块(没有@
符号!) -
tokens
阻止(没有@
符号!) -
@header
块 -
@members
阻止
grammar
definitionoptions
block (no@
sign!)tokens
block (no@
sign!)@header
block@members
block
grammar T;
options {
// options here
}
tokens {
// imaginary tokens here
}
@header {
// ...
}
@members {
// ...
}
编辑
如何添加在lexer/parser类末尾执行的代码?
How can I add code which is executed at the end of the the lexer/parser class?
这种东西没有内置功能.但是您可以在解析器中轻松创建自定义方法wrapUp()
:
There's no built-in functionality for such a thing. But you can easily create a custom method wrapUp()
in your parser:
@members {
// ...
private void wrapUp() {
// wrap up your parser in here
}
}
,然后从语法的入口点自动调用该方法,如下所示:
and then automatically call that method from the entry point of your grammar like this:
parse
@after {this.wrapUp();}
: numbers EOF
;
当规则中的所有内容正确匹配时,将执行放置在规则@after {...}
块中的任何代码.
Any code placed in the @after {...}
block of a rule is executed when everything in the rule is properly matched.
这篇关于ANTLR @ header,@ parser,superClass选项和基本文件io(Java)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!