Java Reference
In-Depth Information
4
Grammars and Parsing
For natural languages such as English or German, we are accustomed to using
rules of grammar to define proper sentence structure. Such rules may define
phrases in terms of subjects, verbs, and objects. Sentences could then defined
in terms of phrases and conjunctions. A properly structured sentence can be
diagrammed to show how its components conform to a language's grammar.
Grammars can also explain what is absent or superfluous in a malformed sen-
tence. A sentence's ambiguity can often be explained by providing multiple
diagrams for the same sentence (see Exercises 1 and 2).
Grammars thus serve as a concise definition of how meaningful sentences
in a language can be constructed and as a tool for diagnosing malformed
sentences. The first test of a sentence's validity is typically its adherence to
the language's grammar. Of course, it is possible to construct sentences in a
natural language that are grammatically correct but still make no sense. In
other words, a natural language's grammar captures a small but important
aspect of a sentence's validity with respect to a language.
A compiler's front-end performs several steps to establish the validity of
its input. An input stream is scanned for tokens as discussed in Chapter 3.
Tokens defined using regular sets could be processed by scanners that were
constructed automatically from the regular-set specifications. Just as regular
sets guide the actions of an automatically constructed scanner, so also can the
actions of the parsers described in Chapters 5 and 6 be guided by a grammar
that specifies a programming language's syntax.
113
 
 
Search WWH ::




Custom Search