UP | HOME

Operational Semantics of SimpleC

// abstract syntax: abstract syntax does not capture all of the concrete syntax's language restrictions

n       ::= [0-9]+                     // numeric values
b       ::= true | false               // boolean values
v       ::= n | b                      // values
e       ::= e op e | op e | (e) | n    // arithmetic expressions
op      ::= + | - | * | /              // numeric operators
op      ::= and | or | not             // boolean operators
op      ::= == | != | < | <= | > | >=  // relational operators
x       ::= [a-z]+                     // identifiers
e       ::= x                          // variable usage
e       ::= f(actuals)                 // function call expression
actuals ::= (e (, e)*)?                // function args
st      ::= x = e;                     // assignment statement
          | if (e) st else st          // conditional statement
          | while (e) st               // while statement
          | { st* }                    // compound statement
          | skip;                      // no op statement
def     ::= f(formals) st return e;    // function definition
formals ::= (x (, x)*)?                // formal arguments


// literals: the symbols mean their equivalent mathmetical values for numbers and boolean true/false.

--------------
S : <n> => n

--------------
S : <b> => b


// operators: the symbols for arithmetic, boolean, and relationship operators have their conventional meaning from mathematics.

S : <e1> => v1     S : <e2> => v2     v = v1 op v2
--------------------------------------------------
S : <e1 op e2> => v

S : <e1> => v1     v = op v2
----------------------------
S : <op e2> => v


// variables: variables assignments evaluate their right-hand side at define-time and are stored and looked up in a storage context.

v = S.lookup(x)
---------------
S : <x> => v

S : <e> => n     S' = S.put(x, n)
---------------------------------
S : <x = e;> => S'


// control-flow: conditionals and iteration are statements that update state but produce no value.

S : <e> => true     S : st1 => S'
---------------------------------  [if-true]
S : <if e st1 else st2> => S'

S : <e> => false    S : st2 => S'
---------------------------------  [if-false]
S : <if e st1 else st2> => S'

S : <e> => true     S : <st> => S'     S' : <while e st> => S''
---------------------------------------------------------------  [while-true]
S  : <while e st> => S''

S : <e> => false
----------------------   [while-false]
S : <while e st> => S

---------------
S : <skip> => S


// functions: functions are call-by-value, have a local storage context, and produce a return value

(formals, st, e) = functions.lookup(f)
S : actuals[1] => v[N] ... S : actuals[N] => v[N]
Slocal = {formals[i] = v[i]} for all i = 1..N
Slocal : st => Slocal'
Slocal' : e => v
-------------------------------------------
S : <f(actuals)> => v

functions.put(f, (formals, st, e))  // store function in global functions store
----------------------------------
S : <f(formals) st return e;> => S

Author: Paul Gazzillo

Created: 2022-02-14 Mon 11:49