在简单整数列表语法中使用AntLR4中的访问者 [英] Using Visitors in AntLR4 in a Simple Integer List Grammar
问题描述
我是AntLR的新手.我正在使用AntLR4版本.
I'm a newbie in AntLR. I'm using AntLR4 version.
我编写了以下属性语法,该属性语法可识别整数列表并在末尾打印该列表的总和.
I wrote the following attribute grammar that recognizes a list of integers and print the sum of the list at the end.
list.g4
grammar list;
@header
{
import java.util.List;
import java.util.ArrayList;
}
list
: BEGL (elems[new ArrayList<Integer>()])? ENDL
{
int sum = 0;
if($elems.text != null)
for(Integer i : $elems.listOut)
sum += i;
System.out.println("List Sum: " + sum);
}
;
elems [List<Integer> listIn] returns [List<Integer> listOut]
: a=elem (SEP b=elem
{ listIn.add($b.value); }
)*
{
listIn.add($a.value);
$listOut = $listIn;
}
;
elem returns [int value]
: NUM { $value = $NUM.int; }
;
BEGL : '[';
ENDL : ']';
SEP : ',';
NUM : [0-9]+;
WS : (' '|'\t'|'\n')+ -> skip;
有效输入为:
[1, 2, 3]
为了测试语法,我使用了TestRig Tool.
For testing my grammar, I'm using TestRig Tool.
现在,我想使用访问者"将代码与语法清楚地分开.
我知道我需要使用带有 -visitor 选项的antlr来为我的应用程序生成Visitor类.
I know that I need to use antlr with the -visitor option to generate the Visitor class for my application.
我想知道如何在Visitor方法类中访问给定产品的属性,以及如何将词法分析器,解析器和访问者代码段粘合"在一起.
I would like to know how to access the atrributes of a given production in the Visitor methods class and how to "glue" the lexer, parser and visitor code pieces together.
推荐答案
您的语法没有动作,并且在WS
规则中包括了\r
:
Your grammar without actions, and including \r
in the WS
rule:
grammar list;
list
: BEGL elems? ENDL
;
elems
: elem ( SEP elem )*
;
elem
: NUM
;
BEGL : '[';
ENDL : ']';
SEP : ',';
NUM : [0-9]+;
WS : [ \t\r\n]+ -> skip;
然后,访问者可能看起来像这样:
The visitor could then look like this:
public class SumVisitor extends listBaseVisitor<Integer> {
@Override
public Integer visitList(@NotNull listParser.ListContext ctx) {
return ctx.elems() == null ? 0 : this.visitElems(ctx.elems());
}
@Override
public Integer visitElems(@NotNull listParser.ElemsContext ctx) {
int sum = 0;
for (listParser.ElemContext elemContext : ctx.elem()) {
sum += this.visitElem(elemContext);
}
return sum;
}
@Override
public Integer visitElem(@NotNull listParser.ElemContext ctx) {
return Integer.valueOf(ctx.NUM().getText());
}
}
,可以进行以下测试:
listLexer lexer = new listLexer(new ANTLRInputStream("[1, 2, 3]"));
listParser parser = new listParser(new CommonTokenStream(lexer));
Integer sum = new SumVisitor().visit(parser.list());
System.out.println("sum=" + sum);
将打印:
sum=6
这篇关于在简单整数列表语法中使用AntLR4中的访问者的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!