Antlr4:获取一个有序的标记列表?

huangapple go评论59阅读模式
英文:

Antlr4: getting an ordered list of tokens?

问题

我有这个解析规则:

multiplication
	: pow (operator = (TIMES | DIVIDE | FLOOR_DIVIDE | MODULO) pow)*
	;

我正在使用ctx.pow()迭代* pow*,但我也想知道那里是什么运算符。不幸的是,ctx.operator只会给出最后遇到的运算符,而ctx.TIMES()只会给出一个带有重复的'*'的愚蠢列表。

我真的需要为此做一个子规则吗?

英文:

I have this parser rule:

multiplication
	: pow (operator = (TIMES | DIVIDE | FLOOR_DIVIDE | MODULO) pow)*
	;

And I'm iterating over the pows using ctx.pow(), but I would like to know too what operator there was. Unfortunately, ctx.operator just gives the last one encountered and ctx.TIMES() just gives a dumb list with a reapeted '*'.

Do I really have to do a sub-rule for that?

答案1

得分: 2

你可以使用 operator += 进行操作:

multiplication
    : pow (operator += (TIMES | DIVIDE | FLOOR_DIVIDE | MODULO) pow)*
    ;

这将会将操作符放入一个 List 中。

英文:

You can do operator +=:

multiplication
    : pow (operator += (TIMES | DIVIDE | FLOOR_DIVIDE | MODULO) pow)*
    ;

which will cause the operators to be placed in a List.

huangapple
  • 本文由 发表于 2020年8月27日 04:10:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/63605080.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定