Programming Language Syntax and Semantics
Programming Language Syntax and Semantics
Part 2:environment
Part 1:computation
2.Syntax and semantics
◦ syntax rules of the language state how to form
expressions, statements, and programs that look right.
◦ How to build meaningful expressions, statements, and
programs.
3. Semantic Elements
Variables- declaration, scope, type, lifetime
x = y; //value of y assigned to x
x = &y; //stores the address of y into x
3 = y; //error, left-hand side requires l-value
Expressions
a=b=c+d
4. Program organization
◦ Thousands or millions line of code,
◦ Modules, e.g. phone.h, iostream.h
Implemented and validated separately
Compiler compile module separatly
◦ Structure of Design reflects structure of program
through PPL
5. Program Data and Algorithms
Data : Declaration, validation, initialization,
modification.
Computation: control structure, functions, structures,
expression
6. External Environment
1.2 Syntax and Semantics
1.2.1 Language Definition
A language definition should enable person or computer program
to determine whether a program is valid and what its meaning is.
• Syntax
- Is a set of rules that define the form of language.
- Syntax of language is defined by two sets of rules:
1. Lexical Rules
2. Syntactic Rules
- For each language these rules are somewhat different
For eg. Uppercase and lowercase letters are treated same in pascal
language but C and Ada consider them different.
Lexical rules also tells us that not equal to symbol is different for
different language
Pascal <> C != Ada /=
Language Definition Cont.
To define infinite syntax of a language we use finite
description.
FORTRAN was defined using simple rules of English.
ALGOL 60 was defined with a CFG which is also called as
BNF(Backus-Naur Form).
EBNF(Extended BNF)
- is a metalanguage
- The symbols :: =, <, > , | , * and + are called as
metasymbols.
- <,> are nonterminal symbols
- { } are terminal symbol
- * represents zero or more occurrence of preceding element
- + represents one or more occurrence of preceding element.
EBNF definition of a simple PL
Syntax Diagrams
Syntax Diagrams Cont.
Syntax Diagrams Cont.
Abstract syntax, concrete syntax and
pragmatics
Abstract syntax, concrete syntax and pragmatics
- C fragment
while (x != y)
{ …. };
- Pascal fragment
while x<> y do
begin
…..
end
Abstract syntax, concrete syntax and
pragmatics cont.
C or Pascal allow brackets to be omitted in case of
single statement, but it needs to insert { } brackets for
multiple statements.
Modula-2 solve this problem by using ‘end’ keyword.
Semantics
Semantics defines the meaning of syntactically correct
program.
eg. int vector [10];
if (a>b) max=a; else max=b;
• Syntactically correct program can be verified before
program execution is called static semantics.
• Dynamic semantics describes the effect of executing
different constructs of the programming language.
An introduction to formal Semantics
Two ways of specifying formal semantics
1. Axiomatic Semantics
- views program as state machine
- Axiomatic semantics specifies each statement of a
language in terms of a function asem, called predicate
transformer, which yields the weakest precondition W
for the statement S and any postcondition P.
y=3 precondition y>=0 Weakest precondition
x=y+1 statement
x>0 postcondition
An introduction to formal Semantics
2. Denotational Semantics
- Denotational sematics associates each language
statement with a function dsem from the state of
program before the execution to the state after the
execution.
Language Processing
How a higher level language is can be executed
on a computer whose machine level language is
very different and low level.
Two ways are there
1. Interpretation
2. Translation
Interpretation
For each possible action there exist a subprogram in
machine language to execute the action.
Thus interpretation of program is completed by calling
subprogram in a proper sequence.
An interpreter is a program that repeatedly executes the
following sequence
◦ Get the next statement
◦ Determine the action to be executed
◦ Perform the action
Translation
Programs written in high level language is translated in
to an equivalent machine language before being
executed.
Program modules is first be separately translated into
relocatable machine code.
Modules of relocatable code are linked together into a
single relocatable unit.
Finally the entire program is loded into computers
memory as executable machine code.
The translators used in each of these step has a different
names: Compiler, Linker, Loader
If the machine on which translation is performed is
different from machine where it is executed then such
translation is called as cross translation.
Translation
The concept of binding
Program entities(Variables, Routines, Statements etc.)
have certain properties(name, parameter, scope etc.)
called attributes.
Specifying the exact nature of an attribute is called as
binding.
A binding that can not be modified is called as static
binding, and a modifiable binding is called as dynamic
binding.
Binding can be done at compile time or run time.