Java运算符在ANTLRv4文法中的结合性和优先级
Associativity and precedence of Java operators in ANTLRv4 grammar
我正在尝试了解 antlr4 的功能,并通过参考代码和书籍,我对 Java.g4 中的 'expression' 规则有疑问(https://raw.githubusercontent.com/antlr/grammars-v4/master/java/Java.g4) 和书(2012 年第 2 版,第 73 页):
根据本书,“+”的优先级低于“^”,因此“+”规则低于“^”规则。但是Java代码正好相反
按照书上的说法,'^'规则有assoc=right。但是Java代码没有,除了'^='和类似的规则。
书籍和代码哪个正确?
顺便说一句,代码和书中还有一个区别。在代码中,assoc=right 在规则的开头,而在书中它在运算符之后,但看起来语法已更改(代码适用于 4.5.3)并且必须更新本书这个。
这里是相关的 Java.g4 代码片段:
expression
: primary
| expression ('*'|'/'|'%') expression
| expression ('+'|'-') expression
| expression '^' expression
// this indicates that precedence is: * > + > ^
// BUT it SHOULD be: ^ > * > +
// and both + and ^ are assoc=left
// BUT it SHOULD be: LEFT for + and RIGHT for ^
| <assoc=right> expression
( '='
| '+='
| '^='
)
expression
;
注意:在上面的问题中,我假设 ^ 表示求幂,但正如答案指出的那样,在 java 中 ^ 具有不同的含义。
说明
正确答案在Java language specification. We will focus on the current specification which is Java SE 8中。
在语言描述中,我们对有关 二元运算符表达式(15.2 表达式形式)的三个小节感兴趣。 描述的运算符越早,它的优先级就越高。
15.18 加法运算符(第 563 页)
The operators + and - are called the additive operators.
然后我们阅读:
The additive operators have the same precedence and are syntactically left-associative (they group left-to-right).
15.22 按位和逻辑运算符(第 575 页)
The bitwise operators and logical operators include the AND operator &, exclusive OR operator ^, and inclusive OR operator |.
然后我们阅读:
Each of these operators is syntactically left-associative (each groups left-to-right)
15.26 赋值运算符(第 588 页)
赋值运算符是:
= *= /= %= += -= <<= >>= >>>= &= ^= |=
There are 12 assignment operators; all are syntactically right-associative.
这允许 chained assignment.
结论
In Java8 加法运算符+
的优先级高于异或运算符^
。 异或^
运算符和加法运算符+
是左结合. 赋值运算符的右结合性(例如^=
)允许chained assignment.
您粘贴的Java.g4代码片段是正确的.
我正在尝试了解 antlr4 的功能,并通过参考代码和书籍,我对 Java.g4 中的 'expression' 规则有疑问(https://raw.githubusercontent.com/antlr/grammars-v4/master/java/Java.g4) 和书(2012 年第 2 版,第 73 页):
根据本书,“+”的优先级低于“^”,因此“+”规则低于“^”规则。但是Java代码正好相反
按照书上的说法,'^'规则有assoc=right。但是Java代码没有,除了'^='和类似的规则。
书籍和代码哪个正确?
顺便说一句,代码和书中还有一个区别。在代码中,assoc=right 在规则的开头,而在书中它在运算符之后,但看起来语法已更改(代码适用于 4.5.3)并且必须更新本书这个。
这里是相关的 Java.g4 代码片段:
expression
: primary
| expression ('*'|'/'|'%') expression
| expression ('+'|'-') expression
| expression '^' expression
// this indicates that precedence is: * > + > ^
// BUT it SHOULD be: ^ > * > +
// and both + and ^ are assoc=left
// BUT it SHOULD be: LEFT for + and RIGHT for ^
| <assoc=right> expression
( '='
| '+='
| '^='
)
expression
;
注意:在上面的问题中,我假设 ^ 表示求幂,但正如答案指出的那样,在 java 中 ^ 具有不同的含义。
说明
正确答案在Java language specification. We will focus on the current specification which is Java SE 8中。
在语言描述中,我们对有关 二元运算符表达式(15.2 表达式形式)的三个小节感兴趣。 描述的运算符越早,它的优先级就越高。
15.18 加法运算符(第 563 页)
The operators + and - are called the additive operators.
然后我们阅读:
The additive operators have the same precedence and are syntactically left-associative (they group left-to-right).
15.22 按位和逻辑运算符(第 575 页)
The bitwise operators and logical operators include the AND operator &, exclusive OR operator ^, and inclusive OR operator |.
然后我们阅读:
Each of these operators is syntactically left-associative (each groups left-to-right)
15.26 赋值运算符(第 588 页)
赋值运算符是:
= *= /= %= += -= <<= >>= >>>= &= ^= |=
There are 12 assignment operators; all are syntactically right-associative.
这允许 chained assignment.
结论
In Java8 加法运算符+
的优先级高于异或运算符^
。 异或^
运算符和加法运算符+
是左结合. 赋值运算符的右结合性(例如^=
)允许chained assignment.
您粘贴的Java.g4代码片段是正确的.