Programming and Problem Solving with Java

(やまだぃちぅ) #1
2.1 The Elements of Java Programs | 43

Metalanguages


Metalanguageis the word languagewith the prefix meta, which means “beyond” or “more compre-
hensive.” In other words, a metalanguage is a language that goes beyond a normal language by
allowing us to speak precisely about that language. It is a language for talking about languages.
One of the oldest computer-oriented metalanguages is the Backus-Naur Form(BNF), which is
named for John Backus and Peter Naur, who developed it in 1960. BNF syntax definitions are
written out using letters, numbers, and special symbols. For example, an identifier(a name for
something) in Java must be at least one letter, underscore, or dollar sign, which may or may not
be followed by additional letters, underscores, dollar signs, or digits. The BNF definition of an
identifier in Java is


::= |
< letter-digit-sequence > ::= | < letter-digit-sequence >
::= |
::= _ | $ | A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | P | Q | R | S | T | U | V | W | X | Y | Z
| a | b | c | d | e | f | g | h | i | j | k | l | m | n | o | p | q | r | s | t | u | v | w | x | y | z

::= 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9

where the symbol ::= is read “is defined as,” the symbol | means “or,” and the symbols < and > are
used to enclose words called nonterminal symbols(symbols that still need to be defined).
Everything else is called a terminal symbol.
The first line of the definition reads as follows: “An identifier is defined as a letter or a letter
followed by a letter-digit-sequence.” This line contains nonterminal symbols that must be
defined. In the second line, the nonterminal symbol is defined as a <let-
ter-or-digit> or as a followed by another . The self-refer-
ence in the definition is a roundabout way of saying that a can be a
series of one or more letters or digits. In the third line, a is defined as either a


or a . In the fourth and last lines, we finally encounter terminal symbols that de-
fine to be an underscore, dollar sign, or any of the uppercase or lowercase letters and
as any one of the numeric characters 0 through 9.
BNF is an extremely simple language, but that simplicity leads to syntax definitions that can
be long and difficult to read. An alternative metalanguage, the syntax diagram, is easier to follow.
It uses arrows to indicate how symbols can be combined. The following syntax diagrams define
an identifier in Java:
Free download pdf