`

The Grammar of the Java Programming Language

    博客分类:
  • Java
阅读更多

Syntax


This chapter presents a grammar for the Java programming language.

The grammar presented piecemeal in the preceding chapters is much better for exposition, but it is not well suited as a basis for a parser. The grammar presented in this chapter is the basis for the reference implementation. Note that it is not an LL(1) grammar, though in many cases it minimizes the necessary look ahead.

The grammar below uses the following BNF-style conventions:

 

  • [x] denotes zero or one occurrences of x .
  • {x} denotes zero or more occurrences of x .

x | y means one of either x or y .

18.1 The Grammar of the Java Programming Language


Identifier:
        IDENTIFIER

QualifiedIdentifier:
        Identifier { . Identifier }

Literal:
        IntegerLiteral
        FloatingPointLiteral
        CharacterLiteral
        StringLiteral
        BooleanLiteral
        NullLiteral

Expression:
        Expression1 [AssignmentOperator Expression1]]

AssignmentOperator: 

        =
        +=
        -=
        *=
        /=
        &=
        |=
        ^=
        %=
        <<=
        >>=
        >>>=

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

TypeArguments:
        < TypeArgument {, TypeArgument} >

TypeArgument:
        Type
        
? 
[( 
extends
 |super
 ) Type]

StatementExpression:
        Expression

ConstantExpression:
        Expression

Expression1:
        Expression2 [Expression1Rest]

Expression1Rest:
        ?   Expression  
 :   Expression1

Expression2 :
        Expression3 [Expression2Rest]

Expression2Rest:
        {InfixOp Expression3}
        Expression3 instanceof Type

InfixOp:

        ||
        &&
        |
        ^
        &
        ==
        !=
        <
        >
        <=
        >=
        <<
        >>
        >>>
        +
        -
        *
        /
        %

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

Primary:
        ParExpression
        NonWildcardTypeArguments (ExplicitGenericInvocationSuffix
 | this

Arguments)

  this
 [Arguments]

  super
 SuperSuffix
        Literal

  new
 Creator
        Identifier { . Identifier }[ IdentifierSuffix]
        BasicType {
[]} .
class

   void.
class



IdentifierSuffix:

        [ (
 ] {
[]}
 .   class
 | Expression 
])
        Arguments
        .   ( 
class
 | ExplicitGenericInvocation 
| this
 | super
 Arguments
 | new

[NonWildcardTypeArguments] InnerCreator )

ExplicitGenericInvocation:
        NonWildcardTypeArguments ExplicitGenericInvocationSuffix

NonWildcardTypeArguments:
        < TypeList >


ExplicitGenericInvocationSuffix:

     super
 SuperSuffix
        Identifier Arguments


PrefixOp:

        ++
        --
        !
        ~
        +
        -

PostfixOp:

        ++
        --

Selector: Selector:
        . Identifier [Arguments]
        . ExplicitGenericInvocation
        . 
this

   .
 super
 SuperSuffix

        . new
 [NonWildcardTypeArguments] InnerCreator
        [ Expression ]

SuperSuffix:
        Arguments
        . Identifier [Arguments]

BasicType:

  byte

  short

  char

  int

  long

  float

  double

  boolean


Arguments:
        ( [Expression { , Expression }] )

Creator:
        [NonWildcardTypeArguments] CreatedName ( ArrayCreatorRest  |
ClassCreatorRest )

CreatedName:
        Identifier [NonWildcardTypeArguments] {. Identifier
[NonWildcardTypeArguments]}

InnerCreator:
        Identifier ClassCreatorRest

ArrayCreatorRest:
        
[ ( 
] {
[]} ArrayInitializer
 | Expression 
] {
[ Expression
 ]} {
[]} )

ClassCreatorRest:
         Arguments [ClassBody]

ArrayInitializer:
        { [VariableInitializer {, VariableInitializer} [,]] }

VariableInitializer:
        ArrayInitializer
        Expression

ParExpression:
        ( Expression )

Block:
        { BlockStatements }

BlockStatements:
        { BlockStatement }

BlockStatement :
        LocalVariableDeclarationStatement
        ClassOrInterfaceDeclaration
        [Identifier :] Statement

LocalVariableDeclarationStatement:
        [
final
] Type VariableDeclarators   
;

Statement:
        Block
        
assert 
Expression [ 
: Expression] 
;

     if
 ParExpression Statement [
else
 Statement]

     for
 ( ForControl ) Statement

     while
 ParExpression Statement

     do
 Statement 
while
 ParExpression   ;

     try
 Block ( Catches 
| [Catches] 
finally
 Block )

     switch
 ParExpression 
{ SwitchBlockStatementGroups 
}

     synchronized
 ParExpression Block

     return
 [Expression]
 ;

     throw
 Expression  
 ;

     break
 [Identifier]

     continue
 [Identifier]
        
;
        StatementExpression 
;
        Identifier   
:   Statement

Catches:
        CatchClause {CatchClause}

CatchClause:

     catch
 ( FormalParameter ) Block

SwitchBlockStatementGroups:
        { SwitchBlockStatementGroup }

SwitchBlockStatementGroup:
        SwitchLabel BlockStatements

SwitchLabel:

     case
 ConstantExpression   
:
        
case EnumConstantName 
:
        default   :

MoreStatementExpressions:
        { ,
 StatementExpression }

ForControl:
        ForVarControl
        ForInit;   [Expression]   ; [ForUpdate]

ForVarControl
        [
final
] [Annotations] Type Identifier ForVarControlRest

Annotations:
        Annotation [Annotations]

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

ElementValue:
        ConditionalExpression
        Annotation
        ElementValueArrayInitializer

ConditionalExpression:
        Expression2 Expression1Rest

    ElementValueArrayInitializer:
        
{
 [ElementValues] [,
] 
}


    ElementValues:
        ElementValue [ElementValues]

ForVarControlRest:
        VariableDeclaratorsRest;   [Expression]   ;   [ForUpdate]
        :
 Expression

ForInit:
        StatementExpression Expressions

Modifier:
  
Annotation

  public

  protected

  private

  static

  abstract

  final

  native

  synchronized

  transient

  volatile
        strictfp


VariableDeclarators:
        VariableDeclarator { ,   VariableDeclarator }

VariableDeclaratorsRest:
        VariableDeclaratorRest { ,   VariableDeclarator }

ConstantDeclaratorsRest:
        ConstantDeclaratorRest { ,   ConstantDeclarator }

VariableDeclarator:
        Identifier VariableDeclaratorRest

ConstantDeclarator:
        Identifier ConstantDeclaratorRest

VariableDeclaratorRest:
        {
[]} [  =   VariableInitializer]

ConstantDeclaratorRest:
        {
[]} =   VariableInitializer

VariableDeclaratorId:
        Identifier {
[]}

CompilationUnit:
        [[Annotations] 
package
 QualifiedIdentifier   ;  ] {ImportDeclaration}
{TypeDeclaration}

ImportDeclaration:

     import
 [ 
static] Identifier {   .   Identifier } [   .     *   ] ;

TypeDeclaration:
        ClassOrInterfaceDeclaration
        ;

ClassOrInterfaceDeclaration:
        {Modifier} (ClassDeclaration 
| InterfaceDeclaration)

ClassDeclaration:
        NormalClassDeclaration
        EnumDeclaration

NormalClassDeclaration:

     class
 Identifier [TypeParameters]
 [
extends
 Type] [
implements
 TypeList]
ClassBody

TypeParameters:
        < TypeParameter {, TypeParameter} >

TypeParameter:
        Identifier [
extends Bound]

Bound:
         Type {& Type}


EnumDeclaration:
        
enum Identifier [
implements
 TypeList] EnumBody

EnumBody:
        { [EnumConstants] [,] [EnumBodyDeclarations] }

EnumConstants:
        EnumConstant
        EnumConstants , EnumConstant

EnumConstant:
        Annotations Identifier [Arguments] [ClassBody]

EnumBodyDeclarations:
        ; {ClassBodyDeclaration}

InterfaceDeclaration:
        NormalInterfaceDeclaration
        AnnotationTypeDeclaration

NormalInterfaceDeclaration:

     interface
 Identifier [ TypeParameters] [
extends
 TypeList] InterfaceBody

TypeList:
        Type {  ,   Type}

AnnotationTypeDeclaration:
        
@ interface Identifier AnnotationTypeBody

    AnnotationTypeBody:
        { [AnnotationTypeElementDeclarations] }

    AnnotationTypeElementDeclarations:
        AnnotationTypeElementDeclaration
        AnnotationTypeElementDeclarations AnnotationTypeElementDeclaration

AnnotationTypeElementDeclaration:
        {Modifier} AnnotationTypeElementRest

AnnotationTypeElementRest:
         Type Identifier AnnotationMethodOrConstantRest;
        ClassDeclaration
        InterfaceDeclaration
        EnumDeclaration
        AnnotationTypeDeclaration

        AnnotationMethodOrConstantRest:
        AnnotationMethodRest
        AnnotationConstantRest

AnnotationMethodRest:
        ( ) [DefaultValue]

AnnotationConstantRest:
        VariableDeclarators


    DefaultValue:
        
default
 ElementValue

ClassBody:
        
{ {ClassBodyDeclaration} 
}

InterfaceBody:
        
{ {InterfaceBodyDeclaration} 
}

ClassBodyDeclaration:
        
;
        [
static
] Block
        {Modifier} MemberDecl

MemberDecl:
        GenericMethodOrConstructorDecl
        MethodOrFieldDecl
        
void
 Identifier VoidMethodDeclaratorRest
        Identifier ConstructorDeclaratorRest
        InterfaceDeclaration
        ClassDeclaration

GenericMethodOrConstructorDecl:
        TypeParameters GenericMethodOrConstructorRest

GenericMethodOrConstructorRest:
        (Type 
| void
) Identifier MethodDeclaratorRest
        Identifier ConstructorDeclaratorRest

MethodOrFieldDecl:
        Type Identifier MethodOrFieldRest

MethodOrFieldRest:
        VariableDeclaratorRest
        MethodDeclaratorRest

InterfaceBodyDeclaration:
        
;
        {Modifier} InterfaceMemberDecl

InterfaceMemberDecl:
        InterfaceMethodOrFieldDecl
        InterfaceGenericMethodDecl
        
void
 Identifier VoidInterfaceMethodDeclaratorRest
        InterfaceDeclaration
        ClassDeclaration

InterfaceMethodOrFieldDecl:
        Type Identifier InterfaceMethodOrFieldRest

InterfaceMethodOrFieldRest:
        ConstantDeclaratorsRest ;
        InterfaceMethodDeclaratorRest

MethodDeclaratorRest:
        FormalParameters {
[]} [
throws
 QualifiedIdentifierList] ( MethodBody 
|   ;
)

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

InterfaceMethodDeclaratorRest:
        FormalParameters {
[]} [
throws
 QualifiedIdentifierList]  
 ;

InterfaceGenericMethodDecl:
        TypeParameters (Type 
| void
) Identifier InterfaceMethodDeclaratorRest

VoidInterfaceMethodDeclaratorRest:
        FormalParameters [
throws
 QualifiedIdentifierList]   
;

ConstructorDeclaratorRest:
        FormalParameters [
throws
 QualifiedIdentifierList] MethodBody

QualifiedIdentifierList:
        QualifiedIdentifier {  ,   QualifiedIdentifier}

FormalParameters:
        ( [FormalParameterDecls] )

FormalParameterDecls:
        [
final
] [Annotations] Type FormalParameterDeclsRest]

FormalParameterDeclsRest:
        VariableDeclaratorId [ , FormalParameterDecls]
        ...
 VariableDeclaratorId

MethodBody:
        Block

EnumConstantName:
        Identifier

分享到:
评论

相关推荐

    The Grammar of Graphics

    The Grammar of Graphics可视化图形语法 书籍下载

    The C programming Language(chm格式完整版)

    The C programming Language By Brian W. Kernighan and Dennis M. Ritchie. Published by Prentice-Hall in 1988 ISBN 0-13-110362-8 (paperback) ISBN 0-13-110370-9 目录结构: Contents Preface Preface ...

    A Stochastic Grammar of Images

    set of all possible valid configurations produced by the grammar. (ii) The grammar is embodied in a simple And–Or graph representation where each Or-node points to alternative sub-configurations and ...

    The Go Programming Language Specification.

    Go is a general-purpose language designed with systems programming in mind. It is strongly typed and garbage-collected and has explicit support for concurrent programming. Programs are constructed ...

    The C Programming Language 第二版 英文版

    The C programming Language 第二版英文版 內容列表 Table of Contents Preface.......................................................... Preface to the first edition.......................................

    The C++ Programming Language (Special Ed-Bjarne Stroustrup) - 1997

    A The C++ Grammar ...................................................................... 793 B Compatibility .............................................................................. 815 C ...

    Foundations of C++/CLI : The Visual C++ Language for .NET 3.5

    you’ll have a thorough grounding in the core language elements together with the confidence to explore further that comes from a solid understanding of a language’s syntax and grammar. What you’...

    The Grammar

    图形学方面的经典教材,英文版第二版,希望对大家有帮助

    C++CLI:The Visual C++ Language for .NET

    you'll have a thorough grounding in the core language elements together with the confidence to explore further that comes from a solid understanding of a languages syntax and grammar.

    The C programming Language

    The C programming Language.chm &lt;br&gt;The C programming Language By Brian W. Kernighan and Dennis M. Ritchie. Published by Prentice-Hall in 1988 &lt;br&gt;ISBN 0-13-110362-8 (paperback) ISBN 0...

    The Objective-C 2.0 Programming Language

    1、Introduction to The Objective-C 2.0 Programming Language 2、Objects and Classes 3、Defining a Class 4、Categories and Extensions 5、Properties 6、Protocols 7、Fast Enumeration 8、How Messaging ...

    American accent training Grammar

    This book and audio compact disc program instructs students of English as a second language in the elements of grammar, presenting a grammar review that emphasizes speech and correct pronunciation....

    2-basic-grammar-of-java.zip_Grammar_java课程ppt

    java语言基本语法学习,课程PPT详解,简单清晰,适合初学者

    英文原版-Swift Programming The Big Nerd Ranch Guide 2nd Edition

    Through the authors' carefully constructed explanations and examples, you will develop an understanding of Swift grammar and the elements of effective Swift style. This book is written for Swift 3.0 ...

    java傻瓜编程系列英文版kindle

    INTRODUCTION. 1 How to Use This Book. 1 Conventions Used in This Book....What You Don’t Have to Read....Speaking the Java Language . 43 The grammar and the common names. 44 The words in a Java program. 45

    Grammar (C#)

    the grammar of C#

    Making java groovy

    more productive by removing the boilerplate of Java, and to simplify their programming lives by giving them compelling and straightforward APIs to work with. I’m proud to say that the Groovy team ...

    Corpus linguistics and English reference grammars

    differences between the new Cambridge Grammar of the English Language (CamGr), the Comprehensive Grammar of the English Language (CGEL), and the Longman Grammar of Spoken and Written English (LGSWE). ...

    addison.wesley.opengl.shading.language.2nd.edition.jan.2006

    This material is organized to present the details of a programming language. This section serves as a useful reference section for readers who have developed a general understanding of the language. ...

Global site tag (gtag.js) - Google Analytics