Saturday, November 01, 2003

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.

No comments:

Post a Comment