Java7规范中的语法是否真的相同? [英] Is the grammars in Java7 spec really equivalent?

查看:124
本文介绍了Java7规范中的语法是否真的相同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

JLS v7的第18章中的语法似乎有所不同从文档中其他地方的构造,但对我来说似乎有差异。特别是在第15章中,规则是:

The grammar in chapter 18 of JLS v7 seem to differ from the constructs elsewhere in the documentation, but to me there seem to be differences. Specifically in chapter 15 the rules are:

RelationalExpression:
  ShiftExpression
  RelationalExpression < ShiftExpression
  RelationalExpression > ShiftExpression
  RelationalExpression <= ShiftExpression
  RelationalExpression >= ShiftExpression
  RelationalExpression instanceof ReferenceType

使得 foo instanceof Bar 一个RelationalExpression(因此是一个EqualityExpresson),它又可以在EqualityExpression规则中用作LHS,它使 foo instanceof Bar == false 一个EqualityExpression。

which makes foo instanceof Bar a RelationalExpression (and therefore an EqualityExpresson) which in turn can be used as LHS in the EqualityExpression rule which makes foo instanceof Bar == false an EqualityExpression.

但是在查看第18章中的语法时,他们对它进行了简化: / p>

But when looking at the grammar in chapter 18 they've simplified it a bit:

Expression2:
  Expression3 [Expression2Rest]

Expression2Rest:
  { InfixOp Expression3 }
  instanceof Type

这看起来很奇怪,这意味着我们可以链接在一起 Expression3 s带有二元运算符或者我们可以检查一个 Expression3 的类型。具体来说现在 foo instanceof Bar 是一个 Expression2 ,但我没有看到使用<是有效的 Expression2 作为平等比较的LHS。

Which looks odd, which means that we can chain together Expression3s with binary operators OR we can check the type of one Expression3. Specifically now foo instanceof Bar is an Expression2, but I don't see that it would be valid to use an Expression2 as LHS of an equality comparision.

我是否错过了第18章语法中的一些内容 foo instanceof Bar == false 一个有效的表达式?请注意,根据第15章中的规则并根据我的编译器,它是一个有效的表达式。

Have I missed something in the grammar of chapter 18 that makes foo instanceof Bar == false a valid expression? Note that it is a valid expression according to the rules in chapter 15 and according to my compiler.

推荐答案

这个问题值得一试回答,让我们仔细看看。

This question deserves a good answer, so let's take a good close look.

完全基于第18章的语法:

Based solely on the grammar in Chapter 18:

一切使用 InfixOp (例如 == )要么适合 Expression2Rest ,要么什么都不适合。并且 Expression2Rest 仅属于 Expression2 。所以,如果 foo instanceof Bar == false 是合法的Java,那意味着 foo instanceof Bar 必须是 Expression3

Anything with an InfixOp (e.g. ==) either fits Expression2Rest or fits nothing. And Expression2Rest only belongs inside Expression2. So, if foo instanceof Bar == false is legal Java, that means that foo instanceof Bar has to be an Expression3.


Expression2:

Expression3 [Expression2Rest]

Expression2Rest:

{InfixOp Expression3} < br>
instanceof 类型

但是 foo instanceof Bar 不是 Expression3 。没有 PrefixOp 且没有强制转换,因此要成为 Expression3 ,它必须是。但它只是不适合。

But foo instanceof Bar is not an Expression3. There's no PrefixOp and no cast, so to be an Expression3 it would have to be a Primary. But it just doesn't fit.


Expression3:

PrefixOp Expression3

(表达式 | 类型) Expression3

主要{选择器} {PostfixOp}

Expression3:
PrefixOp Expression3
( (Expression | Type) ) Expression3
Primary { Selector } { PostfixOp }

主要:

Literal

ParExpression

[参数]

超级 SuperSuffix

创作者

NonWildcardTypeArguments(ExplicitGenericInvocationSuffix |
参数)

标识符{标识符} [IdentifierSuffix]

BasicType { [] } * .class

void.class

Primary:
Literal
ParExpression
this [Arguments]
super SuperSuffix
new Creator
NonWildcardTypeArguments (ExplicitGenericInvocationSuffix | this Arguments)
Identifier { . Identifier } [IdentifierSuffix]
BasicType { [] }* .class
void.class

结论:完全基于第18章中的语法, foo instanceof Bar == false 不是合法的Java表达式。 !?!?!

Conclusion: based solely on the grammar presented in Chapter 18, foo instanceof Bar == false is not a legal Java expression. !?!?!

当然这是胡说八道。 foo instanceof Bar 产生一个布尔结果,该结果当然可以与 false 进行比较。表达式编译并运行。

Of course that's nonsense. foo instanceof Bar yields a boolean result, and that result can certainly be compared to false. The expression compiles and runs.

更好的结论:第18章不具有权威性,但本书的其余部分是。

Better conclusion: Chapter 18 isn't authoritative, but the rest of the book is.

第2.3节说明


第4章给出了Java编程语言的语法语法 ,第6章,第14章和第15章。第18章还给出了Java编程语言的语法语法,比说明书更适合实现。

A syntactic grammar for the Java programming language is given in Chapters 4, 6-10, 14, and 15. ... Chapter 18 also gives a syntactic grammar for the Java programming language, better suited to implementation than exposition.

根据第15章中提供的语法规则, foo instanceof Bar == false 是一个合法的Java表达式。但请查看第15.20.1节之前的最后一句:关系表达式的类型总是布尔。这与15.20中的 RelationalExpression 规则本身直接冲突。 (特别是,它意味着 instanceof 的LHS必须评估为布尔。)它可能不是真的。

By the grammar rules presented in Chapter 15, foo instanceof Bar == false is a legal Java expression. But check out the last sentence before section 15.20.1: "The type of a relational expression is always boolean." That directly conflicts with the RelationalExpression rules themselves in 15.20. (In particular, it implies that the LHS of instanceof must evaluate to boolean.) It can't possibly be true.

最后的结论:这本书有问题。如果你想知道某些东西是否是合法的Java,你必须编译并运行它,最好是在Oracle的参考实现上。即便如此,也可能存在错误。毕竟,这只是软件。

Bestest conclusion: This book has issues. If you want to know whether something is legal Java, you have to compile and run it, preferably on Oracle's reference implementation. And even then there might be bugs. It is, after all, just software.

我认为如果他们稍微改变了 Expression2 规则,那么第18章可能是对。像这样:

And I think that if they changed the Expression2 rule just a little bit, Chapter 18 could be right. Like so:


* Expression2:

Expression3 [ instanceof 类型]

Expression2 {InfixOp Expression3}

但谁知道,这可能会导致其他问题。在任何情况下,它都在Java 8中修复。

But who knows, that might cause other problems. In any case, it's fixed in Java 8.

这篇关于Java7规范中的语法是否真的相同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆