King Fahd University of Petroleum and Minerals

Department of Information and Computer Science

ICS 313: Fundamentals of Programming Languages

Fall 1999-2000

Date: 16th September 1999              Assignment # 1: Syntax and Semantics         Due date: 25th September 1999

Deliverables and Submission guidelines

 

Download this assignment from the course page and take it on a floppy. If you open it in a browser, you will have the advantage of easy navigation to some web documents through hot links. Deliverables and Submission guidelines are provided on-line.

 

On-line Exploration and Reading Part

 

Exercise 1 [Online Reference Section]:

Programming Languages Reference Section, Java Reference Section, and other Online Books. Examine the various programming language books in it. In particular review your C-programming basics as you will be doing a programming assignment in C shortly. Skim through the following books (just) for the discussion of C:

(i)                  Visual C++ Unleashed

(ii)                C++ Builder Unleashed

(iii)               Visual C++ in 12 Easy Lessons

(iv)              Teach Yourself  C++ in 21 Days

If you have any good book on C detailed examination of the above can be skipped. In addition, slowly, start familiarizing yourself with the books and tutorials on Java from the Java Reference Section.

 

Exercise 2 [Reading from Textbook]:

Chapter 3 (excluding Section 3.6.3) of the textbook.

 

Written Part

 

1. Written Exercises [Problems from Chapter 3 of the Textbook]:

Solve the following problems from pages 147-149 of the textbook.

 

Problems

Topic

2 (c—d)

EBNF

4                    

Sentence Derivation

5                      

Ambiguity

12 (c—d)             

Operational Semantics

14                    

Weakest pre-Conditions

(see below)

Program Correctness

 

2. Written Exercises [Problems from Chapter 3 of the Textbook]:

Prove the following program is correct.

 

{ x = Vx and y = Vy }

if (x > y) then

    temp = x;

    x = y;

    y = temp;

endif

{ x = min(Vx , Vy) and y = max(Vx , Vy)}

 

3. Optional Written Exercises for Extra Credit:

Solve problem 20 from page 154 of the textbook.


King Fahd University of Petroleum and Minerals

Department of Information and Computer Science

ICS 313: Fundamentals of Programming Languages

Fall 1999-2000

Date: 16th September 1999              Assignment # 1: Syntax and Semantics         Due date: 25th September 1999

 

1. Written Exercises [Problems from Chapter 3 of the Textbook]:

Q. 2. (pp. 147)                                                                                                                         (10 points)

c)      A C switch statement

<switch_stmt> ® switch (<expr>) case <literal> : <stmt_list>

{case <literal> : <stmt_list>} [default : <stmt_list>]}

 

d)   A C union definition

<union> ::= union [<identifier>] { { <structure-declaration> } }
<structure-declaration> ::= <type-specifier> {<identifier>}

 

 

Q. 4. (pp. 147)                                                                                                                         (40 points)

In the following parts we will use the following appreviations: <a> for <assign>, <e> for <expr>, <t> for <term>, <f> for <factor> and <id> for <identifier>.

a)   A := (A + B) * C

                        Parse Tree                                                      Left-most Derivation

 

                        <assign>                                                     <assign>     => <id> := <e>

                                                                                                            => A := <e>

            <id>          :=              <e>                                                        => A := <t>

                                                                                                            => A := <f> * <t>

              A                             <t>                                                          => A := ( <e> ) * <t>

                                                                                                            => A := ( <e> + <t> ) * <t>

                           <f>              *                  <t>                                     => A := ( <t> + <t> ) * <t>

                                                                                                            => A := ( <f> + <t> ) * <t>

            (              <e>               )                 <f>                                     => A := ( <id> + <t> ) * <t>

                                                                                                            => A := ( A + <t> ) * <t>

            <e>            +                <t>             <id>                                    => A := ( A + <f> ) * <t>

                                                                                                            => A := ( A + <id> ) * <t>

            <t>                               <f>              C                                      => A := ( A + B ) * <t>

                                                                                                            => A := ( A + B ) * <f>

            <f>                               <id>                                                      => A := ( A + B ) * <id>

                                                                                                            => A := ( A + B ) * C

            <id>                               B                                                       

                                                                                                        

              A

 

 


b)   A := B + C + A

 

                        Parse Tree                                                      Left-most Derivation

 

                        <assign>                                                     <assign>     => <id> := <e>

                                                                                                            => A := <e>

            <id>          :=              <e>                                                         => A := <e> + <t>

                                                                                                            => A := <e> + <t> + <t>

              A           <e>               +                <t>                                     => A := <e>

                                                                                                            => A := <t> + <t> + <t>

            <e>           +             <t>                  <f>                                     => A := <f> + <t> + <t>

                                                                                                            => A := <id> + <t> + <t>

            <t>                           <f>                 <id>                                    => A := B + <t> + <t>

                                                                                                            => A := B + <f> + <t>

            <f>                          <id>                  A                                      => A := B + <id> + <t>

                                                                                                            => A := B + C + <t>

            <id>                            C                                                           => A := B + C + <f>

                                                                                                            => A := B + C + <id>

           B                                                                                            => A := B + C + A

 

c)   A := A * (B + C)

                        Parse Tree                                                      Left-most Derivation

 

            <assign>                                                                 <assign>     => <id> := <e>

                                                                                                            => A := <e>

<id>          :=           <e>                                                                        => A := <t> * <f>

                                                                                                            => A := <f> * <f>

A           <t>            *               <f>                                                       => A := <id> * <f>

                                                                                                            => A := A * <f>

               <f>             (              <e>         )                                            => A := A * (<e>)

                                                                                                            => A := A * (<e> + <t>)

               <id>            <e>           +            <t>                                        => A := A * (<e> + <t>)

                                                                                                            => A := A * (<t> + <t>)

                 A              <t>                         <f>                                        => A := A * (<f> + <t>)

                                                                                                            => A := A * (<id> + <t>)

                                 <f>                         <id>                                       => A := A * (B + <t>)

                                                                                                            => A := A * (B + <f>)

                                 <id>                         C                                         => A := A * (B + <id>)

                                                                                                            => A := A * (B + C)

                                    B                                                                     


d)   A := B * (C * (A + B))

                        Parse Tree                                                      Left-most Derivation

 

         <assign>>                                                                  <assign>     => <id> := <e>

                                                                                                            => A := <e>

<id>         :=               <e>                                                                     => A := <t> * <f>

                                                                                                            => A := <f> * <f>

A                 <t>         *            <f>                                                       => A := <id> * <f>

                                                                                                            => A := B * <f>

                     <f>          (           <e>            )                                         => A := B * (<e>)

                                                                                                            => A := B * (<t> * <f>)

                     <id>         <t>         *               <f>                                     => A := B * (<f> * <t>)

                                                                                                            => A := B * (<id> * <t>)

                        B          <f>          (              <e>         )                          => A := B * (C * <t>)

                                                                                                            => A := B * (C * <f>)

                                    <id>         <e>           +               <e>                  => A := B * (C * (<e>))

                                                                                                            => A := B * (C * (<e> + <e>))

                                      C           <t>                            <t>                   => A := B * (C * (<t> + <e>))

                                                                                                            => A := B * (C * (<id> + <e>))

                                                   <f>                            <f>                   => A := B * (C * (A + <e>))

                                                                                                            => A := B * (C * (A + <t>))

                                                   <id>                           <id>                  => A := B * (C * (A + <f>))

                                                                                                            >=> A := B * (C * (A + <id>))

                                                      A                             C                    => A := B * (C * (A + B))

 

 

Q. 5. (pp. 147)                                                                                                                         (10 points)

We prove that this grammar is ambiguous by showing at least owing two distinct parse tree for the same string. For the expression a+b+c we can generate the following parse trees:

 

            <S>                                                                                         <S>

 


            <A>                                                                                         <A>

 


<A>           +          <A>                                                           <A>           +                <A>

 


<A>      +        <A>                                                                                                     <A>      +          <A>

 


a                      b                c                                                                a                       b                       c

 

 

 


Q. 12. (pp. 148)                                                                                                                       (10 points)

c)   FORTRAN 77 DO Statement                                                                      Operational Semantics

  DO  N   K = START, END, STEP                                        K := start

 ……….                                                                                  loop:                 if K > end goto out

N  CONTINUE                                                                         ……….

                                                                                          K := K + step

                                                                                          goto loop

                                                                                                      out: ...

 

c)   Pascal if-then-else                                                                                     Operational Semantics

if (Bolean Expression )                                                                  if not (Bolean Expression) goto a2

then  S1                                                                                          S1

else   S2                                                                                         goto exit……….

S3                                                                                            a2    S2

                                                                                          exit: S3

 

 

Q. 14. (pp. 148)                                                                                                                       (10 points)

a)      a := a2*b+1;

{P`} b := a-3  {Q} is {b<0} 

P` is a - 3 < 0 Þ a < 3

Now, we have:

{P} a := 2 * b + 1 {a < 3}

P is 2 * b + 1 < 3 Þ 2 * b < 2 Þ b < 1

 

b)      a := 3 * (2 * b + a);

{P`} b := 2 * a – 1 {Q} is {b > 5}

P` is 2 * a - 1 > 5 Þ 2 * a > 6 Þ a > 3

Now, we have:

{P} a := 3 * (2 * b + a) {a > 3}

P is 3 * (2 * b + a) > 3 Þ 6 * b + 3 * a > 3 Þ 2 * b + a > 1 Þ b > (1 - a) / 2

 

 

2. Written Exercises [Problems from Chapter 3 of the Textbook]:                                                     (20 points)

Prove the following program is correct.

 

{ x = Vx and y = Vy }

if (x > y) then

    temp = x;

    x = y;

    y = temp;

endif

{ x = min(Vx , Vy) and y = max(Vx , Vy)}

 

1.       {P`} y = temp { x = min(Vx , Vy) and y = max(Vx , Vy)

P`  is  x = min(Vx , Vy) and temp = max(Vx , Vy) - Based on the rule of assignment.

2.       {P``} x = y { x = min(Vx , Vy) and temp = max(Vx , Vy)}

P``  is y = min(Vx , Vy) and temp = max(Vx , Vy)- Based on the rule of assignment.

3.       {P```} temp = x {y = min(Vx , Vy) and temp = max(Vx , Vy)}

P``(P)  is y = min(Vx , Vy) and x = max(Vx , Vy)  - Based on the rule of assignment.

4.       {P and B} is y = min(Vx , Vy) and x = max(Vx , Vy) and x >y

               is y = Vy and x = Vx