On Jan 22, 2012, at 5:34 AM, Luca Cerone wrote:
> first of all I just wanted to say hi to everybody, since I'm new in
> the group.
> I'm trying to learn programming techniques and C++ following
> Stroustrup's book,
> but I got a bit confused by Chapter 6.
>
> The coding part is ok for me (and I liked the trial and error approach
> to explain how to write
> code to the readers), but the grammar confuses me a bit.
>
> Take the case of parsing 2+3, for example, as it is shown in the book.
> We start from 2 and say that it is an expression because
> 2 -> number -> primary -> term -> expression.
> So far so good.
> I have a problem to understand why 3 stops to be a term, and is not
> "brought up" to be considered an expression.
>
> I know it is silly but I really can't understand how the grammar stops
> 3 in being a Term rather than an Expression.
Try working this top-down, rather than bottom-up. After all, the end goal is recognizing an expression; terms, factors, and suchlike are merely intermediate steps.
Art Werschulz (8-{)} "Metaphors be with you." -- bumper sticker
GCS/M (GAT): d? -p+ c++ l u+(-) e--- m* s n+ h f g+ w+ t++ r- y?
Internet: agw STRUDEL comcast.net
<snip>
> Take the case of parsing 2+3, for example, as it is shown in the book.
> We start from 2 and say that it is an expression because 2 -> number -
>> primary -> term -> expression.
> So far so good.
I try to match the grammar with expressions.
Start with the simplest expression you can: 2
The grammar is:
Expression:
Term
Term
Primary
Primary
literal number
So, an expression is a term; a term is a primary; a primary is a literal
number.
expression -> term -> primary -> literal number.
This also corresponds to the order that the functions are called.
expression() calls term().
term() calls primary().
primary parses a literal number.
When primary() parses a literal number successfully it has matched the
grammar rule
Primary
literal number
When primary() returns to term() that matches the grammar rule
Term
Primary
And when term() returns to expression() that matches the grammar rule
Expression:
Term
So 2 is a valid expression all by itself.
Now tackle 2+3.
The grammar is:
Expression:
Term
Expression + Term
Term
Primary
Primary
literal number
> I have a problem to understand why 3 stops to be a term, and is not
> "brought up" to be considered an expression.
Because expression() hasn't finished yet. It is looking for any trailing "+
Term" values.
2+3
2+3+4
2+3+4+5+...
are all valid expressions. The grammar rule that says that 2+3+4 is an
expression is
Expression:
Expression + Term
If we match up the terms with the grammar here we get:
"2+3" == Expression
"+4" == "+ Term"
The same grammar rule says that 2+3 is an expression. Match the terms again,
2 == Expression
"+3" == "+ Term
Now we put it all together, the way the parser see it, with 2+3,
expression -> term -> primary -> literal == 2
expression + term
expression -> (expression -> term -> primary -> literal == 2)
+
term -> primary -> literal == 3
Now there are no more tokens so expression stops with
expression -> expression + term
2 + 3
== 5
>
> I know it is silly but I really can't understand how the grammar stops
> 3 in being a Term rather
> than an Expression.
So the answer is that expression is looking for the trailing "+ term"
grammar production rules before it completes. When term() returns
to expression(), expression looks at the next token to see if it is
'+' or '-'.
- tim
> --
> You received this message because you are subscribed to the Google Groups
> "PPP-public" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/ppp-public/-/3t7ZD2Bkv5IJ.
>
> To post to this group, send email to ppp-p...@googlegroups.com.
> To unsubscribe from this group, send email to
> ppp-public+...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/ppp-public?hl=en.