Java Reference
In-Depth Information
Chapter 2. Grammars
This chapter describes the context-free grammars used in this specification to define the lex-
ical 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 ab-
stract 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 se-
quences 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. This grammar has as
its terminal symbols the characters of the Unicode character set. It defines a set of produc-
tions, 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 ter-
minal 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 ), sep-
arators (§ 3.11 ), and operators (§ 3.12 ) of the Java programming language.
2.3. The Syntactic Grammar
A syntactic grammar for the Java programming language is given in Chapters 4 , 6 - 10 , 14 ,
and 15 . This grammar has tokens defined by the lexical grammar as its terminal symbols.
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.
Chapter 18 also gives a syntactic grammar for the Java programming language, better suited
to implementation than exposition. The same language is accepted by both syntactic gram-
mars.
Search WWH ::




Custom Search