Saturday, November 01, 2003

Ok, now on to solving A in the book under number 7. What is the difference between it and what we just did? Instead of doing just one statement, we are doing a series of statements. Before we just delt with

while (a <= 9) S1

Now we are dealing with a whole block of statements...

{ if (C1)
S1;
S2;
}


I am going to use [ ] to mark out seperate statements and put it on one line here.

{ [if (C1) S1][S2]}

another words we have two statements we are dealing with here not just one. So what do we do. Well the example as it is in the book at this point in time is no good, as we are at a step before that step. To me, what I see in the grammar rules that fits this is statement list. But in checking what out instructor posted in the doc share I see that I am wrong. He says that it is a compound-statement. I see that he went back one step further than I did. That being the case.... <compund-statement> breaks down to { <declaration-list> <statement-list>}

OK I hear it now, we do not have a decloration list... well one of the items under the declaration list is that fully looking E, that means that it can be empty string. so for review, here is our chart so far.


<compound-statement>
|
________________________________________
| | | |
{ <declration-lists> <statemen-tlist }
|
E


Any questions.... nope, gee I must be good. ok from our original we have the { and the } taken care of, now to work on the statment-list. statement-list breaks down to the E OR <statement> OR <statement-list><statement>. Since we have more than one statement to deal with we have to go with <statement-list><statement>


<compound-statement>
|
________________________________________
| | | |
{ <declration-lists> <statemen-tlist> }
| |
E |
____________________
| |
<statement-list><statement>


We know that the <statement> breaks down to S2 but it takes a convulted way of getting there cause we have to worry about the ;.
<statement> breaks down to <expression> and ; and we now can make the <expression> S2

<compound-statement>
|
________________________________________
| | | |
{ <declration-lists> <statemen-tlist> }
| |
E |
____________________
| |
<statement-list><statement>
|
__________
| |
<expression> ;
|
S2

So now we have {. } and S2 taken care of. All we have left is if (C1) S1; to fit it into <statement-list> Again we cheat a bit here and work backwards... we need a if statement. What can we use from grammar rules to get it? Only one thing will let us to it, a <selection-statement>. So we look back and how to we get from <statement> to <selection-statement>, well <statement> can become a <selection-statement> along with several others. But right now we are sitting at <statement list> Look carefully what is under <statement list>, yup <statement>. Or in other words:


<compound-statement>
|
________________________________________
| | | |
{ <declration-lists> <statemen-tlist> }
| |
E |
____________________
| |
<statement-list><statement>
| |
| __________
| | |
| <expression> ;
| |
| S2
<statement>
|
<selection-statement>

<selection-statement> breaks down to the one we are looking fo: if (<expression> ) <statement>. <expression>, in this case breaks down easy also (it does not in later assignments). It is C1 So:

<compound-statement>
|
________________________________________
| | | |
{ <declration-lists> <statemen-tlist> }
| |
E |
____________________
| |
<statement-list><statement>
| |
| __________
| | |
| <expression> ;
| |
| S2
<statement>
|
<selection-statement>
_________________________
| | |
if (<expression>) <statement>
| |
(C1)


Now the other half. statement must break down into an expression and a ; . It must be come a <expression-statement>. That will break down to an <expression> and a ;.

<compound-statement>
|
________________________________________
| | | |
{ <declration-lists> <statemen-tlist> }
| |
E |
____________________
| |
<statement-list><statement>
| |
| __________
| | |
| <expression> ;
| |
| S2
<statement>
|
<selection-statement>
_________________________
| | |
if (<expression>) <statement>
| |
(C1) <expression-statement>
__________
| |
<expression> ;


Now replace the <expression> with S1 and our job is done:


<compound-statement>
|
________________________________________
| | | |
{ <declration-lists> <statemen-tlist> }
| |
E |
____________________
| |
<statement-list><statement>
| |
| __________
| | |
| <expression> ;
| |
| S2
<statement>
|
<selection-statement>
_________________________
| | |
if (<expression>) <statement>
| |
(C1) <expression-statement>
__________
| |
<expression> ;
|
S1


If this does not line up I am not sure what to tell you, I used standard fonts on my browser to put it together.
I hope this helps at least one classmate, if it does the time putting it together was worth it.

To understand a you have to 1) understand figure 7.4 in the book and also how it relates to grammar 7.5. Since the instructor has posted the answer to a I will post how I got mine as well. But before I get there:

explaning 7.4

We want to end up with [while (a < = 9) S1]
If you understand C or C++ this is a statement, that is why statement is at the top off the chart

Now go back to grammar 7.5 and look for statement aligned with the far left margin.. found it, good what is under it?
statement can break down to one of 4 things: compound-statement OR expression-statement OR selection-statement OR iteration-statement. Here I cheated a bit. I wanted to figure out how to get to a while statement so I looked to see what would get it. a while statement is under the <iteration-statemetn>. So at this point it looks like this:

statement
|
<iteration-statement>

and iteration statement gets what we want so we have:


statement
|
<iteration-statement>
|
____________________________________________________
| | |
while (<expression>) <statement>

So far so good? Ok thanks lets go on: We have while where we want it, so we stop with it. Now we have to get expression and statement where we need them. The simpliest is statement, that will be S1, so:

statement
|
<iteration-statement>
|
____________________________________________________
| | |
while (<expression>) <statement>
|
S1

All that is left is to convert expression to a <= 9, back to the grammar rules, lets look at expression.
expression breaks down into <relational-expression> OR <identifier>=<expression>. Again drawing upon my half vast knowledge of C and C++ I know that this is a <relational-expression>. So the graph now looks like this:



statement
|
<iteration-statement>
|
____________________________________________________
| | |
while (<expression>) <statement>
| |
<relational-expression> S1

Ok back to grammar rules, realtional-expression breaks down to what? A big long list I do not feel like typing out now, but notice one has <= in it? It is <relational-expression> <= <additive-expression>, lets throw that in the mix, shall we?


statement
|
<iteration-statement>
|
____________________________________________________
| | |
while (<expression>) <statement>
| |
<relational-expression> S1

|
_______________________________________
| | |
<relational-expression> <= <additive-expression>

To wrap this up quickly:
relational-expression can become additive expression
both additive expressions can become multipicative-expressions
both multipicative- expressions can become unary-expressions, then the left side becomes (again all this from grammar rules) can become a primary-expression which becomes an identifier which becomes a letter which we get to choose from the alphabet from and we choose a.

Meanwhile the right side, where we left off at the unary also becomes a primary-expression, but this time we follow the other primary-expression rules and take it to the constant. Constant chooses out of its two to ecome a integer-constant, choosing integer, choosing 9.

I would draw that last part out but the book does a better job.

Now a break, and change messages so that I can not over load this system. When we return we will look at 7A.

The Doc.