473,396 Members | 1,743 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Stroustrup "desk calculator" chapter 6

Stroustrup starts chapter 6 with a programme for desk-calculator:

here is a grammer for the langugae accepted by the calcualtor:

program:
END // END is end-of-input
expr_list END
expr_list:
expression PRINT // PRINT is semicolon
expression PRINT expr_list
expression:
expression + term
expression - term
term
.................................................
.................................................

what the heck it is?

Nov 9 '06 #1
14 4921
arnuld wrote:
Stroustrup starts chapter 6 with a programme for desk-calculator:

here is a grammer for the langugae accepted by the calcualtor:

program:
END // END is end-of-input
expr_list END
expr_list:
expression PRINT // PRINT is semicolon
expression PRINT expr_list
expression:
expression + term
expression - term
term
................................................
................................................

what the heck it is?
It's the grammar for the language to be accepted by the calculator, the
program for which appears in the pages following the one you're looking
at.

Best regards,

Tom

Nov 9 '06 #2
It's the grammar for the language to be accepted by the calculator, the
program for which appears in the pages following the one you're looking
at.
i know. i have seen the code but that code does not mean anything like
the DC programme (DC == desk calculcator).

why he started "expr_list:" after he did "expr_list END" on previous
line?
( he ended the list first & then started it)

Nov 9 '06 #3
does Stroustrup say this:

C++ programme - DC programme ( DC == Desk Calculator)

i mean, we will write a programme that will generate DC programme & we
will assum that will compute results (rather than computing results
directly with C++).

am i right?

Nov 9 '06 #4
arnuld wrote:
i know. i have seen the code but that code does not mean anything like
the DC programme (DC == desk calculcator).

why he started "expr_list:" after he did "expr_list END" on previous
line?
( he ended the list first & then started it)
The grammar only says what's accepted after a token, that's it. It's
only saying that you can have a "expr_list" after "program" but it have
to END there. Than, later, it says what have to be after "expr_list",
that's all.

You should implement your parser following those rules, that's all.

--renato

--
Reclaim your digital rights, eliminate DRM, learn more at
http://www.defectivebydesign.org/what_is_drm
Nov 9 '06 #5
arnuld <ar*****@gmail.comwrote:
Stroustrup starts chapter 6 with a programme for desk-calculator:

here is a grammer for the langugae accepted by the calcualtor:

program:
END // END is end-of-input
expr_list END
expr_list:
expression PRINT // PRINT is semicolon
expression PRINT expr_list
expression:
expression + term
expression - term
term
................................................
................................................

what the heck it is?
It is describing the format of what the calculator will accept. Your
calculator will take in a program.

A program is either empty (all it has is END), or it is an expr_list
followed by END.

An expr_list is an expression followed by PRINT (semicolon), or it is an
expression followed by PRINT followed by another expr_list.

An expression is either an expression followed by + followed by a term,
or an expression followed by - followed by a term, or just a term.

As you can see, these kinds of grammars tend to be very recursive in
nature.

The syntax is a little different, but maybe this explanation will be
enlightening:
http://en.wikipedia.org/wiki/Backus-Naur_form

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
Nov 9 '06 #6
arnuld wrote:
It's the grammar for the language to be accepted by the calculator, the
program for which appears in the pages following the one you're looking
at.

i know. i have seen the code but that code does not mean anything like
the DC programme (DC == desk calculcator).
The dc program you find on some Unix systems uses reverse polish
notation.

This textbook program uses infix, and can only add and subtract.
why he started "expr_list:" after he did "expr_list END" on previous
line?
Let's see:

program:
END // END is end-of-input
expr_list END
The above grammar production rule means that a program is one of two
things. Either the symbol END, or an expr_list followed by the symbol
END. In other words, a program is an optional expression list followed
by an indication of the end of input.
expr_list:
expression PRINT // PRINT is semicolon
expression PRINT expr_list
This definition further specifies what an expression list is. Without
this definition, the production rule for "program" is incomplete.

Stroustrup is using the convention that names in ALL CAPS are terminal
symbols: symbols which do not name phrase structure rules, but
represent atoms in the input.

Lower case names are names of grammar production rules.

Another convention he's using is that there is an implicit "or" between
the lines of the production. Each line of the production rule is an
alternate production. So an expr_list is either an expression followed
by PRINT, or an expression followed by PRINT followed by another
expr_list.

He makes the note that PRINT is semicolon. That is to say, the the
abstract PRINT token is supposed to be produced when a semicolon
character is read from the input.

Nov 9 '06 #7
Marcus Kwok wrote:
what the heck it is?

It is describing the format of what the calculator will accept. Your
calculator will take in a program.

A program is either empty (all it has is END), or it is an expr_list
followed by END.

An expr_list is an expression followed by PRINT (semicolon), or it is an
expression followed by PRINT followed by another expr_list.

An expression is either an expression followed by + followed by a term,
or an expression followed by - followed by a term, or just a term.

As you can see, these kinds of grammars tend to be very recursive in
nature.
thanks.
The syntax is a little different, but maybe this explanation will be
enlightening:
http://en.wikipedia.org/wiki/Backus-Naur_form
yes, it helped a lot to understand the desk calculator.

Nov 10 '06 #8
Kaz Kylheku wrote:
program:
END // END is end-of-input
expr_list END

The above grammar production rule means that a program is one of two
things. Either the symbol END, or an expr_list followed by the symbol
END. In other words, a program is an optional expression list followed
by an indication of the end of input.
ok

He makes the note that PRINT is semicolon. That is to say, the the
abstract PRINT token is supposed to be produced when a semicolon
character is read from the input.
this is what i said: our job is to produce a C++ programme that will
output DC notation, rather than computing results directly with C++.
right?

Nov 10 '06 #9
* arnuld:
>Kaz Kylheku wrote:
>>program:
END // END is end-of-input
expr_list END
The above grammar production rule means that a program is one of two
things. Either the symbol END, or an expr_list followed by the symbol
END. In other words, a program is an optional expression list followed
by an indication of the end of input.

ok

>He makes the note that PRINT is semicolon. That is to say, the the
abstract PRINT token is supposed to be produced when a semicolon
character is read from the input.

this is what i said: our job is to produce a C++ programme that will
output DC notation, rather than computing results directly with C++.
right?
You have it backwards. The C++ program is to analyze a calculator
"program" text, as a specification of what it should do, and then either
produce the numerical result (acting as an interpreter) or some
intermediate form of the specification (acting as a compiler) that's
easier and more efficient to evaluate. Anyway it will probably involve
recursive functions, that is, functions calling themselves, which is
then probably much of the point.

Personally I like much better to use L-systems to introduce the power of
recursive functions, creating "interesting" graphics, but then Bjarne
can't assume availability of any graphics package in that book.

And parsing is very much AT&T Bell Labs stuff...

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Nov 10 '06 #10
Alf P. Steinbach wrote:
You have it backwards. The C++ program is to analyze a calculator
"program" text, as a specification of what it should do, and then either
produce the numerical result (acting as an interpreter) or some
intermediate form of the specification (acting as a compiler) that's
easier and more efficient to evaluate. Anyway it will probably involve
recursive functions, that is, functions calling themselves, which is
then probably much of the point.

Personally I like much better to use L-systems to introduce the power of
recursive functions, creating "interesting" graphics, but then Bjarne
can't assume availability of any graphics package in that book.

And parsing is very much AT&T Bell Labs stuff...
is it really necessary to understand DC programme to understand rest of
the book? it is too complex for me to understand it. can i leave this
& go directly to section 6.2 without much loss?

Nov 10 '06 #11
* arnuld:
>Alf P. Steinbach wrote:
>You have it backwards. The C++ program is to analyze a calculator
"program" text, as a specification of what it should do, and then either
produce the numerical result (acting as an interpreter) or some
intermediate form of the specification (acting as a compiler) that's
easier and more efficient to evaluate. Anyway it will probably involve
recursive functions, that is, functions calling themselves, which is
then probably much of the point.

Personally I like much better to use L-systems to introduce the power of
recursive functions, creating "interesting" graphics, but then Bjarne
can't assume availability of any graphics package in that book.

And parsing is very much AT&T Bell Labs stuff...

is it really necessary to understand DC programme to understand rest of
the book?
I don't think so, because I don't recall the book mentioning any program
called "DC".

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Nov 10 '06 #12
Alf P. Steinbach wrote:
I don't think so, because I don't recall the book mentioning any program
called "DC".
ok, ok here is the complete thing:

is it really necessary to understand "Desk Calculator programme" in
chapter 6 to understand rest of
the book?

it is too complex for me to understand it. can i leave this section
(6.1) of Desk Calculatro and go directly to section 6.2 without much
loss?

Nov 10 '06 #13
* arnuld:
>Alf P. Steinbach wrote:
>I don't think so, because I don't recall the book mentioning any program
called "DC".

ok, ok here is the complete thing:

is it really necessary to understand "Desk Calculator programme" in
chapter 6 to understand rest of
the book?

it is too complex for me to understand it. can i leave this section
(6.1) of Desk Calculatro and go directly to section 6.2 without much
loss?
What's the exact first /word/ in that text where it starts sounding
difficult?

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Nov 10 '06 #14
* Alf P. Steinbach:
* arnuld:
>>Alf P. Steinbach wrote:
>>I don't think so, because I don't recall the book mentioning any program
called "DC".

ok, ok here is the complete thing:

is it really necessary to understand "Desk Calculator programme" in
chapter 6 to understand rest of
the book?

it is too complex for me to understand it. can i leave this section
(6.1) of Desk Calculatro and go directly to section 6.2 without much
loss?

What's the exact first /word/ in that text where it starts sounding
difficult?
Sorry, I'm beginning to see your problem.

I learned C++ from the first edition of TCPPPL, which was clear and
short and sweet, being based on K&R TCPL. Comparing it to the second
edition (someone got away with "borrowing" and never returning the third
edition, so I don't have it), which is four to five times heavier (in
weight) and much less readable: The very clear & short language overview
at the start of the first edition has been replaced with /long/ sections
of nothing particularly useful to know, in the second edition.

And without the clear language overview first, the desktop calculator
program may be difficult to grok; I mean, in the second edition not even
the 'for' loop has been explained at this point, not that 'for' loops
are explained later on either, except in the reference section. It was
of course properly explained in the first edition...

I therefore suggest looking up things in the reference section at the
back of the book.

That reference section is a very very positive feature of the book, and
before the standard of 1998 it served as a kind of de facto standard
(together with the Annotated Reference Manual book, the "ARM", now not
particularly useful).

Another reason why the program can be hard to understand is that it's
presented one small piece at a time, has some unreadable names, and uses
raw arrays and pointers and such.

So for your & others' convenience I've placed a version that has
(hopefully) more readable names, no pointer or raw array usage for most
of the code, and is in one single file, at <url:
http://home.no.net/alfps/misc/dc.cpp>.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Nov 11 '06 #15

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Greg Andora | last post by:
Hello, I've had an ASP page that worked for at the minimum for a year and now it is acting very odd and I need some help to fix it. What my page does/did is creates a Word.Application object and...
13
by: Squid Seven | last post by:
This is just bizarre. for the following snippet of code: #include <string> using std::string; I get the error message:
13
by: Simon Wigzell | last post by:
Can I trap when the scroll is ended? This is what I want - the calculator disapears when the site visitor starts scrolling the main page. I'm doing that with an "onScroll()" function. I'm bringing...
31
by: Xero | last post by:
I have an array in my program. (Declared as letters(16, 16)). When I paused the program in debugging mode, I saw the following in the Autos window: letters |- (0, 0) | Nothing |- (0, 1) ...
13
by: Fao | last post by:
Hello, I am having some problems with inheritance. The compiler does not not return any error messages, but when I execute the program, it only allows me to enter the number, but nothing else...
4
by: pcnerd | last post by:
I've been playing with "classic" VB since version 3. I have VB6 Learning Edition. Recently, I wanted to try VB.NET. I got a beginner's book with a CD with the software & installed it. There are...
11
by: arnuld | last post by:
this is the code which runs without any trouble: ----------------------------------------------------- #include <iostream> #include <string> #include <vector> struct Entry { std::string...
5
by: arnuld | last post by:
it compiles without any trouble but produces "Segmentation Fault" when i try to run it. since i am at chapter 2 so my knowledge of arrays is limited to chapter 1: -------------------------...
2
by: Chamila | last post by:
Why am I getting errors when I try to run the calculator progam using Netbeans5.5? error :-" class,interface or enum expected" Also, explain what's"enum"?
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.