Java注释的语法

时间:2010-03-22 00:41:57

标签: java annotations grammar bnf ebnf

是否有描述Java注释语法的BNF或EBNF?

3 个答案:

答案 0 :(得分:4)

Java相关语法的权威来源,当然是JLS。

JLS 18.1 The Grammar of the Java Programming Language

Annotations:
        Annotation [Annotations]

Annotation:
        @ TypeName [( [Identifier =] ElementValue)]

ElementValue:
        ConditionalExpression
        Annotation
        ElementValueArrayInitializer

... rest ommitted

答案 1 :(得分:1)

`/ *注释语法如下。 * /

Annotation :: = NormalAnnotation   | SingleMemberAnnotation   | MarkerAnnotation NormalAnnotation :: =“@”Name“(”(MemberValuePairs)?“)” MarkerAnnotation :: =“@”名称 SingleMemberAnnotation :: =“@”Name“(”MemberValue“)” MemberValuePairs :: = MemberValuePair(“,”MemberValuePair)* MemberValuePair :: =“=”MemberValue MemberValue :: = Annotation   | MemberValueArrayInitializer   |条件表达式 MemberValueArrayInitializer :: =“{”(MemberValue(“,”MemberValue)*(“,”)?)? “}”

/ *注释类型。 * /

AnnotationTypeDeclaration :: =“@”“interface”AnnotationTypeBody AnnotationTypeBody :: =“{”(AnnotationTypeMemberDeclaration)*“}” AnnotationTypeMemberDeclaration :: =修饰符(类型“(”“)”(DefaultValue)?“;”| ClassOrInterfaceDeclaration | EnumDeclaration | AnnotationTypeDeclaration | FieldDeclaration)   | (“;”) DefaultValue :: =“default”MemberValue` from here。另见他的blog post

答案 2 :(得分:1)

Java语言语法

  1. 任何类型都可以以[注释]为前缀:

    Type: [Annotations] Identifier [TypeArguments] {. Identifier [TypeArguments]} {[]} [Annotations] BasicType

  2. 要允许对数组的级别进行注释(在声明中,而不是构造函数中),请将“{[]}”更改为“{[Annotations] []}”。 (这在JLS [GJSB00]的第2版中被抽象为“BracketsOpt”。)例如:

  3. Type: [Annotations] Identifier [TypeArguments]{ . Identifier [TypeArguments]} {[Annotations] []} [Annotations] BasicType

    Also permit annotations on varargs (...): FormalParameterDeclsRest: VariableDeclaratorId [, FormalParameterDecls] [Annotations] ... VariableDeclaratorId

    1. 通过将“FormalParameters”(在语法中出现的所有5个位置)的使用更改为“FormalParameters [Annotations]”,可以在接收器类型上显示注释。例如:

      VoidMethodDeclaratorRest: FormalParameters [Annotations] [throws QualifiedIdentifierList] ( MethodBody | ; )