Principals of Programming Language 1.2
Principals of Programming Language 1.2
Languages
UNIT – 1
Lexemes Tokens
index identifier
= equal_sign
2 int_literal
* mult_op
count identifier
+ plus_op
17 int_literal
; semicolon
Language Recognizers
• A language can be defined in two ways: by recognition and
by generation.
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.
BNF – Fundamentals (cont...)
• The LHS and RHS combined is called a rule or
production.
In one parse tree * is lower and in another + is lower. Which one to choose?
Operator Precedence (cont...)
• Correct ordering is specified by using separate non-terminals to
represent the operands of operators that require different
precedence.
• If the LHS of a rule appears first in its RHS, such grammar is said
to be left recursive.
Associativity (cont...)
• If the LHS of a rule appears last in its RHS, such
grammar is said to be right recursive.
Ex:
<if_stmt> -> if (<expr>) <stmt> [ else <stmt> ]
Extended BNF (EBNF) (cont...)
• Second extension is the use of braces in the
RHS to indicate that the enclosed part can be
repeated indefinitely.
Ex:
<iden_list> -> <identifier> {, <identifier> }
Extended BNF (EBNF) (cont...)
• Third extension deals with multiple-choice options
using the parentheses and OR operator, |.
Ex:
<term> -> <term> (* | / | % ) <factor>
• The set A(X) contains two disjoint sets S(X) and I(X),
called synthesized attributes and inherited attributes.
actual_type:
Synthesized
Attribute
expected_type:
Inherited
Attribute
Attribute Grammar – Example 2 (cont...)
Attribute Grammar – Example 2 (cont...)
Attribute Grammar – Example 2 (cont...)
Dynamic Semantics
• Dynamic semantics deals with meaning of the
expressions, statements and program units.
• Disadvantages:
– Very complex for large programs
– Lacks mathematical rigor
• Uses:
– Vienna Definition Language (VDL) used to define PL/I
– Compiler work
Denotational Semantics
• A formal method for specifying the meaning of programs.
Denotational semantics is based on recursive function theory.
• These state changes are used to define the meanings of programs and
program constructs.
Denotational Semantics – Expressions
• Assumptions:
– Only operators are + and *
– An expression can have at most one operator
– Only operands are integer variables and integer
constants
– No parentheses
Denotational Semantics – Expressions (cont...)
• Disadvantages:
– Requires mathematical knowledge
– Hard for programmer to use
• Uses:
– Semantics for ALGOL 60 and Pascal
– Used in compiler generation and optimization
Axiomatic Semantics
• Axiomatic semantics is based on formal logic (first order predicate
calculus).
Ex:
sum = 2 * x + 1 {sum > 1}
• The above rule states that if S1,...,Sn are true, then the truth of S
can be inferred. The top part of a rule is called its antecedent and
the bottom part is called its consequent.
• Example:
a = b / 2 – 1 { a < 10 }
b / 2 – 1 < 10
b < 22
Axiomatic Semantics – Assignment
Statements (cont...)
• The notation for specifying the axiomatic
semantics of a given statement is{P} S {Q}.
Where, P is the pre-condition, Q is the
post-condition and S is the statement.
• Disadvantages:
– Predicate transforms are hard to define
– Hard to give complete meaning
– Does not suggest implementation
• Uses:
– Semantics of Pascal
– Reasoning about correctness
Describing Semantics - Summary
• Operational Semantics
– Informal descriptions
– Compiler work
• Denotational Semantics
– Formal definitions
– Provably correct implementations
• Axiomatic Semantics
– Reasoning about particular properties
– Proofs of correctness
UNIT – 1
• Lexical analyzers extracts lexemes from a given string and produce the
corresponding tokens.
• Lexical analyzer inserts lexemes for user-defined names into symbol table.
• There might be more than one RHS in the rules that matches a
substring in the sentential form. The correct RHS is called the
handle.
Bottom-Up Parsers (cont...)
• Consider the following grammar:
S -> aAc
A -> aA | b
Example grammar:
E -> iE’
E’ -> +iE’ | ε
Recursive Descent Parser
E’( )
– Example (cont...)
{
if(l == ‘+’)
E( ) {
{ match(‘+’);
if(l == ‘i’) match(‘i’);
{ E’( );
match(‘i’); }
E’( ); else
} return;
} }
match(char t) main( )
{ {
if(l == t) E( );
l = getchar(); if(l == ‘$’)
else printf(“Parsing done!”);
printf(“error”); }
}
Recursive Descent Parser – Example
(cont...)
Example string: i + i $
i E’
+ i E’