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

3-Description of Syntax and Semantics

The document discusses the syntax and semantics of programming languages. It covers topics like formal descriptions of syntax using BNF, describing semantics, attribute grammars for static semantics checking, and operational semantics for describing the dynamic meaning of programs.

Uploaded by

SimbaBabdu
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

3-Description of Syntax and Semantics

The document discusses the syntax and semantics of programming languages. It covers topics like formal descriptions of syntax using BNF, describing semantics, attribute grammars for static semantics checking, and operational semantics for describing the dynamic meaning of programs.

Uploaded by

SimbaBabdu
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 57

Principles of Programming

Languages(CS F301)
Prof.R.Gururaj
BITS Pilani CS&IS Dept.
Hyderabad Campus
Description of Syntax and
Semantics (Ch.3 of T1)
BITS Pilani Prof R Gururaj
Hyderabad Campus
Introduction

The Formal description of Programming Languages first


started with ALGOL.
But due to new notations used, it was not easily
understandable.

For any description, the questions like–


For whom (target)? And Who are trying to understand it?
are the important.

Prof.R.Gururaj CSF301 PPL BITS Pilani, Hyderabad Campus


For Programming languages descriptions are
meant for three sections of audience.
1. Initial evaluators: (designers)
2. Implementers: (they need to understand how
expressions, statements and program units
are formed, and what is the effect of their
execution.
3. Users of the language: who use the language
to write programs to solve problems ; they
use user manuals.
Prof.R.Gururaj CSF301 PPL BITS Pilani, Hyderabad Campus
Syntax and semantics of a PL

Syntax of a programming language:


Is the form of its expressions, statements, and program units.
Semantics of a programming language:
Is the meaning of those expressions, statements, and program
units.
Ex: while (bool_expression)
{statement} - is the syntax.
The semantics says that- when the current value of the
Boolean expression is true, the embedded statement is
executed.
Note:
Prof.R.Gururaj CSF301 PPL BITS Pilani, Hyderabad Campus
Describing the syntax

Languages- Natural and Artificial.

What is language? – set of strings formed over an


alphabet
What is a lexeme? - lower level of syntactic units.
What is a token? -Category of its lexemes.

Prof.R.Gururaj CSF301 PPL BITS Pilani, Hyderabad Campus


Example

Prof.R.Gururaj CSF301 PPL BITS Pilani, Hyderabad Campus


Language Recognizers

A language is formally defined in two ways:


Language recognizer
Language generator

Language L; Alphabet is ∑
The language Recognizer R is a device which tells a
string/sentence is in L or not.
It is not meant for enumerating strings of that Language.

Prof.R.Gururaj CSF301 PPL BITS Pilani, Hyderabad Campus


The syntax analysis step in compilation has a
recognizer for the language it is translating.

Hence a syntax analyzer determines whether the


given programs are syntactically correct or not.

Prof.R.Gururaj CSF301 PPL BITS Pilani, Hyderabad Campus


Language Generators

A language generator is a device that can be used to


generate a sentence of a language.

We use language generator mechanism called


“Grammar” that are commonly used to describe the
syntax of a programming language.

Prof.R.Gururaj CSF301 PPL BITS Pilani, Hyderabad Campus


BNF (Backus-Naur Form)

In the middle of 1950s by


Noam Chomsky & John Backus developed the similar
syntax description formalism that became popular
approach to describe languages.
Chomsky in 1950 proposed four categories of
Grammars.
1. Regular Grammars Popularly used to describe languages

2. Context Free Grammars


3. Context Sensitive Grammars
4. Recursively Enumerable Grammars
Prof.R.Gururaj CSF301 PPL BITS Pilani, Hyderabad Campus
BNF

In 1958 John Backus proposed formalism for


describing languages.
Peter Naur modified it. It became BNF used first for
describing ALGOL.
BNF is metalanguage.
BNF uses abstract syntax structures.
Uses Rules or Productions.
Ex:
<assign>  <var> = <expr>
Abstractions are called non-terminals.
Prof.R.Gururaj CSF301 PPL BITS Pilani, Hyderabad Campus
BNF

Abstractions are called non-terminals.


Lexemes and tokens are called terminals.
A grammar is a collection of rules.

Prof.R.Gururaj CSF301 PPL BITS Pilani, Hyderabad Campus


Grammar Derivations.

A Grammar is a generative device for defining


languages.
Sentences of a language are generator through a
sequence of applications of these rules beginning with
a special NT called as Start symbol.

For programming languages, usually the start symbol is


<program>

Prof.R.Gururaj CSF301 PPL BITS Pilani, Hyderabad Campus


Example

Prof.R.Gururaj CSF301 PPL BITS Pilani, Hyderabad Campus


Sample derivation

Prof.R.Gururaj CSF301 PPL BITS Pilani, Hyderabad Campus


Prof.R.Gururaj CSF301 PPL BITS Pilani, Hyderabad Campus
Derivation:
Sentential Form:
Sentence:
LMD:
RMD:

Prof.R.Gururaj CSF301 PPL BITS Pilani, Hyderabad Campus


LMD Ex:

Prof.R.Gururaj CSF301 PPL BITS Pilani, Hyderabad Campus


Parse Tree:
The hierarchical structure of the sentences
described by the grammars is known as
Parse Tree.

Prof.R.Gururaj CSF301 PPL BITS Pilani, Hyderabad Campus


Prof.R.Gururaj CSF301 PPL BITS Pilani, Hyderabad Campus
Ambiguity:
A grammar that generates a sentential form
for which there exist two or more distinct
parse trees is known as ‘Ambiguous
Grammar’.
Otherwise if there exist two or more LMDs or
two or more RMDs for sentential form,
then the grammar is called as ambiguous
grammar.
Prof.R.Gururaj CSF301 PPL BITS Pilani, Hyderabad Campus
Prof.R.Gururaj CSF301 PPL BITS Pilani, Hyderabad Campus
Prof.R.Gururaj CSF301 PPL BITS Pilani, Hyderabad Campus
Left Recursive grammar
E E+T

Right Recursive grammar


ET+E

Prof.R.Gururaj CSF301 PPL BITS Pilani, Hyderabad Campus


Extended BNF:
- Optional part of RHS

- Repetitions

- Alternation

Prof.R.Gururaj CSF301 PPL BITS Pilani, Hyderabad Campus


Grammars and Recognizers
Lex
Yacc

Prof.R.Gururaj CSF301 PPL BITS Pilani, Hyderabad Campus


Attribute Grammars.

An attribute grammar is an extension of CFG.


Thee extensions allow certain rules to be these
extensions follow certain language rules to be
conveniently described such as type compatibility.

Prof.R.Gururaj CSF301 PPL BITS Pilani, Hyderabad Campus


Static semantics

Type compatibility
Declaration of variable before it is referenced.
The static semantics of a language are only indirectly
related to the meaning of programs during the
execution, rather they have to do with the legal
format of the program. (syntax rather than semantics)
Static because these checks can be performed at compile
time itself.

Prof.R.Gururaj CSF301 PPL BITS Pilani, Hyderabad Campus


BNF can’t describe static semantics.
Attribute Grammar is a mechanism devised to describe
and check the correctness of static semantic rules of a
program.
Basic Concepts:
1. Attribute grammars are CFGs , which have been
added with: (i) attributes
(ii) attribute computation functions
(iii) predicate functions.

Prof.R.Gururaj CSF301 PPL BITS Pilani, Hyderabad Campus


(i) Attributes: are associated with the
grammar symbols (both terminals and non terminals) ,
and are similar o variables with assigned values.
(ii) Attribute computation functions (Semantic
functions): are associated with grammar rules and are
used to specify how the attribute values are
computed.
(iii) Predicate functions: state the static semantic rules
associated with the grammar rules.

Prof.R.Gururaj CSF301 PPL BITS Pilani, Hyderabad Campus


Other features of Attributes Grammars:
1.Attributes associate with Grammar Symbols.
A(X) = S(X) U I(X)
Synthesized attributes- computed based values of children
Inherited attributes- based on parents and siblings
2. Semantic functions
3. Predicate functions
Intrinsic attributes: are synthesized attributes of leaf
nodes whose values are determined outside the parse
tree.

Prof.R.Gururaj CSF301 PPL BITS Pilani, Hyderabad Campus


BITS Pilani, Hyderabad Campus
Prof.R.Gururaj CSF301 PPL BITS Pilani, Hyderabad Campus
Prof.R.Gururaj CSF301 PPL BITS Pilani, Hyderabad Campus
Prof.R.Gururaj CSF301 PPL BITS Pilani, Hyderabad Campus
Computing the Attribute values

Computing the Attribute values of parse tree: Which is


sometimes known as decorating the parse tree.
Top-down approach (if all attributes are inherited)
Bottom-up approach (if all attributes are synthesized)
Mixed approach (if a attributes are of both types)

If all attributes have been computed, the tree is called


fully attributed tree.

Prof.R.Gururaj CSF301 PPL BITS Pilani, Hyderabad Campus


Prof.R.Gururaj CSF301 PPL BITS Pilani, Hyderabad Campus
Flow of attribute values

Prof.R.Gururaj CSF301 PPL BITS Pilani, Hyderabad Campus


BITS Pilani, Hyderabad Campus
Describing the meaning of
Programs. (Dynamic semantics)
Now we take up the task of describing the dynamic
semantics of a (constructs) programming language.
It is easy to describe syntax than semantics.
No universally accepted notation of describing the
semantics.

Prof.R.Gururaj CSF301 PPL BITS Pilani, Hyderabad Campus


Need for describing the dynamic
semantics(meaning):
1. Compiler designers
2. SW Developers

Prof.R.Gururaj CSF301 PPL BITS Pilani, Hyderabad Campus


Operational semantics

The idea behind Operational semantics is to describe the


meaning of statements or program by specifying the
effects of running it on the machine.
The effects on the machine are viewed as the sequence
of changes in its state.
The machine’s state is the collection of the values in its
storage.

Usually intermediate languages are used to describing


the operational semantics.

Prof.R.Gururaj CSF301 PPL BITS Pilani, Hyderabad Campus


Prof.R.Gururaj CSF301 PPL BITS Pilani, Hyderabad Campus
Denotational semantics

Is the most rigorous and most widely known formal


method for describing the meaning of Programs.
It is solidly based on recursive function theory.

In operational semantics-Programming constructs are


translated to simpler PL constructs.
In Denotational semantics- Language constructs
(entities) are mapped to mathematical objects
(sets/functions). But it does not model step-by-step
computational processing of programs.

Prof.R.Gururaj CSF301 PPL BITS Pilani, Hyderabad Campus


Prof.R.Gururaj CSF301 PPL BITS Pilani, Hyderabad Campus
Prof.R.Gururaj CSF301 PPL BITS Pilani, Hyderabad Campus
Meaning
(Denoted objects)

Syntax object

We assume that the syntax and static syntax are correct.


Prof.R.Gururaj CSF301 PPL BITS Pilani, Hyderabad Campus
Axiomatic Semantics

An Axiom is a logical statement which is assumed to be


true.
Axiomatic Semantics is based on mathematical logic.
Rather directly specifying the meaning of a program,
axiomatic semantics specifies what can be proven
about the program.
The meaning of the statements is based on the
relationships among program variables and constants
which are same for every execution of the program.

Prof.R.Gururaj CSF301 PPL BITS Pilani, Hyderabad Campus


Axiomatic Semantics has two applications:
1. Program verification
2. Program semantic specification
We use notions:
Assertions
Precondition
Post-condition
Weakest precondition

Prof.R.Gururaj CSF301 PPL BITS Pilani, Hyderabad Campus


Using Precondition and post-condition

In axiomatic semantics the meaning of a specific


statement is defined by its precondition and post-
condition.
In effect the two assertions specify precisely the effect
of executing the statement.

Prof.R.Gururaj CSF301 PPL BITS Pilani, Hyderabad Campus


Assignment statements

Ex.1

Weekest precondition of the statement.

Ex.2

Prof.R.Gururaj CSF301 PPL BITS Pilani, Hyderabad Campus


Sequences

Prof.R.Gururaj CSF301 PPL BITS Pilani, Hyderabad Campus


Selection

Prof.R.Gururaj CSF301 PPL BITS Pilani, Hyderabad Campus


Loops

Prof.R.Gururaj CSF301 PPL BITS Pilani, Hyderabad Campus


Program Proofs

Same as precondition of the program. Hence correct.

Prof.R.Gururaj CSF301 PPL BITS Pilani, Hyderabad Campus


Summary

 What is Syntax and Semantics?


 Describing Syntax.
 Grammar, Derivation, Parse tree and ambiguity.
 Specifying the semantics:
 Static semantics: Attribute Grammar
 Dynamic Semantics- Operational semantics,
Denotational semantics, Axiomatic semantics

Prof.R.Gururaj CSF301 PPL BITS Pilani, Hyderabad Campus

You might also like