我正在 ANTLRv3 中构建一种预处理器,它当然只适用于模糊解析。目前我正在尝试解析包含语句并将它们替换为相应的文件内容。我用了这个例子:
基于此示例,我编写了以下代码:
grammar preprocessor;
options {
language='Java';
}
@lexer::header {
package antlr_try_1;
}
@parser::header {
package antlr_try_1;
}
parse
: (t=. {System.out.print($t.text);})* EOF
;
INCLUDE_STAT
: 'include' (' ' | '\r' | '\t' | '\n')+ ('A'..'Z' | 'a'..'z' | '_' | '-' | '.')+
{
setText("Include statement found!");
}
;
Any
: . // fall through rule, matches any character
;
此语法仅用于打印文本并将包含语句替换为“找到包含语句!” 细绳。要解析的示例文本如下所示:
some random input
some random input
some random input
include some_file.txt
some random input
some random input
some random input
结果的输出如下所示:
C:\Users\andriyn\Documents\SandBox\text_files\asd.txt line 1:14 mismatched character 'p' expecting 'c'
C:\Users\andriyn\Documents\SandBox\text_files\asd.txt line 2:14 mismatched character 'p' expecting 'c'
C:\Users\andriyn\Documents\SandBox\text_files\asd.txt line 3:14 mismatched character 'p' expecting 'c'
C:\Users\andriyn\Documents\SandBox\text_files\asd.txt line 7:14 mismatched character 'p' expecting 'c'
C:\Users\andriyn\Documents\SandBox\text_files\asd.txt line 8:14 mismatched character 'p' expecting 'c'
C:\Users\andriyn\Documents\SandBox\text_files\asd.txt line 9:14 mismatched character 'p' expecting 'c'
some random ut
some random ut
some random ut
Include statement found!
some random ut
some random ut
some random ut
据我判断,它被“输入”一词中的“输入”弄糊涂了,因为它“认为”它将是 INCLUDE_STAT 标记。
有更好的方法吗?我不能使用过滤器选项,因为我不仅需要包含语句,还需要其余代码。我尝试了其他几件事,但找不到合适的解决方案。