简体   繁体   English

如何在Java语言解析器中正确标记'@'符号?

[英]How is the '@' symbol correctly tokenized in a Java language parser?

I'm working on a Java source code parser, following the lexical and syntactic specifications. 我正在研究Java源代码解析器,遵循词法语法规范。

I'm stuck on annotations, though; 不过,我仍然坚持注释; the relevant rules are: 相关规则是:

Annotation:
    @ QualifiedIdentifier [ ( [AnnotationElement] ) ]

AnnotationTypeDeclaration:
    @ interface Identifier AnnotationTypeBody

but I don't see the @ symbol mentioned anywhere in the lexical spec. 但我没有看到词汇规范中任何地方提到的@符号。 What is the correct rule for tokenizing @ , or where can I find such a rule? 标记@的正确规则是什么,或者我在哪里可以找到这样的规则?

I realize that I could easily add a token definition for this, and it might even work --- sometimes. 我意识到我可以轻松地为此添加一个令牌定义,它甚至可能有效。 But I'd prefer to do it correctly according to the spec if possible. 但是如果可能的话,我希望根据规范正确地做到这一点。

In 9.6. 9.6。 Annotation Types , it says: 注释类型 ,它说:

Note that the at-sign (@) and the keyword interface are two distinct tokens. 请注意,at符号(@)和关键字界面是两个不同的标记。 Technically it is possible to separate them with whitespace, but this is discouraged as a matter of style. 从技术上讲,可以将它们与空格分开,但不鼓励这样做。

You are correct, in the sense that '@' is not mentioned as either an operator or as punctuation in the "3. Lexical Structure" section of the JLS. 你是正确的,因为'@'在JLS的“3.词汇结构”部分中没有被提及为操作符或标点符号。

However, it is used as a symbol in the JLS 9.6, and also the "18. Collected Syntax" section, so it is obvious that your lexer should treat it as one. 但是,它在JLS 9.6中用作符号,也用作“18. Collected Syntax”部分,因此很明显您的词法分析器应将其视为一个。

This is just a minor editorial issue, and not something that should cause you any concern in implementing a Java parser. 这只是一个次要的编辑问题,而不应该引起您对实现Java解析器的任何关注。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM