0% found this document useful (0 votes)
5 views60 pages

Chapter 03 Describing Programming Languages

The document provides an overview of programming languages, focusing on the concepts of syntax, semantics, and the compilation process. It introduces key terms such as lexemes, tokens, and describes the use of Backus-Naur Form (BNF) for defining language grammar. Additionally, it discusses ambiguity in grammar and methods for removing it through associativity and precedence rules.

Uploaded by

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

Chapter 03 Describing Programming Languages

The document provides an overview of programming languages, focusing on the concepts of syntax, semantics, and the compilation process. It introduces key terms such as lexemes, tokens, and describes the use of Backus-Naur Form (BNF) for defining language grammar. Additionally, it discusses ambiguity in grammar and methods for removing it through associativity and precedence rules.

Uploaded by

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

Spring 2022

Slides Courtesy From :


“Concepts of Programming Languages” –by
Robert W. Sebesta.
Published by Pearson Education, Inc. USA.
Eleventh Edition. 2016

IUBAT CSC 461 01/05/25 1


Introduction
 Language and sentence
 Let  be a set of characters. A language over  is
a set of strings of characters drawn from .
 Alphabet = English characters
Language = English sentences
 Alphabet = ASCII
Language = C programs
 Each string over  is a sentence

 A language is a set of sentences


 For Java programming language, a Java program is a
sentence
 Java programming language consists of all legal Java
programs
 Infinite ?

IUBAT CSC 461 01/05/25 2


Introduction
 A language is a set of strings of
characters from some alphabet
 The strings of a language are called
sentences or statements
 These small units are called lexemes
 The lexemes of a programming
language include its numeric literals,
operators, and special words
 Lexemes are partitioned into groups—
for example, the names of variables,
methods, classes
IUBAT CSC 461 01/05/25 3
Introduction

 Each lexeme group is represented by


a name, or token.
 So, a token of a language is a
category of its lexemes.
 An identifier is a token that can have
lexemes, or instances, such as sum
and total

IUBAT CSC 461 01/05/25 4


Example

IUBAT CSC 461 01/05/25 5


The Compilation Process
Source Program IF (a<b) THEN c=1*d;

Lexical Analyzer
ID ID ID CONST ID
IF ( < THEN = “1” * “d”
“a” “b” “c”
Token Sequence
cond_expr a
Syntax Analyzer <
IF_stmt b lhs
Syntax Tree list c
assign_stmt rhs 1
Semantic Analyzer *
d
GE a, b, L1
3-Address Code MUlT 1, d, c
L1:
Code Optimizer GE a, b, L1
MOV d, c
L1:
Optimized 3-Addr. Code loadi R1,a
cmpi R1,b
jge L1
Code Generation loadi R1,d
storei R1,c
Assembly Code L1:

IUBAT CSC 461 01/05/25 6


Describing a Programming Language
 Describe programming languages-Not easy
to provide a concise and precise description
 Syntax:
 A study of rule for formation of grammatical sentence in language
 The syntax of a programming language is the form of its
expressions, statements, and program units.
 Semantic: is the meaning of those expressions, statements and
program units.
 Three phases
 Describing tokens
 A category of lowest level syntactic unit over alphabet
cond_expr
 e.g. ID e
IF THEN
 Describing syntax “a”
IF_stmt
 The structure of program structures, expression, statements, etc. stmt
 Describing semantics
 The meaning of the program, expression, statement, etc.
 e.g. only then-branch is executed if cond_expr is evaluated to be true

IUBAT CSC 461 01/05/25 7


Regular Expression
Definition:
The regular expressions over  are the
smallest set of expressions including

`c` where c
A+B where A, B are RE over 
AB where A, B are RE over 
A* where A is a RE over 

Each RE corresponds to a regular


language
We use them interchangeably
IUBAT CSC 461 01/05/25 8
Example
s
 Keywords: “else” or “if” or “while” or …
 `else` + `if` + `while` + …
 `else` abbreviates `e` `l` `s` `e`
 keywords = { `else`, `if`, `then`, `while`,
…}

 Integer:
 digit =
`0`+`1`+`2`+`3`+`4`+`5`+`6`+`7`+`8`
+`9`
 integer = digit digit*

IUBAT CSC 461 01/05/25 9


Examples
PhonesAround
number: consider (412) 624-0000
∑ = digit  { -, (, ) }
area = digit3
Exchange = digit3
phone = digit4
phone_number = '(' area ')' exchange '-'
phone

Email address: [email protected]


∑ = letter  { . , @ }
name = letter+
address = name`@`name`.`name

IUBAT CSC 461 01/05/25 10


Describing Language
Syntax
 Context Free Language to describe
language syntax
 RE is not powerful enough
 Using BNF (Backus-Naur Form, 1959)
 Invented to describe ALGOL 58

 BNF fundamentals
 Two types of symbols
 Non-terminal symbols: BNF abstraction
 Terminal symbols: tokens
 A set of grammar rules
 LHS → RHS

IUBAT CSC 461 01/05/25 11


Backus-Naur Form
(BNF)
 A metalanguage is a language that is
used to describe another language.
 BNF is a metalanguage for programming
languages.

 BNF uses abstractions for syntactic structures

 Java assignment statement, for example,


might be represented by the abstraction
<assign>
 Definition of <assign> can be given by
 <assign> → <var> = <expression>
IUBAT CSC 461 01/05/25 12
Backus-Naur Form (BNF)
 This particular rule specifies that
 The abstraction <assign> is defined as an
instance of the abstraction <var>,
 Followed by the lexeme =,
 Followed by an instance of the abstraction
<expression>.

 One example sentence whose


syntactic structure is described by the
rule is
 total = subtotal1 + subtotal2
IUBAT CSC 461 01/05/25 13
Backus-Naur Form (BNF)
 The abstractions in a BNF description, or
grammar, are often called non-terminal
symbols, or simply non-terminals

 And the lexemes and tokens of the rules


are called terminal symbols, or simply
terminals

IUBAT CSC 461 01/05/25 14


Backus-Naur Form (BNF)
 A grammar is a finite nonempty set of
rules
 A rule has one left hand symbol (LHS)
and can have more than one right hand
symbols

<program>  <stmts>
<stmts>  <stmt> | <stmt> , <stmts>
<stmt>  <var> = <expr>
<var>  a | b | c | d
<expr>  <term> + <term> | <term> - <term>
<term>  <var> | const
IUBAT CSC 461 01/05/25 15
Backus-Naur Form (BNF)
 <if_stmt> → if ( <logic_expr> ) <stmt>

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


else <stmt>

Or with the rule


 <if_stmt> → if ( <logic_expr> ) <stmt>
| if ( <logic_expr> )
<stmt> else <stmt>

IUBAT CSC 461 01/05/25 16


BNF Rules
 A rule is recursive if its LHS
appears in its RHS.

 The following rules illustrate how


recursion is used to describe lists:
<ident_list> → identifier
| identifier,
<ident_list>

IUBAT CSC 461 01/05/25 17


From Rules to
DerivationSentences
A sentence can be generated by a repeat
application of rules, starting from the start
symbol
Start symbol is always a non-terminal symbol

<program> => <stmts>


=> <stmt>
=> <var> = <expr>
=> a =<expr>
=> a = <term> + <term>
=> a = <var> + <term>
=> a = b + <term>
=> a = b + const IUBAT CSC 461 01/05/25 18
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 non-terminal in each
sentential form is the one that is
expanded
A rightmost derivation is one in which
the rightmost non-terminal in each
sentential form is the one that is
expanded IUBAT CSC 461 01/05/25 19
Grammars and Derivations

: Derives
•Derivations are called

sentential form
•Leftmost derivation

IUBAT CSC 461 01/05/25 20


Grammars and Derivations

IUBAT CSC 461 01/05/25 21


Parse Tree
 A hierarchical representation of a
derivation
 Hierarchical syntactic structure of the sentences
<program>
 These hierarchical structures
are called Parse Trees <stmts>
 Every internal node of a
<stmt>
parse tree is labeled with a
non-terminal symbol <var> = <expr>
 Every leaf is labeled with a a <term> + <term>
terminal symbol.
 Every sub-tree of a parse tree<var> const

describes one instance of an b


abstraction in the sentence.
IUBAT CSC 461 01/05/25 22
1.A=A*(B+ Parse Tree of
(C*A)) A=B*(A+C)
2.A=A*(B+(C))

IUBAT CSC 461 01/05/25 23


Ambiguity
 A grammar is ambiguous if and only if it
generates a sentential form that has two or
more distinct parse trees.
Other Characteristics of ambiguous grammar:
 If the grammar generates a sentence with more than one leftmost
derivation
 If the grammar generates a sentence with more than one leftmost
derivation

 Ambiguity is related to the grammar not the language


 What is the problem? – The compiler chooses the
code to be generated for a statement by examining its
parse tree. If a language structure has more than one parse
tree, then the meaning of the structure cannot be
determined uniquely.
IUBAT CSC 461 01/05/25 24
Ambiguous Grammar
<expr>  <expr> <op> <expr> | const
<op>  / | -
<expr>
<expr>

<expr> <op> <expr> <expr> <op> <expr>

<expr> <op> <expr> <expr> <op> <expr>

const - const / const const - const / const

IUBAT CSC 461 01/05/25 25


Ambiguous Grammar

IUBAT CSC 461 01/05/25 26


Removing Ambiguity
 By introducing Associativity and Precedence

1. Precedence: When an expression includes two different operators, for


example, x + y * z, assigning different precedence levels to operators is
important.
For example, in the 1st parse tree, the multiplication operator is generated lower in
the tree, which could indicate that it has precedence over the addition operator
in the expression. The second parse tree, however, indicates just the opposite.
It appears, therefore, that the two parse trees indicate conflicting precedence
information.

The correct ordering is specified by using separate nonterminal symbols


to represent the operands of the operators that have different
precedence. This
requires additional nonterminal and some new rules.

Instead of using <expr> for both operands of both + and *, we could use
three nonterminal to represent operands, which allows the grammar
to force different operators to different levels in the parse tree.

IUBAT CSC 461 01/05/25 27


If <expr> is the root symbol for expressions, + can be forced to
the top of the parse tree by having <expr> directly generate
only + operators, using the new nonterminal, <term>, as the
right operand of +. Next, we can define <term> to generate *
operators, using <term> as the left operand and a new
nonterminal, <factor>, as its right operand. Now, * will always
be lower in the parse tree, simply because it is farther from the
start symbol than + in every derivation

IUBAT CSC 461 01/05/25 28


Removing Ambiguity
(2) Associativity: When an expression includes two operators that have
the same precedence (as * and / usually have)—for example, A / B * C —
a semantic rule is required to specify which should have precedence.
This rule is named Associativity.
When a grammar rule has its LHS also appearing at the beginning
of
its RHS, the rule is said to be left recursive. This left recursion
specifies left
associativity.

IUBAT CSC 461 01/05/25 29


Example Question

 Consider the grammar:


 <E> <E>+<E> | <E>*<E> |
(<E>) | a

(i)Check whether the grammar is


ambiguous or not.
(ii)If it is ambiguous, then remove the
ambiguity.

IUBAT CSC 461 01/05/25 30


IUBAT CSC 461 01/05/25 31
Extended
BNF
 Optional parts are placed in brackets [ ]
<proc_call> -> ident [(<expr_list>)]
<if_stmt> → if (<expression>) <statement> [else <statement>]

 Alternative parts of RHSs are placed inside


parentheses and separated via vertical bars
<term> → <term> (+|-) const <term> →
<term> + <const>
|
<term> - <const>
 Repetitions (0 or more) are placed inside braces
{}
<ident_list> → <identifier> {, <identifier>}
instead of
<ident_list> → <identifier> | <identifier> , <ident_list>
IUBAT CSC 461 01/05/25 32
BNF and
 BNF EBNF
<expr>  <expr> + <term> | <expr> - <term> | <term>
<term>  <term> * <factor> | <term> / <factor | <factor>
<factor>  <exp> ** <factor> | <exp>
<exp>  (<expr>) | id

 EBNF
After 2nd rule: <expr>  <expr> (+ | -) <term>
<term>  <term> (* | /) <factor>
<factor>  <exp> ** <factor>
<exp>  (<expr>) | id

After 3rd rule: <expr>  <term> {(+ | -) <term>}


<term>  <factor> {(* | /) <factor>}
<factor>  <exp> {** <exp>}
<exp>  (<expr>) | id
IUBAT CSC 461 01/05/25 33
Practice

Convert the following BNF to EBNF:

IUBAT CSC 461 01/05/25 34


Describing Language

Semantics
What is an Attribute grammar
An attribute grammar is an extension to a context-free grammar.
 The extension allows certain language rules to be conveniently
described, such as type compatibility rule
 In Java, for example, a floating-point value cannot be assigned to an
integer type variable, although the opposite is legal.
Describing static semantics
 These problems exemplify the categories of language rules called static
semantics rules.
 Static semantics is so named because the analysis required to check
these specifications can be done at compile time
 The static semantics of a language is only indirectly related to the
meaning of programs during execution.
 Many static semantic rules of a language state its type constraints.
Describing dynamic semantics
 Operational semantics
 Axiomatic semantics
 Denotational semantics
IUBAT CSC 461 01/05/25 35
Attribute
Example Attribute grammars make
use of 2 types of
functions:
E  T + E1 {E.val = T.val + E1.val}
ET {E.val = T.val}
1.Attribute Computation
T  int * T1 {T.val = int.val * T1.val}
function: This can be used
T  int {T.val = int.val} to compute values for the
attribute
2.Predicate function: It
Context Free Grammar states some of the syntax
or semantic rules of a
Semantic actions grammar
attributes

Attribute: An attribute is a property which is assigned to a


grammar symbol/rule.
Ex: The data type of a variable or its value or its addressing
memory etc.
IUBAT CSC 461 01/05/25 36
Two Types of Attributes
P

c1 c2 c3 c4

Synthesized attributes: values are computed from ones of the children nodes
Synthesized of P = f(c1, c2, c3, c4)

S1 S2 S3 S4

Inherited attributes: values are computed from attributes of the siblings and parent of

the node
Inherited of S4= f(P, S1, S2, S3)

• Semantic rules create dependencies between attributes


•Represent the dependencies as a graph
IUBAT CSC 461 01/05/25 37
•Fom the graph, derive evaluation order for semantic rules
Exampl
e
A A

D E F D E F

b is synthesized attribute of A b is Inherited attribute of D


ADEF DAEF
ci’s attributes of D, E, F ci’s attributes of A, E, F
 Terminal symbols – have synthesized attributes only
 Start symbol – is assumed not to have any inherited attributes

 Synthesized attributes are naturally computed from bottom-up parsing


 Inherited attributes are naturally computed from top-down parsing
(Intrinsic attributes are synthesized attributes whose properties are
found outside the grammar. For example the symbol table which can
list the declared type of the variable)

IUBAT CSC 461 01/05/25 38


Static Semantics
(The Attributes for the Non terminals)
The syntax portion of our example attribute grammar is:
<assign> → <var> = <expr>
<expr> → <var> + <var> | <var>
<var> → A | B | C
The attributes for the nonterminals in the example attribute grammar are
described in the following paragraphs:

 actual_type
A synthesized attribute associated with the nonterminals <var> and
<expr>. It is used to store the actual type, int or real, of a variable
or expression. In the case of a variable, the actual type is intrinsic. In
the case of an expression, it is determined from the actual types of
the child nodes of the <expr> nonterminal.
 expected_type
An inherited attribute associated with the nonterminal <expr>. It is
used to store the type, either int or real, that is expected for the
expression, as determined by the type of the variable on the left
IUBAT CSC 461 01/05/25 39
side of the assignment statement
Example-1
To check the type rules of a simple assignment
Question: The syntax statement.
portion of our example attribute
grammar is:
<assign> → <var> = <expr>
<expr> → <var> + <var> | <var>
<var> → A | B | C
Find the General rule, syntax and static semantics & flow of
attributes in the parse tree of this assignment statement.
General rules:
1. The only variable names are A, B, and C.
2. The right side of the assignments can be either a variable or an
expression in the form of a variable added to another variable.
3. The variables can be one of two types: int or real.
4. When there are two variables on the right side of an assignment, they
need not be the same type.
5. The type of the expression when the operand types are not the same is
always real.
6. When they are the same, the expression type is that of the operands.
7. The type of the left side of the assignment must match the type of the
right side. IUBAT CSC 461 01/05/25 40
The look-up function looks up a given variable name in the symbol
IUBAT CSC 461 01/05/25 41
table and returns the variable’s type.
process of computing
the attribute values of a
parse tree, which is
sometimes called
decorating the parse
tree. If all attributes
were inherited, this
could proceed in a
completely top-down
order, from the root to
the leaves. Alternatively,
it could proceed in a
completely bottom-up
order, from the leaves to
the root, if all the
attributes were
synthesized. Because
our grammar has both
synthesized and
inherited attributes, the
evaluation process
cannot be in any single
direction. The following
is an evaluation of the
attributes, in an order in
which it is possible to
compute them: IUBAT CSC 461 01/05/25 42
The flow of Attributes
in the Tree

IUBAT CSC 461 01/05/25 43


Example
 Write General rules and Syntax and
Static Semantics of the assignment
statement “C = A * B + C ”. Also Show
the flow of Attributes in the Tree by
using following:

<assign> → <var> = <expr>


<expr> → <expr> * <var> | <expr> +
<var> | <var>
<var> → A | B | C

IUBAT CSC 461 01/05/25 44


Dynamic
Semantics
In dynamic semantics, we describe the meaning of the various
components of the program including expressions, statements, and so
on.
Unlike static semantics, they cannot be checked at compile time, rather
they are only checked at runtime. There are 3 methods of dynamic
statement:
I: Operational semantics:
 Describe the meaning of a program by specifying the effects
of executing these statements or programs on a machine,
either simulated or actual. The effects on machine will be
viewed as change in the state of the machines (memory,
registers, etc.) that defines the meaning
Meaning of of
(Semantic the
thestatement.
statement):
expr1;
 Example:
loop: if expr2 == 0 goto out
C- statement:
...
for (expr1; expr2; expr3) { expr3;
... goto loop
} out: ...
IUBAT CSC 461 01/05/25 45
Discussion
 Describing operational semantics requires a real or
virtual machine to work on Intermediate language
form where the primary concern is that every
construct of that intermediate form must have an
obvious and unambiguous meaning.

 There are different levels of uses of operational semantics.


 Natural Operational Semantics:
 Meaning is determined only in final result of the execution (At the
Highest level)
 Definition of lower level computer required
 A Virtual Machine (an Interpreter) required
 Structural Operational Semantics:
 Meaning is determined through an examination of the complete
sequence of state changes (At the lowest level)
 Machine-dependent
 The intermediate code can be visually inspected.
 May not be easy to understand
 Evaluation
 Good only when it is used informally
 IUBAT CSC 461 01/05/25 46
Extremely complex & serves no practical purposes if used formally
II: Axiomatic Semantics
 Axiomatic semantics is based on
mathematical logic.

 Rather than directly specifying the meaning


of a program, axiomatic semantics specifies
what can be proven about the program to
prove the correctness of programs.

 When axiomatic semantics is used to specify


formally the meaning of a statement, the
meaning is defined by the statement’s effect
on assertions about the data affected by the
statement
IUBAT CSC 461 01/05/25 47
II: Axiomatic Semantics
 The logical expressions used in axiomatic
semantics are called predicates, or assertions. 2
types of assertions:
 Pre-condition: An assertion immediately preceding a program
statement describes the constraints on the program variables
at that point in the program
 Post-condition: An assertion immediately following a
statement describes the new constraints on those variables
(and possibly others) after execution of the statement
Form
Assertions
 Pre-, post- conditions: {P} statement {Q}
{b > 0}
a = b + 1
{a > 1}
Axioms (logical statement that is assumed to be
true) 48
IUBAT CSC 461 01/05/25
Pre- and Post- Condition of Assignment
statement
 The precondition and postcondition of an assignment
statement together define its meaning.
 Let x = E be a general assignment statement and Q be its
postcondition.
Then, its weakest precondition, P, is defined by the axiom

which means that P is computed as Q with all instances of x replaced


by E
For example, if we have the assignment statement and postcondition
{?} a = b / 2 - 1 {a < 10}

the weakest precondition is computed by substituting b / 2 - 1 for a in the


postcondition {a < 10}, as follows:

b / 2 - 1 < 10
b < 22
Thus, the weakest precondition for the given assignment statement and
postcondition is {b < 22}
IUBAT CSC 461 01/05/25 49
Example
Compute the Weakest Pre-
Condition for each of the
following Assignment
Statements and for the Post-
Conditions:
I. a = 3*a - 2 * b + 21
{a > -7}
II.x = 4 * y – 5 * x + 13
{x < 9}
IUBAT CSC 461 01/05/25 50
Evaluation of Axiomatic
Semantics
Developing axioms or inference rules
for all of the statements in a
language is difficult

 Good for correctness proofs, for


reasoning about programs

 Limited power for language


designers or compiler writers

IUBAT CSC 461 01/05/25 51


III: Denotational
Semantics
 Based on “Recursive Function theory”
 The most rigorous and widely used semantics description method
 The denotational semantics of a program could be defined in terms of state
changes in an ideal computer
 Denotational semantics is related to operational semantics.
 In operational semantics, programming language constructs are
translated into simpler programming language constructs, which become
the basis of the meaning of the construct. But in denotational semantics,
programming language constructs are mapped to mathematical objects,
either sets or, more often, functions .
 So, denotational semantics uses the state of the program to describe
meaning, whereas operational semantics uses the state of a machine.
 The key difference between operational semantics and denotational
semantics is that state changes in operational semantics are defined by
coded algorithms, written in some programming language, whereas in
denotational semantics, state changes are defined by mathematical
functions .

IUBAT CSC 461 01/05/25 52


Building Denotational
Specification
 The change of states in denotational semantics is defined
in terms of only the values of all of the program’s
variables. The state of a program is the values of all its
current variables are:
s = {<i1, v1>, <i2, v2>, …, <in, vn>}
Each i is the name of a variable, and the associated v’s
are the current values of those variables.

 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

IUBAT CSC 461 01/05/25 53


Building Denotational
Specification
The process of building a denotational
specification for a language
 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

Most semantics mapping functions for


programs and program constructs map
states to states. These state changes
are used to define the meanings of
programs and program constructs .

IUBAT CSC 461 01/05/25 54


Exampl
e
We use a very simple language construct,
character string representations of binary
numbers, to introduce the denotational
method.
The syntax of such binary numbers can be
described by the following grammar rules:

<bin_num> → '0'
| '1'
| <bin_num> '0'
| <bin_num> '1'

IUBAT CSC 461 01/05/25 55


A parse tree of the binary
number 110 The syntactic domain of the mapping
function for binary numbers is the set of
all character string representations of
binary numbers.
The semantic domain is the set of
<bin_num> nonnegative decimal numbers,
symbolized by N.
To describe the meaning of binary
numbers using denotational semantics,
<bin_num> ‘0’ we associate the actual meaning (a
decimal number) with each rule that
has a single terminal symbol as its RHS.
In our example, decimal numbers must be
associated with the first two grammar
<bin_num> ‘1’
rules. The other two grammar rules are,
in a sense, computational rules.
because they combine a terminal
symbol, to which an object can be
‘1’ associated, with a nonterminal, which
can be expected to represent some
construct
IUBAT CSC 461 01/05/25 56
A parse tree of the binary
number 110
The “Mapping semantic function”,
named Mbin, maps the syntactic objects to
6 N, which is the set of nonnegative decimal
numbers.
<bin_num> The function Mbin is defined as follows:
Mbin('0') = 0
3 Mbin('1') = 1
<bin_num> ‘0’ Mbin(<bin_num> '0') = 2 * Mbin(<bin_num>)
Mbin(<bin_num> '1') = 2 * Mbin(<bin_num>)
+1
1
<bin_num> ‘1’ The meanings, or denoted objects (which in
this case are decimal numbers), can be
attached to the nodes of the parse tree
shown on the tree in Figure. This is syntax-
‘1’ directed semantics. Syntactic entities are
mapped to mathematical objects with
concrete meaning.
IUBAT CSC 461 01/05/25 57
Exercise

 Using the following Grammar find the Mapping


Semantic Functions and also the De-notational
Semantic meaning/values in decimal numbers:
<bin_num> -> ‘0’ | ‘1’
| <bin_num> ‘0’
| <bin_num> ‘1’
Show a Parse Tree with Functions and values of
the objects of a Binary number: 101010

IUBAT CSC 461 01/05/25 58


Evaluation
 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

IUBAT CSC 461 01/05/25 59


Summar
Describingyprogramming languages
Describing tokens
Using RE
Describing syntax
Using BNF/CFG
Describing semantics
Static semantics
Using attributed grammar
Dynamic semantics
No widely accepted approaches
Three possible ways:
operational, axiomatic, denotational

IUBAT CSC 461 01/05/25 60

You might also like