Java 使用什么技巧来避免 >> 中出现空格?

发布于 2024-08-31 16:34:04 字数 176 浏览 2 评论 0原文

在 Java Generic 书中,在对比 C++ 模板和 Java Generic 之间的差异时说:

在 C++ 中,出现问题是因为 >> 没有空格表示 右移运算符。 Java修复了 语法中的技巧问题。)

这个技巧是什么?

In the Java Generic Book, while contrasting the difference between C++ Templates and Java Generic says:

In C++, a problem arises because >>
without the space denotes the
right-shift operator. Java fixes the
problem by a trick in the grammar.)

What is this trick?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

半步萧音过轻尘 2024-09-07 16:34:04

OpenJDK javac 解析器 JavacParser,处理词法分析器标记 GTGTGTEQ (>>>=)、GTGTEQ >、GTEQGTGTGT (>>>) 和 GTGT 到令牌中少一个 '解析类型参数时的 >' 字符。

下面是来自 JavacParser#typeArguments() 的魔术片段:

    switch (S.token()) {
    case GTGTGTEQ:
        S.token(GTGTEQ);
        break;
    case GTGTEQ:
        S.token(GTEQ);
        break;
    case GTEQ:
        S.token(EQ);
        break;
    case GTGTGT:
        S.token(GTGT);
        break;
    case GTGT:
        S.token(GT);
        break;
    default:
        accept(GT);
        break;
    }

人们可以清楚地看到这确实是一个技巧,而且它就在语法中:)

The OpenJDK javac parser, JavacParser, massages the lexer tokens GTGTGTEQ (>>>=), GTGTEQ, GTEQ, GTGTGT (>>>) and GTGT into the token with one less '>' character when parsing type arguments.

Here is a snippet of the magic from JavacParser#typeArguments():

    switch (S.token()) {
    case GTGTGTEQ:
        S.token(GTGTEQ);
        break;
    case GTGTEQ:
        S.token(GTEQ);
        break;
    case GTEQ:
        S.token(EQ);
        break;
    case GTGTGT:
        S.token(GTGT);
        break;
    case GTGT:
        S.token(GT);
        break;
    default:
        accept(GT);
        break;
    }

One can clearly see that it is indeed a trick, and it's in the grammar :)

做个ˇ局外人 2024-09-07 16:34:04

实际上,这个问题将在下一版本的 C++ 中得到修复。确实没有太多技巧;如果你遇到>>在解析泛型或模板的过程中,如果您期望的是 >,那么您已经有足够的信息来生成错误消息。而且,如果您有足够的信息来生成错误消息,那么您也有足够的信息来解释 >>作为两个单独的标记:>随后是>。

This is actually being fixed in C++ in the next version. There really isn't much of a trick; if you encounter >> while in the process of parsing a generic or template where instead you expected >, then you already have enough information to generate an error message. And, if you have enough information to generate an error message, you also have enough information to interpret >> as two separate tokens: > followed by >.

七度光 2024-09-07 16:34:04

这是一个简单的解析器/词法分析器黑客。词法分析器通常将 >> 对识别为单个标记。但是,在解析泛型类型的过程中,解析器会告诉词法分析器不要识别 >>

从历史上看,C++ 并不是为了实现简单而这样做的,但它可以(并且将会)使用相同的技巧来修复。

It's a simple parser/lexer hack. The lexical analyser normally recognises the pair >> as a single token. However, when in the middle of parsing a generic type, the parser tells the lexer not to recognise >>.

Historically, C++ didn't do this for the sake of implementation simplicity, but it can (and will) be fixed using the same trick.

氛圍 2024-09-07 16:34:04

这并不是真正的技巧,他们只是定义了语法,使得右移标记与两个直角括号同义(从而允许该标记关闭模板)。您仍然可以创建必须用括号解决的歧义,但无需开发人员干预即可解析明确的序列。这也是在 C++0x 中完成的。

It's not really a trick, they just defined the grammar such that a right shift token is synonymous with with two right angle brackets (thus allowing that token to close a template). You can still create ambiguities that have to be resolved with parentheses, but unambiguous sequences are parsed without developer intervention. This is also done in C++0x.

浅笑轻吟梦一曲 2024-09-07 16:34:04

Java 语言规范,第三版 显示完整的语法,两个移位运算符都列在 InfixOp 产生式中,没有(明显的)技巧。确定哪个操作>,>>或>>是有意的,将由扫描仪使用 lookahead 技术来决定。

The Java Language Specification, Third Edition shows the full grammar, both shift operators are listed in the InfixOp production, there is no (obvious) trick. to determine which operation >, >> or >>> is intented, will be decided by the scanner using a lookahead technique.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文