0% found this document useful (0 votes)
77 views

Programming Language Syntax and Semantics

The document discusses various concepts related to programming languages including: 1. It describes the software development process and the role of programming languages and development environments. 2. It discusses the relationship between languages, software design methods, and computer architecture. Different programming paradigms like procedural, object-oriented, and declarative are also covered. 3. Key concepts like syntax, semantics, variables, scoping, types, routines and binding are introduced at a high level. Different approaches to language processing like interpretation and translation are also summarized.

Uploaded by

kiran
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
77 views

Programming Language Syntax and Semantics

The document discusses various concepts related to programming languages including: 1. It describes the software development process and the role of programming languages and development environments. 2. It discusses the relationship between languages, software design methods, and computer architecture. Different programming paradigms like procedural, object-oriented, and declarative are also covered. 3. Key concepts like syntax, semantics, variables, scoping, types, routines and binding are introduced at a high level. Different approaches to language processing like interpretation and translation are also summarized.

Uploaded by

kiran
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 54

Chapter 1

Programming Language Syntax and Semantics


Software Development Process

 Requirement Analysis and Specification


 Software Design
 Implementation
 Verification and Validation
 Maintenance
Languages and Software Development
Environment
 A Software development environment is an integrated set
of tools and techniques that aids in the development of
software
 The environment is used in all phases of software
development.
 Such environment can provide tools to simulate or
animate the customers requirements, keeping
documentation, automating some development steps etc.
 Eg. Editors, compilers, simulators, interpreter, linker etc.
 Such tools have increased programming productivity by
reducing the chances of errors.
Languages and software design methods
A design method gives guideline while producing a design. eg.
Structured design, top-down, object oriented design.
Some languages support better support to specific or some design
methods.
Tounderstand the relationship between language and design method, we
must understand different programming paradigm.
o Procedural programming
o Functional programming
o Abstract data type programming
o Module based programming
o Object oriented programming
o Generic programming
o Declarative Programming
Ifthe design method and language paradigm is same then design abstraction
can be directly mapped into program components.
Languages and Computer Architecture
 Computer Architecture uses language designs to check
what can be implemented efficiently on current
machines.
 Conventional programming languages can be viewed
as abstraction of a von Neumann architecture.
Abstraction model highlights only relevant aspects.
Programming language qualities
 Programming language is a tool for the development of
software, so programming language qualities are related
to quality of a software.
1. Software must be reliable
2. Software must be maintainable
3. Software must execute efficiently
• Reliability supports writability, readability,
simplicity, safety and robustness.
• Two main features that languages can provide to
support modification are factoring and locality.
• Efficiency is concerned with execution speed and
space, and also reusable and portable software.
A brief historical perspective
A birds eye view of programming language
concepts
Part 1:organization

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.

Eg. int a=10 The value 10 is bind with ‘a’ at


compile time.
Variables, Names and Scope
 Variable is a name assigned to a memory location. We
can access the memory location using that variable, and
store and modify values in it.
 Name is assigned to a variable by a declaration statement.
 The scope of program variable is the range of program
instructions over which the name is known.
◦ Eg. {
int a=10;
}
Above statement allocate a memory location of two bytes for a
integer variable, give its name as ‘a’, store a value of 10, and its
scope is within the {} brackets only.
Variable
A variable is a 5-tuple <name, scope, type, l_value,
r_value>
• name is a string of characters used by program
statements to denote the variable;
• scope is the range of program instructions over
which the name is known;
• type is the variable’s type;
• l_value is the memory location associated with the
variable;
• r_value is the encoded value stored in the variable’s
location.
Type
 A variable of given type is said to be instance of the type.
 static typing: the binding between a variable and its type is
done at compile time.
 Ex: FORTRAN, COBOL, Pascal, C, C++, Modula-2, and
Ada
 dynamic typing: run-time binding between variables and
their type
 Ex:Assembly languages, LISP, APL, SNOBOL4, and
Smalltalk

 Type can be of built in type or user defined type.


 User can define a new type y using type declaration
Scope
Static Scope Binding: defines the scope
in terms of the lexical structure of a
program.
 Ex: C,C++
Dynamic scope binding defines the scope
of a variable's name in terms of program
execution.
 Ex:APL, LISP, SNOBOL4
Dynamic Scope
{
/* block A */
int x;
. . .
}
. . .
{
/* block B */
int x;
. . .
}
. . .
{
/* block C */
. . .
x = ...;
. . .
}
Routines
 Programming languages allow a program to be
composed of a number of units called routines.
 Routines usually come in two forms: Procedures and
functions. Functions returns value while procedures not.
Routines cont.
 Routines also have name, scope, type, l-value, r-value.
 Routine activation is done through routine call.
 Routine can refer to local and nonlocal(Global) items.
 A routines l-value constitutes the memory area where
routine body is saved. The r-value bound to the routine at
run time.
 Routine declaration defines the routine header without
specifying the body whereas routine definition specifies
routine header and body.
 The activation record contains all the information
necessary to execute the routine for eg. local, nonlocal
variables, return address etc.
Routines cont.
Generic Routines
 Generic routines are the routines that perform a same
task for different parameter types.
eg. Following is a generic swap routine in c++.
Aliasing and overloading
 A name is said to be overloaded if more than one entity is
bound to the name at a given point of program .
◦ Eg. int a, b, c;
b() { }
……
a=b+c+b();

 Aliasing is exactly opposite of overloading. Two names are


aliases if they denote the same entity at the same program
point.
◦ Eg. int x=0;
int *i=&x;
int *j=&x;
Would make *i, *j and x aliases.
An Abstract Semantic Processor
 SIMPLESEM is a simple abstract processor.
 SIMPLESEM consist of instruction pointer(ip), memory
and processor.
 We assume that there are two separate memory section,
code memory and data memory.
An Abstract Semantic Processor cont.
 We use the notation D[X] and C[X] to denote the values
stored in the X-th cell of D and C resp.
 Modification of value is performed by set instruction.

Eg. set 10,D[20]


will assign the value at location 20 into location 10.
 Input/Output can be achieved using set instruction and
special registers read and write.
Eg. set 15,read
value read from input device is to be stored at location 15
set write, D[15]
value stored at location 15 is to be transferred to the output device
An Abstract Semantic Processor cont.
 The SIMPLESEM machine operates by executing the
following steps repeatedly, until it encounters a special
halt instruction.
1. Get the current instruction to be executed
2. Increment ip
3. Execute the current instruction.
 Someinstruction might modify the control flow such as
jump or jumpt instruction.
Eg. Jump 47 it sets ip to 47
Run Time Structure
 Languages can be classified according to their execution time
structure.
1. Static Language- Memory requirement for any program
can be evaluated before execution. It can not allow recursion.
Eg. Early versions of FORTRAN,COBOL
2. Stack Based Languages- Memory requirement can be
evaluated at compile time or run time but memory usage is
predictable and follows a first-in-first-out discipline. The
lastly allocated activation record is the next one to
deallocated.
Eg. ALGOL 60
Run Time Execution Cont.
3. Fully Dynamic Language- These languages have
unpredictable memory usage i.e. data is dynamically
allocated only at execution time.
C1 Language
 This language have simple types and simple statements(no
functions). Following is a C1 program & initial state of
SIMPLESEM machine for that program.
C2 Language
 C2 program consist of sequence of following items.
◦ Set of data declaration
◦ Set of routine definition and declaration
◦ A main routine
 For simplicity it is assumed that routines can not call
themselves recursively, do not have parameters and do
not return values.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy