Contents | Prev | Next | Index | Java Language Specification Second Edition |
CHAPTER 2
This chapter describes the context-free grammars used in this specification to define the lexical and syntactic structure of a program.
Starting from a sentence consisting of a single distinguished nonterminal, called the goal symbol, a given context-free grammar specifies a language, namely, the set of possible sequences of terminal symbols that can result from repeatedly replacing any nonterminal in the sequence with a right-hand side of a production for which the nonterminal is the left-hand side.
These input elements, with white space (§3.6) and comments (§3.7) discarded, form the terminal symbols for the syntactic grammar for the Java programming language and are called tokens (§3.5). These tokens are the identifiers (§3.8), keywords (§3.9), literals (§3.10), separators (§3.11), and operators (§3.12) of the Java programming language.
fixed
width
font in the productions of the lexical and syntactic grammars, and throughout this specification whenever the text is directly referring to such a terminal symbol. These are to appear in a program exactly as written. Nonterminal symbols are shown in italic type. The definition of a nonterminal is introduced by the name of the nonterminal being defined followed by a colon. One or more alternative right-hand sides for the nonterminal then follow on succeeding lines. For example, the syntactic definition:
states that the nonterminal IfThenStatement represents the tokenIfThenStatement: if ( Expression ) Statement
if
, followed by a left parenthesis token, followed by an Expression, followed by a right parenthesis token, followed by a Statement. As another example, the syntactic definition:
states that an ArgumentList may represent either a single Argument or an ArgumentList, followed by a comma, followed by an Argument. This definition of ArgumentList is recursive, that is to say, it is defined in terms of itself. The result is that an ArgumentList may contain any positive number of arguments. Such recursive definitions of nonterminals are common.ArgumentList: Argument ArgumentList , Argument
The subscripted suffix "opt", which may appear after a terminal or nonterminal, indicates an optional symbol. The alternative containing the optional symbol actually specifies two right-hand sides, one that omits the optional element and one that includes it.
This means that:
is a convenient abbreviation for:BreakStatement: break Identifieropt ;
and that:BreakStatement: break ; break Identifier ;
is a convenient abbreviation for:ForStatement: for ( ForInitopt ; Expressionopt ; ForUpdateopt ) Statement
which in turn is an abbreviation for:ForStatement: for ( ; Expressionopt ; ForUpdateopt ) Statement for ( ForInit ; Expressionopt ; ForUpdateopt ) Statement
which in turn is an abbreviation for:ForStatement: for ( ; ; ForUpdateopt ) Statement for ( ; Expression ; ForUpdateopt ) Statement for ( ForInit ; ; ForUpdateopt ) Statement for ( ForInit ; Expression ; ForUpdateopt ) Statement
so the nonterminal ForStatement actually has eight alternative right-hand sides.ForStatement: for ( ; ; ) Statement for ( ; ; ForUpdate ) Statement for ( ; Expression ; ) Statement for ( ; Expression ; ForUpdate ) Statement for ( ForInit ; ; ) Statement for ( ForInit ; ; ForUpdate ) Statement for ( ForInit ; Expression ; ) Statement for ( ForInit ; Expression ; ForUpdate ) Statement
A very long right-hand side may be continued on a second line by substantially indenting this second line, as in:
which defines one right-hand side for the nonterminal ConstructorDeclaration.ConstructorDeclaration: ConstructorModifiersoptConstructorDeclarator Throwsopt
ConstructorBody
When the words "one of" follow the colon in a grammar definition, they signify that each of the terminal symbols on the following line or lines is an alternative definition. For example, the lexical grammar contains the production:
which is merely a convenient abbreviation for:ZeroToThree: one of0 1 2 3
When an alternative in a lexical production appears to be a token, it represents the sequence of characters that would make up such a token. Thus, the definition:ZeroToThree:0
1
2
3
in a lexical grammar production is shorthand for:BooleanLiteral: one oftrue false
The right-hand side of a lexical production may specify that certain expansions are not permitted by using the phrase "but not" and then indicating the expansions to be excluded, as in the productions for InputCharacter (§3.4) and Identifier (§3.8):BooleanLiteral:t r u e f a l s e
Finally, a few nonterminal symbols are described by a descriptive phrase in roman type in cases where it would be impractical to list all the alternatives:InputCharacter: UnicodeInputCharacter but notCR
orLF
Identifier: IdentifierName but not a Keyword or BooleanLiteral or NullLiteral
RawInputCharacter: any Unicode character
Contents | Prev | Next | Index | Java Language Specification Second Edition |