问题描述
我想对基本文件 io (Java) 使用解析器操作,例如.G.ANTLR 语法中的 PrintWriter.我必须使用 superClass 选项还是可以使用@header?在这两种情况下,我如何声明 PrintWriter 对象以及我必须如何处理异常?
选项 superClass=...
用于让您的 Parser
扩展自定义类.所以,我不认为这就是你所追求的.
@header
部分中的所有内容都将放置在您的 Parser
类的开头.这用于导入类:
@header {导入 java.io.PrintWriter;}
请注意,@header {...}
是 @parser::header {...}
的缩写.您还可以为您的词法分析器定义:@lexer::header {...}
.
在 @member {...}
里面(或者:@parser::member {...}
, @lexer::member {...}
) 部分,您可以添加可以在 Parser
或 Lexer
中使用的实例变量和方法:
@header {导入 java.io.PrintWriter;}@会员{PrintWriter 作家;}
语法的一个小演示,其解析器将解析的数字写入特定的作者:
grammar T;@header {导入 java.io.PrintWriter;}@会员{PrintWriter 作家;公共 TParser(TokenStream 输入,字符串文件名){超级(输入);尝试 {writer = new PrintWriter(fileName);} 捕获(异常 e){e.printStackTrace();}}}解析: 数字EOF;数字:(NUM {writer.println("解析:" + $NUM.text);writer.flush();})+;数字:'0'..'9'+;WS : ' ' {skip();};
可以测试:
import java.io.File;导入 org.antlr.runtime.*;公共课主要{public static void main(String[] args) 抛出异常 {字符串源 = "42 500000000 666";TLexer 词法分析器 = new TLexer(new ANTLRStringStream(source));TParser parser = new TParser(new CommonTokenStream(lexer), "log.txt");parser.parse();}}
如果你运行上面的类,一个名为 log.txt
的文件已经被创建,其中包含:
解析:42解析:500000000解析:666
请注意,所有这些 @...
和 options {...}
等实例都有严格的顺序:
语法
定义options
块(没有@
符号!)tokens
块(没有@
符号!)@header
块@members
块
语法T;选项 {//这里的选项}令牌{//这里是虚构的标记}@header {//...}@会员{//...}
编辑
ANTLRStarter 写道:
如何添加在词法分析器/解析器类末尾执行的代码?
这种事情没有内置功能.但是您可以在解析器中轻松创建自定义方法 wrapUp()
:
@members {//...私人无效包装(){//在这里结束你的解析器}}
然后像这样从语法的入口点自动调用该方法:
解析@after {this.wrapUp();}: 数字EOF;
当规则中的所有内容都正确匹配时,规则的 @after {...}
块中的任何代码都会被执行.
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?
The option superClass=...
is used to let your Parser
extend a custom class. So, I don't think that is what you're after.
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;
}
Note that @header {...}
is short for @parser::header {...}
. You can also define: @lexer::header {...}
for your 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();
}
}
If you run the class above, a file called log.txt
has been created containing:
parsed: 42
parsed: 500000000
parsed: 666
Note that there is a strict order of all these @...
and options {...}
etc. instances:
grammar
definitionoptions
block (no@
sign!)tokens
block (no@
sign!)@header
block@members
block
grammar T;
options {
// options here
}
tokens {
// imaginary tokens here
}
@header {
// ...
}
@members {
// ...
}
EDIT
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
;
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)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!