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-
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-
2.3. The Syntactic Grammar
and
15
. This grammar has tokens defined by the lexical grammar as its terminal symbols.
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.