Principles of Programming Languages
P. S. Suryateja
Asst. Professor, CSE Dept
Vishnu Institute of Technology
Vishnu Institute of technology – Website: www.vishnu.edu.in
UNIT – 1��SYNTAX & SEMANTICS
Vishnu Institute of technology – Website: www.vishnu.edu.in
General Problem of Describing Syntax
Vishnu Institute of technology – Website: www.vishnu.edu.in
General Problem of Describing Syntax (cont...)
Vishnu Institute of technology – Website: www.vishnu.edu.in
General Problem of Describing Syntax (cont...)
index = 2 * count + 17;
Lexemes Tokens
index identifier
= equal_sign
2 int_literal
* mult_op
count identifier
+ plus_op
17 int_literal
; semicolon
Vishnu Institute of technology – Website: www.vishnu.edu.in
Language Recognizers
Vishnu Institute of technology – Website: www.vishnu.edu.in
Language Generators
Vishnu Institute of technology – Website: www.vishnu.edu.in
Formal Methods of Describing Syntax – Context-Free Grammars
Vishnu Institute of technology – Website: www.vishnu.edu.in
Formal Methods of Describing Syntax – Backus-Naur Form (BNF)
Vishnu Institute of technology – Website: www.vishnu.edu.in
BNF - Fundamentals
<assign> -> <var> = <expression>
The text on the left side of the arrow is called left-hand side (LHS), is the abstraction being defined. The text to the right of the arrow is called as right-hand side (RHS), which is the definition of LHS and can contain a mixture of tokens, lexemes or other abstractions.
Vishnu Institute of technology – Website: www.vishnu.edu.in
BNF – Fundamentals (cont...)
total = s1 + s2
Vishnu Institute of technology – Website: www.vishnu.edu.in
BNF – Fundamentals (cont...)
<if_stmt> -> if (<logic_expr>) <stmt>
<if_stmt> -> if (<logic_expr>) <stmt> else <stmt>
Above two rules can be combined as follows:
<if_stmt> -> if (<logic_expr>) <stmt> |
if (<logic_expr>) <stmt> else <stmt>
Vishnu Institute of technology – Website: www.vishnu.edu.in
BNF – Fundamentals (cont...)
<iden_list> -> identifier |
identifier, <iden_list>
Vishnu Institute of technology – Website: www.vishnu.edu.in
Grammars and Derivations
Vishnu Institute of technology – Website: www.vishnu.edu.in
Grammars and Derivations (cont...)
Adopted from Concepts of Programming Languages - Sebesta
Vishnu Institute of technology – Website: www.vishnu.edu.in
Grammars and Derivations (cont...)
Adopted from Concepts of Programming Languages - Sebesta
Vishnu Institute of technology – Website: www.vishnu.edu.in
Grammars and Derivations (cont...)
Vishnu Institute of technology – Website: www.vishnu.edu.in
Parse Trees
Vishnu Institute of technology – Website: www.vishnu.edu.in
Parse Trees (cont...)
Adopted from Concepts of Programming Languages - Sebesta
Vishnu Institute of technology – Website: www.vishnu.edu.in
Parse Trees (cont...)
Adopted from Concepts of Programming Languages - Sebesta
Vishnu Institute of technology – Website: www.vishnu.edu.in
Ambiguity
Adopted from Concepts of Programming Languages - Sebesta
Vishnu Institute of technology – Website: www.vishnu.edu.in
Ambiguity (cont...)
Adopted from Concepts of Programming Languages - Sebesta
Parse trees for the string A = B + C * A
Vishnu Institute of technology – Website: www.vishnu.edu.in
Operator Precedence
Vishnu Institute of technology – Website: www.vishnu.edu.in
Operator Precedence (cont...)
Adopted from Concepts of Programming Languages - Sebesta
Parse trees for the string A = B + C * A
In one parse tree * is lower and in another + is lower. Which one to choose?
Vishnu Institute of technology – Website: www.vishnu.edu.in
Operator Precedence (cont...)
Adopted from Concepts of Programming Languages - Sebesta
Vishnu Institute of technology – Website: www.vishnu.edu.in
Operator Precedence (cont...)
Adopted from Concepts of Programming Languages - Sebesta
Vishnu Institute of technology – Website: www.vishnu.edu.in
Associativity
Adopted from Concepts of Programming Languages - Sebesta
Vishnu Institute of technology – Website: www.vishnu.edu.in
Associativity (cont...)
Vishnu Institute of technology – Website: www.vishnu.edu.in
Extended BNF (EBNF)
Ex:
<if_stmt> -> if (<expr>) <stmt> [ else <stmt> ]
Vishnu Institute of technology – Website: www.vishnu.edu.in
Extended BNF (EBNF) (cont...)
Ex:
<iden_list> -> <identifier> {, <identifier> }
Vishnu Institute of technology – Website: www.vishnu.edu.in
Extended BNF (EBNF) (cont...)
Ex:
<term> -> <term> (* | / | % ) <factor>
Vishnu Institute of technology – Website: www.vishnu.edu.in
Extended BNF (EBNF) (cont...)
Adopted from Concepts of Programming Languages - Sebesta
Vishnu Institute of technology – Website: www.vishnu.edu.in
Attribute Grammars
Vishnu Institute of technology – Website: www.vishnu.edu.in
Attribute Grammars – Static Semantics
Vishnu Institute of technology – Website: www.vishnu.edu.in
Attribute Grammars – Basic Concepts
Vishnu Institute of technology – Website: www.vishnu.edu.in
Attribute Grammars – Definition
Vishnu Institute of technology – Website: www.vishnu.edu.in
Attribute Grammars – Definition (cont...)
Vishnu Institute of technology – Website: www.vishnu.edu.in
Attribute Grammars – Definition (cont...)
Vishnu Institute of technology – Website: www.vishnu.edu.in
Intrinsic Attributes
Vishnu Institute of technology – Website: www.vishnu.edu.in
Attribute Grammar – Example 1
Adopted from Concepts of Programming Languages - Sebesta
Attribute grammar that describes the rule that the name on the end of an Ada procedure must match the procedure’s name. (This rule cannot be stated using BNF).
Note: Numbers represented as subscripts are used to denote the instances of an abstraction.
Vishnu Institute of technology – Website: www.vishnu.edu.in
Attribute Grammar – Example 2
Adopted from Concepts of Programming Languages - Sebesta
actual_type:
Synthesized Attribute
expected_type:
Inherited Attribute
Vishnu Institute of technology – Website: www.vishnu.edu.in
Attribute Grammar – Example 2 (cont...)
Adopted from Concepts of Programming Languages - Sebesta
Vishnu Institute of technology – Website: www.vishnu.edu.in
Attribute Grammar – Example 2 (cont...)
Adopted from Concepts of Programming Languages - Sebesta
Vishnu Institute of technology – Website: www.vishnu.edu.in
Attribute Grammar – Example 2 (cont...)
Adopted from Concepts of Programming Languages - Sebesta
Vishnu Institute of technology – Website: www.vishnu.edu.in
Dynamic Semantics
Vishnu Institute of technology – Website: www.vishnu.edu.in
Operational Semantics
Vishnu Institute of technology – Website: www.vishnu.edu.in
Operational Semantics - Ex
Vishnu Institute of technology – Website: www.vishnu.edu.in
Operational Semantics - Evaluation
Vishnu Institute of technology – Website: www.vishnu.edu.in
Denotational Semantics
Vishnu Institute of technology – Website: www.vishnu.edu.in
Denotational vs. Operational
Vishnu Institute of technology – Website: www.vishnu.edu.in
Denotational Semantics - Process
Vishnu Institute of technology – Website: www.vishnu.edu.in
Denotational Semantics – Ex 1
Example: Representing binary strings as decimal numbers
a) Syntax
b) Mapping Function Mbin
Adopted from Concepts of Programming Languages - Sebesta
Vishnu Institute of technology – Website: www.vishnu.edu.in
Denotational Semantics – The State of a Program
s = {<i1,v1>, ...... , <in,vn>}
where i is a variable and v is the corresponding value of that variable.
Vishnu Institute of technology – Website: www.vishnu.edu.in
Denotational Semantics – Expressions
Adopted from Concepts of Programming Languages - Sebesta
Vishnu Institute of technology – Website: www.vishnu.edu.in
Denotational Semantics – Expressions (cont...)
Adopted from Concepts of Programming Languages - Sebesta
Vishnu Institute of technology – Website: www.vishnu.edu.in
Denotational Semantics – Evaluation
Vishnu Institute of technology – Website: www.vishnu.edu.in
Axiomatic Semantics
Vishnu Institute of technology – Website: www.vishnu.edu.in
Axiomatic Semantics – Weakest Preconditions
Ex:
sum = 2 * x + 1 {sum > 1}
Vishnu Institute of technology – Website: www.vishnu.edu.in
Axiomatic Semantics (cont...)
S1, S2, ..... , Sn
-------------------
S
Vishnu Institute of technology – Website: www.vishnu.edu.in
Axiomatic Semantics – Assignment Statements
a = b / 2 – 1 { a < 10 }
Weakest pre-condition is computed by substituting b / 2 – 1 for a in the post-condition:
b / 2 – 1 < 10
b < 22
Vishnu Institute of technology – Website: www.vishnu.edu.in
Axiomatic Semantics – Assignment Statements (cont...)
{ Qx->E} x = E {Q}
Vishnu Institute of technology – Website: www.vishnu.edu.in
Axiomatic Semantics - Evaluation
Vishnu Institute of technology – Website: www.vishnu.edu.in
Describing Semantics - Summary
Vishnu Institute of technology – Website: www.vishnu.edu.in
UNIT – 1��LEXICAL ANALYSIS & PARSING
Vishnu Institute of technology – Website: www.vishnu.edu.in
Reasons for Separating Lexical Analysis and Syntax Analysis
Vishnu Institute of technology – Website: www.vishnu.edu.in
Lexical Analysis
Vishnu Institute of technology – Website: www.vishnu.edu.in
Lexical Analysis (cont...)
Vishnu Institute of technology – Website: www.vishnu.edu.in
Lexical Analysis (cont...)
Vishnu Institute of technology – Website: www.vishnu.edu.in
Lexical Analysis (cont...)
State diagram for arithmetic statements:
Adopted from Concepts of Programming Languages - Sebesta
Vishnu Institute of technology – Website: www.vishnu.edu.in
Parsing
Vishnu Institute of technology – Website: www.vishnu.edu.in
Parsing (cont...)
Vishnu Institute of technology – Website: www.vishnu.edu.in
Top-down Parsers
Vishnu Institute of technology – Website: www.vishnu.edu.in
Top-down Parsers (cont...)
Vishnu Institute of technology – Website: www.vishnu.edu.in
Top-down Parsers (cont...)
Vishnu Institute of technology – Website: www.vishnu.edu.in
Bottom-Up Parsers
Vishnu Institute of technology – Website: www.vishnu.edu.in
Bottom-Up Parsers (cont...)
S -> aAc
A -> aA | b
S => aAc => aaAc => aabc
Vishnu Institute of technology – Website: www.vishnu.edu.in
Complexity of Parsing
Vishnu Institute of technology – Website: www.vishnu.edu.in
Recursive Descent Parser
Vishnu Institute of technology – Website: www.vishnu.edu.in
Recursive Descent Parser - Example
Example grammar:
E -> iE’
E’ -> +iE’ | ε
Vishnu Institute of technology – Website: www.vishnu.edu.in
Recursive Descent Parser – Example (cont...)
E( )
{
if(l == ‘i’)
{
match(‘i’);
E’( );
}
}
E’( )
{
if(l == ‘+’)
{
match(‘+’);
match(‘i’);
E’( );
}
else
return;
}
match(char t)
{
if(l == t)
l = getchar();
else
printf(“error”);
}
main( )
{
E( );
if(l == ‘$’)
printf(“Parsing done!”);
}
Vishnu Institute of technology – Website: www.vishnu.edu.in
Recursive Descent Parser – Example (cont...)
Example string: i + i $
E
i E’
+ i E’
Vishnu Institute of technology – Website: www.vishnu.edu.in