You are on page 1of 30

Formal Semantics

Basic Definition

 Syntax (arrangement): This deals with the


question “Which strings of symbols (or words, or
tokens) count as valid expressions in the language?”

Semantics (meaning): This deals with the question


“What do valid expressions actually mean?” In the
case of programming languages, we typically
interpret this question as “What do programs
actually do when you run them?”
What is Formal semantics?

 It is the study of the


semantics, or interpretations, of
formal and also natural
languages.
Formal VS Natural Language

 Natural languages are the languages that people speak


like English, Spanish, and French. They were not
designed by people (although people try to impose some
order on them) they evolved naturally.

 Formal languages are languages that are designed by


people for specific applications.

Example: Chemists use a formal language to represent


the chemical structure of molecules.
Water = H2O
Question and Answer

 Question:
Is a Programming Language can be
considered as an example of Formal
Language?

 Answer:
Programming languages are formal
languages that have been designed to
express computations.
Cont.. What is Formal language?

 A formal language can be defined a part


from any interpretation of it.

 This is done by designating a set of


symbols (also called an alphabet) and a
set of formation rules (also called a formal
grammar) which determine which strings
of symbols are well-formed formulas.
Example

 The statement 3=+6$ is structurally illegal, because you can't


have a plus sign immediately after an equals sign.

 3+3 = 6 - √
 3=+6$ - x

 Molecular formulas have to have subscripts after the element


name, not before.

 H 2O - √
2HO - x

Cont… What is Formal semantics?

 It is the field concerned with the difficult


mathematical study of the meaning of
programming languages.
What is Formal semantics?

 It is a mathematically precise description


of the intended meaning of each
construct in the language.

 In contrast to a formal syntax for a


language, which tells us which sequences
of symbols are correctly formed programs.
Why study Formal semantics?

 A formal semantics would tells us on what


programs will actually do when we run them.

 The ideas of semantics are of importance for


language designers, compiler writers, and
programmers

 It also provide a basis for mathematical proofs


of the correctness of programs.
Goals of formal semantics

 Some of the reasons why one might want to


study the formal semantics of programming
languages.

1. Standardizing language definitions


2. Proving properties about programs
3. Assisting in language design.
1. Standardizing language definitions

 A). A formal semantics can offer a complete,


difficult definition of a language. This can serve
both as a reference for programmers wishing to
understand delicate points of the language, and
as a touchstone for implementers of the
language (e.g. compiler writers), by providing a
standard against which the correctness of an
implementation may be judged.
1. Standardizing language definitions

 B). For a language with a large user


community, providing a robust definition is
clearly a worthwhile investment of effort, and
helps to pre-empt the possibility that different
implementers might interpret the definition of
the language differently, with the consequent
possibility that a user’s program might run OK
on one implementation but not on another.
Advantages: Formal versus informal
definitions

1. It is mathematically precise and unambiguous

*** (in practice, informal definitions are sometimes


interpreted differently by different
implementers)

2. It is typically much more concise than a


corresponding English prose description.
Disadvantage: Formal versus informal
definitions

1. It is that the ideas of formal semantics are as


yet not very widely known and understood —
and many programmers are frightened of
anything that looks like maths!
2. Proving properties about programs.

A formal semantics can also provide a


foundation for mathematical proofs about
programs.

Formal semantics provides not only a framework for


making precise statements of correctness, but also
many of the logical and conceptual tools needed for
proving them.

*** Normally, “correct” will mean that the program


conforms to some formal specification
2 Kinds
Statements about the language as a whole

Example: various good properties of Standard ML can be


proved mathematically from the definition: for instance,
the fact that type errors and null pointers can never arise
at run time.

 Statements about particular programs

Example: Those statements asserting the correctness of


some program — that it does what it is supposed to do.
3. Assisting in language design

Ideas of semantics can provide guidance for language


designers.

Designing a good programming language, in which the


various features interact cleanly and in a
principled way and unnecessary complications are
avoided, is a difficult problem, and the attempt to
provide a formal semantics that can highlight
unnecessary complications and suggest simpler,
cleaner definitions.
4. Applications to compiler techniques

The theoretical ideas behind


semantics can actually be applied
to make code run faster!
4. Applications to compiler techniques.

Example

The ideas of formal semantics can be used to


design type systems which a compiler may use
to determine that a certain code optimization is
legal.
In principle, any mathematical description of a
programming language that correctly
predicted the behaviour of programs would
count as a “semantics”.

 Whatever technique we use, we want to strive


for clarity and simplicity in our description,
avoiding unnecessary detail as far as possible.
In practice, there are three particular styles of
semantics that are traditionally considered.

1. Axiomatic semantics
2. Denotational semantics
3. Operational semantics
Application Areas

 Axiomatic: initial specification


– Which properties shall language have?

 Denotational: meaning
– Which semantics provides properties?

 Operational: implementation
– How can semantics be implemented?
Relationship
Axiomatic semantics

 This define the meaning of a command in a program


by describing its effect on assertions about the
program state.

 Assertions are logical statements - predicates with


variables, where the variables define the state of the
program.

 This makes no distinction between a phrase's


meaning and the logical formulas that describe it; its
meaning is exactly what can be proven about it in
some logic.
Denotational semantics

 It is an approach to formalizing the meanings of


programming languages by constructing
mathematical objects (called denotations) which
describe the meanings of expressions from the
languages.

 Denotational semantics is concerned with finding


mathematical objects that represent what programs
do. Collections of such objects are called domains.
Denotational semantics

 An important tenet of denotational semantics is


that semantics should be compositional:

 the denotation of a program phrase should be


built out of the denotations of its subphrases.

 Example the meaning of "3 + 4" is determined


by the meanings of "3", "4", and "+".
Operational semantic

It is a way to give meaning to computer


programs in a mathematically rigorous way.

This describes how a valid program is interpreted as


sequences of computational steps.

These sequences then are the meaning of the


program. In the context of functional programs, the
final step in a terminating sequence returns the value
of the program.
Example:If we have a program that utilizes Boolean expressions over
the state, ranged over by B, then we can define the semantics of the
while command:

Where S – state
C - command

Consider the following program that utilizes while statement:

#include<stdio.h>
main(){
   
    int b = 0;
    while (b<5){
        printf(" the value of b is %d\n", i);
        b = b + 1;
    }
}
Source
 http://www.inf.ed.ac.uk/teaching/courses/fpls/
 http://www.vias.org/cppcourse/chap01_05.html
 http://en.wikipedia.org/wiki/Formal_semantics_
of_programming_languages
 http://www.csci.csusb.edu/dick/cs620/denotatio
nal.html
 http://www.csci.csusb.edu/dick/cs620/while.htm
l#A

You might also like