java_language_specification_java_se_17_edition_chapter_2_-_grammar

Java Language Specification Java SE 17 Edition Chapter 2 - Grammar

Return to Java 17 Language Spec Table of Contents, Java 17 Language Spec, Java Language Specifications, Language Specifications, Java bibliography, Java DevOps, Java, Java topics, Awesome Java, Java development tools

“ (JSR-392 JvLngSpc17 2021)

Grammars

THIS chapter describes the context-free grammars used in this specification to define the lexical and syntactic structure of a program.

2.1 Context-Free Grammars

A context-free grammar consists of a number of productions. Each production has an abstract symbol called a nonterminal as its left-hand side, and a sequence of one or more nonterminal and terminal symbols as its right-hand side. For each grammar, the terminal symbols are drawn from a specified alphabet.

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.

2.2 The Lexical Grammar

A lexical grammar for the Java programming language is given in §3 ( Lexical

Structure). This grammar has as its terminal symbols the characters of the Unicode character set. It defines a set of productions, starting from the goal symbol Input

(§3.5), that describe how sequences of Unicode characters (§3.1) are translated into

a sequence of input elements (§3.5).

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), 11

2.3

The Syntactic Grammar

GRAMMARS

keywords (§3.9), literals (§3.10), separators (§3.11), and operators (§3.12) of the Java programming language.

2.3 The Syntactic Grammar

The syntactic grammar for the Java programming language is given in Chapters 4, 6-10, 14, and 15. This grammar has as its terminal symbols the tokens defined by the lexical grammar. It defines a set of productions, starting from the goal symbol CompilationUnit (§7.3), that describe how sequences of tokens can form syntactically correct programs.

For convenience, the syntactic grammar is presented all together in Chapter 19.

2.4 Grammar Notation

Terminal symbols are shown in 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 definitions for the nonterminal then follow on succeeding lines.

For example, the syntactic production:

IfThenStatement:

if ( Expression ) Statement

states that the nonterminal IfThenStatement represents the token if, followed by a left parenthesis token, followed by an Expression, followed by a right parenthesis token, followed by a Statement.

The syntax {x} on the right-hand side of a production denotes zero or more occurrences of x.

For example, the syntactic production:

Argument[[List:

Argument {, Argument}

12

GRAMMARS

Grammar Notation

2.4

states that an Argument[[List consists of an Argument, followed by zero or more occurrences of a comma and an Argument. The result is that an Argument[[List may contain any positive number]] of arguments.

The syntax [x] on the right-hand side of a production denotes zero or one occurrences of x. That is, x is an optional symbol. The alternative which contains the optional symbol actually defines two alternatives: one that omits the optional symbol and one that includes it.

This means that:

BreakStatement]]:

break Identifier ;

is a convenient abbreviation for:

BreakStatement]]:

break ;

break Identifier ;

As another example, it means that:

BasicForStatement:

for ( [ForInit ; Expression ; [ForUpdate ) Statement is a convenient abbreviation for:

BasicForStatement:

for ( ; Expression ; [ForUpdate ) Statement for ( ForInit ; Expression ; [ForUpdate ) Statement which in turn is an abbreviation for:

BasicForStatement:

for ( ; ; [ForUpdate ) Statement

for ( ; Expression ; [ForUpdate ) Statement for ( ForInit ; ; [ForUpdate ) Statement

for ( ForInit ; Expression ; [ForUpdate ) Statement which in turn is an abbreviation for:

13

2.4

Grammar Notation

GRAMMARS

BasicForStatement:

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 so the nonterminal BasicForStatement actually has eight alternative right-hand sides.

A very long right-hand side may be continued on a second line by clearly indenting the second line.

For example, the syntactic grammar contains this production:

NormalClassDeclaration:

{ClassModifier} class TypeIdentifier [ TypeParameters]

ClassExtends ] ClassImplements] [ ClassPermits ] ClassBody

which defines one right-hand side for the nonterminal NormalClassDeclaration.

The phrase (one of) on the right-hand side of a production signifies that each of the symbols on the following line or lines is an alternative definition.

For example, the lexical grammar contains the production:

ZeroToThree:

(one of)

0 1 2 3

which is merely a convenient abbreviation for:

ZeroToThree:

0

1

2

3

When an alternative in a production appears to be a token, it represents the sequence of characters that would make up such a token.

Thus, the production:

BooleanLiteral:

(one of)

true false

14

GRAMMARS

Grammar Notation

2.4

is shorthand for:

BooleanLiteral:

t r u e

f a l s e

The right-hand side of a production may specify that certain expansions are not

permitted by using the phrase “but not” and then indicating the expansions to be excluded.

For example:

Identifier:

IdentifierChars but not a Keyword or BooleanLiteral or NullLiteral

Finally, a few nonterminals are defined by a narrative phrase in roman type where it would be impractical to list all the alternatives.

For example:

RawInputCharacter:

any Unicode character

15

Java Language Specification Java SE 17 Edition Chapter 2 - Grammar

Fair Use Sources

Java: Java Fundamentals, Java Inventor - Java Language Designer: James Gosling of Sun Microsystems, Java Docs, JDK, JVM, JRE, Java Keywords, JDK 17 API Specification, java.base, Java Built-In Data Types, Java Data Structures - Java Algorithms, Java Syntax, Java OOP - Java Design Patterns, Java Installation, Java Containerization, Java Configuration, Java Compiler, Java Transpiler, Java IDEs (IntelliJ - Eclipse - NetBeans), Java Development Tools, Java Linter, JetBrains, Java Testing (JUnit, Hamcrest, Mockito), Java on Android, Java on Windows, Java on macOS, Java on Linux, Java DevOps - Java SRE, Java Data Science - Java DataOps, Java Machine Learning, Java Deep Learning, Functional Java, Java Concurrency, Java History,

Java Bibliography (Effective Java, Head First Java, Java - A Beginner's Guide by Herbert Schildt, Java Concurrency in Practice, Clean Code by Robert C. Martin, Java - The Complete Reference by Herbert Schildt, Java Performance by Scott Oaks, Thinking in Java, Java - How to Program by Paul Deitel, Modern Java in Action, Java Generics and Collections by Maurice Naftalin, Spring in Action, Java Network Programming by Elliotte Rusty Harold, Functional Programming in Java by Pierre-Yves Saumont, Well-Grounded Java Developer, Second Edition, Java Module System by Nicolai Parlog

), Manning Java Series, Java Glossary, Java Topics, Java Courses, Java Security - Java DevSecOps, Java Standard Library, Java Libraries, Java Frameworks, Java Research, Java GitHub, Written in Java, Java Popularity, Java Awesome List, Java Versions. (navbar_java and navbar_java_detailed - see also navbar_jvm, navbar_java_concurrency, navbar_java_standard_library, navbar_java_libraries, navbar_java_navbars)


© 1994 - 2024 Cloud Monk Losang Jinpa or Fair Use. Disclaimers

SYI LU SENG E MU CHYWE YE. NAN. WEI LA YE. WEI LA YE. SA WA HE.


java_language_specification_java_se_17_edition_chapter_2_-_grammar.txt · Last modified: 2024/04/28 03:38 (external edit)