Axiomatic Semantics
Syntax in programming is defined as the form of expressions, program units, and statements.
Semantics, on the other hand, is the meaning of these expressions, program units, and statements (Stroustrup, 2014).
- Language descriptions include the syntax and semantics for every potential user or evaluator that is concerned in knowing and improving their skills in a given programming language
- Semantics and syntax both provides a language’s definition. In a good language, its semantics follows straightforwardly from the syntax.
- Approaches for formally defining languages:
- Axiomatic semantics
- Denotational semantics
Axiomatic Semantics – An approach which attempts to provide rules that indicate the relevant changes I data immediately after the execution of a feature in a given programming language.
Denotational semantics – defines programs basing on mathematical functions and utilize these functions to obtain the properties of the program (Stroustrup, 2014).
Syntax: Set of instructions which specify how to write a given programming language.
Semantics: Relates to the meaning of an instruction e.g. a++.
Parse trees and derivation connection is so close. Parse trees or derivations can be constructed from one another. Derivations that have unambiguous grammar contain unique parse trees.
Values of synthesized attributes are computed basing on the attribute values of child nodes while the value on an inherited attribute is computed basing on the attribute values of parent and sibling nodes
Attribute grammars are used for:
Compiler design
Static semantic specification
Problem Set
a). Associativity:
→ |
from left to the right |
= |
is right to the left |
+ |
is left to the right |
| |
is left to the right |
* |
is left to the right |
b). Precedence of operators from high to low
Precedence |
Operator |
2 |
→ |
5 |
* |
6 |
+ |
13 |
| |
16 |
= |
c). Left most derivation
E = Expression
T = Term
E = E + E
= E * E + E
=T * E + E
=factor * E+ E
= id * E + E
=B * T + E
=B *factor + E
=B * id +expr
=B * D +expr
= B * D + term
= B * D + factor
= B * D + id
= B * D +C
Parse tree
This grammar contains two parse trees hence it is ambiguous.
E
E + E
b E * E
c b
TREE 1
E
E * E
E + E b
b c
TREE 2
There is more than one parse tree when deriving b + c * a Thus this grammar is ambiguous
Unambiguous grammar
E → E * T | T
T → T * int | int (E)
This is unambiguous as it only has one parse tree
EBNF Grammar
→ begin < stm_list> end
→ {; }
→ =
→ A | B |C
→ [(+| -)
→ a |
A → aA | bc A
- a).
(c + 10) / 3 > 6
c + 10 > 18
c > 8
Denotational semantics
Weakest precondition is c>8
b). x – 3 > -2 * 1
x > -2 *1 + 3
x > 1
Weakest precondition is x > 1
Chapter 5
- Case-sensitive names are a detriment to readability because the names which look similar represent distinct entities. This way, case-sensitive violates the design principle which states that similar looking language constructs need to have the same meaning.
- An alias is a single name of a variable that is used in accessing the same location of memory
- L-value of a variable is the address of a variable. This is what is needed when the name of the variable is on the left side of an assignment.
R-value is the value of the variable. This is what is needed when the variable name appears at the right side of an assignment.
- Binding is the bringing together of an entity and an attribute or a symbol and an operation
Binding time is the time taken when performing binding.
- Language implementation time:
Load time: used for binding a C++ static variable to a memory cell
Runtime: binding a non-static local variable to a memory cell
Compile time: binding a variable to a type in Java or C++
Static binding happens before runtime and does not change all through the process of execution.
Dynamic binding happens in the process of execution and is able to change during this phase of execution.
Static memory is apportioned before execution starts and stays bound to a similar memory all through the execution time
Advantages – Run-time proficient.
Disadvantages – There is no recursion.
Stack-dynamic memory designation of the system is a pile when declarations are examined/depicted.
Advantages – There recursion and memory-efficiency.
Detriments: Run-time isn’t as effective as static.
Explicit heap-dynamic designation and deallocation are with explicit directions, determined by the programmer, which is substantial for execution.
Advantages – adaptable capacity administration.
Weaknesses: Prone to blunder in making new statements or delete statements.
Implicit heap-dynamic distribution and deallocation is caused by a statement.
Advantages – Flexibility is high.
Drawbacks – When the run-time to keep all the attributes dynamic compiler mistake discovery is decreased.
Lifetime is when memory is apportioned for a variable (if the variable it exists).
The scope is the range of statements whereby the variable is visible. E.g. variables show up in an if statement.
The static scope is a degree that depends on program text and link reference to a variable name.
Dynamic scope is a scope that depends on run-time call sequence.
A reference to a non-local variable in a static-scoped language needs access through a two-step process, that is; Finding the right activation record and Noting the right offset in an activation record.
The static ancestors are every one of the strategies in the program inside where the methodology is characterized, for instance, the meaning of the procedure is nested. The meaning might be specifically settled inside just a single procedure, known as a static parent procedure. In any case, this strategy may itself be nested inside another method, et cetera up to the program that is main(). Every one of these techniques are thought to be static predecessors of the procedure sub(). Basically, those that entirely contain the subprogram being referred to are the static predecessors/ancestors.
Dynamic ancestors – These are every one of the procedures that are called before the sub program between the running of a program and those which have not yet completed the process of execution. They are the strategies that are sitting tight for procedure sub() to complete the process of executing before finishing. Basically, dynamic predecessors are those which are accessed to achieve the subprogram that is being referred to.
ML does not require type presentations if the understanding is unambiguous and can be deduced from other data.
Blocks are a dialect-level component added to common programming languages which enable you to make unmistakable portions of code that get passed around to methods or functions as though they were values (Gordon, Henzinger, Nori & Rajamani, 2014).
Blocks are Objective-C objects, which implies that they can be added to accumulations like NSArray or NSDictionary.
Named constant is a descriptive name given to a particular constant value. It is efficient to declare a constant by assigning it a name so that it can be used many times in a program.
Int p =3.14159
Int r = 7
Int area =0.5 * p * r*r
p will be used anywhere in the program instead of repetitively writing the number 3.14159
Problem Set
The variables a and b are bound during load time.
Variable c is bound at compilation time while variable d is bound during runtime of the program
The referencing environment inside sub 2 is:
Dynamic referencing: will contain the variables c, f, h, and j.
Static referencing: will consist of the variables b, e, h and i.
- This code will not print any output because there is a conflicting type for the function func()
- This will print the value 5. This is because 5 is greater that 0.
int main()
{
int x = 0;
++x;
{
int x = 1;
x = 12;
cout<<x<<endl;
}
}int y = x;
If this program is executed, an error is found in the last line:
{error: ‘x’ was not declared in this scope} , however, when we get rid of the last line in the code. We shall have an output that prints the value 12.
References
Gordon, A. D., Henzinger, T. A., Nori, A. V., & Rajamani, S. K. (2014, May). Probabilistic programming. In Proceedings of the on Future of Software Engineering (pp. 167-181). ACM.
Stroustrup, B. (2014). Programming: principles and practice using C++. Pearson Education.