Chapter 6: Calculator Grammar confuses me

2,172 views
Skip to first unread message

Luca Cerone

unread,
Jan 22, 2012, 5:34:49 AM1/22/12
to PPP-public
Dear all,
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 liket 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.

I thank you all guys in advance for your help!
Cheers, Luca

Art Werschulz

unread,
Jan 22, 2012, 10:52:36 AM1/22/12
to ppp-p...@googlegroups.com
Hi.

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

tfmccarthy_verizon_mail

unread,
Jan 22, 2012, 10:53:09 AM1/22/12
to ppp-p...@googlegroups.com
----- Original Message -----
From: "Luca Cerone" <luca_...@yahoo.it>
To: "PPP-public" <ppp-p...@googlegroups.com>
Sent: Sunday, January 22, 2012 5:34 AM
Subject: Chapter 6: Calculator Grammar confuses me


<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


Luca Cerone

unread,
Jan 24, 2012, 9:06:48 AM1/24/12
to ppp-p...@googlegroups.com
Thanks agw and tim.
I have got it now :)
Thanks a lot for your help!

Peter Russell

unread,
Feb 7, 2012, 3:18:31 AM2/7/12
to ppp-p...@googlegroups.com
I gave up on this. All this stuff had my head spinning. I skipped chapters 6 & 7

> --
> 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.

Benjamin Baird

unread,
Oct 18, 2014, 10:54:32 PM10/18/14
to ppp-p...@googlegroups.com
I know you posted this over 2 years ago but...  I pretty much gave up on chapter 6 because he went back and forth so often that I also had a very hard time understanding what he was doing.  I haven't gone back in like 6 months because of it, but I really would like to pick it back up.   I have to ask, did you have any problems when you skipped those chapters?

Jonewman

unread,
Oct 19, 2014, 11:44:03 PM10/19/14
to ppp-p...@googlegroups.com
This chapter was definitely difficult...I remember I spent a solid week on the calculator. Theres no particular concept here that will prevent you from understanding the rest of book. But it is very revealing in how programming languages actually work and it shows that writing your own grammar to solve a problem is often very useful!

To understand this chapter I just put a ton of statements to log out what was happening at each point in the program and went through alot of examples by hand. Id really recommend doing this. It took me a long time to be able to understand all the pieces and how they all came together. And just take the chapter for what it is: a great example at how a reasonably complex program works and how it is structured. Don't jump to any conclusions about your 'ability' if you can't understand parts of it and try not to get frustrated! Just set aside a few hours to go through it piece by piece and appreciate its cleverness! If you do that it will benefit you in the future for certain and will make other smaller/easier problems you encounter much less daunting.

So just give it a shot, but just don't worry about it too much.

Peter Russell

unread,
Oct 25, 2014, 10:22:02 AM10/25/14
to ppp-p...@googlegroups.com
Hello

Thanks for the response. I had been away for quite a while. But your
post inspired me to go back and take a closer and more steady approach
to these chapters.

Thanks and regards Pete
> To unsubscribe from this group and stop receiving emails from it, send an
> email to ppp-public+...@googlegroups.com.
> To post to this group, send email to ppp-p...@googlegroups.com.
> Visit this group at http://groups.google.com/group/ppp-public.
> For more options, visit https://groups.google.com/d/optout.

Osman Zakir

unread,
Jan 3, 2017, 11:21:26 AM1/3/17
to PPP-public, luca_...@yahoo.it
I wonder if it's still to post here.  I also have a question about Chapter 6. 

In Section 6.3.1, where Stroustrup says that testing the calculator where it tried to handle +, -, *, and / using a switch-case construct by giving it the input 1+3*3x ('x' terminates the expression here) gave the answer 9 instead of 7, when I tested it I got 1.  I don't understand what I'm doing wrong, if anything.  Could somebody please help me out?

I've attached my .cpp file to this message for reference.  Thanks in advance to anyone who can help.
expression_calc_1.cpp

thejoh...@gmail.com

unread,
Jan 25, 2017, 5:50:42 PM1/25/17
to PPP-public, luca_...@yahoo.it
Hi Osman, looking at your code, why all the cin.ignore()'s ?

If you remove these you get the expected behaviour I think. Note also that Stroustrup is talking about 1+2*3 (not 1+3*3).

Does this help?

Thanks,
John.

cortical_iv

unread,
Apr 25, 2018, 12:23:55 PM4/25/18
to PPP-public
I just worked through Chapter 6 and am through Section 7.8.

I just found this thread, and want to say I wish I had seen TimM's answer first: it is incredibly helpful!

I almost gave up on Chapter 6 about three times before I finally got stubborn and decided to dig and and just do it. I spent about six hours working through the code by hand, adding 'cout' pretty much everyone to make sure I understood what was going on, why the 'putback' was needed ever place it was needed, etc.. I frankly had to just ignore the book because I found it more confusing than helpful the way he went back and forth and threw out sometimes unrelated things.

I first made a program to just explore token streams, to make sure I understood their deal.

I then studied the full program (calculator02.cpp or whatever: the non-buggy version), and studied expression(), then term(), and then primary(). The nice thing is, once you understand the basic logic of *one* of the rules (e.g., term()), then the basic flow of all the rules is basically the same.

I then studied main() to make sure I understood how it was all fitting together. at that point, I was able to fix certain bugs (e.g., it wasn't exiting properly, but calling expression() after the quit key was entered by the user).

Then, once I worked through the code, and played with it, commented it, and fixed little things myself, *then* I went through and read the chapter. This time I got a lot out of it, and really felt like I understood it, and got some things out of it.

It is not a chapter I would call well-written. It is what I would call a challenge. It helps that I have studied classes before in C++ otherwise I would have been really confused. It would have been really nice if the book included the basics of classes in a separate chapter before this one.


Reply all
Reply to author
Forward
0 new messages