Download Programming Language Semantics: Objectives, Definitions, Rules, Operational, Axiomatic, Fu and more Study notes Computer Science in PDF only on Docsity!
Semantics
- OBJECTIVES
- DEFINITIONS.....................................................................................
- SEMANTICS RULES
- OPERATIONAL SEMANTICS.........................................................
- AXIOMATIC SEMANTICS
- FUNCTIONAL (DENOTATIONAL) SEMANTICS:
- LANGUAGE PROCESSING
1. Objectives
- BNF does not provide additional specifications about the language: How BNF can check whether a variable is declared before it is referenced? How to enforce the length of an identifier? How do we know the maximum integer value?
2. Definitions
It is the meaning of any syntactically valid program written in the language, e.g., expressions, statements, and program units. Note that all syntactically correct programs have valid. There is no single widely acceptable notation or formalism for describing semantics. It is hard to describe the meaning of a language: o Informal description : describe in English the meaning of a construct. o Formal description : A mathematical model using formal notation to describe each construct.
Example: o The syntax of a C if statement is:
If ()
The meaning of this statement form is that the current value of the expression is true, the embedded statement is selected for execution.
4. Operational Semantics
Describe the meaning of a program by executing its statements on an abstract machine. Each program statement is described by a set of operations of this machine. SIMPLESEM:
5. Axiomatic semantics
- It is based on formal logic (First order predicate calculus).
- Approach: Define axioms or inference rules for each statement type in the language (to allow transformations of expressions to other expressions). They are used to formally prove a property (post- condition) of the execution of a program, assuming another property (pre-condition) of the program:
{Q} Program {P}
Where Q is a precondition (also called assertion) of the program (A precondition states the relationships and constraints among variables that are true at that point in execution), and P is a postcondition of the program.
- Example: Q={y=3} x = y + 1; P={x>0}
- A Weakest Precondition : It is the least restrictive precondition that will guarantee the postcondition.
Example: a = b + 1 P={a > 1}
One possible precondition: Q={b > 10} Weakest precondition: Q={b > 0}
- Axiom Definition or Predicate Transformer:
It is a function, called asem, that provides the weakest precondition W of a statement S and a postcondition P:
Example: Given an assignment statement: S:? x = expr; P
The weakest precondition, denoted Px→expr, of S is obtained by replacing each occurrence of x in P by expr:
asem(S,P) = Px→expr
Given a sequence S: S1; S2; is:
{P1} S1; {P2}
{P2} S2; {P4}
The axiom of S is:
The initial functions Composition Primitive Recursion Minimization
- The operations of the computer are simulated by writing mathematical functions.
- The difference between denotational and operational semantics: In operational semantics, the state changes are defined by coded algorithms; in denotational semantics, they are defined by rigorous mathematical functions
- The state of a program is the values of all its current variables s = {<i1, v1>, <i2, v2>, …, <in, vn>}
- Let VARMAP be a function that, when given a variable name and a state, returns the current value of the variable VARMAP(ij, s) = vj
- The following donotational semantics description maps decimal numbers as strings of symbols into numeric values:
<dec_num> → 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | <dec_num> (0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9)
Mdec('0') = 0, Mdec ('1') = 1, …, Mdec ('9') = 9
Mdec (<dec_num> '0') = 10 * Mdec (<dec_num>)
Mdec (<dec_num> '1’) = 10 * Mdec (<dec_num>) + 1
… Mdec (<dec_num> '9') = 10 * Mdec (<dec_num>) + 9
- Assignment Statements o Maps state sets to state sets
Ma(x := E, s) ∆ = if Me(E, s) == error then error else s’ = {<i1’,v1’>,<i2’,v2’>,...,<in’,vn’>}, where for j = 1, 2, ..., n, vj’ = VARMAP(ij, s) if ij <> x = Me(E, s) if ij == x
- Evaluation: Can be used to prove the correctness of programs Provides a rigorous way to think about programs Can be an aid to language design Has been used in compiler generation systems It is not very useful for readers trying to learn a language, or implementers trying write a compiler using traditional methods. It is easy to generate a compiler of the language from the formal definition.
7. Language Processing
- Implementation Methods: A programming language requires various system supports. Operating system provides runtime process management, file system for storing programs, I/O
A linker links the different user and system object files into the final executable. The von Neumann bottle between CPU and memory limits the execution speed.
Compiler
Linker Loader
Interpreter
Source Code
Output Data
Linker : o It takes all the independent object files and links them together: your program in assembly language and the assembly version of all libraries used by your program. o The output of the linker is the executable file or executable.
Loader : o The loader performs the following tasks:
- Reads the executable and create an address space large enough for the program and its data.
- Copies the instructions and data into memory.
- Initializes the machines registers and sets the stack pointer.
- Load the Program Counter with the address of the entry point of the program (main method in Java or C)
- Start the execution.
- Hybrid Implementation Systems Source code is translated into an intermediate form and run by a software virtual machine. E.g., Java compiler produces byte code that can be run on any Java virtual machine (JVM).
- Programming Environment Programming Environment is a set of tools that help programmer to develop applications.