Fall 2002 1
Semantics
Juan Carlos GuzmánCS 3123 Programming Languages ConceptsSouthern Polytechnic State University
Fall 2002 2
Why Semantics?
We need to establish precisely the meaning of programs
English is not enough – it is imprecise!
Fall 2002 3
Examples
What does it mean to compute x+5 x++ x+y
Fall 2002 4
More Examples
What does it mean to compute fact(5) fact(-5)
where fact is defined asint fact(n) {
if (n==0)
return 1;
return n * fact(--n);
}
Fall 2002 5
Still more Examples
What does it mean to defineint fact(n) {
if (n==0)
return 1;
return n * fact(--n);
}
Fall 2002 6
Issues
Any construct can be given a semantics, if one is persistent enough!The more complex the semantics, the less intuitive your language is!Be simple and elegant
Fall 2002 7
Classes of Semantics
Operational Semantics Meaning is given by presenting an “interpreter”:
the meaning of a language is the behavior of such an interpreter
Axiomatic Semantics Meaning given by using Logic to establish
assertions (properties) about programs
Denotational Semantics Based on mathematics (Function Theory,
Calculus) Meaning given by modeling each construct as a
function
Fall 2002 8
Operational Semantics
Meaning by interpretationEasy to construct: just build the machineTo know the meaning of a program, run the program in the machineThe machine provided is simple, of easy implementationThe limitation of this approach is that the meaning is given in the realm of computation: only computable concepts can be understood
Fall 2002 9
Operational Semantics (II)
Typical machine: JVM von Neumann machine Turing Machine!
for (exp1; exp2; exp3)
body
exp1
loop: if exp2 = 0 goto out
body
exp3
goto loop
out: …
Fall 2002 10
Limitations of Operational Semantics
Because meaning is given by another machine, there is no reasoning about noncomputable properties: nontermination function equality
Fall 2002 11
Axiomatics Semantics
Meaning by using LogicAssertions everywhere!Each assertion establishes the state of the system at any given timeComputation changes the system from one state to anotherTherefore, computation can be thought of a transformer of states
Fall 2002 12
Axiomatic Semantics (II)
State Transformer:{ P } S { Q }S being the statement, or construct, P is its precondition, and Q its postconditionP being valid before S is executed implies that Q must be valid after SThe “name of the game” is finding the weakest precondition so that the postcondition holds true!
Fall 2002 13
Example of Axiomatic Semantics
{ x>10 } sum = 2*x+1 { sum>1 }
Why the weakest precondition? The expected result of the program happens
to be the postcondition of the program, i.e., the postcondition of its last statement
We can work all the way back to find the weakest precondition of the program that would satisfy the expected result!
Anything stronger that the weakest precondition will work
Fall 2002 14
More Examples
{ P1 } S1; S2 { P3 }
{ P1 } S1 { P2 } { P2 } S2 { P3 }
Sequence
{ P } if B then S1 else S2 { Q }
{ B P } S1 { Q } {B P } S2 { Q }
If
{ I } while B do S { I B }
{ I B } S { I }While
Fall 2002 15
A Correct Program
{ n >= 0 }count = n;
fact =1;
while count <> 0 do
fact = fact * count;
count = count –1;
end
{ fact = n! }
Fall 2002 16
A Correct Program
{ n 0 }count = n;
fact =1;
{count 0 n 0 fact * count! = n! }while count <> 0 do
{count > 0 n 0 fact * count! = n! } fact = fact * count;
count = count –1;
end
{count = 0 n 0 fact * count! = n! }{ i.e., fact = n! }
Fall 2002 17
Denotational Semantics
Meaning given by modeling each construct as a functionMaps all syntactic objects to corresponding semantic elements: The number 12 present in the
program means the mathematical concept 12
Fall 2002 18
Example
Let e ::= n (number)| e1+e2 (sum)
Eval [[n]] = Number [[n]]Eval [[e1+e2]] = (Eval [[e1]]) + (Eval [[e2]])
Number [[n]], not defined, maps the input n into its corresponding mathematical concept
Fall 2002 19
Other Semantics
Many other ways of expressing the semantics of a program have been invented: you can always express the semantics if you try hard enough!Simple semantics usually means simple implementation and a better mathematical foundation
Fall 2002 20
Attribute Grammars
Attribute grammars are extensions to Context-Free grammars that allow certain language rules to be expressedThey are used to further restrict the phrases in a language
building and use of symbol tables: enforce static binding (decl. & use of id’s)
compute properties of phrases Construction of the AST
Fall 2002 21
Attribute Grammars (II)
There is the tendency associate syntax with the CFG used in parsing, and to call all other properties pertaining to the phrases of the language as semantics, or, more properly, static semanticsYour professor would rather call them “abstract interpretations”, with AG being one convenient way of expressing these
Fall 2002 22
Attribute Grammars (III)
An attribute grammar is a context-free grammar, with
attributes attribute computation functions predicate functions
Attributes are variables that can hold values on specific nodes of a syntax treeAttribute computation functions indicate how the values to those attributes are computedPredicate functions state properties that must be satisfied by the tree
Fall 2002 23
Attributes
Attributes are variables, associated to tree nodes, that will be given a value Synthesized attribute: the value of the
attribute is dependant on the information contained in the subtree where it is located
Inherited attribute: the value of the attribute is determined by the environment where the attribute is located
Fall 2002 24
Grammar for Simple Assignment Statements
assign var = exprexpr var2 + var3
expr varvar A | B | C
Allowable types are int or realAttributes:
actual_type, for nonterminals expr and var expected_type, for nonterminal expr string, for var
Fall 2002 25
Attribute Grammar for Simple Assignment Statements
assign var = expr expr.expected_type := var.actual_type
expr var2 + var3
expr.actual_type := if (var2.actual_type==int) && (var3.actual_type==int)
then int else real expr.actual_type == expr.expected_type
expr var expr.actual_type := var.actual_type expr.actual_type == expr.expected_type
var A | B | C var.actual_type := lookup(var.string)
Fall 2002 26
Parse Tree for A=A+B
assign
expr
var var var
A = A + BThis tree does not reflect the checking of the Predicate Functions, in this case, just in the expr node
Fall 2002 27
Flow of Attributes for A=A+B
assign
expr
var var var
A = A + B
actual_type actual_type actual_type
actual_typeexpected_type
This tree does not reflect the checking of the Predicate Functions, in this case, just in the expr node
Fall 2002 28
Fully Attributed Parse Tree for A=A+B
assign
expr
var var var
A = A + B
actual_type = intstring = A
actual_type = intstring = A
actual_type = intstring = B
expected_type = intactual_type = int
This tree does not reflect the checking of the Predicate Functions, in this case, just in the expr node