0% found this document useful (0 votes)
18 views34 pages

PPL Unit1

The document provides an overview of preliminary concepts in programming languages, including reasons for studying them, programming domains, and language evaluation criteria. It discusses influences on language design, categories of programming languages, trade-offs in language design, and implementation methods. Additionally, it describes programming environments and the syntax and semantics of programming languages.

Uploaded by

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

PPL Unit1

The document provides an overview of preliminary concepts in programming languages, including reasons for studying them, programming domains, and language evaluation criteria. It discusses influences on language design, categories of programming languages, trade-offs in language design, and implementation methods. Additionally, it describes programming environments and the syntax and semantics of programming languages.

Uploaded by

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

UNIT-1

Preliminary Concepts
Background
 Frankly, we didn‘t have the vaguest idea how the thing [FORTRAN language and
compiler] would work out in detail. …We struck out simply to optimize the object
program, and the running time because most people at that time believed you
couldn‘t do that kind of thing. They believed that machined-coded programs
would be so inefficient that it would be impractical for many applications.
 John Backus, unexpected successes are common – the browser is another
example of an unexpected success
1.1 Reasons for Studying Concepts of Programming Languages-
 Increased ability to express ideas
 Improved background for choosing appropriate languages
 Increased ability to learn new languages
 Better understanding of t h e significance of implementation
 Overall advancement of computing

1.2 Programming Domains –


Computers have been used in several fields. The areas of computer applications
and theirassociated languages are given in the figure.

 Scientific applications
– Large number of floating point computations
– Fortran
 Business applications
– Produce reports, use decimal numbers and characters
– COBOL
1
 Artificial intelligence

– Symbols rather than numbers manipulated


– LISP
 Systems programming
– Need efficiency because of continuous use
– C
 Web Software or Scripting Language
– Eclectic collection of languages: markup (e.g., XHTML), scripting (e.g., PHP),
general-purpose (e.g., Java)
 Special-Purpose Language
– Used to produce business reports to APT
– Used for instructing programmable machine tools, to GPSS which is used
for systems simulation.
1.3 Language Evaluation Criteria –

 Readability : the ease with which programs can be read and understood
 Writability : the ease with which a language can be used to create programs
 Reliability : conformance to specifications (i.e., performs to its specifications)
 Cost : the ultimate total cost
 Others

Readability
The following sections describe characteristics that contribute to the readability of a
programming language
Table: Language evaluation criteria and the characteristics that affect them
2
 Overall simplicity
– A manageable set of features and constructs
- A statement can have more than one way to accomplish a particular operation.
For example, in Java/C/C++, a user can increment a simple integer
variable in fourdifferent ways:
a=a+1; a++ ; a+=1; ++a

almost all the above statement can have the same meaning when we uses
a stand- alone expressions. But the last two statements have slightly
different meanings from each other and from the others in some contexts.
– Minimal operator overloading
 Orthogonality
– A relatively small set of primitive constructs can be combined in a relatively
small number of ways
– Every possible combination is legal
For Example: Add two 32 bit integers and replace one of the two with the sum.
IBM mainframe two instructions required–
A Reg1, memory_cell and AR Reg1, Reg2

 Control statements
– The presence of well-known control structures (e.g., while statement)
 Data types and structures
– The presence of adequate facilities for defining data structures
 Syntax considerations
– Identifier forms: flexible composition
– Special words and methods of forming compound statements
- Form and meaning: self-descriptive constructs, meaningful keywords
Writability
 Simplicity and Orthogonality
3
– Few constructs, a small number of primitives, a small set of rules for
– combining them
 Support for abstraction
– The ability to define and use complex structures or operations in ways that
allow details to be ignored
 Expressivity
– A set of relatively convenient ways of specifying operations
– Example: the inclusion of for statement in many modern languages

Reliability

 Type checking
– Testing for type errors

 Exception handling
– Intercept run-time errors and take corrective measures

 Aliasing
– Presence of two or more distinct referencing methods for the same memory

location
 Readability and writability

– A language that does not support “natural” ways of expressing an algorithm will necessarily
use “unnatural” approaches, and hence reduced reliability

Cost
 Training programmers to use language
 Writing programs (closeness to particular applications)
 Compiling programs
 Executing programs
 Language implementation system: availability of free compilers
 Reliability: poor reliability leads to high costs
 Maintaining programs
Others
 Portability
– The ease with which programs can be moved from one implementation to
another
 Generality
– The applicability to a wide range of applications
 Well-definedness
– The completeness and precision of the language‘s official definition

4
1.4 Influences on Language Design –
 Computer Architecture
– Languages are developed around the prevalent computer architecture,
known as the von Neumann architecture
 Programming Methodologies
– New software development methodologies (e.g., object-oriented software
development) led to new programming paradigms and by extension, new
programming languages
Computer Architecture
 Well-known computer architecture: Von Neumann
 Imperative languages, most dominant, because of von Neumann computers
– Data and programs stored in memory
– Memory is separate from CPU
– Instructions and data are piped from memory to CPU
– Basis for imperative languages
 Variables model memory cells
 Assignment statements model piping
 Iteration is efficient

Figure 1.1 The von Neumann Computer Architecture


Example:

initialize the program counter


repeat forever
fetch the instruction pointed to by the program counter
increment the program counter to point at the next
instructiondecode the instruction
execute the instruction
end repeat

Programming Methodologies
 1950s and early 1960s: Simple applications; worry about machine efficiency

5
 Late 1960s: People efficiency became important; readability, better control
structures
– structured programming
– top-down design and step-wise refinement
 Late 1970s: Process-oriented to data-oriented
– data abstraction
 Middle 1980s: Object-oriented programming
– Data abstraction + inheritance + polymorphism

1.5 Language Categories –

 Imperative
– Central features are variables, assignment statements, and iteration
– Examples: C, Pascal
 Functional
– Main means of making computations is by applying functions to
given parameters
– Examples: LISP, Scheme
 Logic
– Rule-based (rules are specified in no particular order)
– Example: Prolog
 Object-oriented
– Data abstraction, inheritance, late binding
– Examples: Java, C++

6
 Markup
– New; not a programming per se, but used to specify the layout of
information in Web documents
– Examples: XHTML, XML

1.6 Language Design Trade-Offs –

 Reliability vs. cost of execution


– Conflicting criteria
– Example: Java demands all references to array elements be checked for
proper indexing but that leads to increased execution costs
 Readability vs. writability
– Another conflicting criteria
– Example: APL provides many powerful operators (and a large number of new
symbols), allowing complex computations to be written in a compact program
but at the cost of poor readability
 Writability (flexibility) vs. reliability
– Another conflicting criteria
– Example: C++ pointers are powerful and very flexible but not reliably used

1.7 Implementation Methods –


 Compilation
– Programs are translated into machine language
 Pure Interpretation
– Programs are interpreted by another program known as an interpreter
 Hybrid Implementation Systems
– A compromise between compilers and pure interpreters

7
We now briefly describe several programming environments.

Figure 1.2 Layered View of Computer: The operating system and language implementation are layered over
the Machine interface of a compute

Figure 1.3 General compilation process

9
In the above diagram, a lexical analyzer is used to ignore comments in the source program
because the compiler has no use for them. It converts characters in the source program to lexical units.
The syntax analyzer takes the lexical units from the lexical analyzer and uses them to construct
hierarchical structures called parse trees.

The intermediate code generator produces a program in a different language, at an


intermediate level between the source program and the final output of the compiler: the machine
language. The semantic analyzer is an integral part of the intermediate code generator. The semantic
analyzer checks for errors, (if not impossible, to detect during syntax analysis)Optimization, which
improves programs (usually in their intermediate code version) by making them smaller or faster or
both. Because many kinds of optimization are difficult to do on machinelanguage, most optimization
is done on the intermediate code.
The code generator translates the intermediate code version of the program into an
equivalent machine language program. The symbol table serves as a database for the compilation
process. The primary contents of the symbol table are the type and attribute information of each
user-defined name in the program. This information is placed in the symbol table by the lexical and
syntax analyzers and is used by the semantic analyzer and the code generator.

The linking operation connects the user program to the system programs by placing the addresses
of the entry points of the system programs in the calls to them in the user program. The user and
system code together are sometimes called a load module, or executable image. The process of
collecting system programs and linking them to user programs is called linking and loading, or
sometimes just linking. It is accomplished by a systems program called a linker
Pure Interpretation
Here, programs are interpreted by another program called an interpreter, with no translation.
The interpreter program acts as a software simulation of a machine whose fetch-execute cycle deals
with high-level language program statements rather than machine instructions. This software
simulation obviously provides a virtual machine for the language.

Figure 1.5 Pure Interpretation

10
Advantages:
1. Pure interpretation allows easy implementation of many source-level debugging operations.
Because all run-time error messages can refer to source-level units. For example, if an array
index is found to be out of range, the error message can easily indicate the source line of the
error and the name of the array.
Disadvantages:
1. Execution is very slow (10 to 100 times).
2. It requires more space
3. A symbol table must be present during the interpretation
Hybrid Implementation Systems
 A compromise between compilers and pure interpreters
 A high-level language program is translated to an intermediate language that
allows easy interpretation
 Faster than pure interpretation
 Examples
– Perl programs are partially compiled to detect errors before interpretation
– Initial implementations of Java were hybrid; the intermediate form, byte code,
provides portability to any machine that has a byte code interpreter and a runtime
system (together, these are called Java Virtual Machine)

Figure 1.6 Hybrid Implementation

11
Some language implementation systems are a compromise between compilersand pure
interpreters; they translate high-level language programs to an intermediate language designed to
allow easy interpretation. This method is faster than pure interpretation because the source language
statements are decoded only once. Such implementations are called hybrid implementation
systems.

The process used in a hybrid implementation system is shown in the above Figure. Instead of
translating intermediate language code to machine code, it simply interprets the intermediate code.
Perl is implemented with a hybrid system. Perl programs are partially compiled to detect errors
before interpretation and to simplify the interpreter.

Initial implementations of Java were all hybrid. Its intermediate form, called byte code,
provides portability to any machine that has a byte code interpreter and an associated run-time
system.

1.8 Programming Environments –


A programming environment is a collection of tools used in the development of software.
This collection may consist:-
A file system,
A text editor,
A linker,
A compiler,
Integrated tools
These tools may be accessed through a uniform interface (GUI).
Some of the examples of programming environments are-
1) Microsoft Visual Studio .NET, which is a large collection of software development tools, used through a
windows interface.
It is used to develop software in the following languages-
C#,
Visual Basic .NET,
JScript(MS JavaScript version),
J# (MS Java version),
managed C++.
2) NetBeans
3) Turbo C, C++
4) Dreamweaver
5) Arduino, etc.

Programming Environment
A programming environment is a collection of tools used in the development of software. This
collection may consist of only a file system, a text editor, a pre-processor, a linker,and a compiler. All
programs pass through a number of important stages before it is executed by the computer. The
figure shows the typical programming environment.

12
Editor:

It is a program that allows the user to key his/her program. It may be a line editor or graphical
editor; this affects to a large extent, your programming environment. Typically, it provides the
following facilities
• Entering and editing the program
• Loading program into memory
• Compiling the program
• Debugging the program
• Running the program

Preprocessor

A preprocessor is a program that processes a program just before the programis


compiled. It is a program that removes all comments from the source code and modify it according
to directives supplied to the program. Preprocessor instructions are embedded in programs. The
preprocessor is essentially a macro expander. Preprocessor instructions are commonly used to
specify that the code from another file is to be included. For example, in C language, the directives
begin with pound (#) symbol. #include <stdio.h>.

Compiler

It is a source program that accepts as inputs, the preprocessed source code, analyze itfor
syntax errors and produces one or more possible outputs.
• If syntax error(s) is/are found, an error listing is provided
• If the program is free of syntax errors, it is converted to object code.

Note: If the preprocessed code is converted to an assembler code, then the assembler is required
to convert it into machine code.

Linker:
A Linker (Linkage Editor) is a program that combines all object code of a program withother necessary external
items to form an executable code

13
Syntax and Semantics
Introduction
 Syntax: the form or structure of the expressions, statements, and program units
 Semantics: the meaning of the expressions, statements, and program units
 Syntax and semantics provide a language‘s definition
– Users of a language definition
– Other language designers
– Implementers
– Programmers (the users of the language)

1.9 The General Problem of Describing Syntax –

A language (computer programming language or Natural language – English), is a setof


strings of characters from some alphabet.
Formal descriptions of the syntax of programming language is defined as follows:

Sentence: it is a string of characters over an alphabet

Language : a set of sentences


Lexeme: it is the lowest level of the syntactic unit of a language. The lexemes of a programming
language include its numeric literals, operators, and special words, among others.
Token : it is a category of lexemes (Example: Identifier)
Example:
Consider the following statement:
a = 2 * b + 5;
The lexemes and tokens of this statement are as follows:

Lexemes : a, = , 2, *, b, +, 5, ;
Tokens: variables : a, b
Operator : = , * and +
Constants = 2 and 5

 Languages Recognizers
– A recognition device reads input strings of the language and decides whether the
input strings belong to the language
– Example: syntax analysis part of a compiler
 Languages Generators
– A device that generates sentences of a language
– One can determine if the syntax of a particular sentence is correct by
comparing it to the structure of the generator

14
1.10 Formal Methods of Describing Syntax –
The formal language-generation mechanism called grammar is used to describe the syntax
of programming languages. There are several forms for describing the syntax of a programming
language.

 Backus-Naur Form and Context-Free Grammars


The most widely known method for describing programming language syntax
 Extended BNF
Improves readability and writability of BNF
 Grammars and Recognizers
 Syntax Graphs

1.10.1 Backus-Naur Form and Context-Free Grammars and Origin of Backus-Naur Form and
Fundamentals
BNF is the most widely used method for programming language syntax. BNF is a metalanguage
(is used to describe another language). BNF uses abstraction for syntacticstructures. BNF (Backus-Naur
Form) is a metalanguage for describing context-free grammar.
• The symbol::= (or →) is used for may derive.
• The symbol | separates alternative strings on the right-hand side.
Example 1:
E ::= E + T | T
T ::= T * F | F
F ::= id | constant | (E)
where E is Expression, T is Term, and F is Factor
Example 2:
<Assign> ::= <var> <expression> is a simple assignment statement.
<program> ::= {<statement*>}
<statement> ::= <assignment> | <condition> | <loop>
<condition> ::= if <expr> {<statement> }
<loop> ::= while <expr> {<statement> }
<expr> ::= <identifer> | <number> |<expr>

Here, the LHS of the arrow represents abstraction is being defined and RHS consists of a
combination of tokens, lexemes and references to other abstractions. The abstractions in a BNF
description, or grammar, are often called nonterminal symbols, or simply nonterminals, and the
lexemes and tokens of the rules are called terminal symbols, or simply terminals.
BNF Rules:
a. Non-terminals are BNF abstractions and Terminals are lexemes and tokens
b. A rule has a LHS and RHS which consists of T and NT symbols
c. A grammar is a finite nonempty set of rules
d. An abstraction (or nonterminal symbol) can have more than one RHS
<ident_list> → identifier | identifer, <ident_list>
<stmt> : <single_stmt> | begin <stmt_list> end letter : A | B | … | Z | a | b | … | z
digit : 0 | 1 | … | 9

15
1.10.1.1 Describing Lists
 Syntactic lists are described using recursion
<ident_list> → ident
| ident, <ident_list>
 A derivation is a repeated application of rules, starting with the start symbol and
ending with a sentence (all terminal symbols)
An Example Grammar
1.<program> → <stmts>
<stmts> → <stmt> | <stmt> ; <stmts>
<stmt> → <var> = <expr>
<var> → a | b | c | d
<expr> → <term> + <term> | <term> - <term>
<term> → <var> | const
1.10.1.2 Grammars and Derivations
BNF is a generative device for defining language. The sentences of the language are
generated through a sequence of application of the rules, beginning with a nonterminal of the grammar
called the Start symbol. A sentence generation is called a derivation.
 Every string of symbols in the derivation is a sentential form
 A sentence is a sentential form that has only terminal symbols
 A leftmost derivation is one in which the leftmost nonterminal in each sentential form
is the one that is expanded
 A derivation may be neither leftmost nor rightmost

Example:
<program> →<stmts> → <stmt>
<var> = <expr> → a =<expr>
a = <term> + <term>
a = <var> + <term>
a = b + <term>
a = b + const
Left and Right Derivations:

16
Example:

1.10.1.3 Parse Trees:


It is a hierarchical representation of a derivation.

17
Example:

1.10.1.4 Ambiguity in Grammars


 A grammar is ambiguous if it generates a sentential form that has two or more
distinct parse trees

18
Operator Precedence:
used to indicate that it has Precedence over an operator produced higher up in the tree.
Example: Unambiguous Grammar: Every derivation with an unambiguous grammar has a
unique parse tree, although that treecan be represented by different derivations.
<expr> → <expr> - <term>|<term>
<term> → <term> / const|const

Fig : An Unambiguous Expression Grammar Fig: Parse Tree for Associativity operator

Associativity of Operators
Operator associativity can also be indicated by a grammar
<expr> → <expr> + <expr> | const (ambiguous)
<expr> → <expr> + const | const (unambiguous)

1.10.2 EBNF (Extended Backus-Naur Form):


It adds a few additional met symbols whose main advantage is replacing recursion with iteration.
• {a} means that a is occur zero or more times.
• [a] means that a appears once or not at all. Example

Our expression grammar can become:

E ::= T { + T }
T ::= F { * F }
F ::= id | constant | (E)
Example: if statement rules
The symbol | (logical OR) is used to define multiple rules. For example, if a statement canbe
described with the rules
<if_stmt> → if ( <logic_expr> ) <stmt>

19
<if_stmt> → if ( <logic_expr> ) <stmt> else <stmt> or

write the rule

<if_stmt> → if ( <logic_expr> ) <stmt>

| if ( <logic_expr> ) <stmt> else <stmt>


here, <stmt> represents either a single statement or a compound statement.

Note:

Variable length list is usually written as ellipsis (…). BNF does not include the ellipsis, instead, it
uses the recursion rule. A rule is recursive if its LHS appears in its RHS.

Example:

<ident_list> → identifier
| identifier, <ident_list>

This defines <ident_list> as either a single token (identifier) or an identifier followed by acomma and
another instance of <ident_list>.

Difference between BNF and Extended BNF


S.No BNF Extended BNF
1 <expr> <expr> + <term> <expr> <term> {(+ | -) <term>}
| <expr> - <term> <term> <factor> {(* | /) <factor>}
| <term>
<term> <term> * <factor>
| <term> / <factor>
| <factor>
2 Arrow is used : colon is used and RHS is placed on next
line
3 | vertical bar is used to separate line Simply write a separate lines

1.10.3 Syntax Graphs:


A syntax graph is a representation of a grammar. It is a set of syntax diagrams, each of which
defines a "nonterminal" stage in a process. A main diagram defines the language, and a word
must describe a path in the main diagram to belong to the language

20
1.11 Attribute Grammars

 Context-free grammars (CFGs) cannot describe all of the syntax of programming


languages
 Additions to CFGs to carry some semantic info along parse trees
 Primary value of attribute grammars (AGs):
– Static semantics specification
– Compiler design (static semantics checking)

Definition
 An attribute grammar is a context-free grammar G = (S, N, T, P) with the
following additions:
– For each grammar symbol x there is a set A(x) of attribute values

– Each rule has a set of functions that define certain attributes of the
nonterminals in the rule
– Each rule has a (possibly empty) set of predicates to check for attribute
consistency
– Let X0 X1 ... Xn be a rule
– Functions of the form S(X0) = f(A(X1), ... , A(Xn)) define synthesized attributes
– Functions of the form I(Xj) = f(A(X0), ... , A(Xn)), for i <= j <= n, define
inherited attributes
– Initially, there are intrinsic attributes on the leaves

Example
 Syntax
<assign> → <var> = <expr>
<expr> → <var> + <var> | <var>
<var> → A | B | C
 actual_type: synthesized for <var> and <expr>
 expected_type: inherited for <expr>
 Syntax rule :<expr> → <var>[1] + <var>[2]
Semantic rules :<expr>.actual_type → <var>[1].actual_type
Predicate :<var>[1].actual_type == <var>[2].actual_type
<expr>.expected_type == <expr>.actual_type
 Syntax rule :<var> → id
Semantic rule :<var>.actual_type lookup (<var>.string)
 How are attribute values computed?
– If all attributes were inherited, the tree could be decorated in top-down order.
– If all attributes were synthesized, the tree could be decorated in bottom-up
order.
– In many cases, both kinds of attributes are used, and it is some combination of
top-down and bottom-up that must be used.

21
<expr>.expected_type inherited from parent
<var>[1].actual_type lookup (A)
<var>[2].actual_type lookup (B)
<var>[1].actual_type =? <var>[2].actual_type
<expr>.actual_type <var>[1].actual_type
<expr>.actual_type =? <expr>.expected_type

Categories of Attributes

There are two types of attributes namely Synthesized Attributes and Inherited Attributes.

S.No Synthesized Attributes Inherited Attributes


1 LHS attributes depend on RHS attributes RHS attributes depend upon LHS
attributes
2 These attributes pass information up These attributes pass information down
theparse tree from children to root the parse tree from root to the children
3 Usually initialized at the leaf nodes Usually initially at the root

1.12 Describing the Meanings of Programs: Dynamic Semantics

 There is no single widely acceptable notation or formalism for describing semantics


 Operational Semantics
– Describe the meaning of a program by executing its statements on a machine,
either simulated or actual. The change in the state of the machine (memory,
registers, etc.) defines the meaning of the statement
 To use operational semantics for a high-level language, a virtual machine is
needed
 A hardware pure interpreter would be too expensive
 A software pure interpreter also has problems:
 To use operational semantics for a high-level language, a virtual machine is
needed
 A hardware pure interpreter would be too expensive
 A software pure interpreter also has problems:
– The detailed characteristics of the particular computer would make actions
difficult to understand
– Such a semantic definition would be machine-dependent

1.12.1 Operational Semantics


 A better alternative: A complete computer simulation
 The process:
– Build a translator (translates source code to the machine code of an idealized
computer)
– Build a simulator for the idealized computer
 Evaluation of operational semantics:
– Good if used informally (language manuals, etc.)
– Extremely complex if used formally (e.g., VDL), it was used for describing the
semantics of PL/I.
22
 Axiomatic Semantics
– Based on formal logic (predicate calculus)
– Original purpose: formal program verification
– Approach: Define axioms or inference rules for each statement type in the
language (to allow transformations of expressions to other expressions)
– The expressions are called assertions
1.12.2 Axiomatic Semantics
 An assertion before a statement (a precondition) states the relationships and
constraints among variables that are true at that point in execution
 An assertion following a statement is a postcondition
 The weakest precondition is the least restrictive precondition that will guarantee
the postcondition
 Pre-post form: {P} statement {Q}
 An example: a = b + 1 {a > 1}
 One possible precondition: {b > 10}
 Weakest precondition: {b > 0}
 Program proof process: The postcondition for the whole program is the desired result.
Work back through the program to the first statement. If the precondition on the
first statement is the same as the program spec, the program is correct.
 An axiom for assignment statements (x =
E):
{Qx->E} x = E {Q}
 An inference rule for sequences
– For a sequence S1;S2:
– {P1} S1 {P2}
– {P2} S2 {P3}
 An inference rule for logical pretest loops
For the loop construct:
{P} while B do S end {Q} Characteristics of the loop invariant I must meet the following
conditions:
– P => I (the loop invariant must be true initially)
– {I} B {I} (evaluation of the Boolean must not change the validity of I)
– {I and B} S {I} (I is not changed by executing the body of the loop)
– (I and (not B)) => Q (if I is true and B is false, Q is implied)
– The loop terminates (this can be difficult to prove)
 The loop invariant I is a weakened version of the loop postcondition, and it is also
a precondition.
 I must be weak enough to be satisfied prior to the beginning of the loop, but when
combined with the loop exit condition, it must be strong enough to force the truth of
the postcondition.

Evaluation of Axiomatic Semantics:


– Developing axioms or inference rules for all of the statements in a language is
difficult
It is a good tool for correctness proofs and an excellent framework for reasoning about programs,
but it is not as useful for language users and compiler writers

23
– Its usefulness in describing the meaning of a programming language is limited for
language users or compiler writers

Example

1.12.4 Denotational Semantics


– Based on t h e recursive function theory
– The most abstract semantics description method
– Originally developed by Scott and Strachey (1970)
– The process of building a denotational spec for a language (not necessarily easy):
– Define a mathematical object for each language entity
– Define a function that maps instances of the language entities onto instances of
the corresponding mathematical objects
– The meaning of language constructs are defined by only the values of the program's
variables
– 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
 Decimal Numbers
– The following denotational 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

24
Evaluation of Denotational Semantics

1. It can be used to prove the correctness of programs


2. It provides a rigorous way to think about programs
3. It can be an aid to language design
4. It has been used in compiler generation systems

1.12.5 Expressions
 Map expressions onto Z {error}
 We assume expressions are decimal numbers, variables, or binary expressions having one
arithmetic operator and two operands, each of which can be an expression
 Assignment Statements
– Maps state sets to state sets
 Logical Pretest Loops
– Maps state sets to state sets
 The meaning of the loop is the value of the program variables after the statements
in the loop have been executed the prescribed number of times, assuming there have
been no errors
 In essence, the loop has been converted from iteration to recursion, where the recursive
control is mathematically defined by other recursive state mapping functions
 Recursion, when compared to iteration, is easier to describe with mathematics.

Evaluation of denotational semantics

– 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
– Because of its complexity, they are of little use to language users

25
IMPORTANT QUESTIONS: UNIT – IPART – A
1. What are general-purpose languages? Give examples.
2. List out the language categories
3. What are the factors influencing the writability of a language?
4. What are the fundamental features of imperative languages?
5. Explain generic methods
6. What is the purpose of the assignment statement?
7. Define syntax and semantics
8. Define derivation and parse tree
9. Give an attribute grammar for a simple assignment statement
10. What are the difficulties in using an attribute grammar to describe all of the syntax
andstatic semantics of a contemporary programming language?
11. Differentiate between static and dynamic semantic
12. Write EBNF description for the C union
13. Describe the approach of using axiomatic semantics to convert the correctness of a
givenprogram.

PART – B

1. What are the potential benefits of studying programming language concepts?


2. Discuss the disadvantages of having too many features in a programming language.
3. Describe the steps involved in the language evaluation criteria.
4. Explain the different categories of languages
5. Discuss various programming domains and their associated languages.
6. Draw and explain the flowchart for the compilation process.
7. Give any three reasons for separating Lexical analysis from Syntax analysis.
8. Discuss language recognizers and language generators
9. Compare and contrast between the special-purpose and general-purpose languages.
10. Explain lexical analysis. Write short notes on context-free grammar.
11. Explain different phases of compilation.
12. Explain the parse tree for the sum and average program by using the grammar
13. Differentiate between syntax and semantics
14. Explain with an example how operator associativity can be incorporated into grammar.
15. Define grammar, derivation and parse tree.
16. Explain attribute grammar and also write attribute grammar for simple
assignmentstatement
17. Write BNF notation for 'for loop', 'if-else condition' and structure definition in C.
18. Consider the grammar: <assign> → = <id> + <expr>
<id> → A | B | C
<expr> → <id> + <expr> | <id> * <expr> | (expr) | <id>

Give parse tree and left most derivation for A = A * (B + (C * A)) and A = A * (B + (C)).

19. Discuss in detail the attribute grammar.


20. Explain how is the order of evaluation of attributes determined for the tree of a
givengrammar
21. How do you describe the meanings of programs using dynamic semantics?
22. Differentiate operational semantics and denotational semantics
23. Discuss about the basic concepts of axiomatic semantics
24. Explain about denotational semantics and axiomatic semantics for
commonprogramming language features.
25. Explain about the precondition and post condition of a given statement mean in
axiomaticsemantics
Important Questions and Answers
1. Many languages distinguish between uppercase and lowercase letters in user-defined names. What
are the pros and cons of this design decision?

Ans:

The reasons why a language would distinguish between uppercase and lowercase in its identifiers are:
Variable identifiers may look different than identifiers that are names for constants. Due to this, in C
language we are using uppercase for constant names and using lowercase for variable names.
Name of the variable names can have their first letter distinguished. If a languagewould not
distinguish between uppercase and lowercase in identifiers is it makes programs less readable. For
example, the words SUM and Sum are actually verysimilar but are completely different.

2. What is the difference between an intrinsic attribute and a nonintrinsic synthesizedattribute?

Ans: The value of an intrinsic attribute is supplied from outside the attribute evaluation process,
usually from the lexical analyzer. The value of a synthesized attribute is computed by an attribute
evaluation function.

3. Write EBNF descriptions for the following:


A. A Java class definition header statement
B. A C switch statement

Ans:

<class_head> ? {<modifier>} class <id> [ extends class_name][implements <interface_name> {,


<interface_name>}]

<switch_stmt> ? switch (<expr>) { case <literal> : <stmt_list>


{ case <literal> : <stmt_list> } [Default: <stmt_list>] }

4. Using the grammar show a parse tree and a leftmost derivation for each of the following
statements:
a. A=A*(B + (C * A))
b. A = (A + B) * CAns: (a) A=A*(B + (C * A))
Ans: b). A = (A + B) * C

4. Prove that the following grammar is ambiguous:


<S> <A>
<A> <A> + <A> | <id>
<id>  a | b | c

Ans:

The following two distinct parse trees for the same string prove that the grammar is ambiguous.
5. Describe, in English, the language defined by the following grammar:
<S> → <A> <B> <C>
<A> → a <A> | a
<B> → b <B> | b
<C> →

Ans: One or more's followed by one or more's followed by one or more c's.

6. Convert the following BNF grammar into EBNF

Ans:
<assign> <id> = <expr>
<id> A | B | C
<expr> <expr> (+ | *) <expr>
| (<expr>)
| <id>

7. Compute the weakest precondition for each of the following assignment statementsand
postconditions:
a. a = 2 * (b - 1) - 1 {a > 0}
b. b = (c + 10) / 3 {b > 6}
c. a = a + 2 * b - 1 {a > 1}
d. x = 2 * y + x - 1 {x > 11}

Ans:

(a) a = 2 * (b - 1) - 1 {a > 0}
2 * (b - 1) - 1 > 0
2*b-2-1>0
2*b>3
b>3/2

(b) b = (c + 10) / 3 {b > 6}


(c + 10) / 3 > 6
c + 10 > 18
c>8

(c) a = a + 2 * b - 1 {a > 1}a + 2 * b - 1 > 1


2 * b > 2 – ab > 1 - a / 2

(d) x = 2 * y + x - 1 {x > 11}


2 * y + x - 1 > 11
2 * y + x > 12

8. Compute the weakest precondition for each of the following sequences of assignmentstatements
and their postconditions:
a. a = 2 * b + 1;
b=a–3
{b < 0}

b. a = 3 * (2 * b + a);
b=2*a–1
{b > 5}

Ans:

a. a=2*b+1
b = a – 3 {b < 0}
a–3<0
a<3

Now, we have:
a = 2 * b + 1 {a < 3}
2*b+1<3
2*b<2
b<
b. a = 3 * (2 * b + a);
b = 2 * a - 1 {b > 5}
2*a-1>5
2*a>6
a>3

Now we have:

a = 3 * (2 * b + a) {a > 3}
3 * (2 * b + a) > 3
6*b+3*a>3
2 * b + a > 1 (Divide by
3 both sides)
b > (1 - a) /2

You might also like