Sak Notes
Sak Notes
http://www.cse.iitd.ac.in/˜sak/courses/pl/2019-20/2019-20.index.html
S. Arun-Kumar
Department of Computer Science and Engineering
I. I. T. Delhi, Hauz Khas, New Delhi 110 016.
April 2, 2020
2 Introduction to Compiling 19
4 Runtime Structure 54
Translated
Source
Macro−
processor
Pre−processor
Macro−translation
Errors
Pre−processing
Errors
Assembly
code
Compilation Linking Runtime
Errors Errors Assembler Errors
Loading
Errors Machine
code
Translated
Macro− Source
processor
Assembling
Errors
Pre−processor
Macro−translation
Errors
Pre−processing
Errors
Translated TeX
latex source LaTeX
Source
latex source Macro−
nw source
processor
noweave
Pre−processor
notangle Macro−translation
Errors
Errors
JJ J I II G O B ACK F ULL S CREEN C LOSE 11 OF 600
PL April 2, 2020
The Landscape of General PLs
7]QFSP TVSGIWWMRK
7MQYPEXMSR
)\TIVX
8IEGLMRK
7]WXIQW
1SHYPEV
4VSKVQK
6IEGXMZI
7]WXIQW
6IRHIVMRK
4VMRXMRK
6ITSVXMRK
;IF TVSKVEQQMRK
(]REQMG WGSTI
(]REQMG 1IQSV] EPPSGEXMSR
7XEXMG X]TI
7XEXMG WGSTI
,IET EPPSG
7XVSRK X]TMRK
6YRXMQI WXEGO
(]REQMG EPPSG
,IET EPPSG
7XEXMG WGSTI
6YRXMQI WXEGO
,IET EPPSG
7XEXMG WGSTI
7XEXMG X]TIW
6YRXMQI WXEGO
,IET EPPSG
stream of
characters
SCANNER
stream of
tokens
stream of
characters
SCANNER
stream of
tokens
PARSER
parse tree
stream of
characters
SCANNER
stream of
tokens
PARSER
parse tree
SEMANTIC ANALYZER
abstract
syntax tree
stream of
characters
SCANNER
stream of
tokens
PARSER
parse tree
SEMANTIC ANALYZER
abstract
syntax tree
stream of
characters
SCANNER
stream of
tokens
PARSER
parse tree
SEMANTIC ANALYZER
abstract
syntax tree
optimized
intermediate
representation
stream of
characters
SCANNER
stream of
tokens
PARSER
parse tree
SEMANTIC ANALYZER
abstract
syntax tree
optimized
intermediate
representation
CODE GENERATOR
target code
stream of
characters
SCANNER
stream of
tokens
PARSER
parse tree
ERROR− SEMANTIC ANALYZER SYMBOL
abstract
HANDLER TABLE
syntax tree
MANAGER
I.R. CODE GENERATOR
intermediate
representation
OPTIMIZER
optimized
intermediate
representation
CODE GENERATOR
target code
stream of
characters
SCANNER
stream of
tokens
PARSER
parse tree
ERROR− SEMANTIC ANALYZER SYMBOL
abstract
HANDLER TABLE
syntax tree
MANAGER
I.R. CODE GENERATOR
intermediate
representation
OPTIMIZER
optimized
intermediate
representation
CODE GENERATOR
target code
let
val x = 15
in
x + y
end
in
fun1 x
end
...
...
in ...
fun1 (fun2 x)
end
...
fun fun2 z =
...
in ...
fun1 (fun2 x)
end
let
val x = 15
in
x + y * z
end
in
fun1 x
end
let
val x = 15
in
x + y * z
end
in
fun1 x
end
let
val x = 15
in
x + y * z
end
in
fun1 x
end
let
val x = 15
in
x + y * z
end
in
fun1 x
end
let
val x = 15
in
x + y * z
end
in
fun1 x
end
let
val x = 15
in
x + y * z
end
in
fun1 x
end
let
val x = 15
in
x + y * z
end
in
fun1 x
end
in
fun fun3 x = ...
fun2 ...
fun1 ...
end
Procedure P2
Locals of P2
Procedure P21
Locals of P21
Body of P21
Call P21
Body of P2
Call P21
Procedure P1
Locals of P1
Body of P1
Call P2
Main body
Call P1
Globals
Procedure P2
Locals of P2
Procedure P21
Locals of P21
Body of P21
Body of P2
Procedure P1
Locals of P1
Body of P1
Main body
Globals
Main
Globals
Procedure P2
Locals of P2
Procedure P21
Locals of P21
Body of P21
Body of P2
Main → P1
Globals
Procedure P2
Locals of P2
Procedure P21
Locals of P21
Main → P1 → P2
Globals
Procedure P2
Return address to last of P2
Locals of P2
Dynamic link to last P2
Locals of P21
Procedure P21 Static link last P2
Locals of P21 Formal par P21
Main → P1 → P2 → P21
Lexical Analysis
• Keeps track of line numbers and column numbers and passes them as
parameters to the other phases to enable error-reporting and handling
to the user.
Regular Expressions
• Every language has a non-empty finite set of symbols called letters. This non-empty finite set is called the
alphabet.
• Each word is a finite sequence of symbols called letters.
• The words of a language usually constitute its vocabulary. Certain sequences of symbols may not form a word
in the vocabulary. A vocabulary for a natural language is defined by a dictionary, whereas for a programming
language it is usually defined by formation rules.
• A phrase, clause or sentence is a finite sequence of words drawn from the vocabulary.
• Every natural language or programming language is a finite or infinite set of sentences.
• In the case of formal languages, the formal language is the set of words that can be formed using the formation
rules. The language is also said to be generated by the formation rules.
Natural languages. These are the usual languages such as English, Hindi, French, Tamil which we employ for
daily communication and in teaching, reading and writing.
Programming languages. These are the languages such as C, Java, SML, Perl, Python etc. that are used to write
computer programs in.
Formal languages. These are languages which are generated by certain formation rules.
JJ J I II G O B ACK F ULL S CREEN C LOSE 82 OF 600
PL April 2, 2020
Meta-languages. These are usually natural languages used to explain concepts related to programming languages
or formal languages. We are using English as the meta-language to describe and explain concepts in program-
ming languages and formal languages.
In addition, we do have the concept of a dialect of a natural language or a programming language. For example
the natural languages like Hindi, English and French do have several dialects. A dialect (in the case of natural
languages) is a particular form of a language which is peculiar to a specific region or social group. Creole (spoken in
Mauritius) is a dialect of French, Similarly Brij, Awadhi are dialects of Hindi. A dialect (in the case of programming
languages) is a version of the programming language. There are many dialects of C and C++. Similarly SML-NJ
and poly-ML are dialects of Standard ML. The notion of a dialect does not really exist for formal languages.
Closer home to what we are discussing, the language of regular expressions is a formal language which describes
the rules for forming the words of a programming language. Each regular expression represents a finite or infinite
set of words in the vocabulary of a programming language. We may think of the language of regular expressions
also as a functional programming language for describing the vocabulary of a programming language. It allows us
to generate words belonging to the vocabulary of a programming language
Any formally defined language also defines an algebraic system of operators applied on a carrier set. Every
operator in any algebraic system has a pre-defined arity which refers to the number of operands it requires. In the
case of regular expressions, the operators are concatenation and alternation are 2-ary operators (binary operators),
whereas the Kleene closure and plus closure are 1-ary operators (unary). In addition the letters of the alphabet,
which are constants may be considered to be operators of arity 0.
x0 = ””
x1 = x
x2 = x.x
..
xn+1 = x.xn = xn.x
..
Then
x∗ = {xn | n ≥ 0}
Then x∗ is the language consisting of all strings that are finite repetitions
of the string Selassie
X0 = ””
X1 = X
X2 = X.X
..
X n+1 = X.X n = X n.X
..
Then
X∗ = Xn
[
n≥0
r∗ = |r.r∗ (1)
r∗ = (r∗)∗ (2)
All expressions may be made unambiguous by specifying them in a fully parenthesised fashion. However, that
leads to too many parentheses and is often hard to read. Usually rules for precedence of operators is defined
and we may use the parentheses “(“ and “)” to group expressions over-riding the precedence conventions of the
language.
For the operators of regular expressions we will use the precedence convention that | has a lower precedence than
. and that all unary operators have the highest precedence.
Example 5.4 The language of arithmetic expressions over numbers uses the “BDMAS” convention that brackets
have the highest precedence, followed by division and multiplication and the operations of addition and subtraction
have the lowest precedence.
Example 5.5 The regular expression r.s|t.u is ambiguous because we do not know beforehand whether it rep-
resents (r.s)|(t.u) or r.(s|t).u or even various other possibilities. By specifying that the operator | has lower
precedence than . we are disambiguating the expression to mean (r.s)|(t.u).
Example 5.6 The language of arithmetic expressions can also be extended to include the unary post-fix operation
JJ J I II G O B ACK F ULL S CREEN C LOSE 94 OF 600
PL April 2, 2020
in which case an expression such as −a! becomes ambiguous. It could be interpreted to mean either (−a)! or
−(a!). In the absence of a well-known convention it is best adopt parenthesisation to disambiguate the expression.
Besides the ambiguity created by multiple binary operators, there are also ambiguities created by the same operator
and in deciding in what order two or more occurrences of the same operator need to be evaluated. A classic example
is the case of subtraction in arithmetic expressions.
Example 5.7 The arithmetic expression a−b−c, in the absence of any well-defined convention could be interpreted
to mean either (a − b) − c or a − (b − c) and the two interpretations would yield different values in general. The
problem does not exist for operators such addition and multiplication on numbers, because these operators are
associative. Hence even though a + b + c may be interpreted in two different ways, both interpretations yield
identical values.
Example 5.8 Another non-associative operator in arithmetic which often leaves students confused is the exponen-
c
tiation operator. Consider the arithmetic expression ab . For a = 2, b = 3, c = 4 is this expression to be interpreted
c
as a(b ) or as (ab)c?
Exercise 5.1
Accepting state
a Typical transition
Nε Input Symbol
State a ··· ε
ε0 ∅ ∅ · · · ∅ {εf }
εf ∅ ∅···∅ ∅
r|s f N r|s
r|s 0
ε s0 Ns sf ε
ε Nr ε r*f
r*0 r0 rf Nr*
a0 a af Na
a0 a af Na
b0 b bf Nb
N a|b
a0 a af ε
ε
a|b 0 a|b f
ε b0 b bf ε
a0 a af ε
ε
ε
(a|b)*0 ε a|b 0 a|b f (a|b)*f
ε b0 b bf ε
a0 a af ε
ε
ε
(a|b)*0 ε a|b 0 a|b f (a|b)*f
ε b0 b bf ε
a
ε
(a|b)* a
a0 a af ε
ε
ε
(a|b)*0 ε a|b 0 a|b f (a|b)*f
ε b0 b bf ε
a
ε
(a|b)* ab b (a|b)* a
a0 a af ε
ε
ε
(a|b)*0 ε a|b 0 a|b f (a|b)*f
ε b0 b bf ε
a
ε
ε a a
ε ε a
a 4 ε
ε 2
ε ε
0 1 6 7
ε b
3 5 ε
a
ε
10 b b
9 8
z
j−
0−9
h,
5 6
a−
real
0−9 0−9
1
0−9
white chars
space 7 8 real
( int
14
other
~* *
* 10 * 11 )
13 9 12
error error comment
~* ~)
1. Write a regular expression to specify all numbers in binary form that are multiples of of 4.
2. Write regular expressions to specify all numbers in binary form that are not multiples of 4.
3. Each comment in the C language
• begins with the characters “//” and ends with the newline character, or
• begins with the characters “/*” and ends with “*/” and may run across several lines.
(a) Write a regular expression to recognize comments in the C language.
(b) Transform the regular expression into a NFA.
(c) Transform the NFA into a DFA.
(d) Explain why most programming languages do not allow nested comments.
(e) modified C comments. If the character sequences “//”, “/*” and “*/” are allowed to appear in ’quoted’
form as “’//’”, “’/*’” and “’*/’” respectively within a C comment, then give
i. a modified regular expression for C comments
ii. a NFA for these modified C comments
iii. a corresponding DFA for modified C comments
4. Many systems such as Windows XP and Linux recognize commands, filenames and folder names by the their
shortest unique prefix. Hence given the 3 commands chmod, chgrp and chown, their shortest unique prefixes
are respectively chm, chg and cho. A user can type the shortest unique prefix of the command and the system
will automatically complete it for him/her.
JJ J I II G O B ACK F ULL S CREEN C LOSE 141 OF 600
PL April 2, 2020
(a) Draw a DFA which recognizes all prefixes that are at least as long as the shortest unique prefix of each of
the above commands.
(b) Suppose the set of commands also includes two more commands cmp and cmpdir, state how you will
include such commands also in your DFA where one command is a prefix of another.
6.1. Grammars
Grammars
Definition 6.1 A grammar G = hN, T , P, Si consists of
• a set N of nonterminal symbols, or variables,
• a start symbol S ∈ N ,
• a set T of terminal symbols or the alphabet,
• a set P of productions or rewrite rules where each rule is of the form
α → β for α, β ∈ (N ∪ T )∗
Definition 6.2 Given a grammar G = hN, T , P, Si, any α ∈ (N ∪ T )∗ is
called a sentential form. Any x ∈ T ∗ is called a sentencea.
Note. Every sentence is also a sentential form.
a
some authors call it a word. However we will reserve the term word to denote the tokens of a programming language.
S S
S S εε
a S b a S b
Derivation tree of
ε a S b
abaabb
S S
S S a S b
a S b ε a S b
ε ε
Another
Derivation tree of
abaabb
S S
S S a S b
ε a b
a S b S
ε ε
Yet another
Derivation tree of
abaabb
Ambiguity Disambiguation
E E
E E
E + E E * E
E E
E + E E * E
* E E + E
I E I
E E
E + E E * E
* E E + E
I E I
I I C
y C z
E E
E + E E * E
* E E + E
I E I
I I C
y C z
z y 4
4
1. Two context-free grammars are considered equivalent if they generate the same language. Prove that G1 and
G01 are equivalent.
2. Palindromes. A palindrome is a string that is equal to its reverse i.e. it is the same when read backwards (e.g.
aabbaa and abaabaaba are both palindromes). Design a grammar for generating all palindromes over the
terminal symbols a and b.
3. Matching brackets.
(a) Design a context-free grammar to generate sequences of matching brackets when the set of terminals con-
sists of three pairs of brackets {(, ), [, ], {, }}.
(b) If your grammar is ambiguous give two rightmost derivations of the same string and draw the two derivation
trees. Explain how you will modify the grammar to make it unambiguous.
(c) If your grammar is not ambiguous prove that it is not ambiguous.
4. Design an unambiguous grammar for the expression language on integers consisting of expressions made up
of operators +, -, *, /, % and the bracketing symbols ( and ), assuming the usual rules of precedence among
operators that you have learned in school.
5. Modify the above grammar to include the exponentiation operator ˆ which has a higher precedence than the
other operators and is right-associative.
6. How will you modify the grammar above to include the unary minus operator - where the unary minus has a
higher precedence than other operators?
7. The language specified by a regular expression can also be generated by a context-free grammar.
JJ J I II G O B ACK F ULL S CREEN C LOSE 180 OF 600
PL April 2, 2020
(a) Design a context-free grammar to generate all floating-point numbers allowed by the C language.
(b) Design a context-free grammar to generate all numbers in binary form that are not multiples of 4.
(c) Write a regular expression to specify all numbers in binary form that are multiples of of 3.
8. Prove that the G01 is indeed unambiguous.
9. Prove that the grammar of fully parenthesized expressions is unambiguous.
10. Explain how the grammar G01 implements left associativity and precedence.
Introduction to Parsing
( a − ( a / b ) )
Principle:
Reduce
whenever possible.
Shift only when
reduce is
impossible
Shift
a − ( a / b ) )
Principle:
Reduce
whenever possible.
Shift only when
reduce is
impossible
(
Shift
− ( a / b ) )
Principle:
Reduce
whenever possible.
Shift only when
reduce is
impossible
a
(
Shift
− ( a / b ) )
Principle:
Reduce
whenever possible.
Shift only when
reduce is
impossible
a
(
Reduce
− ( a / b ) )
Principle:
Reduce
whenever possible.
Shift only when
reduce is
impossible
D
(
Reduce
− ( a / b ) )
Principle:
Reduce
whenever possible.
Shift only when
reduce is
impossible
T
(
Reduce
− ( a / b ) )
Principle:
Reduce
whenever possible.
Shift only when
reduce is
impossible
E
(
Shift
( a / b ) )
Principle:
Reduce
whenever possible.
Shift only when
reduce is
−
impossible
E
(
Shift
/ b ) )
Principle:
Reduce
whenever possible. a
Shift only when (
reduce is
−
impossible
E
(
Reduce
/ b ) )
Principle:
Reduce
whenever possible. D
Shift only when (
reduce is
−
impossible
E
(
Reduce
/ b ) )
Principle:
Reduce
whenever possible. T
Shift only when (
reduce is
−
impossible
E
(
Reduce?
/ b ) )
Principle:
Reduce
whenever possible. T
Shift only when (
reduce is
−
impossible
E
(
Reduce? Shift
b ) )
Principle:
Reduce /
whenever possible. T
Shift only when (
reduce is
−
impossible
E
(
Shift
) )
Principle: b
Reduce /
whenever possible. T
Shift only when (
reduce is
−
impossible
E
(
Reduce
) )
Principle: D
Reduce /
whenever possible. T
Shift only when (
reduce is
−
impossible
E
(
Reduce
) )
Principle: D
Reduce /
whenever possible. T
Shift only when (
reduce is
−
impossible
E
(
Reduce?
) )
Principle: D
Reduce /
whenever possible. T
Shift only when (
reduce is
−
impossible
E
(
No, REDUCE!
) )
Principle:
Reduce
whenever possible. T
Shift only when (
reduce is
−
impossible
E
(
Reduce?
) )
Principle:
Reduce
whenever possible. E
Shift only when (
reduce is
−
impossible
E
(
Shift
Principle:
)
Reduce
whenever possible. E
Shift only when (
reduce is
−
impossible
E
(
Reduce
Principle:
Reduce
whenever possible.
Shift only when D
reduce is
−
impossible
E
(
Reduce
Principle:
Reduce
whenever possible.
Shift only when T
reduce is
−
impossible
E
(
Reduce?
Principle:
Reduce
whenever possible.
Shift only when T
reduce is
−
impossible
E
(
No, REDUCE!
Principle:
Reduce
whenever possible.
Shift only when
reduce is
impossible
E
(
Shift
Principle:
Reduce
whenever possible.
Shift only when
reduce is
impossible )
E
(
Reduce
Principle:
Reduce
whenever possible.
Shift only when
reduce is
impossible
D Reduce
Principle:
Reduce
whenever possible.
Shift only when
reduce is
impossible
T Reduce
Principle:
Reduce
whenever possible.
Shift only when
reduce is
impossible
E Reduce
r1. E E − T
r2 E T
r3 T T / D
r4 T D
r5 D a | b | ( E )
a − a / b
r1. E E − T
r2 E T
r3 T T / D
r4 T D
r5 D a | b | ( E )
− a / b
Principle:
Reduce
whenever possible.
Shift only when
reduce is
impossible
a
Shift
r1. E E − T
r2 E T
r3 T T / D
r4 T D
r5 D a | b | ( E )
− a / b
D Reduce by r5
r1. E E − T
r2 E T
r3 T T / D
r4 T D
r5 D a | b | ( E )
− a / b
T Reduce by r4
r1. E E − T
r2 E T
r3 T T / D
r4 T D
r5 D a | b | ( E )
− a / b
E Reduce by r2
r1. E E − T
r2 E T
r3 T T / D
r4 T D
r5 D a | b | ( E )
a / b
− Shift
E
r1. E E − T
r2 E T
r3 T T / D
r4 T D
r5 D a | b | ( E )
/ b
a
Shift
−
E
r1. E E − T
r2 E T
r3 T T / D
r4 T D
r5 D a | b | ( E )
/ b
D Reduce by r5
−
E
r1. E E − T
r2 E T
r3 T T / D
r4 T D
r5 D a | b | ( E )
/ b
T Reduce by r4
−
E
r1. E E − T
r2 E T
r3 T T / D
r4 T D
r5 D a | b | ( E )
/ b
T Reduce by r4
−
r1. E E − T
r2 E T
r3 T T / D
r4 T D
r5 D a | b | ( E )
/ b
E Reduce by r1
r1. E E − T
r2 E T
r3 T T / D
r4 T D
r5 D a | b | ( E )
/
Shift
E
r1. E E − T
r2 E T
r3 T T / D
r4 T D
r5 D a | b | ( E )
b Shift
/
r1. E E − T
r2 E T
r3 T T / D
r4 T D
r5 D a | b | ( E )
D Reduce by r5
/
r1. E E − T
r2 E T
r3 T T / D
r4 T D
r5 D a | b | ( E )
T
Reduce by r4
/
r1. E E − T
r2 E T
r3 T T / D
r4 T D
r5 D a | b | ( E )
k !
uc
St
E Reduce by r2
/
Get back!
E
r1. E E − T
r2 E T
r3 T T / D
r4 T D
r5 D a | b | ( E )
E Reduce by r2
/
Get back!
E
r1. E E − T
r2 E T
r3 T T / D
r4 T D
r5 D a | b | ( E )
T
Reduce by r4
Get back! /
r1. E E − T
r2 E T
r3 T T / D
r4 T D
r5 D a | b | ( E )
D Reduce by r5
Get back! /
r1. E E − T
r2 E T
r3 T T / D
r4 T D
r5 D a | b | ( E )
r1. E E − T
r2 E T
r3 T T / D
r4 T D
r5 D a | b | ( E )
/
Get back! Shift
E
r1. E E − T
r2 E T
r3 T T / D
r4 T D
r5 D a | b | ( E )
/ b
Get back to
where you
once belonged!
E Reduce by r1
o d ified
r1. E E − T
m Principle:
r2 E T Reduce whenever possible, but
r3 T T / D but depending upon
r4 T D
lookahead
r5 D a | b | ( E )
/ b
Shift instead
of reduce here!
r e duce
−
Shift flict
con
T Reduce by r4
−
r1. E E − T
r2 E T
r3 T T / D
r4 T D
r5 D a | b | ( E )
/ b
T Reduce by r4
−
E
r1. E E − T
r2 E T
r3 T T / D
r4 T D
r5 D a | b | ( E )
/ Shift
T
−
E
r1. E E − T
r2 E T
r3 T T / D
r4 T D
r5 D a | b | ( E )
b
Shift
/
T
−
E
r1. E E − T
r2 E T
r3 T T / D
r4 T D
r5 D a | b | ( E )
D Reduce by r5
/
T
−
E
r1. E E − T
r2 E T
r3 T T / D
r4 T D
r5 D a | b | ( E )
T Reduce by r3
−
r1. E E − T
r2 E T
r3 T T / D
r4 T D
r5 D a | b | ( E )
E Reduce by r1
Bottom-Up Parsing
r1. E E − T
r4 T D
r2 E T
r3 T T / D r5 D a | b | ( E )
a − a / b
r1. E E − T
r4 T D
r2 E T
r3 T T / D r5 D a | b | ( E )
a − a / b
r1. E E − T
r4 T D
r2 E T
r3 T T / D r5 D a | b | ( E )
a − a / b
r1. E E − T
r4 T D
r2 E T
r3 T T / D r5 D a | b | ( E )
a − a / b
r1. E E − T
r4 T D
r2 E T
r3 T T / D r5 D a | b | ( E )
a − a / b
r1. E E − T
r4 T D
r2 E T
r3 T T / D r5 D a | b | ( E )
a − a / b
r1. E E − T
r4 T D
r2 E T
r3 T T / D r5 D a | b | ( E )
D D
a − a / b
r1. E E − T
r4 T D
r2 E T
r3 T T / D r5 D a | b | ( E )
T T
D D
a − a / b
r1. E E − T
r4 T D
r2 E T
r3 T T / D r5 D a | b | ( E )
T T
D D
a − a / b
r1. E E − T
r4 T D
r2 E T
r3 T T / D r5 D a | b | ( E )
T T
D D
a − a / b
r1. E E − T
r4 T D
r2 E T
r3 T T / D r5 D a | b | ( E )
T T
D D D
a − a / b
r1. E E − T
r4 T D
r2 E T
r3 T T / D r5 D a | b | ( E )
E
T
T T
D D D
a − a / b
r1. E E − T
r4 T D
r2 E T
r3 T T / D r5 D a | b | ( E )
E
T
T T
D D D
a − a / b
Parsing Problems 1
The main question in shift-reduce parsing is:
How much of the input token stream would the parser require?
Disregarding the very next input token as always available, the length of
the extra amount of input required for a shift-reduce decision is called
the lookahead.
S → E$
S1 $ S6
S3
E S E$
S E$ S E $
E E T E E T
E T S5
T a T E E T
T ( E)
S7
−−
E T T
( a
T S4
S8 a
a E E T
S2 T a T a
( T (E)
T ( E)
E E T
S9
E T −−
T a T (E ) S10
E )
T ( E) E E T T (E)
S1 a$ Shift S8
S1 a$ Shift S8
S1 a S8 $ Reduce Rule 4
S1 a$ Shift S8
S1 a S8 $ Reduce Rule 4
S1 T $ Goto S7
S1 a$ Shift S8
S1 a S8 $ Reduce Rule 4
S1 T $ Goto S7
S1 T S7 $ Reduce Rule 3
S1 a$ Shift S8
S1 a S8 $ Reduce Rule 4
S1 T $ Goto S7
S1 T S7 $ Reduce Rule 3
S1 E $ Goto S3
S1 a$ Shift S8
S1 a S8 $ Reduce Rule 4
S1 T $ Goto S7
S1 T S7 $ Reduce Rule 3
S1 E $ Goto S3
S1 E S3 $ Accept
S1 a − (a − a)$ Shift S8
S1 a − (a − a)$ Shift S8
S1 a − (a − a)$ Shift S8
S1 T −(a − a)$ Go to S7
S1 a − (a − a)$ Shift S8
S1 T −(a − a)$ Go to S7
S1 a − (a − a)$ Shift S8
S1 T −(a − a)$ Go to S7
S1 E −(a − a)$ Go to S3
S1 a − (a − a)$ Shift S8
S1 T −(a − a)$ Go to S7
S1 E −(a − a)$ Go to S3
S1 a − (a − a)$ Shift S8
S1 T −(a − a)$ Go to S7
S1 E −(a − a)$ Go to S3
S1 E S3 − S4 (a − a)$ Shift S2
S1 a − (a − a)$ Shift S8
S1 T −(a − a)$ Go to S7
S1 E −(a − a)$ Go to S3
S1 E S3 − S4 (a − a)$ Shift S2
S1 E S3 − S4 ( S2 a − a)$ Shift S8
S1 a − (a − a)$ Shift S8
S1 T −(a − a)$ Go to S7
S1 E −(a − a)$ Go to S3
S1 E S3 − S4 (a − a)$ Shift S2
S1 E S3 − S4 ( S2 a − a)$ Shift S8
S1 a − (a − a)$ Shift S8
S1 T −(a − a)$ Go to S7
S1 E −(a − a)$ Go to S3
S1 E S3 − S4 (a − a)$ Shift S2
S1 E S3 − S4 ( S2 a − a)$ Shift S8
S1 E S3 − S4 ( S2 T −a)$ Go to S7
S1 a − (a − a)$ Shift S8
S1 T −(a − a)$ Go to S7
S1 E −(a − a)$ Go to S3
S1 E S3 − S4 (a − a)$ Shift S2
S1 E S3 − S4 ( S2 a − a)$ Shift S8
S1 E S3 − S4 ( S2 T −a)$ Go to S7
S1 a − (a − a)$ Shift S8
S1 T −(a − a)$ Go to S7
S1 E −(a − a)$ Go to S3
S1 E S3 − S4 (a − a)$ Shift S2
S1 E S3 − S4 ( S2 a − a)$ Shift S8
S1 E S3 − S4 ( S2 T −a)$ Go to S7
S1 E S3 − S4 ( S2 E −a)$ Go to S9
JJ J I II G O B ACK F ULL S CREEN C LOSE 293 OF 600
PL April 2, 2020
DFA Parsing Table
S1 E S3 − S4 ( S2 E S9 −a)$ Shift S4
S1 E S3 − S4 ( S2 E S9 −a)$ Shift S4
S1 E S3 − S4 ( S2 E S9 − S4 a)$ Shift S8
S1 E S3 − S4 ( S2 E S9 −a)$ Shift S4
S1 E S3 − S4 ( S2 E S9 − S4 a)$ Shift S8
S1 E S3 − S4 ( S2 E S9 − S4 a S8 )$ Reduce Rule 4
S1 E S3 − S4 ( S2 E S9 −a)$ Shift S4
S1 E S3 − S4 ( S2 E S9 − S4 a)$ Shift S8
S1 E S3 − S4 ( S2 E S9 − S4 a S8 )$ Reduce Rule 4
S1 E S3 − S4 ( S2 E S9 − S4 T )$ Go to S5
S1 E S3 − S4 ( S2 E S9 −a)$ Shift S4
S1 E S3 − S4 ( S2 E S9 − S4 a)$ Shift S8
S1 E S3 − S4 ( S2 E S9 − S4 a S8 )$ Reduce Rule 4
S1 E S3 − S4 ( S2 E S9 − S4 T )$ Go to S5
S1 E S3 − S4 ( S2 E S9 − S4 T S5 )$ Reduce Rule 2
S1 E S3 − S4 ( S2 E S9 −a)$ Shift S4
S1 E S3 − S4 ( S2 E S9 − S4 a)$ Shift S8
S1 E S3 − S4 ( S2 E S9 − S4 a S8 )$ Reduce Rule 4
S1 E S3 − S4 ( S2 E S9 − S4 T )$ Go to S5
S1 E S3 − S4 ( S2 E S9 − S4 T S5 )$ Reduce Rule 2
S1 E S3 − S4 ( S2 E )$ Go to S9
S1 E S3 − S4 ( S2 E S9 −a)$ Shift S4
S1 E S3 − S4 ( S2 E S9 − S4 a)$ Shift S8
S1 E S3 − S4 ( S2 E S9 − S4 a S8 )$ Reduce Rule 4
S1 E S3 − S4 ( S2 E S9 − S4 T )$ Go to S5
S1 E S3 − S4 ( S2 E S9 − S4 T S5 )$ Reduce Rule 2
S1 E S3 − S4 ( S2 E )$ Go to S9
S1 E S3 − S4 ( S2 E S9 )$ Shift S10
S1 E S3 − S4 ( S2 E S9 −a)$ Shift S4
S1 E S3 − S4 ( S2 E S9 − S4 a)$ Shift S8
S1 E S3 − S4 ( S2 E S9 − S4 a S8 )$ Reduce Rule 4
S1 E S3 − S4 ( S2 E S9 − S4 T )$ Go to S5
S1 E S3 − S4 ( S2 E S9 − S4 T S5 )$ Reduce Rule 2
S1 E S3 − S4 ( S2 E )$ Go to S9
S1 E S3 − S4 ( S2 E S9 )$ Shift S10
S1 E S3 − S4 ( S2 E S9 −a)$ Shift S4
S1 E S3 − S4 ( S2 E S9 − S4 a)$ Shift S8
S1 E S3 − S4 ( S2 E S9 − S4 a S8 )$ Reduce Rule 4
S1 E S3 − S4 ( S2 E S9 − S4 T )$ Go to S5
S1 E S3 − S4 ( S2 E S9 − S4 T S5 )$ Reduce Rule 2
S1 E S3 − S4 ( S2 E )$ Go to S9
S1 E S3 − S4 ( S2 E S9 )$ Shift S10
S1 E S3 − S4 T $ Go to S5
S1 E S3 − S4 ( S2 E S9 −a)$ Shift S4
S1 E S3 − S4 ( S2 E S9 − S4 a)$ Shift S8
S1 E S3 − S4 ( S2 E S9 − S4 a S8 )$ Reduce Rule 4
S1 E S3 − S4 ( S2 E S9 − S4 T )$ Go to S5
S1 E S3 − S4 ( S2 E S9 − S4 T S5 )$ Reduce Rule 2
S1 E S3 − S4 ( S2 E )$ Go to S9
S1 E S3 − S4 ( S2 E S9 )$ Shift S10
S1 E S3 − S4 T $ Go to S5
S1 E S3 − S4 T S5 $ Reduce Rule 2
S1 E S3 − S4 ( S2 E S9 −a)$ Shift S4
S1 E S3 − S4 ( S2 E S9 − S4 a)$ Shift S8
S1 E S3 − S4 ( S2 E S9 − S4 a S8 )$ Reduce Rule 4
S1 E S3 − S4 ( S2 E S9 − S4 T )$ Go to S5
S1 E S3 − S4 ( S2 E S9 − S4 T S5 )$ Reduce Rule 2
S1 E S3 − S4 ( S2 E )$ Go to S9
S1 E S3 − S4 ( S2 E S9 )$ Shift S10
S1 E S3 − S4 T $ Go to S5
S1 E S3 − S4 T S5 $ Reduce Rule 2
S1 E $ Go to S3
S1 E S3 − S4 ( S2 E S9 −a)$ Shift S4
S1 E S3 − S4 ( S2 E S9 − S4 a)$ Shift S8
S1 E S3 − S4 ( S2 E S9 − S4 a S8 )$ Reduce Rule 4
S1 E S3 − S4 ( S2 E S9 − S4 T )$ Go to S5
S1 E S3 − S4 ( S2 E S9 − S4 T S5 )$ Reduce Rule 2
S1 E S3 − S4 ( S2 E )$ Go to S9
S1 E S3 − S4 ( S2 E S9 )$ Shift S10
S1 E S3 − S4 T $ Go to S5
S1 E S3 − S4 T S5 $ Reduce Rule 2
S1 E $ Go to S3
S1 E S3 $ Accept
1. Design a LR(0) parser for the grammar of palindromes. Identify whether there are any conflicts in the parsing
table.
2. Design a LR(0) parser for the grammar of Matching brackets and identify any conflicts.
3. Design a context-free grammar for a language on the terminal symbols a and b such that every string has more
as than bs. Design a LR(0) parser for this grammar and find all the conflicts, if any.
4. Since every regular expression may also be represented by a context-free grammar design an LR(0) parser for
comments in C.
The EBNF specification of a programming language is a collection of rules that defines the (context-free) grammar
of the language. It specifies the formation rules for the correct grammatical construction of the phrases of the
language.
Start symbol. The rules are written usually in a “top-down fashion” and the very first rule gives the productions of
the start symbol of the grammar.
Non-terminals. Uses English words or phrases to denote non-terminal symbols. These words or phrases are sug-
gestive of the nature or meaning of the constructs.
Metasymbols.
• Sequences of constructs enclosed in “{” and “}” denote zero or more occurrences of the construct (c.f.
Kleene closure on regular expressions).
• Sequences of constructs enclosed in “[” and “]” denote that the enclosed constructs are optional i.e. there
can be only zero or one occurrence of the sequence.
• Constructs are enclosed in “(” and “)” to group them together.
• “ | ” separates alternatives.
• “ ::= ” defines the productions of each non-terminal symbol.
• “ .” terminates the possibly many rewrite rules for a non-terminal.
Terminals. Terminal symbol strings are usually enclosed in double-quotes when written in monochrome (we shall
additionally colour-code them).
All words written in bold font are reserved words and cannot be used as identifiers in any program.
P rogram ::= “program” Identif ier “::”Block .
Block ::= DeclarationSeq CommandSeq .
DeclarationSeq ::= {Declaration} .
Declaration ::= “var” V ariableList“:”T ype“;” .
T ype ::= “int” | “bool” .
V ariableList ::= V ariable{“,” V ariable} .
CommandSeq ::= “{”{Command“;”}“}” .
Command ::= V ariable“:=”Expression |
“read” V ariable |
“write” IntExpression |
“if ” BoolExpression
“then”CommandSeq
“else” CommandSeq
“endif ” |
“while” BoolExpression “do”
CommandSeq
“endwh” .
Syntax of Standard ML
Tobias Nipkow and Larry Paulson
TopLevelDeclaration
Program
;
TopLevelDeclaration
Expression
ObjectDeclaration
SignatureDeclaration
FunctorDeclaration
ObjectDeclaration
Declaration
Ident
Signature
Structure
structure :
=
and
ObjectDeclaration
ObjectDeclaration
local in end
;
1
JJ J I II G O B ACK F ULL S CREEN C LOSE 328 OF 600
PL April 2, 2020
Syntax Diagrams of SML: 2
Ident
Signature
SignatureDeclaration
signature
=
and
;
FunctorBinding
FunctorDeclaration
functor
and
;
FunctorBinding
FunctorArguments Signature Structure
Ident
(
) :
=
Ident
Signature
FunctorArguments
Specication
:
ObjectDeclaration
Structure
CompoundIdent
struct end
Ident
Structure
ObjectDeclaration
( )
ObjectDeclaration
Structure
let in
end
Specication
Signature
Ident
sig end
2
JJ J I II G O B ACK F ULL S CREEN C LOSE 329 OF 600
PL April 2, 2020
Syntax Diagrams of SML: 3
Ident
Type
Specication
val :
TypeVarList Ident
and
type
DatatypeBinding
eqtype and
datatype
Ident
Type
and
exception of
Ident
Signature
and
structure
:
CompoundIdent
and
sharing
type
=
CompoundIdent
local in end
open
Ident
include
;
3
JJ J I II G O B ACK F ULL S CREEN C LOSE 330 OF 600
PL April 2, 2020
DECLARATIONS
Pattern
Declaration
Expression
val =
rec
FunHeading Expression
and
fun
Type =
:
|
TypeBinding
and
DatatypeBinding
type
TypeBinding
datatype
DatatypeBinding
withtype
TypeBinding
abstype
withtype
Declaration
Name
with end
Type
CompoundName
exception of
Declaration
Declaration
and
local in end
CompoundIdent
open
Ident
Digit
infix
infixr
nonfix
JJ J ;
I II
PL April 2, 2020
G O B ACK F ULL S CREEN C LOSE 331 OF 600
(
AtomicPattern InxOperator AtomicPattern
)
AtomicPattern
AtomicPattern InxOperator AtomicPattern
TypeVarList Ident
Type
TypeBinding
=
and
= of
|
and
TypeVarList
TypeVar
TypeVar
( )
,
andalso
Expression Match
orelse
Expression
handle
Expression Expression
raise
Expression
Expression
if then else
while
Expression
Match
Expression
do
Match
case of
fn
InxExpression
AtomicExpression
InxExpression InxOperator InxExpression
,
Expression
[ ]
Label
f
Expression
g
=
,
Label
Expression
#
( )
Declaration
;
let Expression
in end
;
|
Pattern
AtomicPattern
CompoundName AtomicPattern
Pattern InxOperator Pattern
Pattern
Type
Name Pattern
:
JJ J I
as II
PL April 2, 2020
G O B ACK F ULL S CREEN C LOSE 334 OF 600
CompoundName
Constant
( Pattern
)
,
[ Pattern ]
,
FieldPattern
f
g
FieldPattern
Label Pattern
...
=
Ident
Type
Pattern
: as
FieldPattern
,
*
Type
Type
Label
Type
->
f
: g
,
Type
( )
LEXICAL MATTERS: IDENTIFIERS, CONSTANTS, COMMENTS
CompoundIdent
Ident
.
CompoundName
CompoundIdent
InxOperator
op
Name
Ident
InxOperator
op
StringEscape
\ "
\
StringEscape
n
one of
t
Digit Digit Digit
^ @ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_
"
Space
\
\
Tab
Newline
Formfeed
Numeral
Digit
~
AlphanumericIdent
TypeVar
'
JJ J _
I II G O B ACK F ULL S CREEN C LOSE 337 OF 600
PL April 2, 2020
one of
!%&$#+-/:<=>?@\~`^|*
Label
Ident
Digit
AlphanumericIdent
Letter
Letter
Digit
_
'
Digit
one of
0123456789
Letter
one of ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz
any text that does not include
Comment
as a substring
(* (* or *)
*)
Comment
1. Translate all the context-free grammars that we have so far seen into EBNF specifications.
2. Specify the language of regular expressions over a non-empty finite alphabet A in EBNF.
3. Given a textual EBNF specification write an algorithm to render each non-terminal as a syntax diagram.
The parser for a context-free grammar transforms the token stream into a derivation tree (which we also call a
concrete parse tree)1. What we actually require in order to perform a computation is really an abstract syntax tree.
Example 7.1 consider the two sentences a − a/b and a − (a/b) which are both valid sentences generated by the
grammar of our favourite example.
r1. E E − T r1. E E − T
r4 T D r4 T D
r2 E T r2 E T
r3 T T / D r5 D a | b | ( E) r3 T T / D r5 D a | b | ( E)
a − a / b a − ( a / b )
E E
T
D
E E
T
E
T T T
T
T
D D D D D
D
a − a / b a − ( a / b )
Both expressions in example eg:concrete-parse-trees have the same meaning (semantics). But the sentences are
syntactically different and correspondingly have different parse trees. Actually both the expressions may be repre-
sented by the following abstract syntax tree (AST) which gives the hierarchical structure of the expression.
a /
a b
Syntax-directed Translation
E → T E.val := T.val
T → F T.val := F.val
F → n F.val := n.val
Note: The attribute n.val is the value of the numeral n computed during
scanning (lexical analysis).
JJ J I II G O B ACK F ULL S CREEN C LOSE 357 OF 600
PL April 2, 2020
Attributes: Basic Assumptions
• Terminal symbols are assumed to have only synthesized attributes.
Their attributes are all supplied by the lexical analyser during scan-
ning.
• The start symbol of the grammar can have only synthesized attributes.
• In the case of LR parsing with its special start symbol, the start symbol
cannot have any inherited attributes because
1. it does not have any parent nodes in the parse tree and
2. it does not occur on the right-hand side of any production.
Synthesized Attributes
Evaluating the expression (4 − 1)/2 generated by the grammar for sub-
traction and division
E T F E
/ ( ) n
T
−
T F
/
n
F
( )
E
E T
−
T
F
F
n
E T F E
/ ( ) n
T
−
T F
/
n
F
( )
E
E T
− Synthesized Attributes
4 3 2 1
T
F
F
n
4 n
E T F E
/ ( ) n
T
−
T F
/
n
F
( )
E
E T
− Synthesized Attributes
4 3 2 1
T
F
4 F
n
4 n
E T F E
/ ( ) n
T
−
T F
/
n
F
( )
E
E T
− Synthesized Attributes
4 3 2 1
4 T
F
4 F
n
4 n
E T F E
/ ( ) n
T
−
T F
/
n
F
( )
E
4 E T
− Synthesized Attributes
4 3 2 1
4 T
F
4 F
n
4 n
E T F E
/ ( ) n
T
−
T F
/
n
F
( )
E
4 E T
− Synthesized Attributes
4 3 2 1
4 T
F
4 F
n 1
4 n
E T F E
/ ( ) n
T
−
T F
/
n
F
( )
E
4 E T
− Synthesized Attributes
4 3 2 1
4 T
F 1
4 F
n 1
4 n
E T F E
/ ( ) n
T
−
T F
/
n
F
( )
E
4 E T 1
− Synthesized Attributes
4 3 2 1
4 T
F 1
4 F
n 1
4 n
E T F E
/ ( ) n
T
−
T F
/
n
F
( )
3
E
4 E T 1
− Synthesized Attributes
4 3 2 1
4 T
F 1
4 F
n 1
4 n
E T F E
/ ( ) n
T
−
T F
/
n
3
F
( )
3
E
4 E T 1
− Synthesized Attributes
4 3 2 1
4 T
F 1
4 F
n 1
4 n
E T F E
/ ( ) n
T
−
T 3 F
/
n
3
F
( )
3
E
4 E T 1
− Synthesized Attributes
4 3 2 1
4 T
F 1
4 F
n 1
4 n
E T F E
/ ( ) n
T
−
T 3 F
/
n 2
3
F
( )
3
E
4 E T 1
− Synthesized Attributes
4 3 2 1
4 T
F 1
4 F
n 1
4 n
E T F E
/ ( ) n
T
−
T 3 F 2
/
n 2
3
F
( )
3
E
4 E T 1
− Synthesized Attributes
4 3 2 1
4 T
F 1
4 F
n 1
4 n
E T F E
/ ( ) n
T 1
−
T 3 F 2
/
n 2
3
F
( )
3
E
4 E T 1
− Synthesized Attributes
4 3 2 1
4 T
F 1
4 F
n 1
4 n
E T F E 1
/ ( ) n
T 1
−
T 3 F 2
/
n 2
3
F
( )
3
E
4 E T 1
− Synthesized Attributes
4 3 2 1
4 T
F 1
4 F
n 1
4 n
T 3
/
F 2
E → T E.val := T.val
n 2
3
F
(
E
3
)
T0 → T1/F T0.val := div(T1.val, F.val)
4 E T 1
−
4 T
F 1
T → F T.val := F.val
4 F
n 1
4 n
F → (E) F.val := E.val
F → n F.val := n.val
Inherited Attributes: 0
C-style declarations generating int x, y, z.
D → T L T → int | float
L → L,I | I I → x | y | z
D
T L
I
,
int L
z
I
L ,
I
D L T I
x y z int x
T L
I
,
int int L
z
I
L ,
I
D L T I
x y z int x
, int
int T L
I
,
int int L
z
I
L ,
I
D L T I
x y z int x
, int int
int T L int
I
,
int int L
z
I
L ,
I
D L T I
x y z int x
, int int
int T L int
I int
,
int int L int
z
I
L ,
I
D L T I
x y z int x
, int int
int T L int
I int
,
int int L int
z int
I int
L int ,
I
D L T I
x y z int x
, int int
int T L int
I int
,
int int L int
z int
I int
L int ,
y int
I int
D L T I
x y z int x
, int int
int T L int
I int
,
int int L int
z int
I int
L int ,
y int
I int
D L T I
x y z int x int
, int int
int T L int
D → TL L.in := T.type
I int
y int
T → float T.type := float.f loat
I int
x int
L0 → L1,I L1 := L0.in
L → I I.in := L.in
I → id id.type := I.in
In the first semantic rule the symbol L.in is inherited from a symbol to its
right viz. T.type and hence is not L-attributed.
−− 2
4 1
−− 2
4 1
−− 2
4 1
3 2
3 2
But what we actually get during parsing is a tree that looks like . . .
T F
/
n
F
( )
E
/
E T
−
−− n
T
F
n n
F
n
E → T E.ptr := T.ptr
T → F T.ptr := F.ptr
E → T E.ast := T.ast
T → F T.ast := F.ast
Symbol Table
“The name of the song is called ‘Haddock's Eyes’.”
“Oh, that's the name of the song, is it?” Alice said, trying to feel interested.
“No, you don't understand,” the Knight said, looking a little vexed. “That's what the name is called. The
name of the song really is, ‘The Aged Aged Man’.”
Then I ought to have said ‘That's what the song is called’?” Alice corrected herself.
“No you oughtn't: that's quite another thing! The song is called ‘Ways and Means’: but that's only what
it's called, you know!”
“Well, what is the song, then?” said Alice, who was by this time completely bewildered.
“I was coming to that”, the Knight said. “The song really is ‘A-Sitting On a Gate’: and the tune's my own
invention.
Intermediate Representation
x y x y
*x *y x := ^y @y *y
x y x y
*x @z *z @z
x := *y
*z *z
z z
y y
*y *y
x x
*x := y @z
@z z
z
*z *y
JJ J I II G O B ACK F ULL S CREEN C LOSE 433 OF 600
PL April 2, 2020
IR: Generation Basics
• Can be generated by recursive traversal of the abstract syntax tree.
• Can be generated by syntax-directed translation as follows:
For every non-terminal symbol N in the grammar of the source lan-
guage there exist two attributes
N.place , which denotes the address of a temporary variable where the
result of the execution of the generated code is stored
N.code , which is the actual code segment generated.
• In addition a global counter for the instructions generated is main-
tained as part of the generation process.
• It is independent of the source language but can express target ma-
chine operations without committing to too much detail.
E.place := id.place;
E.code := emit()
E0 → E1 − E 2
E0.place := newtemp;
E0.code := E1.code;
E2.code;
emit(E0.place := E1.place − E2.place)
S.code := E.code
emit(id.place:=E.place)
S0 → S1; S2
S0.begin := S1.begin;
S0.af ter := S2.af ter;
S0.code := emit(S0.begin:)
S1.code
S2.code
emit(S0.af ter:)
S0.begin := newlabel;
S0.af ter := S2.af ter;
S0.code := emit(S0.begin:)
E.code;
emit(if E.place= 0 goto S2.begin);
S1.code;
emit(goto S0.af ter);
S2.code;
emit(S0.af ter:)
S0.begin := newlabel;
S0.af ter := newlabel;
S0.code := emit(S0.begin:)
E.code
emit(if E.place= 0 goto S0.af ter);
S1.code;
emit(goto S0.begin);
emit(S0.af ter:)
L, M , N ::= x Variable
λx[L] Abstraction
(L M ) Application
where x ∈ V .
• A Variable denotes a possible binding in the external environment.
• An Abstraction denotes a function which takes a formal parameter.
• An Application denotes the application of a function to an actual pa-
rameter.
JJ J I II G O B ACK F ULL S CREEN C LOSE 445 OF 600
PL April 2, 2020
Free and Bound Variables
Definition 11.1 For any term N the set of free variables and the set of all
variables are defined by induction on the structure of terms.
N F V (N ) V ar(N )
x {x} {x}
λx[L] F V (L) − {x} V ar(L) ∪ {x}
(L M ) F V (L) ∪ F V (M ) V ar(L) ∪ V ar(M )
• The set of bound variables BV (N ) = V ar(N ) − F V (N ).
• The same variable name may be used with different bindings in a sin-
gle term (e.g. (λx[x] λx[(x y)]))
• The brackets “[” and “]” delimit the scope of the bound variable x in the
term λx[L].
• Λ0 ⊆ Λ is the set of closed λ-terms (i.e. terms with no free variables).
L ρc M L ρc M
ρAppL ρAppR
(L N ) ρc (M N ) (N L) ρc (N M )
Lemma 11.5
1. ρc ⊇ ρ.
2. The compatible closure of any relation is compatible.
3. If ρ is compatible then ρc = ρ.
Example 11.6
1. ≡α is a compatible relation
2. →1β is by definition a compatible relation.
JJ J I II G O B ACK F ULL S CREEN C LOSE 450 OF 600
PL April 2, 2020
α -equivalence
Definition 11.7 (α-equivalence) ≡α⊆ Λ × Λ is the compatible closure of
the relation {(λx[L] ≡α λy[{y/x}L]) | y 6∈ F V (L)}.
• Here again if y ∈ F V (L) it must not be captured by a change of bound
variables.
L →1β M L →1β M
β1AppL β1AppR
(L N ) →1β (M N ) (N L) →1β (N M )
• →1β is the compatible closure of basic β-reduction to all contexts.
• We will often omit the superscript 1 as understood.
JJ J I
((K ω) Ω) →1β ((K ω) Ω) →1β · · ·
II
PL April 2, 2020
G O B ACK F ULL S CREEN C LOSE 456 OF 600
Examples of Strong Normalization
Example 11.13
1. ((K ω) ω) is strongly normalising because it reduces to the normal form
ω in two β-reduction steps.
2. Consider the term ((S K) K). Its reduction sequences go as follows:
((S K) K) →1β λz[((K z) (K z))] →1β λz[z] ≡ I
Notions of Reduction
z }| {
=β (L (YC L))
Hence (YC L) =β (L (YC L)). However (L (YC L)) will never β-reduce to
(YC L).
JJ J I II G O B ACK F ULL S CREEN C LOSE 463 OF 600
PL April 2, 2020
Recursion and the Y combinator.
Since the lambda calculus only has variables and expressions and there is no place for names themselves (we use
names such as K and S for our convenience in discourse, but the language itself allows only (untyped) variables
and is meant to define functions anonymously as expressions in the language). In such a situation, recursion poses
a problem in the language.
Recursion in most programming languages requires the use of an identifier which names an expression that contains
a call to the very name of the function that it is supposed to define. This is at variance with the aim of the lambda
calculus wherein the only names belong to variables and even functions may be defined anonymously as mere
expressions.
This notion of recursive definitions may be generalised to a system of mutually recursive definitions.
The name of a recursive function, acts as a place holder in the body of the definition (which in turn has the name
acting as a place holder for a copy of the body of the definition and so on ad infinitum). However no language can
have sentences of infinite length.
The combinator YC helps in providing copies of any lambda term L whenever demanded in a more disciplined
fashion. This helps in the modelling of recursive definitions anonymously. What the YC combinator provides is
mechanism for recursion “unfolding” which is precisely our understanding of how recursion should work. Hence
it is easy to see from (YC L) =β (L (YC L)) that
Many other researchers have defined other combinators which mimic the behaviour of the combinator YC. Of
By definition of →nβ
for all x ∈ V , λx[L] →m
β λx[Lm ], λx[Lm] →1β λx[M ] λx[L] →nβ λx[M ],
for all N ∈ Λ, (L N ) →m 1 n
β (Lm N ), (Lm N ) →β (M N ) (L N ) →β (M N )
for all N ∈ Λ, (N L) →m 1 n
β (N Lm ), (N Lm ) →β (N M ) (N L) →β (N M )
End (→∗β )
(=β ) Assume L =β M . We proceed by induction on the length of the proof of L =β M using the definition of
β-equality.
Basis. n = 1. Then either L ≡ M or L →∗β M . The case of reflexivity is trivial and the case of L →∗β M follows
from the previous proof.
Induction Hypothesis (IH).
JJ J I II G O B ACK F ULL S CREEN C LOSE 467 OF 600
PL April 2, 2020
For all terms L and M , such that the proof of L =β M requires less than n steps for n > 1, the compatibility
result holds.
Induction Step. Suppose the proof requires n steps and the last step is obtained by use of either =β Symmetry or
=β Transitivity on some previous steps.
Case (=β Symmetry). Then the (n − 1)-st step proved M =β L. By the induction hypothesis and then by
applying =β Symmetry to each case we get
By =β Symmetry
for all variables x, λx[M ] =β λx[L] λx[L] =β λx[M ]
for all terms N , (M N ) =β (L N ) (L N ) =β (M N )
for all terms N , (N M ) =β (N L) (N M ) =β (N L)
Case (=β Transitivity). Suppose L =β M was inferred in the n-th step from two previous steps which proved
L =β P and P =β M for some term P . Then again by induction hypothesis and then applying =β Transi-
tivity we get
By =β Transitivity
for all variables x, λx[L] =β λx[P ], λx[P ] =β λx[M ] λx[L] =β λx[M ]
for all terms N , (L N ) =β (P N ), (P N ) =β (M N ) (L N ) =β (M N )
for all terms N , (N L) =β (N P ), (N P ) =β (N M ) (N L) =β (N P )
End (=β )
QED
Confluence: Definitions
−→
L ⇒∃ P
−→
−→
N
JJ J I II G O B ACK F ULL S CREEN C LOSE 475 OF 600
PL April 2, 2020
Reduction Relations: Termination
Let −→ be a reduction relation, −→∗ the least preorder containing −→
∗
and ←→ the least equivalence relation containing −→∗. Then
Definition 13.3 −→ is terminating iff there is no infinite sequence of the
form
L0 −→ L1 −→ · · ·
Lemma 13.4 −→η is a terminating reduction relation.
Proof: By induction on the structure of terms. QED
We are mostly interested in β-reduction which is not guaranteed to terminate. We already know that there are
several terms which are only weakly normalising (β-WN). This means that there are several possible reduction
sequences, some of which may yield β-normal forms while the others may yield infinite computations. Hence in
order to obtain normal forms for such terms we need to schedule the β-reductions carefully to be guaranteed a
normal form. The matter would be further complicated if there are multiple unrelated normal forms.
Each β-reduction step may reveal fresh β-redexes. This in turn raises the disquieting possibility that each termina-
tion sequence may yield a different β-normal form. If such is indeed the case, then it raises fundamental questions
on the use of β-reduction (or function application) as a notion of reduction. If β-reduction is to be considered
fundamental to the notion of computation then all β-reduction sequences that terminate in β-nfs must yield the
same β-nf upto α-equivalence.
Hence our interest in the notion of confluence. Since the issue of confluence of β-reduction is rather complicated
we approach it in terms of inductively easier notions such as local confluence, and semi-confluence which finally
lead up to confluence and the Church-Rosser property.
−→
−→
∗
L ⇒∃ P
∗
−→
−→
N
−→
−→
∗
L ⇒∃ P
∗
−→
−→
∗
−→
−→
∗
L ⇒∃ P
∗
−→
−→
∗
N
Fact 13.8 Any confluent relation is also semi-confluent.
−→
−→
∗
∗
∃P
QED
Basis. m = 0. This case is trivial since for any P , L −→∗ P iff M −→∗ P
Induction Hypothesis (IH).
⇓
−→
∗
∃Q M
−→
−→
⇓
∗
∃P
−→
−→
⇓
∗
∃P
QED
Proof: Assume L −→ M and L −→∗ N . We need to show that there exists P such that M −→∗ P and N −→∗ P . We prove this by
induction on the length of L −→∗ N . If L ≡α N then P ≡α M , otherwise assume L −→ N1 −→ · · · −→ Nn = N for some n > 0.
By the local confluence we have there exists P1 such that M −→∗ P1 . By successively applying the induction hypothesis we get terms
P2 , . . . , Pn such that Pj−1 −→∗ Pj and Nj −→∗ Pj for each j, 1 ≤ j ≤ m. In effect we complete the following rectangle
L −→ N1 −→ N2 −→ · · · −→ Nn ≡ M
↓ ↓ ↓ ··· ↓
M −→ P1 −→ P2 −→ · · · −→ Pn
QED
From lemma 14.3 and theorem 14.2 we have the following theorem.
Proof:
−→ on Λ is given to be terminating and locally confluent. We need to show that it is confluent. That is for any L, we are given that
1. there is no infinite sequence of reductions of L, i.e. every maximal sequence of reductions of L is of length n for some n ≥ 0.
2.
N1 1←− L −→1 M1 ⇒ ∃P : M1 −→∗ P ∗←− N1 (7)
JJ J I II G O B ACK F ULL S CREEN C LOSE 489 OF 600
PL April 2, 2020
We need to show for any term L that
N ∗←− L −→∗ M ⇒ ∃S : M −→∗ S ∗←− N (8)
Let L be any term. Consider the graph G(L) = hΓ(L), −→1 i such that Γ(L) = {M | L −→∗ M }. Since −→ is a terminating reduction
If G(L) is not acyclic, there must be a cycle of length k > 0 such that M0 −→1 M1 −→1 · · · −→1 Mk−1 −→1 M0 which implies there is
also an infinite reduction sequence of the form L −→∗ M0 −→k M0 −→k · · · which is impossible.
Since there are only a finite number of sub-terms of L that may be reduced under −→, for each L there is a maximum number p ≥ 0,
which is the length of the longest reduction sequence.
We proceed by induction on p.
Basis. p = 0. Then Γ(L) = {L} and there are no reductions possible, so it is trivially confluent.
Induction Hypothesis (IH).
For any L whose longest reduction sequence is of length k, 0 ≤ k < p, property (8) holds.
Induction Step. Assume L is a term whose longest reduction sequence is of length p > 0. Also assume N ∗ ←− L −→∗ M i.e.
∃m, n ≥ 0 : N n←− L −→m M .
Case m = 0. If m = 0 then M ≡α L and hence S ≡α N .
Case n = 0. Then N ≡α L and we have S ≡α M .
JJ J I II G O B ACK F ULL S CREEN C LOSE 490 OF 600
PL April 2, 2020
Case m, n > 0. Then consider M1 and N1 such that
See figure (3). By (7), ∃P : M1 −→∗ P ∗←− N1 . Clearly M1 , N1 , P ∈ Γ(L) − {L}. Hence by fact 14.6, G(M1 ), G(N1 ) and G(P )
are all sub-graphs of G(L) and all their reduction sequences are of length smaller than p. Hence by induction hypothesis, we get
P ∗←− M1 −→∗ M ⇒ ∃Q : M −→∗ Q ∗←− P (10)
and
N ∗←− N1 −→∗ P ⇒ ∃R : P −→∗ R ∗←− N (11)
But by (10) and (11) and the induction hypothesis we have
R ∗←− P −→∗ Q ⇒ ∃S : Q −→∗ S ∗←− R (12)
Combining (12) with (9), (10) and (11) we get
N ∗←− L −→∗ M ⇒ ∃S : M −→∗ S ∗←− N (13)
QED
L −→1||β L0
||β1 L −→1 L ||β1Abs1
||β λx[L] −→1||β λx[L0]
QED
Proof: Since −→∗β =−→∗||β it follows from theorem 15.4 that −→1β is confluent. QED
Corollary 15.6 If a term reduces to a β-normal form then the normal form is unique (upto ≡α ).
JJ J I II G O B ACK F ULL S CREEN C LOSE 499 OF 600
PL April 2, 2020
Proof: If N1 ∗β←− L −→∗β N2 and both N1 N2 are β-nfs, then by the corollary 15.5 they must both be β-reducible
to a third element N3 which is impoosible if both N1 and N2 are β-nfs. Hence β-nfs are unique whenever they
exist. QED
An Applied Lambda-Calculus
1. Find examples of expressions in FL0 which have more than one computation.
2. Prove that −→δ is terminating.
3. Prove that −→δ is Church-Rosser.
4. Assume we remove the rule 19 from δ-reduction.
(a) Is it still guaranteed that every boolean expression has a δ-normal form? Prove or find a counter-example.
(b) Do the δ-normal forms of boolean expressions (whenever they exist) remain the same as before? Prove or
find a counter-example.
5. The language FL(X) extends FL0 with variables. What are the new δ-normal forms in FL(X)?
plusc =βδ λx[λy[ITE h(IZ x), y, ITE h(GTZ x), (plusc (P x) (S y)), (plusc (S x) (P y))ii]] (31)
We may think of equation (31) as an equation to be solved in the unknown variable plusc.
Consider the (applied) λ-term obtained from the right-hand-side of equation (31) by simply abstracting the un-
known plusc.
df
addc = λf [λx y[ITE h(IZ x), y, ITE h(GTZ x), (f (P x) (S y)), (f (S x) (P y))ii]] (32)
Claim 16.3
(REC addc) −→δ (addc (REC addc)) (33)
and hence
(REC addc) =βδ (addc (REC addc)) (34)
Claim 16.4 (REC addc) satisfies exactly the equation (31). That is
((REC addc) x y) =βδ ITE h(IZ x), y, ITE h(GTZ x), ((REC addc) (P x) (S y)), ((REC addc) (S x) (P y))ii (35)
Typing FL expressions
We have already seen that the simple language FL has
• two kinds of expressions: integer expressions and boolean expres-
sions,
• there are also constructors which take integer expressions as argu-
ments and yield boolean values
• there are also function types which allow various kinds of functions to
be defined on boolean expressions and integer expressions.
S P
Γ ` S : int→int ∅ ∅ Γ ` P : int→int ∅ ∅
IZ GTZ
Γ ` IZ : int→bool ∅ ∅ Γ ` GTZ : int→bool ∅ ∅
ITEB
Γ ` ITE : bool∗bool∗bool→bool ∅ ∅
Notice that the constructor ITE is overloaded and actually is two con-
structors ITEI and ITEB. Which constructor is actually used will depend
on the context and the type-inferencing mechanism.
Γ, x : σ ` L : τ T C
Abs
Γ ` λx[L] : σ→τ T C
Γ ` L : σ T1 C1
App Γ ` M : τ T2 C2 (Conditions 1. and 2.)
Γ ` (L M ) : 0a T 0 C 0
where
• Condition 1. T1 ∩ T2 = T1 ∩ T V ar(τ ) = T2 ∩ T V ar(σ) = ∅
Condition 2. 0a 6∈ T1 ∪ T2 ∪ T V ar(σ) ∪ T V ar(τ ) ∪ T V ar(C1) ∪ T V ar(C2).
• T 0 = T1 ∪ T2 ∪ {0a}
• C 0 = C1 ∪ C2 ∪ {σ = τ →0a}
JJ J I II G O B ACK F ULL S CREEN C LOSE 533 OF 600
PL April 2, 2020
Example 16.6 Consider the following simple combinator λx[λy[λz[(x (y z))]]] which defines the function compo-
sition operator. Since there are three bound variables x, y and z we begin with an initial assumption Γ = x : 0a, y :
0
b, z : 0c which assign arbitrary types to the bound variables, represented by the type variables 0a, 0b and 0c respec-
tively. Note however, that since it has no free variables, its type does not depend on the types of any variables. We
expect that at the end of the proof there would be no assumptions.Our inference for the type of the combinator then
proceeds as follows.
Hence λx[λy[λz[(x (y z))]]] : 0a→0b→0c→0e subject to the constraints given by {0b = 0c→0d, 0a = 0d→0e} which
yields λx[λy[λz[(x (y z))]]] : (0d→0e)→(0c→0d)→0c→0e
1. The language has several constructors which behave like functions. Derive the following rules for terms in
TΩ(X) from the basic typing axioms and the rule App.
Γ ` t : τ T C
Sx
Γ ` (S t) : int T C ∪ {τ = int}
Γ ` t : τ T C
Px
Γ ` (P t) : int T C ∪ {τ = int}
Γ ` t : τ T C
IZx
Γ ` (IZ t) : bool T C ∪ {τ = int}
Γ ` t : τ T C
GTZx
Γ ` (GTZ t) : bool T C ∪ {τ = int}
Γ ` t : σ T C
Γ ` t1 : τ T1 C1
ITEx (T ∩ T1 = T1 ∩ T0 = T0 ∩ T = ∅)
Γ ` t0 : υ T0 C0
Γ ` (ITE ht, t1, t0i) : τ T 0 C 0
where T 0 = T ∪ T1 ∪ T0 and C 0 = C ∪ C1 ∪ C0 ∪ {σ = bool, τ = υ}
JJ J I II G O B ACK F ULL S CREEN C LOSE 536 OF 600
PL April 2, 2020
2. Use the rules to define the type of the combinators K and S?
3. How would you define a type assignment for the recursive function addc defined by equation (32).
4. Prove that the terms, ω = λx[(x x)] and Ω = (ω ω) are ill-typed.
5. Are the following well-typed or ill-typed? Prove your answer.
(a) (K S)
(b) ((K S) ω)
(c) (((S K) K) ω)
(d) (ITE h(IZ x), T, (K x)i)
An Imperative Language
k
−87567
x y z
k
−87567
l m
m
78663
x y z w
The terms “l-value” (for “left-value”) and “r-value” (for “right-value”) come from the practice in most imperative
languages of writing assignment commands by overloading the variable name to denote both its address (γ(x)) in
Loc as well as the value σ(γ(x)) stored in memory. Consider the example,
• x := x + y (Pascal)
• x = x + y (C, C++, Java, Python, Perl)
The occurrence of “x” on the left-hand side of the assignment command denotes a location γ(x) whereas the occur-
rences of “x” and “y” on the right-hand-side of the assignment denote the values σ(γ(x)) and σ(γ(y)) respectively.
The term “dereferencing” is used to denote the action of “reading” the value stored in a location.
• This notation for assignment becomes a source of tremendous confusion when locations are also valid values,
as in the case of indirect addressing (look at w) and may be manipulated.
• The confusion is further exacerbated when locations are also integers indistinguishable from the integers stored
in the locations. The result of dereferencing an integer variable may be one of the following.
– An invalid location leading to a segmentation fault. For instance, the integer could be negative or larger than
any valid memory address.
– Another valid location with an undefined value or with a value defined previously when the location was
assigned to some other variable in a different job. This could lead to puzzling results in the current program.
– Another valid location which is already the address of a variable in the program (leading to an aliasing
totally unintended by the programmer). This could also lead to puzzling results in the current program.
• Modern impure functional languages (which have strong-typing facilties) usually clearly distinguish between
locations and values as different types. Hence every imperative variable represents only an l-value. Its r-value is
JJ J I II G O B ACK F ULL S CREEN C LOSE 545 OF 600
PL April 2, 2020
obained by applying a dereferencing operation (the prefix operation 1). Hence the same assignment command
in ML-like languages would be written
– x :=!x+!y (ML and OCaml)
The following interactive ML session illustrates aliasing and the effect on the aliased variables.
The following ML-session illustrates indirect addressing (and if you get confused, don’t come to me, I am confused
JJ J I II G O B ACK F ULL S CREEN C LOSE 546 OF 600
PL April 2, 2020
too; confusion is the price we pay for indiscriminate modification of state).
i
T
j
132456
k
−87567
l m
m
78663
z
x y u w
γ ` hσ, ei −→e m
Assgn
γ ` hσ, x := ei −→1c [γ(x) 7→ m]σ
Notes:
1. The Skip rule corresponds to any of the following:
• a noop
• the identity function or identity relation on states
• a command which has no effect on states
2. The assignment is the only command in our language which creates
a side-effect (actually changes state)
γ ` hσ, ei −→e T ,
Cond1 γ ` hσ, c1i −→1c σ1
γ ` hσ, if e then c1 else c1i −→1c σ1
γ ` hσ, ei −→e T,
γ ` hσ, ci −→1c σ 0,
While1
γ ` hσ 0, while e do ci −→1c σ 00
γ ` hσ, while e do ci −→1c σ 00
T F
γ ` hσ, Ti −→e hσ, Ti γ ` hσ, Fi −→e hσ, Fi
Z x
γ ` hσ, Zi −→e hσ, 0i γ ` hσ, xi −→e hσ, σ(γ(x))i
Local Declarations
We introduce declarations through a new syntactic category Decls de-
fined as follows:
d1, d2, d ::= int x bool y d1 ; d2
c ::= · · · {d; c}
• Most languages insist on a “declaration before use” discipline,
• Declarations create “little new environments”.
• Need to be careful about whether a variable is at all defined.
• Even if the l-value of a variable is defined, its r-value may not be
defined. The rules for variables and assignments then need to be
changed to the following.
x0 (σ(γ(x)) 6= ⊥)
γ ` hσ, xi −→e hσ, σ(γ(x))i
Assgn00 1 (γ(x) 6= ⊥)
γ ` hσ, x := mi −→c [γ(x) 7→ m]σ
γ ` hσ, ei −→ hσ, e 0i
e
Assgn10 1 0 (γ(x) 6= ⊥)
γ ` hσ, x := ei −→c hσ, x := e i
int − x (l ∈
/ Range(γ))
γ ` hσ, int xi −→d h[x 7→ l], [l 7→ ⊥]σi
bool − x (l ∈
/ Range(γ))
γ ` hσ, bool xi −→d h[x 7→ l], [l 7→ ⊥]σi
let
val x = 15
in
x + y
end
in
fun1 x
end
...
...
in ...
fun1 (fun2 x)
end
...
fun fun2 z =
...
in ...
fun1 (fun2 x)
end
let
val x = 15
in
x + y * z
end
in
fun1 x
end
let
val x = 15
in
x + y * z
end
in
fun1 x
end
let
val x = 15
in
x + y * z
end
in
fun1 x
end
let
val x = 15
in
x + y * z
end
in
fun1 x
end
let
val x = 15
in
x + y * z
end
in
fun1 x
end
let
val x = 15
in
x + y * z
end
in
fun1 x
end
let
val x = 15
in
x + y * z
end
in
fun1 x
end
local
exception invalidArg;
fun perfect n =
if n <= 0
then raise invalidArg
else
let
val nby2 = n div 2
in
n = sum_div2 (n, 1, nby2)
end
end
in
fun fun3 x = ...
fun2 ...
fun1 ...
end
M
*
*
M1
*Q
1
* *
L *P S
*
*
1
N1 R
*