s0, Sl, - -- , Sn
such that s,-~ ~ s, for 1 ~< i ~< n is said tobe a
of s~ from ~0. We sometimesalso say s~ is
from s0.The start symbol of a grammar is called a
A string derivable from thestart symbol is also a
of thegrammar. A sentential form containing onlyterminal symbols is said to be a
generated by the grammar.
languagegenerated by a grammar (;,
is the set of sentences generated byG.
2.1: The following grammar,hereafter called G~, has LIST as its startsymbol:LIST --~ LIST ',' ELEMENTLIST --* ELEMENTELEMENT ~ 'a'ELEMENT --~
The sequence:LIST ~ LIST ',' ELEMENTLIST ',a'LIST ',' ELEMENT ',a'LIST
is a derivation of the sentence
consists of nonempty strings of a's and b's,separated by commas.Note that in the derivation in Example2.1, the rightmost nonterminal in each sen-tential form is rewritten to obtain the fol-lowing sentential form. Such a derivation issaid to be a
and each sen-tential form in such a derivation is called a
mght se~le~t~al form.
is a right sentential form of C1.If
is a right sentential form in whichw is a string of terminal symbols, and
then ~ is said to be a
of s~w *For example,
is the handle of the rightsentential formLIST
in Example 2.1.
• Some authors use a more restmctmg dehnltmn ofhandle
LR Parsing •
101A prefix of a~ in the right sentential form
is said to be a
of the gram-mar. For example,LIST ','is a viable prefix of G1, since it is a prefix ofthe right sentential form,LIST ',' ELEMENT(Both s and w are null here.)Restating this definition, a viable prefix ofa grammar is any prefix of a right sententialform that does not extend past the right endof a handle in that right sentential form.Thus we know that there is always somestring of grammar symbols that can be ap-pended to the end of a viable prefix to ob-tain a right sentential form. Viable prefixesarc important in the construction of com-pilers with good error-detecting capabilities,as long as the portion of the input we haveseen can be derived from a viable prefix,we can be sure that there are no errors that
be detected having scanned only thatpart of the input.
3. DERIVATION TREES
Frequently, our interest in a grammar isnot only in the language it generates, butalso in the structure it imposes on the sen-tences of the language. This is the case be-cause grammatical analysis is closely con-nected with other processes, such as compila-tion and translation, and the translations oractions of the other processes are frequentlydefined in terms of the productions of thegrammar. With this in mind, we turn ourattention to the representation of a deriva-tion by its
For each derivation in a grammar we canconstruct a corresponding derivation tree.Let us consider the derivation in Example2.1. To model the first step of the derivation,in which LIST is rewritten asLIST ',' ELEMENTusing production 1, we first create a rootlabeled by the start symbol LIST, and thencreate three direct descendants of the root,labeled LIST, ',', and ELEMENT:
Coraputmg Surveys, Vol 6, No 2, June 1974