CIS 400 LECTURE 6

 

BNF

 

Expression ::= term | expression  addop  term

Term ::= operand | term  mulop  operand

Operand ::= a | b | c

Addop ::=   + | -

Mulop ::=  * | /

 

 

Natural ::= digit

Integer ::= [ +|- ]  natural

Digit ::= 0 | 1 | 2 | 3 | 4 | …

 

Top down parsing:  a*b+c

 

 

Bottom up parsing:

 

 

Syntax graphs:

 

 

 

 

CBL, (Cobol-Like-Grammar)

·        Optional element   []

·        Alternatives           <>

·        Optional alternatives             

                                              

·        repeated elements     

·        underline or boldface       key words or reserved words

 

Parser:

Series of sub-routine calls:

 

 

Nested "if/then else", (with error trap)

 

 

 

"while" (conditions) "do"

 

if (symbol a) = terminal

     read next char

else

     error

 

 

 


Semantics:

·        operational semantics

·        formal semantics

v    axiomatic semantics

v    denotational semantics

v    attribute grammars

 

Formal semantics, (why?)

·        rigorous and unambiguous definition

·        provides a basis for language comparison

·        implementation independent

·        basis for correctness group of language implementation

·        basis for program proofs

 

Axiomatic semantics, (chapter 3, page 134)

·        predicate calculus

·        logical expression involving program variables used to describe state of computation

 

EXAMPLE: for assignment statement

 

Pre-condition  à  {x = x0 and y = y0}

                                     z ß x + y 

 

Post-condition  à {z = x0 + y0}

 

Default assertion:

If you don't see a value of a variable, either it doesn't exist or doesn't change

 

à  {x = x0} where x0 is initial value

if (x < 0) then

   {x = x0 and x0 < 0}

    y = -x

   {y = |x0|}

else  {x = x0 and (x0 >=0)}

      y¬x

      {y = |x0|}

if {( x0 < 0 and y = -x0 ) or (x0 > 0 and y = x0)}

 

The above is obviously "absolute value"

 

®{x = x0, z = z0, y >= 0}

while  y<> 0 do

    {x = x0, z = z0}                                                  

     z ¬z + x

     {z = x0 + z0}

     {y = y0}

      y ¬ y - 1

     {y = y0 - 1}

end while

     { y = 0, z = z0 + y0 * x0 }

 

Loop invariant

    Execution continues

        while   (y >=0 and z = z0 + y0 * x0) where y0Î natural numbers