This preview shows pages 1–3. Sign up to view the full content.
Chapter 17 – Introduction to Parsing
We have learnt that trees are convenient way of representing expressions.
The question
now becomes  how does one get from a sequence of characters to a tree?
Or, to put it
another way, how can we take an expression, as typed in by the user, and convert it to the
corresponding tree?
This involves “parsing” the input expression.
The parsing process requires that we have
a very precise set of rules as to what the input can look like.
This set of rules is called a
“grammar”.
We’ll start with the following grammar:
<operator>
::=
+    *  /
<digit>
::=
0123456789
<number>
::=
<digit>  <digit><number>
<expression> ::= <number> 
( <expression><operator><expression> )
The grammar is expressed in “Backus Naur Form” (BNF).
::=
means
“is defined to be”
or “looks like” or
“can be replaced by” or just plain “is a”

means
OR (either alternative may apply)
<…>
indicates a “nonterminal” (an item defined
within the grammar).
A grammar must contain
a definition (a “rule”) for every nonterminal used.
(, ), +, , …, 0, 1 …
are “terminals” (items which
correspond directly to input items).
<expression>
is the “goal symbol” (what we’re
out to recognize) for this particular grammar.
This isn’t explicitly indicated.
This grammar defines “fully parenthesized” expressions.
It says that an expression is
either a <number> or the combination of a left parenthesis, an <expression>, an
<operator>, another <expression>, and a right parenthesis.
Note that this definition is
recursive.
A <number> is defined to be either a single <digit> or the combination of a <digit >
followed by a <number>.
This is a rather roundabout way of saying that a <number>
consists of one or more <digit>’s. An <operator> is one of the four possibilities listed,
and a <digit> is one of ‘0’ through ‘9’.
This preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentA string conforms to a grammar if and only if we can produce the string by starting with
the grammar’s “goal” and applying the grammar’s rules.
Example: is “(3+24)” valid?
start with
<expression>
(the “goal”)
apply <expression> := (<expression><operator>…,
giving
(<expression><operator><expression>)
apply <expression> := <number>
giving
(<number><operator><expression>)
apply <number> := <digit>,
giving
(<digit><operator><expression>)
apply <digit> := 3, giving
(3<operator><expression>)
apply <operator> := +, giving
(3+<expression>)
apply <expression> := <number>, giving
(3+<number>)
apply <number> := <digit><number>, giving
(3+<digit><number>)
apply <digit> := 2, giving
(3+2<number>)
apply <number> := <digit>, giving
(3+2<digit>)
apply digit := 4, giving
(3+24)
“(3+24)” is valid.
Note that, given this grammar, “3+24” is not valid.
The rule applications necessary to get from the goal to the input define a tree  the “parse
tree”.
Note the difference between this tree and the expression trees we looked at earlier.
In
this one the nonleaf nodes correspond to grammar nonterminals instead of to operators.
This is the end of the preview. Sign up
to
access the rest of the document.
 Fall '07
 Unknown

Click to edit the document details