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

Sak Notes

Uploaded by

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

Sak Notes

Uploaded by

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

Programming Languages

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

JJ J I II G O B ACK F ULL S CREEN C LOSE 1 OF 600


PL April 2, 2020
Contents

1 The Programming Languages Overview 3

2 Introduction to Compiling 19

3 Static Scope Rules 35

4 Runtime Structure 54

5 Scanning or Lexical Analysis 65


5.1 Regular Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
5.2 Nondeterministic Finite Automata (NFA) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94
5.3 Deterministic Finite Automata (DFA) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127

6 Parsing or Syntax Analysis 141


6.1 Grammars . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141
6.2 Context-Free Grammars . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145
6.3 Ambiguity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 160
6.4 Shift-Reduce Parsing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 180

JJ J I II G O B ACK F ULL S CREEN C LOSE 2 OF 600


PL April 2, 2020
6.5 Bottom-Up Parsing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 246
6.6 Simple LR Parsing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 264
6.7 Recursive Descent Parsing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 308
6.8 Specification of Syntax: Extended Backus-Naur Form . . . . . . . . . . . . . . . . . . . . . . . . 314
6.8.1 The Extended Backus-Naur Form (EBNF) . . . . . . . . . . . . . . . . . . . . . . . . . . 315
6.9 The WHILE Programming Language: Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . 321

7 Attributes & Semantic Analysis 338


7.1 Context-sensitive analysis and Semantics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 339
7.2 Syntax-Directed Translation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 347
7.2.1 Synthesized Attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 358
7.2.2 Inherited Attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 379

8 Abstract Syntax 394

9 Symbol Table 410

10 Intermediate Representation 417

11 The Pure Untyped Lambda Calculus: Basics 442


JJ J I II G O B ACK F ULL S CREEN C LOSE 3 OF 600
PL April 2, 2020
12 Notions of Reduction 457

13 Confluence Definitions 470


13.1 Why confluence? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 474

14 Confluence: Church-Rosser 483

15 The Church-Rosser Property 490

16 An Applied Lambda-Calculus 499


16.1 FL with recursion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 499
16.2 FL with type rules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 520

17 An Imperative Language 536


17.1 The Operational Semantics of Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 538
17.1.1 l-values, r-values, aliasing and indirect addressing . . . . . . . . . . . . . . . . . . . . . . 542
17.2 The Operational Semantics of Commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 548
17.3 The Semantics of Expressions in FL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 561
17.4 The Operational Semantics of Declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 568

JJ J I II G O B ACK F ULL S CREEN C LOSE 4 OF 600


PL April 2, 2020
18 Logic Programming and Prolog 597

JJ J I II G O B ACK F ULL S CREEN C LOSE 5 OF 600


PL April 2, 2020
1. The Programming Languages Overview

JJ J I II G O B ACK F ULL S CREEN C LOSE 6 OF 600


PL April 2, 2020
Programs: Source to Runs
Source
Linked Target
Compiler/ IR IR code Runtime Results
Interpreter Linker Loader
System

Compilation Linking Loading Runtime


Errors Errors Errors
Errors

Translated
Source
Macro−
processor

Pre−processor
Macro−translation
Errors

Pre−processing
Errors

JJ J I II G O B ACK F ULL S CREEN C LOSE 7 OF 600


PL April 2, 2020
Programs: Source to Runs-2
Source
Linked
Compiler/ IR IR Runtime Results
Interpreter Linker Loader
System

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

JJ J I II G O B ACK F ULL S CREEN C LOSE 8 OF 600


PL April 2, 2020
Programs: Source to Runs: LATEX
TeX dvi pdf
Source Render
TeX Linked Display
Compiler/ dvi2pdf
latex .tex IR IR or
Output
nw Interpreter Linker Printer
dvi2ps
Print
ps Target
Postscript printer
Compilation Linking Processor
Errors code
Errors

Translated TeX
latex source LaTeX
Source
latex source Macro−
nw source
processor
noweave
Pre−processor
notangle Macro−translation
Errors

Pre−processing Source Program


Errors
JJ J I II G O B ACK F ULL S CREEN C LOSE 9 OF 600
PL April 2, 2020
Programs: Source to Runs: LATEX
Display
TeX dvi pdf
Source Render
TeX Linked Display
Compiler/ dvi2pdf
latex .tex IR IR or
Output
nw Interpreter Linker Printer
dvi2ps
Print
ps Target
Postscript printer
Compilation Linking Processor
Errors code
Errors
Printer
Translated TeX
latex source LaTeX
Source
latex source Macro−
nw source
processor
noweave
Pre−processor
notangle Macro−translation
Errors

Pre−processing Source Program


Errors
JJ J I II G O B ACK F ULL S CREEN C LOSE 10 OF 600
PL April 2, 2020
Programs: Source to Runs: LATEX
Java Java Virtual Machine (JVM)
bytecode
Source
Linked Target
.java Compiler/ IR IR code Runtime Results
Interpreter Linker Loader
.class System

Compilation Linking Loading Runtime


Errors Errors Errors
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

JJ J I II G O B ACK F ULL S CREEN C LOSE 12 OF 600


PL April 2, 2020
The Usage of General PLs
7GMIRXMöG 2YQIVMGEP 'SQTYXEXMSRW

&YWMRIWW (EXE 4VSGIWWMRK 0MWX TVSGIWWMRK


8LISVIQ 4VSZMRK
7XVYGXYVIH 4VSKVEQQMRK

7]QFSP TVSGIWWMRK

7MQYPEXMSR

)\TIVX
8IEGLMRK
7]WXIQW

7]WXIQW 7XVSRK X]TMRK


4VSKVEQQMRK

1SHYPEV
4VSKVQK

6IEGXMZI
7]WXIQW

6IRHIVMRK
4VMRXMRK

6ITSVXMRK

;IF TVSKVEQQMRK

JJ J I II G O B ACK F ULL S CREEN C LOSE 13 OF 600


PL April 2, 2020
The Major Features of General PLs
7XEXMG 1IQSV] EPPSG
7XEXMG WGSTMRK
,IET EPPSG
9RX]TIH (]REQMG WGSTI
6YRXMQI WXEGO
(]REQMG QIQSV] EPPSG (]REQMG 1IQSV] EPPSG
7XEXMGEPP] WGSTIH

(]REQMG WGSTI
(]REQMG 1IQSV] EPPSGEXMSR

7XEXMG X]TI

YRX]TIH 7XEXMG WGSTI

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

JJ J I II G O B ACK F ULL S CREEN C LOSE 14 OF 600


PL April 2, 2020
FORTRAN
• The very first high-level programming language
• Still used in scientific computation
• Static memory allocation
• Very highly compute oriented
• Runs very fast because of static memory allocation
• Parameter passing by reference

JJ J I II G O B ACK F ULL S CREEN C LOSE 15 OF 600


PL April 2, 2020
COBOL
• A business oriented language
• Extremely verbose
• Very highly input-oriented
• Meant to manage large amounts of data on disks and tapes and gen-
erate reports
• Not computationally friendly

JJ J I II G O B ACK F ULL S CREEN C LOSE 16 OF 600


PL April 2, 2020
LisP
• First functional programming language
• Introduced lists and list-operations as the only data-structure
• Introduced symbolic computation
• Much favoured for AI and NLP programming for more than 40 years
• The first programming language whose interpreter could be written in
itself.

JJ J I II G O B ACK F ULL S CREEN C LOSE 17 OF 600


PL April 2, 2020
ALGOL-60
• Introduced the Backus-Naur Form (BNF) for specifying syntax of a
programming langauge
• Formal syntax defined by BNF (an extension of context-free gram-
mars)
• First language to implement recursion
• Introduction of block-structure and nested scoping
• Dynamic memory allocation
• Introduced the call-by-name parameter mechanism

JJ J I II G O B ACK F ULL S CREEN C LOSE 18 OF 600


PL April 2, 2020
Pascal
• ALGOL-like language meant for teaching structured programming
• Introduction of new data structures – records, enumerated types, sub-
range types, recursive data-types
• Its simplicity led to its “dialects” being adopted for expressing algo-
rithms in pseudo-code
• First language to be ported across a variety of hardware and OS plat-
forms – introduced the concepts of virtual machine and intermediate
code (bytecode)

JJ J I II G O B ACK F ULL S CREEN C LOSE 19 OF 600


PL April 2, 2020
ML
• First strongly and statically typed functional programming language
• Created the notion of an inductively defined type to construct complex
types
• Powerful pattern matching facilities on complex data-types.
• Introduced type-inference, thus making declarations unnecessary ex-
cept in special cases
• Its module facility is inspired by the algebraic theory of abstract data
types
• The first language to introduce functorial programming between alge-
braic structures and modules

JJ J I II G O B ACK F ULL S CREEN C LOSE 20 OF 600


PL April 2, 2020
Prolog
• First Declarative programming language
• Uses the Horn clause subset of first-order logic
• Goal-oriented programming implementing a top-down methodology
• Implements backtracking as a language feature
• Powerful pattern-matching facilities like in functional programming
• Various dialects implement various other features such as constraint
programming, higher-order functions etc.

JJ J I II G O B ACK F ULL S CREEN C LOSE 21 OF 600


PL April 2, 2020
2. Introduction to Compiling

JJ J I II G O B ACK F ULL S CREEN C LOSE 22 OF 600


PL April 2, 2020
Introduction to Compiling
• Translation of programming languages into executable code
• But more generally any large piece of software requires the use of
compiling techniques.
• The processes and techniques of designing compilers is useful in de-
signing most large pieces of software.
• Compiler design uses techniques from theory, data structures and al-
gorithms.

JJ J I II G O B ACK F ULL S CREEN C LOSE 23 OF 600


PL April 2, 2020
Software Examples
Some examples of other software that use compiling techniques
• Almost all user-interfaces require scanners and parsers to be used.
• All XML-based software require interpretation that uses these tech-
niques.
• All mathematical text formatting requires the use of scanning, parsing
and code-generation techniques (e.g. LATEX).
• Model-checking and verification software are based on compiling tech-
niques
• Synthesis of hardware circuits requires a description language and the
final code that is generated is an implementation either at the register-
transfer level or gate-level design.

JJ J I II G O B ACK F ULL S CREEN C LOSE 24 OF 600


PL April 2, 2020
Books and References
1. Appel A W. Modern Compiler Implementation in Java Cambridge Univer-
sity Press, Revised Indian Paperback edition 2001
2. Aho A V, Sethi R, Ullman J D. Compilers: Principles, Techniques, and
Tools, Addison-Wesley 1986.
3. Muchnick S S. Advanced Compiler Design and Implementation, Academic
Press 1997.

JJ J I II G O B ACK F ULL S CREEN C LOSE 25 OF 600


PL April 2, 2020
Source and Target
In general a compiler for a a source language S written in some language
C translates code to a target language T .
Source S could be
• a programming language, or
• a description language (e.g. Verilog, VHDL), or
• a markup language (e.g. XML, HTML, SGML, LATEX)
Target T could be
• another programming language, assembly language or machine lan-
guage, or
• a language for describing various objects (circuits etc.), or
• a low level language for execution, display, rendering etc.
We will be primarily concerned with compiling from a high-level program-
ming language (source) to low-level code.
JJ J I II G O B ACK F ULL S CREEN C LOSE 26 OF 600
PL April 2, 2020
The Compiling Process
In general the process of compiling involves at least three languages
1. The language S of source programs in which the users of the compiler
write code.
2. The language C in which the compiler itself is written. The assumption
is that unless the compiler itself is written in machine language there
is already a compiler or an interpreter for C.
3. The language T into which the compiler translates the user programs.
Besides these three languages there could be several other intermediate
languages I1, I2, . . . (also called intermediate representations) into which
the source program could be translated in the process of compiling or
interpreting the source programs written in S. In modern compilers, for
portability, modularity and reasons of code improvement, there is usually
at least one intermediate representation.

JJ J I II G O B ACK F ULL S CREEN C LOSE 27 OF 600


PL April 2, 2020
Compiling as Translation
Except in the case of a source to source translation (for example, a Pas-
cal to C translator which translates Pascal programs into C programs),
we may think of the process of compiling high-level languages as one
of transforming programs written in S into programs of lower-level lan-
guages such as the intermediate representation or the target language.
By a low-level language we mean that the language is in many ways
closer to the architecture of the target language.

JJ J I II G O B ACK F ULL S CREEN C LOSE 28 OF 600


PL April 2, 2020
Phases of a Compiler
A compiler or translator is a fairly complex piece of software that needs
to be developed in terms of various independent modules.
In the case of most programming languages, compilers are designed in
phases.
The various phases may be different from the various passes in compila-
tion

JJ J I II G O B ACK F ULL S CREEN C LOSE 29 OF 600


PL April 2, 2020
The Big Picture: 1

stream of
characters
SCANNER
stream of
tokens

JJ J I II G O B ACK F ULL S CREEN C LOSE 30 OF 600


PL April 2, 2020
The Big Picture: 2

stream of
characters
SCANNER
stream of
tokens
PARSER
parse tree

JJ J I II G O B ACK F ULL S CREEN C LOSE 31 OF 600


PL April 2, 2020
The Big Picture: 3

stream of
characters
SCANNER
stream of
tokens
PARSER
parse tree
SEMANTIC ANALYZER
abstract
syntax tree

JJ J I II G O B ACK F ULL S CREEN C LOSE 32 OF 600


PL April 2, 2020
The Big Picture: 4

stream of
characters
SCANNER
stream of
tokens
PARSER
parse tree
SEMANTIC ANALYZER
abstract
syntax tree

I.R. CODE GENERATOR


intermediate
representation

JJ J I II G O B ACK F ULL S CREEN C LOSE 33 OF 600


PL April 2, 2020
The Big Picture: 5

stream of
characters
SCANNER
stream of
tokens
PARSER
parse tree
SEMANTIC ANALYZER
abstract
syntax tree

I.R. CODE GENERATOR


intermediate
representation
OPTIMIZER

optimized
intermediate
representation

JJ J I II G O B ACK F ULL S CREEN C LOSE 34 OF 600


PL April 2, 2020
The Big Picture: 6

stream of
characters
SCANNER
stream of
tokens
PARSER
parse tree
SEMANTIC ANALYZER
abstract
syntax tree

I.R. CODE GENERATOR


intermediate
representation
OPTIMIZER

optimized
intermediate
representation

CODE GENERATOR

target code

JJ J I II G O B ACK F ULL S CREEN C LOSE 35 OF 600


PL April 2, 2020
The Big Picture: 7

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

JJ J I II G O B ACK F ULL S CREEN C LOSE 36 OF 600


PL April 2, 2020
The Big Picture: 8

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

Scanner Parser Semantic Analysis Symbol Table


IR Run-time structure

JJ J I II G O B ACK F ULL S CREEN C LOSE 37 OF 600


PL April 2, 2020
3. Static Scope Rules

JJ J I II G O B ACK F ULL S CREEN C LOSE 38 OF 600


PL April 2, 2020
Disjoint Scopes
let
val x = 10;
fun fun1 y =
let
...
in
...
end
fun fun2 z =
let
...
in
in ...
end
fun1 (fun2 x)
end

JJ J I II G O B ACK F ULL S CREEN C LOSE 39 OF 600


PL April 2, 2020
Nested Scopes
let
val x = 10;
fun fun1 y =

let
val x = 15

in
x + y
end
in
fun1 x
end

JJ J I II G O B ACK F ULL S CREEN C LOSE 40 OF 600


PL April 2, 2020
Overlapping Scopes
let
val x = 10;
fun fun1 y =
...

...

...
in ...

fun1 (fun2 x)
end

JJ J I II G O B ACK F ULL S CREEN C LOSE 41 OF 600


PL April 2, 2020
Spannning
let
val x = 10;
fun fun1 y =
...

...

fun fun2 z =
...
in ...

fun1 (fun2 x)
end

JJ J I II G O B ACK F ULL S CREEN C LOSE 42 OF 600


PL April 2, 2020
Scope & Names
• A name may occur either as being defined or as a use of a previously
defined name
• The same name may be used to refer to different objects.
• The use of a name refers to the textually most recent definition in the
innermost enclosing scope
diagram

JJ J I II G O B ACK F ULL S CREEN C LOSE 43 OF 600


PL April 2, 2020
Names & References: 0
let
val x = 10; val z = 5;
fun fun1 y =

let
val x = 15

in
x + y * z
end
in
fun1 x
end

Back to Scope & Names

JJ J I II G O B ACK F ULL S CREEN C LOSE 44 OF 600


PL April 2, 2020
Names & References: 1
let
val x = 10; val z = 5;
fun fun1 y =

let
val x = 15

in
x + y * z
end
in
fun1 x
end

Back to Scope & Names

JJ J I II G O B ACK F ULL S CREEN C LOSE 45 OF 600


PL April 2, 2020
Names & References: 2
let
val x = 10; val z = 5;
fun fun1 y =

let
val x = 15

in
x + y * z
end
in
fun1 x
end

Back to Scope & Names

JJ J I II G O B ACK F ULL S CREEN C LOSE 46 OF 600


PL April 2, 2020
Names & References: 3
let
val x = 10; val z = 5;
fun fun1 y =

let
val x = 15

in
x + y * z
end
in
fun1 x
end

Back to Scope & Names

JJ J I II G O B ACK F ULL S CREEN C LOSE 47 OF 600


PL April 2, 2020
Names & References: 4
let
val x = 10; val z = 5;
fun fun1 y =

let
val x = 15

in
x + y * z
end
in
fun1 x
end

Back to Scope & Names

JJ J I II G O B ACK F ULL S CREEN C LOSE 48 OF 600


PL April 2, 2020
Names & References: 5
let
val x = 10; val z = 5;
fun fun1 y =

let
val x = 15

in
x + y * z
end
in
fun1 x
end

Back to Scope & Names

JJ J I II G O B ACK F ULL S CREEN C LOSE 49 OF 600


PL April 2, 2020
Names & References: 6
let
val x = 10; val z = 5;
fun fun1 y =

let
val x = 15

in
x + y * z
end
in
fun1 x
end

Back to Scope & Names

JJ J I II G O B ACK F ULL S CREEN C LOSE 50 OF 600


PL April 2, 2020
Names & References: 7
let
val x = 10; val x = x − 5;
fun fun1 y =
let
...
in
...
end
fun fun2 z =
let
...
in
...
end
in fun1 (fun2 x)
end

Back to Scope & Names

JJ J I II G O B ACK F ULL S CREEN C LOSE 51 OF 600


PL April 2, 2020
Names & References: 8
let
val x = 10; val x = x − 5;
fun fun1 y =
let
...
in
...
end
fun fun2 z =
let
...
in
...
end
in fun1 (fun2 x)
end

Back to Scope & Names

JJ J I II G O B ACK F ULL S CREEN C LOSE 52 OF 600


PL April 2, 2020
Names & References: 9
let
val x = 10; val x = x − 5;
fun fun1 y =
let
...
in
...
end
fun fun2 z =
let
...
in
...
end
in fun1 (fun2 x)
end

Back to Scope & Names

JJ J I II G O B ACK F ULL S CREEN C LOSE 53 OF 600


PL April 2, 2020
Definition of Names
Definitions are of the form
qualifier name . . . = body
• val name =
• fun name ( argnames ) =
• local def initions
in def inition
end

JJ J I II G O B ACK F ULL S CREEN C LOSE 54 OF 600


PL April 2, 2020
Use of Names
Names are used in expressions.
Expressions may occur
• by themselves – to be evaluated
• as the body of a definition
• as the body of a let-expression
let def initions
in expression
end
use of local

JJ J I II G O B ACK F ULL S CREEN C LOSE 55 OF 600


PL April 2, 2020
Scope & local
local
fun fun1 y =
...

fun fun2 z = ...


fun1

in
fun fun3 x = ...
fun2 ...
fun1 ...
end

JJ J I II G O B ACK F ULL S CREEN C LOSE 56 OF 600


PL April 2, 2020
4. Runtime Structure

JJ J I II G O B ACK F ULL S CREEN C LOSE 57 OF 600


PL April 2, 2020
Run-time Structure

JJ J I II G O B ACK F ULL S CREEN C LOSE 58 OF 600


PL April 2, 2020
Run-time Environment
Memory for running a program is divided up as follows
Code Segment. This is where the object code of the program resides
Run-time Stack. Required in a dynamic memory management technique.
Especially required in languages which support recursion. All data
whose sizes can be determined statically before loading is stored in
an appropriate stack-frame (activation record).
Heap. All data whose sizes are not determined statically and all data that
is generated at run-time is stored in the heap.

JJ J I II G O B ACK F ULL S CREEN C LOSE 59 OF 600


PL April 2, 2020
A Calling Chain
Main program
Globals

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

Main → P1 → P2 → P21 → P21

JJ J I II G O B ACK F ULL S CREEN C LOSE 60 OF 600


PL April 2, 2020
Run-time Structure: 1
Main program

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

JJ J I II G O B ACK F ULL S CREEN C LOSE 61 OF 600


PL April 2, 2020
Run-time Structure: 2
Main program

Globals

Procedure P2
Locals of P2

Procedure P21
Locals of P21

Body of P21

Body of P2

Procedure P1 Return address to Main


Locals of P1 Dynamic link to Main
Locals of P1
Body of P1
Static link to Main
Formal par of P1
Main body
Globals

Main → P1

JJ J I II G O B ACK F ULL S CREEN C LOSE 62 OF 600


PL April 2, 2020
Run-time Structure: 3
Main program

Globals

Procedure P2
Locals of P2

Procedure P21
Locals of P21

Return address to last of P1


Body of P21 Dynamic link to last P1
Locals of P2
Body of P2 Static link to last P1
Formal par P2

Procedure P1 Return address to Main


Locals of P1 Dynamic link to Main
Locals of P1
Body of P1
Static link to Main
Formal par of P1
Main body
Globals

Main → P1 → P2

JJ J I II G O B ACK F ULL S CREEN C LOSE 63 OF 600


PL April 2, 2020
Run-time Structure: 4
Main program

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

Return address to last of P1


Body of P21 Dynamic link to last P1
Locals of P2
Body of P2 Static link to last P1
Formal par P2

Procedure P1 Return address to Main


Locals of P1 Dynamic link to Main
Locals of P1
Body of P1
Static link to Main
Formal par of P1
Main body
Globals

Main → P1 → P2 → P21

JJ J I II G O B ACK F ULL S CREEN C LOSE 64 OF 600


PL April 2, 2020
Run-time Structure: 5
Return address to last of P21
Main program Dynamic link to last P21
Locals of P21
Globals Static link to last P2
Formal par P21
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

Return address to last of P1


Body of P21 Dynamic link to last P1
Locals of P2
Body of P2 Static link to last P1
Formal par P2

Procedure P1 Return address to Main


Locals of P1 Dynamic link to Main
Locals of P1
Body of P1
Static link to Main
Formal par of P1
Main body
Globals

Main → P1 → P2 → P21 → P21


Back to the Big Picture

JJ J I II G O B ACK F ULL S CREEN C LOSE 65 OF 600


PL April 2, 2020
JJ J I II G O B ACK F ULL S CREEN C LOSE 66 OF 600
PL April 2, 2020
JJ J I II G O B ACK F ULL S CREEN C LOSE 67 OF 600
PL April 2, 2020
5. Scanning or Lexical Analysis

Lexical Analysis

JJ J I II G O B ACK F ULL S CREEN C LOSE 68 OF 600


PL April 2, 2020
Programming Language Elements
• Every language is built from a finite alphabet of symbols. The alphabet
of a programming language consists of the symbols of the ASCII set.
• Each language has a vocabulary consisting of words. Each word is a
string of symbols drawn from the alphabet.
• Each language has a finite set of punctuation symbols, which separate
phrases, clauses and sentences.
• A programming language also has a finite set of operators.
• The phrases, clauses and sentences of a programming language are
expressions, commands, functions, procedures and programs.

JJ J I II G O B ACK F ULL S CREEN C LOSE 69 OF 600


PL April 2, 2020
Lexical Analysis
lex-i-cal: relating to words of a language
• A source program (usually a file) consists of a stream of characters.
• Given a stream of characters that make up a source program the com-
piler must first break up this stream into groups of meaningful words,
and other symbols.
• Each such group of characters is then classified as belonging to a
certain token type.
• Certain sequences of characters are not tokens and are completely
ignored (or skipped) by the compiler.

JJ J I II G O B ACK F ULL S CREEN C LOSE 70 OF 600


PL April 2, 2020
Tokens and Non-tokens
Tokens Typical tokens are
• Constants: Integer, Boolean, Real, Character and String constants.
• Identifiers: Names of variables, constants, procedures, functions
etc.
• Keywords/Reserved words: void, public, main
• Operators:+, *, /
• Punctuation: ,, :, .
• Brackets: (, ), [, ], begin, end, case, esac
Non-tokens Typical non-tokens
• whitespace: sequences of tabs, spaces, new-line characters,
• comments: compiler ignores comments
• preprocessor directives: #include ..., #define ...
• macros in the beginning of C programs
JJ J I II G O B ACK F ULL S CREEN C LOSE 71 OF 600
PL April 2, 2020
Scanning: 1
During the scanning phase the compiler/interpreter

• takes a stream of characters and identifies tokens from the lexemes.

• Eliminates comments and redundant whitepace.

• 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.

JJ J I II G O B ACK F ULL S CREEN C LOSE 72 OF 600


PL April 2, 2020
Scanning: 2
Definition 5.1 A lexeme is a basic lexical unit of a language consisting
of one word or several words, the elements of which do not separately
convey the meaning of the whole.
• Whitespace: A sequence of space, tab, newline, carriage-return, form-
feed characters etc.
• Lexeme: A sequence of non-whitespace characters delimited by
whitespace or special characters (e.g. operators or punctuation sym-
bols)
• Examples of lexemes.
– reserved words, keywords, identifiers etc.
– Each comment is usually a single lexeme
– preprocessor directives

JJ J I II G O B ACK F ULL S CREEN C LOSE 73 OF 600


PL April 2, 2020
Scanning: 3
Definition 5.2 A token consists of an abstract name and the attributes of
a lexeme.
• Token: A sequence of characters to be treated as a single unit.
• Examples of tokens.
– Reserved words (e.g. begin, end, struct, if etc.)
– Keywords (integer, true etc.)
– Operators (+, &&, ++ etc)
– Identifiers (variable names, procedure names, parameter names)
– Literal constants (numeric, string, character constants etc.)
– Punctuation marks (:, , etc.)

JJ J I II G O B ACK F ULL S CREEN C LOSE 74 OF 600


PL April 2, 2020
Scanning: 4
• Identification of tokens is usually done by a Deterministic Finite-state
automaton (DFA).
• The set of tokens of a language is represented by a large regular ex-
pression.
• This regular expression is fed to a lexical-analyser generator such as
Lex, Flex or JLex.
• A giant DFA is created by the Lexical analyser generator.

JJ J I II G O B ACK F ULL S CREEN C LOSE 75 OF 600


PL April 2, 2020
Lexical Rules
• Every programming language has lexical rules that define how a token
is to be defined.
Example. In most programming languages identifiers satisfy the fol-
lowing rules.
1. An identifier consists of a sequence of of letters (A . . . Z, a . . . z),
digits (0 . . . 9) and the underscore ( ) character.
2. The first character of an identifier must be a letter.
• Any two tokens are separated by some delimiters (usually whitespace)
or non-tokens in the source program.

JJ J I II G O B ACK F ULL S CREEN C LOSE 76 OF 600


PL April 2, 2020
JJ J I II G O B ACK F ULL S CREEN C LOSE 77 OF 600
PL April 2, 2020
5.1. Regular Expressions

Regular Expressions

JJ J I II G O B ACK F ULL S CREEN C LOSE 78 OF 600


PL April 2, 2020
Specifying Lexical Rules
We require compact and simple ways of specifying the lexical rules of
the tokens of a language. In particular,
• there are an infinite number of legally correct identifiers (names) in any
programming language.
• we require finite descriptions/specifications of the lexical rules so that
they can cover the infinite number of legal tokens.
One way of specifying the lexical rules of a programming language is to
use regular expressions.

JJ J I II G O B ACK F ULL S CREEN C LOSE 79 OF 600


PL April 2, 2020
Regular Expressions Language
• Each regular expression is a finite sequence of symbols.
• A regular expression may be used to describe an infinite collection of
strings.
The regular expression used to define the set of possible identifiers as
defined by the rules is
[A−Za−z][A−Za−z0−9 ]∗

JJ J I II G O B ACK F ULL S CREEN C LOSE 80 OF 600


PL April 2, 2020
Concatenations
Consider a (finite) alphabet (of symbols) A.
• Any set of strings built up from the symbols of A is called a language.
• Given any two strings x and y in a language, x.y or simply xy is the
concatenation of the two strings.
Example Given the strings x = Mengesha and y = Mamo, x.y =
MengeshaMamo and y.x = MamoMengesha.
• Given two languages X and Y , then X.Y or simply XY is the concate-
nation of the languages.
Example Let X = {Mengesha, Gemechis} and Y =
{Mamo, Bekele, Selassie}
XY = {MengeshaMamo, MengeshaBekele,
MengeshaSelassie, GemechisMamo,
GemechisBekele, GemechisSelassie}

JJ J I II G O B ACK F ULL S CREEN C LOSE 81 OF 600


PL April 2, 2020
Note on the Concept of “language”.
Unfortunately we have too many related but slightly different concepts, each of which is simply called a “language”.
Here is a clarification of the various concepts that we use.

• 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.

There are a variety of languages that we need to get familiar with.

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.

JJ J I II G O B ACK F ULL S CREEN C LOSE 83 OF 600


PL April 2, 2020
Simple Language of Regular Expressions
We consider a simple language of regular expressions. Assume a (finite)
alphabet A of symbols. Each regular expression r denotes a set of strings
L(r). L(r) is also called the language specified by the regular expression
r.
Symbol For each symbol a in A, the regular expression a denotes the set
{a}.
(Con)catenation For any two regular expressions r and s, r.s or simply rs
denotes the concatenation of the languages specified by r and s. That
is,
L(rs) = L(r)L(s)

JJ J I II G O B ACK F ULL S CREEN C LOSE 84 OF 600


PL April 2, 2020
Epsilon and Alternation
Epsilon  denotes the language with a single element the empty string
("") i.e.
L() = {""}
Alternation Given any two regular expressions r and s, r|s is the set
union of the languages specified by the individual expressions r and s
respectively.
L(r | s) = L(r) ∪ L(s)
Example L(Menelik|Selassie|) = {Menelik, Selassie, ””}.

JJ J I II G O B ACK F ULL S CREEN C LOSE 85 OF 600


PL April 2, 2020
String Repetitions
For any string x, we may use concatenation to create a string y with as
many repetitions of x as we want, by defining repetitions by induction.

x0 = ””
x1 = x
x2 = x.x
..
xn+1 = x.xn = xn.x
..
Then
x∗ = {xn | n ≥ 0}

JJ J I II G O B ACK F ULL S CREEN C LOSE 86 OF 600


PL April 2, 2020
String Repetitions Example
Example. Let x = Selassie. Then
x0 = ””
x1 = Selassie
x2 = SelassieSelassie
..
x5 = SelassieSelassieSelassieSelassieSelassie
..

Then x∗ is the language consisting of all strings that are finite repetitions
of the string Selassie

JJ J I II G O B ACK F ULL S CREEN C LOSE 87 OF 600


PL April 2, 2020
Language Iteration
The ∗ operator can be extended to languages in the same way. For any
language X, we may use concatenation to create a another language
Y with as many repetitions of the strings in X as we want, by defining
repetitions by induction.

X0 = ””
X1 = X
X2 = X.X
..
X n+1 = X.X n = X n.X
..
Then
X∗ = Xn
[

n≥0

JJ J I II G O B ACK F ULL S CREEN C LOSE 88 OF 600


PL April 2, 2020
Language Iteration Example
Example Let X = {Mengesha, Gemechis}. Then
X0 = {””}
X1 = {Mengesha, Gemechis}
X2 = {MengeshaMengesha, GemechisMengesha,
MengeshaGemechis, GemechisGemechis}
X3 = {MengeshaMengeshaMengesha,
GemechisMengeshaMengesha,
MengeshaGemechisMengesha,
GemechisGemechisMengesha,
MengeshaMengeshaGemechis,
GemechisMengeshaGemechis,
MengeshaGemechisGemechis,
GemechisGemechisGemechis}
..
X n+1 = X.X n
..
JJ J I II G O B ACK F ULL S CREEN C LOSE 89 OF 600
PL April 2, 2020
Kleene Closure
Given a regular expression r, rn specifies the n-fold iteration of the lan-
guage specified by r.
Given any regular expression r, the Kleene closure of r, denoted r∗ spec-
ifies the language (L(r))∗.
In general
r∗ = r0 | r1 | · · · | rn+1 | · · ·
denotes an infinite union of languages.
Further it is easy to show the following identities.

r∗ = |r.r∗ (1)
r∗ = (r∗)∗ (2)

JJ J I II G O B ACK F ULL S CREEN C LOSE 90 OF 600


PL April 2, 2020
Plus Closure
The Kleene closure allows for zero or more iterations of a language. The
+-closure of a language X denoted by X + and defined as
+ Xn
[
X =
n>0
denotes one or more iterations of the language X.
Analogously we have that r+ specifies the language (L(r))+.
Notice that for any language X, X + = X.X ∗ and hence for any regular
expression r we have
r+ = r.r∗
We also have the identity (1)
r∗ =  | r+

JJ J I II G O B ACK F ULL S CREEN C LOSE 91 OF 600


PL April 2, 2020
Range Specifications
We may specify ranges of various kinds as follows.
• [a−c] = a | b | c. Hence the expression of Question 3 may be specified
as [a−c]∗.
• Multiple ranges: [a−c0−3] = [a−c] | [0−3]

Question 6. Try to understand what the regular expression for identifiers


really specifies.
Question 7. Modify the regular expression so that all identifiers start only
with upper-case letters.
Question 8. Give regular expressions to specify
• real numbers in fixed decimal point notation
• real numbers in floating point notation
• real numbers in both fixed decimal point notation as well as floating
point notation.
JJ J I II G O B ACK F ULL S CREEN C LOSE 92 OF 600
PL April 2, 2020
Equivalence of Regular Expressions
Definition 5.3 Let REGEXPA denote the set of regular expressions over
a a finite non-empty set of symbols A and let r, s ∈ REGEXPA. Then
• r 5A r if and only if L(r) ⊆ L(s) and
• they are equivalent (denoted r =A s) if they specify the same language,
i.e.
r =A s if and only if L(r) = L(s)
We have already considered various identities (e.g. (1)) giving the equiv-
alence between different regular expressions.

JJ J I II G O B ACK F ULL S CREEN C LOSE 93 OF 600


PL April 2, 2020
Notes on bracketing and precedence of operators
In general regular expressions could be ambiguous (in the sense that the same expression may be interpreted to
refer to different languages. This is especially so in the presence of

• multiple binary operators


• some unary operators used in prefix form while some others are used in post-fix form. The Kleene-closure and
plus closure are operators in postfix form. We have not introduced any prefix unary operator in the language of
regular expressions.

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

1. For what regular expression r will r∗ specify a finite set?


2. How many strings will be in the language specified by (a | b | c)n?
3. Give an informal description of the language specified by (a | b | c)∗?
4. Give a regular expression which specifies the language {ak | k > 100}.
5. Simplify the expression r∗.r∗, i.e. give a simpler regular expression which specifies the same language.
6. Simplify the expression r+.r+.
JJ J I II G O B ACK F ULL S CREEN C LOSE 95 OF 600
PL April 2, 2020
JJ J I II G O B ACK F ULL S CREEN C LOSE 96 OF 600
PL April 2, 2020
5.2. Nondeterministic Finite Automata (NFA)

Nondeterministic Finite Automata (NFA)

JJ J I II G O B ACK F ULL S CREEN C LOSE 97 OF 600


PL April 2, 2020
Nondeterministic Finite Automata
A regular expression is useful in defining a finite state automaton. An au-
tomaton is a machine (simple program) which can be used to recognize
all valid lexical tokens of a language.
A nondeterministic finite automaton (NFA) N over a finite alphabet A con-
sists of
• a finite set Q of states,
• an initial state q0 ∈ Q,
• a finite subset F ⊆ Q of states called the final states or accepting states,
and
• a transition relation −→⊆ Q × (A ∪ {ε}) × Q. Equivalently
−→: Q × (A ∪ {ε}) → 2Q
is a function that for each source state q ∈ Q and symbol a ∈ A asso-
ciates a (possibly empty) set of target states.
JJ J I II G O B ACK F ULL S CREEN C LOSE 98 OF 600
PL April 2, 2020
Nondeterminism and Automata
• In general the automaton reads the input string from left to right.
• It reads each input symbol only once and executes a transition to new
state.
• The ε transitions represent going to a new target state without reading
any input symbol.
• The NFA may be nondeterministic because of
– one or more ε transitions from the same source state different target
states,
– one or more transitions on the same input symbol from one source
state to two or more different target states,
– choice between executing a transition on an input symbol and a tran-
sition on ε (and going to different states).

JJ J I II G O B ACK F ULL S CREEN C LOSE 99 OF 600


PL April 2, 2020
Acceptance of NFA
• For any alphabet A, A∗ denotes the set of all (finite-length) strings of
symbols from A.
• Given a string x = a1a2 . . . an ∈ A∗, an accepting sequence is a se-
quence of transitions
ε a1 ε ε a2 ε an ε
q0 −→ · · · −→−→ · · · q1 −→ · · · −→ · · · −→−→−→ · · · qn
where qn ∈ F is an accepting state.
• Since the automaton is nondeterministic, it is also possible that there
exists another sequence of transitions
ε a1 ε ε a2 ε an ε
q0 −→ · · · −→−→ · · · q1 −→ · · · −→ · · · −→−→−→ · · · qn0
0

where qn0 is not a final state.


• The automaton accepts x, if there is an accepting sequence for x.

JJ J I II G O B ACK F ULL S CREEN C LOSE 100 OF 600


PL April 2, 2020
Language of a NFA
• The language accepted or recognized by a NFA is the set of strings that
can be accepted by the NFA.
• L(N ) is the language accepted by the NFA N .

JJ J I II G O B ACK F ULL S CREEN C LOSE 101 OF 600


PL April 2, 2020
Construction of NFAs
• We show how to construct an NFA to accept a certain language of
strings from the regular expression specification of the language.
• The method of construction is by induction on the structure of the reg-
ular expression. That is, for each regular expression operator, we
show how to construct the corresponding automaton assuming that
the NFAs corresponding to individual components of expression have
already been constructed.
• For any regular expression r the corresponding NFA constructed is
denoted Nr . Hence for the regular expression r|s, we construct the
NFA Nr|s using the NFAs Nr and Ns as the building blocks.
• Our method requires only one initial state and one final state for each
automaton. Hence in the construction of Nr|s from Nr and Ns, the
initial states and the final states of Nr and Ns are not initial or final
unless explicitly used in that fashion.
JJ J I II G O B ACK F ULL S CREEN C LOSE 102 OF 600
PL April 2, 2020
Constructing NFA
• We show the construction only for the most basic operators on regular
expressions.
• For any regular expression r, we construct a NFA Nr whose initial
state is named r0 and final state rf .
• The following symbols show the various components used in the de-
piction of NFAs.
Initial state

Accepting state

Typical NFA for r


r0 Nr rf

a Typical transition

JJ J I II G O B ACK F ULL S CREEN C LOSE 103 OF 600


PL April 2, 2020
Regular Expressions to NFAs:1
a
a0 af Na

We may also express the automaton in tabular form as follows:


Na Input Symbol
State a ··· ε
a0 {af } ∅ · · · ∅ ∅
af ∅ ∅···∅ ∅
Notice that all the cells except one have empty targets.

JJ J I II G O B ACK F ULL S CREEN C LOSE 104 OF 600


PL April 2, 2020
Regular Expressions to NFAs:2
ε
ε0 εf Nε

Nε Input Symbol
State a ··· ε
ε0 ∅ ∅ · · · ∅ {εf }
εf ∅ ∅···∅ ∅

JJ J I II G O B ACK F ULL S CREEN C LOSE 105 OF 600


PL April 2, 2020
Regular Expressions to NFAs:3
r0 Nr rf ε
ε

r|s f N r|s
r|s 0

ε s0 Ns sf ε

Nr|s Input Symbol


State a ··· ε
r|s0 ∅ · · · {r0, s0}
r0 ··· ··· ···
.. .. .. ..
rf · · · · · · {r|sf }
s0 ··· ··· ···
.. .. .. ..
sf · · · · · · {r|sf }
r|sf ∅ ··· ∅
JJ J I II G O B ACK F ULL S CREEN C LOSE 106 OF 600
PL April 2, 2020
Regular Expressions to NFAs:4
Nr ε
r0 rf s0 Ns sf Nr.s

Nr.s Input Symbol


State a ··· ε
r0 ··· ··· ···
.. .. .. ..
rf · · · · · · {s0}
s0 ··· ··· ···
.. .. .. ..
sf ··· ··· ···
Notice that the initial state of Nr.s is r0 and the final state is sf in this
case.

JJ J I II G O B ACK F ULL S CREEN C LOSE 107 OF 600


PL April 2, 2020
Regular Expressions to NFAs:5
ε

ε Nr ε r*f
r*0 r0 rf Nr*

Nr∗ Input Symbol


State a · · · ε
r0∗ ∅ · · · {r0, rf∗ }
r0 ··· ··· ···
.. .. .. ..
rf · · · · · · {r0, rf∗ }
rf∗ ∅ ∅ ∅

JJ J I II G O B ACK F ULL S CREEN C LOSE 108 OF 600


PL April 2, 2020
Regular expressions vs. NFAs
• It is obvious that for each regular expression r, the corresponding NFA
Nr is correct by construction i.e.
L(Nr ) = L(r)
• Each regular expression operator
– adds at most 2 new states and
– adds at most 4 new transitions
• Every state of each Nr so constructed has
– either 1 outgoing transition on a symbol from A
– or at most 2 outgoing transitions on ε
• Hence Nr has at most 2|r| states and 4|r| transitions.

JJ J I II G O B ACK F ULL S CREEN C LOSE 109 OF 600


PL April 2, 2020
Example
We construct a NFA for the regular expression (a|b)∗abb.
• Assume the alphabet A = {a, b}.
• We follow the steps of the construction as given in Constructing NFA
to Regular Expressions to NFAs:5
• For ease of understanding we use the regular expression itself (sub-
scripted by 0 and f respectively) to name the two new states created
by the regular expression operator.

JJ J I II G O B ACK F ULL S CREEN C LOSE 110 OF 600


PL April 2, 2020
Example:-6

a0 a af Na

Steps in NFA for (a|b)∗abb

JJ J I II G O B ACK F ULL S CREEN C LOSE 111 OF 600


PL April 2, 2020
Example:-5

a0 a af Na

b0 b bf Nb

Steps in NFA for (a|b)∗abb

JJ J I II G O B ACK F ULL S CREEN C LOSE 112 OF 600


PL April 2, 2020
Example:-4

N a|b

a0 a af ε
ε

a|b 0 a|b f

ε b0 b bf ε

Steps in NFA for (a|b)∗abb

JJ J I II G O B ACK F ULL S CREEN C LOSE 113 OF 600


PL April 2, 2020
Example:-3
ε
N (a|b)*

a0 a af ε
ε
ε
(a|b)*0 ε a|b 0 a|b f (a|b)*f

ε b0 b bf ε

Steps in NFA for (a|b)∗abb

JJ J I II G O B ACK F ULL S CREEN C LOSE 114 OF 600


PL April 2, 2020
Example:-2
ε
N (a|b)* a

a0 a af ε
ε
ε
(a|b)*0 ε a|b 0 a|b f (a|b)*f

ε b0 b bf ε
a
ε

(a|b)* a

Steps in NFA for (a|b)∗abb

JJ J I II G O B ACK F ULL S CREEN C LOSE 115 OF 600


PL April 2, 2020
Example:-1
ε N (a|b)* ab

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

Steps in NFA for (a|b)∗abb

JJ J I II G O B ACK F ULL S CREEN C LOSE 116 OF 600


PL April 2, 2020
Example-final
ε N (a|b)* abb

a0 a af ε
ε

ε
(a|b)*0 ε a|b 0 a|b f (a|b)*f

ε b0 b bf ε
a
ε

(a|b)* abb f b (a|b)* ab b (a|b)* a

Steps in NFA for (a|b)∗abb

JJ J I II G O B ACK F ULL S CREEN C LOSE 117 OF 600


PL April 2, 2020
Extensions
We have provided constructions for only the most basic operators on
regular expressions. Here are some extensions you can attempt
1. Show how to construct a NFA for ranges and multiple ranges of sym-
bols
2. Assuming Nr is a NFA for the regular expression r, how will you con-
struct the NFA Nr+ .
3. Certain languages like Perl allow an operator like r{k, n}, where
L(rm)
[
L(r{k, n}) =
k≤m≤n
Show to construct Nr{k,n} given Nr .
4. Consider a new regular expression operator ˆ defined by
L(ˆr) = A∗ − L(r)
What is the automaton Nˆr given Nr ?
JJ J I II G O B ACK F ULL S CREEN C LOSE 118 OF 600
PL April 2, 2020
Scanning Using NFAs

JJ J I II G O B ACK F ULL S CREEN C LOSE 119 OF 600


PL April 2, 2020
Scanning and Automata
• Scanning is the only phase of the compiler in which
every character of the source program is read
• The scanning phase therefore needs to be defined accurately and ef-
ficiently.
• Accuracy is achieved by regular expression specification of the tokens
• Efficiency implies that the input should not be read more than once.

JJ J I II G O B ACK F ULL S CREEN C LOSE 120 OF 600


PL April 2, 2020
Nondeterminism and Token Recognition
• The three kinds of nondeterminism in the NFA construction are de-
picted in the figure below.
(i) (ii) (iii)

ε a a

ε ε a

(i) It is difficult to know which ε transition to pick without reading any


further input
(ii) For two transitions on the same input symbol a it is difficult to know
which of them would reach a final state on further input.
(iii) Given an input symbol a and an ε transition on the current state it
is impossible to decide which one to take without looking at further
input.

JJ J I II G O B ACK F ULL S CREEN C LOSE 121 OF 600


PL April 2, 2020
Nondeterministic Features
• In general it is impossible to recognize tokens in the presence of non-
determinism without backtracking.
• Hence NFAs are not directly useful for scanning because of the pres-
ence of nondeterminism.
• The nondeterministic feature of the construction of Nr for any regular
expression r is in the ε transitions.
• The ε transitions in any automaton refer to the fact that no input char-
acter is consumed in the transition.
• Backtracking usually means algorithms involving them are very com-
plex and hence inefficient.
• To avoid backtracking, the automaton should be made deterministic

JJ J I II G O B ACK F ULL S CREEN C LOSE 122 OF 600


PL April 2, 2020
From NFA to DFA
• Since the only source of nondeterminism in our construction are the ε,
we need to eliminate them without changing the language recognized
by the automaton.
• Two consecutive ε transitions are the same as one. In fact any number
of ε transitions are the same as one. So as a first step we compute
all finite sequences of ε transitions and collapse them into a single ε
transition.
• Two states q, q 0 are equivalent if there are only ε transitions between
them. This is called the ε-closure of states.

JJ J I II G O B ACK F ULL S CREEN C LOSE 123 OF 600


PL April 2, 2020
ε -Closure
Given a set T of states, then Tε = ε-closure(T ) is the set of states which
either belong to T or can be reached from states belonging to T only
through a sequence of ε transitions.
Algorithm 1 ε-C LOSURE
Require: T a set of states of the NFA
Ensure: Tε = ε-C LOSURE(T ).
1. U := T
2. repeat
3. U old := U
ε
4. U := U old ∪ {q 0 | q 0 6∈ U, ∃q ∈ U old : q −→ q 0}
5. until U = U old
6. Tε = U
7. return Tε

JJ J I II G O B ACK F ULL S CREEN C LOSE 124 OF 600


PL April 2, 2020
Analysis of ε-Closure
• U can only grow in size through each iteration
• The set U cannot grow beyond the total set of states Q which is finite.
Hence the algorithm always terminates for any NFA N .
• Time complexity: O(|Q|).

JJ J I II G O B ACK F ULL S CREEN C LOSE 125 OF 600


PL April 2, 2020
Recognition using NFA
The following algorithm may be used to recognize a string using a NFA.
Algorithm 2 Recognition using NFA
Require: A string x ∈ A∗ .
Ensure: Boolean
S := ε-C LOSURE({q0}).
a := nextchar(x)
while a 6= end of string do
a
S := ε-C LOSURE(S −→)
a := nextchar(x)
end while
return S ∩ F 6= ∅

In the above algorithm we extend our notation for targets of transitions


to include sets of sources. Thus
a a
S −→= {q 0 | ∃q ∈ S : q −→ q 0}

JJ J I II G O B ACK F ULL S CREEN C LOSE 126 OF 600


PL April 2, 2020
Analysis of Recognition using NFA
• Even if ε-closure is computed as a call from within the algorithm, the
time taken to recognize a string is bounded by O(|x|.|QNr |) where
|QNr | is the number of states in Nr .
• The space required for the automaton is at most O(|r|).
• Given that ε-closure of each state can be pre-computed knowing the
NFA, the recognition algorithm can run in time linear in the length of
the input string x i.e. O(|x|).
• Knowing that the above algorithm is deterministic once ε-closures are
pre-computed one may then work towards a Deterministic automaton
to reduce the space required.

JJ J I II G O B ACK F ULL S CREEN C LOSE 127 OF 600


PL April 2, 2020
JJ J I II G O B ACK F ULL S CREEN C LOSE 128 OF 600
PL April 2, 2020
JJ J I II G O B ACK F ULL S CREEN C LOSE 129 OF 600
PL April 2, 2020
5.3. Deterministic Finite Automata (DFA)

Conversion of NFAs to DFAs

JJ J I II G O B ACK F ULL S CREEN C LOSE 130 OF 600


PL April 2, 2020
Deterministic Finite Automata
• A deterministic finite automaton (DFA) is a NFA in which
1. there are no transitions on ε and
2. −→ yields a at most one target state for each source state and sym-
bol from A i.e. the transition relation is no longer a relation but a
functiona
δ :Q×A→Q
• Clearly if every regular expression had a DFA which accepts the same
language, all backtracking could be avoided.
a
Also in the case of the NFA the relation −→ may not define a transition from every state on every letter

JJ J I II G O B ACK F ULL S CREEN C LOSE 131 OF 600


PL April 2, 2020
Transition Tables of NFAs
We may think of a finite-state automaton as being defined by a 2-
dimensional table of size |Q| × |A| in which for each state and each letter
of the alphabet there is a set of possible target states defined. In the
case of a non-deterministic automaton,
1. for each state there could be ε transitions to
(a) a set consisting of a single state or
(b) a set consisting of more than one state.
2. for each state q and letter a, there could be
(a) an empty set of target states or
(b) a set of target states consisting of a single state or
(c) a set of target states consisting of more than one state

JJ J I II G O B ACK F ULL S CREEN C LOSE 132 OF 600


PL April 2, 2020
Transition Tables of DFAs
In the case of a deterministic automaton
1. there are no ε transitions, and
2. for each state q and letter a
(a) either there is no transition (in which case we add a new “sink” state
which is a non-accepting state)
(b) or there is a transition to a unique state q 0.
The recognition problem for the same language of strings becomes sim-
pler and would work faster (it would have no back-tracking) if the NFA
could be converted into a DFA accepting the same language.

JJ J I II G O B ACK F ULL S CREEN C LOSE 133 OF 600


PL April 2, 2020
NFA to DFA
Let N = hQN , A ∪ {ε}, sN , FN , −→N i be a NFA with
• QN the set of states of the NFA
• A the alphabet
• sN ∈ QN the start state of the NFA
• FN ⊆ QN the accepting states of the NFA and
• −→N ⊆ QN × A × QN the transition relation.
We would like to construct a DFA D = hQD , A, sD , FD , −→D i where
• QD the set of states of the DFA
• A the alphabet
• sD ∈ QD the start state of the DFA
• FD the final or accepting states of the DFA and
• δD : QD × A −→ QD the transition function of the DFA.
We would like L(N ) = L(D)
JJ J I II G O B ACK F ULL S CREEN C LOSE 134 OF 600
PL April 2, 2020
The Subset Construction
• The ε-closure of each NFA state is a set of NFA states with “similar”
behaviour, since they make their transitions on the same input symbols
though with different numbers of εs.
• Each state of the DFA refers to a subset of states of the NFA which
exhibit “similar” behaviour. Similarity of behaviour refers to the fact that
they accept the same input symbols. The behaviour of two different
NFA states may not be “identical” because they may have different
numbers of ε transitions for the same input symbol.
• A major source of non-determinism is the presence of ε transitions.
The use of ε-C LOSURE creates a cluster of similar states.
• Since the notion of acceptance of a string by an automaton, implies
finding an accepting sequence even though there may be other non-
accepting sequences, the non-accepting sequences may be ignored
and those non-accepting states may be clustered with the accepting
states of the NFA. So two different states reachable by the same se-
quence of symbols may be also thought to be similar.
JJ J I II G O B ACK F ULL S CREEN C LOSE 135 OF 600
PL April 2, 2020
NFA to DFA construction
Algorithm 3 Construction of DFA from NFA
Require: NFA N = hQN , A ∪ {ε}, sN , FN , −→N i
Ensure: DFA D = hQD , A, sD , FD , δD i with L(N ) = L(D)
1. sD := ε-C LOSURE({sN });
2. QD := {sD }; FD := ∅; δD := ∅
3. U := {sD } {U is the set of unvisited states of the DFA}
4. while U 6= ∅ do
5. Choose any qD ∈ U ; U := U − {qD }
6. for all a ∈ A do
a
7. qD0 := ε-C LOSURE(qD −→N ) {Note: qD ⊆ QN }
8. δD (qD , a) := qD0
0
9. if qD ∩ FN 6= ∅ then
10. FD := FD ∪ {qD0 }
11. end if
0
12. if qD 6∈ QD then
13. QD := QD ∪ {qD0 }
14. U := U ∪ {qD0 }
15. end if
16. end for
17. end while

JJ J I II G O B ACK F ULL S CREEN C LOSE 136 OF 600


PL April 2, 2020
Example-NFA
Consider the NFA constructed for the regular expression (a|b)∗abb.
ε N
(a|b)*abb

a 4 ε
ε 2

ε ε
0 1 6 7

ε b
3 5 ε
a
ε

10 b b
9 8

and apply the NFA to DFA construction algorithm

JJ J I II G O B ACK F ULL S CREEN C LOSE 137 OF 600


PL April 2, 2020
Determinising
N(a|b)∗abb D(a|b)∗abb
EC0 = ε-C LOSURE(0) = {0, 1, 2, 3, 7}
a a a
2 −→N 4 and 7 −→N 8. So EC0 −→D ε-C LOSURE(4, 8) = EC4,8. Simi-
larly
b
EC0 −→D ε-C LOSURE(5) = EC5
EC4,8 = ε-C LOSURE(4, 8) = {4, 6, 7, 1, 2, 3, 8}
EC5 = ε-C LOSURE(5) = {5, 6, 7, 1, 2, 3}
a b
EC5 −→D ε-C LOSURE(4, 8) = EC4,8 and EC5 −→D ε-C LOSURE(5)
a b
EC4,8 −→D ε-C LOSURE(4, 8) = EC4,8 and EC4,8 −→D
ε-C LOSURE(5, 9) = EC5,9
EC5,9 = ε-C LOSURE(5, 9) = {5, 6, 7, 1, 2, 3, 9}
a b
EC5,9 −→D ε-C LOSURE(4, 8) = EC4,8 and EC5,9 −→D
ε-C LOSURE(5, 10) = EC5,10
EC5,10 = ε-C LOSURE(5, 10) = {5, 6, 7, 1, 2, 3, 10}
a b
EC5,10 −→D ε-C LOSURE(4, 8) and EC5,10 −→ ε-C LOSURE(5)
JJ J I II G O B ACK F ULL S CREEN C LOSE 138 OF 600
PL April 2, 2020
Final DFA
a D (a|b)*abb
a
EC4,8 EC5,9
a
b
EC0 a a b

b b D(a|b)∗abb Input Symbol


EC5 EC5,10
State a b
b EC0 EC4,8 EC5
EC4,8 EC4,8 EC5,9
EC5 EC4,8 EC5
EC5,9 EC4,8 EC5,10
EC5,10 EC4,8 EC5,9
JJ J I II G O B ACK F ULL S CREEN C LOSE 139 OF 600
PL April 2, 2020
Scanning: 5
a−e,g−z
identifier if identifier
f 0−9,a−z 0−9,a−z
2 3 4 0−9

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
~* ~)

The Big Picture

JJ J I II G O B ACK F ULL S CREEN C LOSE 140 OF 600


PL April 2, 2020
Exercise 5.2

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.

JJ J I II G O B ACK F ULL S CREEN C LOSE 142 OF 600


PL April 2, 2020
JJ J I II G O B ACK F ULL S CREEN C LOSE 143 OF 600
PL April 2, 2020
6. Parsing or Syntax Analysis

6.1. Grammars

Parsing Or Syntax Analysis

JJ J I II G O B ACK F ULL S CREEN C LOSE 144 OF 600


PL April 2, 2020
Formal languages: Definition, Recognition,
Generation
There are three different processes used in dealing with a formal lan-
guage.
Definition : Regular expressions is a formal (functional programming)
language used to define or specify a formal language of tokens.
Recognition : Automata are the standard mechanism used to recognize
words/phrases of a formal language. An automaton is used to deter-
mine whether a given word/phrase is a member of the formal language
defined in some other way.
Generation : Grammars are used to define the generation of the word-
s/phrases of a formal language.

JJ J I II G O B ACK F ULL S CREEN C LOSE 145 OF 600


PL April 2, 2020
Non-regular language
Consider the following two languages over an alphabet A = {a, b}.

R = {anbn|n < 100}


P = {anbn|n > 0}
• R may be finitely represented by a regular expression (even though
the actual expression is very long).
• However, P cannot actually be represented by a regular expression
• A regular expression is not powerful enough to represent languages
which require parenthesis matching to arbitrary depths.
• All high level programming languages require an underlying language
of expressions which require parentheses to be nested and matched
to arbitrary depth.

JJ J I II G O B ACK F ULL S CREEN C LOSE 146 OF 600


PL April 2, 2020
JJ J I II G O B ACK F ULL S CREEN C LOSE 147 OF 600
PL April 2, 2020
6.2. Context-Free 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.

JJ J I II G O B ACK F ULL S CREEN C LOSE 148 OF 600


PL April 2, 2020
Grammars: Notation
• Upper case roman letters (A, B, . . . , X, Y , etc.) denote nonterminals.
• Final upper case roman letters (X, Y, Z etc.) may also be used
as meta-variables which denote arbitrary non-terminal symbols of a
grammar.
• Initial lower case roman letters (a, b, c etc.) will be used to denote
terminal symbols.
• Lower case greek letters (α, β etc.) denote sentential forms (or even
sentences).
• Final lower case letters (u, v, . . . , x, y, z etc.) denote only sentences.
• In each case the symbols could also be decorated with sub-scripts or
super-scripts.

JJ J I II G O B ACK F ULL S CREEN C LOSE 149 OF 600


PL April 2, 2020
Context-Free Grammars: Definition
Definition 6.3 A grammar G = hN, T , P, Si is called context-free if each
production is of the form X −→ α, where
• X ∈ N is a nonterminal and
• α ∈ (N ∪ T )∗ is a sentential form.
• The production is terminal if α is a sentence

JJ J I II G O B ACK F ULL S CREEN C LOSE 150 OF 600


PL April 2, 2020
CFG: Example 1
G = h{S}, {a, b}, P, Si, where S −→ ab and S −→ aSb are the only
productions in P .
Derivations look like this:

S ⇒ ab

S ⇒ aSb ⇒ aabb

S ⇒ aSb ⇒ aaSbb ⇒ aaabbb

S ⇒ aSb ⇒ aaSbb ⇒ aaaSbbb
The first three derivations are complete while the last one is partial

JJ J I II G O B ACK F ULL S CREEN C LOSE 151 OF 600


PL April 2, 2020
Derivations
Definition 6.4 A (partial) derivation (of length n ∈ N) in a context-free
grammar is a finite sequence of the form
α0 ⇒ α1 ⇒ α2 ⇒ · · · αn (3)
where each αi ∈ (N ∪ T )∗ (0 ≤ i ≤ n ) is a sentential form where α0 =
S and αi+1 is obtained by applying a production rule to a non-terminal
symbol in αi for 0 ≤ i < n.
Notation. S ⇒∗ α denotes that there exists a derivation of α from S.
Definition 6.5 The derivation (3) is complete if αn ∈ T ∗ i.e. αn is a sen-
tence. Then αn is said to be a sentence generated by the grammar.

JJ J I II G O B ACK F ULL S CREEN C LOSE 152 OF 600


PL April 2, 2020
Language Generation
Definition 6.6 The language generated by a grammar G is the set of sen-
tences that can be generated by G and is denoted L(G).
Example 6.7 L(G), the language generated by the grammar G is
{anbn|n > 0}. Prove using induction on the length of derivations.

JJ J I II G O B ACK F ULL S CREEN C LOSE 153 OF 600


PL April 2, 2020
Regular Grammars
Definition 6.8 A production rule of a context-free grammar is
Right Linear: if it is of the form X −→ a or X −→ aY
Left Linear: if it is of the form X −→ a or X −→ Y a
where a ∈ T and X, Y ∈ N .
Definition 6.9 A regular grammar is a context-free grammar whose pro-
ductions are either only right linear or only left linear.

JJ J I II G O B ACK F ULL S CREEN C LOSE 154 OF 600


PL April 2, 2020
DFA to Regular Grammar
D(a|b)∗abb Input RLG
a D (a|b)*abb
a
State a b Rules
a
A C S A B S → aA|bB
b
S a a b A A C A → aA|bC
b B
b
E B A B B → aA|bB
b C A E C → aA|bE|b
E A C E → aA|bC
Consider the DFA with the states renamed as shown above. We could
easily convert the DFA to a right linear grammar which generates the
language accepted by the DFA.

JJ J I II G O B ACK F ULL S CREEN C LOSE 155 OF 600


PL April 2, 2020
JJ J I II G O B ACK F ULL S CREEN C LOSE 156 OF 600
PL April 2, 2020
CFG: Empty word
G = h{S}, {a, b}, P, Si, where S −→ SS | aSb | ε
generates all sequences of matching nested parentheses, including the
empty word ε.

A leftmost derivation might look like this:

S ⇒ SS ⇒ SSS ⇒ SS ⇒ aSbS ⇒ abS ⇒ abaSb . . .


A rightmost derivation might look like this:

S ⇒ SS ⇒ SSS ⇒ SS ⇒ SaSb ⇒ Sab ⇒ aSbab . . .


Other derivations might look like God alone knows what!
S ⇒ SS ⇒ SSS ⇒ SS ⇒ . . .
Could be quite confusing!

JJ J I II G O B ACK F ULL S CREEN C LOSE 157 OF 600


PL April 2, 2020
CFG: Derivation trees 1
Derivation sequences
• put an artificial order in which productions are fired.
• instead look at trees of derivations in which we may think of produc-
tions as being fired in parallel.
• There is then no highlighting in red to determine which copy of a non-
terminal was used to get the next member of the sequence.
• Of course, generation of the empty word ε must be shown explicitly in
the tree.

JJ J I II G O B ACK F ULL S CREEN C LOSE 158 OF 600


PL April 2, 2020
CFG: Derivation trees 2
S

S S

S S εε

a S b a S b

Derivation tree of
ε a S b
abaabb

JJ J I II G O B ACK F ULL S CREEN C LOSE 159 OF 600


PL April 2, 2020
CFG: Derivation trees 3
S

S S

S S a S b

a S b ε a S b

ε ε
Another
Derivation tree of
abaabb

JJ J I II G O B ACK F ULL S CREEN C LOSE 160 OF 600


PL April 2, 2020
CFG: Derivation trees 4
S

S S

S S a S b

ε a b
a S b S

ε ε
Yet another
Derivation tree of
abaabb

JJ J I II G O B ACK F ULL S CREEN C LOSE 161 OF 600


PL April 2, 2020
JJ J I II G O B ACK F ULL S CREEN C LOSE 162 OF 600
PL April 2, 2020
6.3. Ambiguity

Ambiguity Disambiguation

JJ J I II G O B ACK F ULL S CREEN C LOSE 163 OF 600


PL April 2, 2020
Ambiguity: 1
G1 = h{E, I, C}, {y, z, 4, ∗, +}, P1, {E}i where P1 consists of the following
productions.
E → I | C | E+E | E∗E
I → y | z
C → 4
Consider the sentence y + 4 ∗ z.

E E

JJ J I II G O B ACK F ULL S CREEN C LOSE 164 OF 600


PL April 2, 2020
Ambiguity: 2
G1 = h{E, I, C}, {y, z, 4, ∗, +}, P1, {E}i where P1 consists of the following
productions.
E → I | C | E+E | E∗E
I → y | z
C → 4
Consider the sentence y + 4 ∗ z.

E E

E + E E * E

JJ J I II G O B ACK F ULL S CREEN C LOSE 165 OF 600


PL April 2, 2020
Ambiguity: 3
G1 = h{E, I, C}, {y, z, 4, ∗, +}, P1, {E}i where P1 consists of the following
productions.
E → I | C | E+E | E∗E
I → y | z
C → 4
Consider the sentence y + 4 ∗ z.

E E

E + E E * E

* E E + E
I E I

JJ J I II G O B ACK F ULL S CREEN C LOSE 166 OF 600


PL April 2, 2020
Ambiguity: 4
G1 = h{E, I, C}, {y, z, 4, ∗, +}, P1, {E}i where P1 consists of the following
productions.
E → I | C | E+E | E∗E
I → y | z
C → 4
Consider the sentence y + 4 ∗ z.

E E

E + E E * E

* E E + E
I E I

I I C
y C z

JJ J I II G O B ACK F ULL S CREEN C LOSE 167 OF 600


PL April 2, 2020
Ambiguity: 5
G1 = h{E, I, C}, {y, z, 4, ∗, +}, P1, {E}i where P1 consists of the following
productions.
E → I | C | E+E | E∗E
I → y | z
C → 4
Consider the sentence y + 4 ∗ z.

E E

E + E E * E

* E E + E
I E I

I I C
y C z

z y 4
4

JJ J I II G O B ACK F ULL S CREEN C LOSE 168 OF 600


PL April 2, 2020
Left-most Derivation 1
Left-most derivation of y+4*z corresponding to the first derivation tree.
E ⇒
E+E ⇒
I+E ⇒
y+E ⇒
y+E∗E ⇒
y+C∗E ⇒
y+4∗E ⇒
y+4∗I ⇒
y+4∗z

JJ J I II G O B ACK F ULL S CREEN C LOSE 169 OF 600


PL April 2, 2020
Left-most Derivation 2
Left-most derivation of y+4*z corresponding to the second derivation
tree.
E ⇒
E∗E ⇒
E+E∗E ⇒
I+E∗E ⇒
y+E∗E ⇒
y+C∗E ⇒
y + 4∗E ⇒
y + 4∗I ⇒
y+4∗z

JJ J I II G O B ACK F ULL S CREEN C LOSE 170 OF 600


PL April 2, 2020
Right-most Derivation 1
Right-most derivation of y+4*z corresponding to the first derivation tree.
E ⇒
E+E ⇒
E+E∗E ⇒
E+E∗I ⇒
E+E∗z ⇒
E+C∗z ⇒
E+4 ∗ z ⇒
I+4 ∗ z ⇒
y+4∗z

JJ J I II G O B ACK F ULL S CREEN C LOSE 171 OF 600


PL April 2, 2020
Right-most Derivation 2
Right-most derivation of y+4*z corresponding to the second derivation
tree.
E ⇒
E∗E ⇒
E∗I ⇒
E∗z ⇒
E+E∗z ⇒
E+C∗z ⇒
E+4 ∗ z ⇒
I+4 ∗ z ⇒
y+4∗z

JJ J I II G O B ACK F ULL S CREEN C LOSE 172 OF 600


PL April 2, 2020
Characterizing Ambiguity
The following statements are equivalent.
• A CFG is ambiguous if some sentence it generates has more than one
derivation tree
• A CFG is ambiguous if there is a some sentence it generates with
more than one left-most derivation
• A CFG is ambiguous if there is a some sentence it generates with
more than one right-most derivation

JJ J I II G O B ACK F ULL S CREEN C LOSE 173 OF 600


PL April 2, 2020
Disambiguation
The only way to remove ambiguity (without changing the language
generated) is to change the grammar by introducing some more non-
terminal symbols and changing the production rules. Consider the gram-
mar G01 = hN 0, {y, z, 4, ∗, +}, P 0, {E}i where N 0 = N ∪ {T, F } with the
following production rules P 0.
E → E+T | T
T → T ∗F | F
F → I|C
I → y|z
C → 4
and compare it with the grammar G1

JJ J I II G O B ACK F ULL S CREEN C LOSE 174 OF 600


PL April 2, 2020
Left-most Derivation 1’
The left-most derivation of y+4*z is then as follows.
E ⇒
E+T ⇒
I+T ⇒
y+T ⇒
y+T ∗F ⇒
y+T ∗F ⇒
y+F ∗F ⇒
y+C∗F ⇒
y+4∗F ⇒
y+4∗I ⇒
y+4∗z

JJ J I II G O B ACK F ULL S CREEN C LOSE 175 OF 600


PL April 2, 2020
Left-most Derivations
Compare it with the Left-most Derivation 1.
G1. E ⇒ E+E ⇒ I+E ⇒ y+E ⇒ y+E∗E ⇒
y+C∗E ⇒ y+4∗E ⇒ y+4∗I ⇒ y + 4 ∗ z
G01. E ⇒ E+T ⇒ I+T ⇒ y+T ⇒ y+T ∗F ⇒ y+T ∗F ⇒ y+F ∗F ⇒
y+C∗F ⇒ y+4∗F ⇒ y+4∗I ⇒ y + 4 ∗ z
There is no derivation in G01 corresponding to Left-most Derivation 2 (Why
not?).

JJ J I II G O B ACK F ULL S CREEN C LOSE 176 OF 600


PL April 2, 2020
Right-most Derivation 1’
Right-most derivation of y+4*z corresponding to the first derivation tree.
E ⇒
E+T ⇒
E+T ∗F ⇒
E+T ∗I ⇒
E+T ∗z ⇒
E+C∗z ⇒
E+4 ∗ z ⇒
F +4 ∗ z ⇒
I+4 ∗ z ⇒
+4 ∗ z ⇒
y+4∗z
Compare it with the Right-most Derivation 1.
There is no derivation corresponding to Right-most Derivation 2.

JJ J I II G O B ACK F ULL S CREEN C LOSE 177 OF 600


PL April 2, 2020
Disambiguation by Parenthesization
Another method of disambiguating a language is to change the language
generated, by introducing suitable bracketing mechanisms.
Example 6.10 Compare the following fully parenthesized grammar G2
(which has the extra terminal symbols ( and )) with the grammar G1
without parentheses
E → I | C | (E+E) | (E∗E)
I → y | z
C → 4
Though unambiguous, the language defined by this grammar is different
from that of the original grammar without parentheses.

JJ J I II G O B ACK F ULL S CREEN C LOSE 178 OF 600


PL April 2, 2020
Associativity and Precedence
The grammar G01 implements
Precedence. ∗ has higher precedence than +.
Associativity. ∗ and + are both left associative operators.

JJ J I II G O B ACK F ULL S CREEN C LOSE 179 OF 600


PL April 2, 2020
Exercise 6.1

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.

JJ J I II G O B ACK F ULL S CREEN C LOSE 181 OF 600


PL April 2, 2020
JJ J I II G O B ACK F ULL S CREEN C LOSE 182 OF 600
PL April 2, 2020
6.4. Shift-Reduce Parsing

Introduction to Parsing

JJ J I II G O B ACK F ULL S CREEN C LOSE 183 OF 600


PL April 2, 2020
Overview of Parsing
Since
• parsing requires the checking whether a given token stream conforms
to the rules of the grammar and
• since a context-free grammar may generate an infinite number of dif-
ferent strings
any parsing method should be guided by the given input (token) string,
so that a deterministic strategy may be evolved.

JJ J I II G O B ACK F ULL S CREEN C LOSE 184 OF 600


PL April 2, 2020
Parsing Methods
Two kinds of parsing methods
Top-down parsing Try to generate the given input sentence from the start
symbol of the grammar by applying the production rules.
Bottom-up parsing Try to reduce the given input sentence to the start
symbol by applying the rules in reverse
In general top-down parsing requires long look-aheads in order to do
a deterministic guess from the given input token stream. On the other
hand bottom-up parsing yields better results and can be automated by
software tools.

JJ J I II G O B ACK F ULL S CREEN C LOSE 185 OF 600


PL April 2, 2020
Reverse of Right-most Derivations
The result of a Bottom-Up Parsing technique is usually to produce a
reverse of the right-most derivation of a sentence.
Example For the ambiguous grammar G1 and corresponding to the right-
most derivation 2 we get
y+4∗z ⇐
I+4 ∗ z ⇐
E+4 ∗ z ⇐
E+C∗z ⇐
E+E∗z ⇐
E∗z ⇐
E∗I ⇐
E∗E ⇐
E ⇐

JJ J I II G O B ACK F ULL S CREEN C LOSE 186 OF 600


PL April 2, 2020
Bottom-Up Parsing Strategy
The main problem is to match parentheses of arbitrary nesting depths.
This requires a stack data structure to do the parsing so that unbounded
nested parentheses and varieties of brackets may be matched.
Our basic parsing strategy is going to be based on a technique called
shift-reduce parsing.
shift. Refers to moving the next token from the input token stream into a
parsing stack.
reduce. Refers to applying a production rule in reverse i.e. given a pro-
duction X → α we reduce any occurrence of α in the parsing stack to
X.

JJ J I II G O B ACK F ULL S CREEN C LOSE 187 OF 600


PL April 2, 2020
Fully Bracketed Expression
Consider an example of a fully bracketed expression

JJ J I II G O B ACK F ULL S CREEN C LOSE 188 OF 600


PL April 2, 2020
Parsing: FB0
r1. E E − T
r2 E T
r3 T T / D
r4 T D
r5 D a | b | ( E)

( a − ( a / b ) )

Principle:
Reduce
whenever possible.
Shift only when
reduce is
impossible

Shift

JJ J I II G O B ACK F ULL S CREEN C LOSE 189 OF 600


PL April 2, 2020
Parsing: FB1
r1. E E − T
r2 E T
r3 T T / D
r4 T D
r5 D a | b | ( E)

a − ( a / b ) )

Principle:
Reduce
whenever possible.
Shift only when
reduce is
impossible

(
Shift

JJ J I II G O B ACK F ULL S CREEN C LOSE 190 OF 600


PL April 2, 2020
Parsing: FB2
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

JJ J I II G O B ACK F ULL S CREEN C LOSE 191 OF 600


PL April 2, 2020
Parsing: FB3
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
(
Reduce

JJ J I II G O B ACK F ULL S CREEN C LOSE 192 OF 600


PL April 2, 2020
Parsing: FB4
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
D
(
Reduce

JJ J I II G O B ACK F ULL S CREEN C LOSE 193 OF 600


PL April 2, 2020
Parsing: FB5
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
T
(
Reduce

JJ J I II G O B ACK F ULL S CREEN C LOSE 194 OF 600


PL April 2, 2020
Parsing: FB6
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
E
(
Shift

JJ J I II G O B ACK F ULL S CREEN C LOSE 195 OF 600


PL April 2, 2020
Parsing: FB7
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
E
(
Shift

JJ J I II G O B ACK F ULL S CREEN C LOSE 196 OF 600


PL April 2, 2020
Parsing: FB8
r1. E E − T
r2 E T
r3 T T / D
r4 T D
r5 D a | b | ( E)

/ b ) )

Principle:
Reduce
whenever possible. a
Shift only when (
reduce is

impossible
E
(
Reduce

JJ J I II G O B ACK F ULL S CREEN C LOSE 197 OF 600


PL April 2, 2020
Parsing: FB9
r1. E E − T
r2 E T
r3 T T / D
r4 T D
r5 D a | b | ( E)

/ b ) )

Principle:
Reduce
whenever possible. D
Shift only when (
reduce is

impossible
E
(
Reduce

JJ J I II G O B ACK F ULL S CREEN C LOSE 198 OF 600


PL April 2, 2020
Parsing: FB10
r1. E E − T
r2 E T
r3 T T / D
r4 T D
r5 D a | b | ( E)

/ b ) )

Principle:
Reduce
whenever possible. T
Shift only when (
reduce is

impossible
E
(
Reduce?

JJ J I II G O B ACK F ULL S CREEN C LOSE 199 OF 600


PL April 2, 2020
Parsing: FB11
r1. E E − T
r2 E T
r3 T T / D
r4 T D
r5 D a | b | ( E)

/ b ) )

Principle:
Reduce
whenever possible. T
Shift only when (
reduce is

impossible
E
(
Reduce? Shift

JJ J I II G O B ACK F ULL S CREEN C LOSE 200 OF 600


PL April 2, 2020
Parsing: FB12
r1. E E − T
r2 E T
r3 T T / D
r4 T D
r5 D a | b | ( E)

b ) )

Principle:
Reduce /
whenever possible. T
Shift only when (
reduce is

impossible
E
(
Shift

JJ J I II G O B ACK F ULL S CREEN C LOSE 201 OF 600


PL April 2, 2020
Parsing: FB13
r1. E E − T
r2 E T
r3 T T / D
r4 T D
r5 D a | b | ( E)

) )

Principle: b
Reduce /
whenever possible. T
Shift only when (
reduce is

impossible
E
(
Reduce

JJ J I II G O B ACK F ULL S CREEN C LOSE 202 OF 600


PL April 2, 2020
Parsing: FB14
r1. E E − T
r2 E T
r3 T T / D
r4 T D
r5 D a | b | ( E)

) )

Principle: D

Reduce /
whenever possible. T
Shift only when (
reduce is

impossible
E
(
Reduce

JJ J I II G O B ACK F ULL S CREEN C LOSE 203 OF 600


PL April 2, 2020
Parsing: FB15
r1. E E − T
r2 E T
r3 T T / D
r4 T D
r5 D a | b | ( E)

) )

Principle: D
Reduce /
whenever possible. T
Shift only when (
reduce is

impossible
E
(
Reduce?

JJ J I II G O B ACK F ULL S CREEN C LOSE 204 OF 600


PL April 2, 2020
Parsing: FB16
r1. E E − T
r2 E T
r3 T T / D
r4 T D
r5 D a | b | ( E)

) )

Principle: D
Reduce /
whenever possible. T
Shift only when (
reduce is

impossible
E
(
No, REDUCE!

JJ J I II G O B ACK F ULL S CREEN C LOSE 205 OF 600


PL April 2, 2020
Parsing: FB17
r1. E E − T
r2 E T
r3 T T / D
r4 T D
r5 D a | b | ( E)

) )

Principle:
Reduce
whenever possible. T
Shift only when (
reduce is

impossible
E
(
Reduce?

JJ J I II G O B ACK F ULL S CREEN C LOSE 206 OF 600


PL April 2, 2020
Parsing: FB18
r1. E E − T
r2 E T
r3 T T / D
r4 T D
r5 D a | b | ( E)

) )

Principle:
Reduce
whenever possible. E
Shift only when (
reduce is

impossible
E
(
Shift

JJ J I II G O B ACK F ULL S CREEN C LOSE 207 OF 600


PL April 2, 2020
Parsing: FB19
r1. E E − T
r2 E T
r3 T T / D
r4 T D
r5 D a | b | ( E)

Principle:
)
Reduce
whenever possible. E
Shift only when (
reduce is

impossible
E
(
Reduce

JJ J I II G O B ACK F ULL S CREEN C LOSE 208 OF 600


PL April 2, 2020
Parsing: FB20
r1. E E − T
r2 E T
r3 T T / D
r4 T D
r5 D a | b | ( E)

Principle:
Reduce
whenever possible.
Shift only when D
reduce is

impossible
E
(
Reduce

JJ J I II G O B ACK F ULL S CREEN C LOSE 209 OF 600


PL April 2, 2020
Parsing: FB21
r1. E E − T
r2 E T
r3 T T / D
r4 T D
r5 D a | b | ( E)

Principle:
Reduce
whenever possible.
Shift only when T
reduce is

impossible
E
(
Reduce?

JJ J I II G O B ACK F ULL S CREEN C LOSE 210 OF 600


PL April 2, 2020
Parsing: FB22
r1. E E − T
r2 E T
r3 T T / D
r4 T D
r5 D a | b | ( E)

Principle:
Reduce
whenever possible.
Shift only when T
reduce is

impossible
E
(
No, REDUCE!

JJ J I II G O B ACK F ULL S CREEN C LOSE 211 OF 600


PL April 2, 2020
Parsing: FB23
r1. E E − T
r2 E T
r3 T T / D
r4 T D
r5 D a | b | ( E)

Principle:
Reduce
whenever possible.
Shift only when
reduce is
impossible
E
(
Shift

JJ J I II G O B ACK F ULL S CREEN C LOSE 212 OF 600


PL April 2, 2020
Parsing: FB24
r1. E E − T
r2 E T
r3 T T / D
r4 T D
r5 D a | b | ( E)

Principle:
Reduce
whenever possible.
Shift only when
reduce is
impossible )
E
(
Reduce

JJ J I II G O B ACK F ULL S CREEN C LOSE 213 OF 600


PL April 2, 2020
Parsing: FB25
r1. E E − T
r2 E T
r3 T T / D
r4 T D
r5 D a | b | ( E)

Principle:
Reduce
whenever possible.
Shift only when
reduce is
impossible

D Reduce

JJ J I II G O B ACK F ULL S CREEN C LOSE 214 OF 600


PL April 2, 2020
Parsing: FB26
r1. E E − T
r2 E T
r3 T T / D
r4 T D
r5 D a | b | ( E)

Principle:
Reduce
whenever possible.
Shift only when
reduce is
impossible

T Reduce

JJ J I II G O B ACK F ULL S CREEN C LOSE 215 OF 600


PL April 2, 2020
Parsing: FB27
r1. E E − T
r2 E T
r3 T T / D
r4 T D
r5 D a | b | ( E)

Principle:
Reduce
whenever possible.
Shift only when
reduce is
impossible

E Reduce

JJ J I II G O B ACK F ULL S CREEN C LOSE 216 OF 600


PL April 2, 2020
Unbracketed Expression
Consider an example of an unbracketed expression which relies on the
precedence rules as defined in the grammar.

JJ J I II G O B ACK F ULL S CREEN C LOSE 217 OF 600


PL April 2, 2020
Parsing: UB0

r1. E E − T
r2 E T
r3 T T / D
r4 T D
r5 D a | b | ( E )

a − a / b

JJ J I II G O B ACK F ULL S CREEN C LOSE 218 OF 600


PL April 2, 2020
Parsing: UB1

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

JJ J I II G O B ACK F ULL S CREEN C LOSE 219 OF 600


PL April 2, 2020
Parsing: UB2

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

JJ J I II G O B ACK F ULL S CREEN C LOSE 220 OF 600


PL April 2, 2020
Parsing: UB3

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

JJ J I II G O B ACK F ULL S CREEN C LOSE 221 OF 600


PL April 2, 2020
Parsing: UB4

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

JJ J I II G O B ACK F ULL S CREEN C LOSE 222 OF 600


PL April 2, 2020
Parsing: UB5

r1. E E − T
r2 E T
r3 T T / D
r4 T D
r5 D a | b | ( E )

a / b

− Shift
E

JJ J I II G O B ACK F ULL S CREEN C LOSE 223 OF 600


PL April 2, 2020
Parsing: UB6

r1. E E − T
r2 E T
r3 T T / D
r4 T D
r5 D a | b | ( E )

/ b

a
Shift

E

JJ J I II G O B ACK F ULL S CREEN C LOSE 224 OF 600


PL April 2, 2020
Parsing: UB7

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

JJ J I II G O B ACK F ULL S CREEN C LOSE 225 OF 600


PL April 2, 2020
Parsing: UB8

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

JJ J I II G O B ACK F ULL S CREEN C LOSE 226 OF 600


PL April 2, 2020
Parsing: UB8a

r1. E E − T
r2 E T
r3 T T / D
r4 T D
r5 D a | b | ( E )

/ b

T Reduce by r4

JJ J I II G O B ACK F ULL S CREEN C LOSE 227 OF 600


PL April 2, 2020
Parsing: UB9a

r1. E E − T
r2 E T
r3 T T / D
r4 T D
r5 D a | b | ( E )

/ b

E Reduce by r1

JJ J I II G O B ACK F ULL S CREEN C LOSE 228 OF 600


PL April 2, 2020
Parsing: UB10a

r1. E E − T
r2 E T
r3 T T / D
r4 T D
r5 D a | b | ( E )

/
Shift
E

JJ J I II G O B ACK F ULL S CREEN C LOSE 229 OF 600


PL April 2, 2020
Parsing: UB11a

r1. E E − T
r2 E T
r3 T T / D
r4 T D
r5 D a | b | ( E )

b Shift
/

JJ J I II G O B ACK F ULL S CREEN C LOSE 230 OF 600


PL April 2, 2020
Parsing: UB12a

r1. E E − T
r2 E T
r3 T T / D
r4 T D
r5 D a | b | ( E )

D Reduce by r5
/

JJ J I II G O B ACK F ULL S CREEN C LOSE 231 OF 600


PL April 2, 2020
Parsing: UB13a

r1. E E − T
r2 E T
r3 T T / D
r4 T D
r5 D a | b | ( E )

T
Reduce by r4
/

JJ J I II G O B ACK F ULL S CREEN C LOSE 232 OF 600


PL April 2, 2020
Parsing: UB14a

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

JJ J I II G O B ACK F ULL S CREEN C LOSE 233 OF 600


PL April 2, 2020
Parsing: UB14b

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

JJ J I II G O B ACK F ULL S CREEN C LOSE 234 OF 600


PL April 2, 2020
Parsing: UB13b

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! /

JJ J I II G O B ACK F ULL S CREEN C LOSE 235 OF 600


PL April 2, 2020
Parsing: UB12b

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! /

JJ J I II G O B ACK F ULL S CREEN C LOSE 236 OF 600


PL April 2, 2020
Parsing: UB11b

r1. E E − T
r2 E T
r3 T T / D
r4 T D
r5 D a | b | ( E )

Get back! b Shift


/

JJ J I II G O B ACK F ULL S CREEN C LOSE 237 OF 600


PL April 2, 2020
Parsing: UB10b

r1. E E − T
r2 E T
r3 T T / D
r4 T D
r5 D a | b | ( E )

/
Get back! Shift
E

JJ J I II G O B ACK F ULL S CREEN C LOSE 238 OF 600


PL April 2, 2020
Parsing: UB9b

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

JJ J I II G O B ACK F ULL S CREEN C LOSE 239 OF 600


PL April 2, 2020
Parsing: UB8b

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

JJ J I II G O B ACK F ULL S CREEN C LOSE 240 OF 600


PL April 2, 2020
Parsing: UB8

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

JJ J I II G O B ACK F ULL S CREEN C LOSE 241 OF 600


PL April 2, 2020
Parsing: UB9

r1. E E − T
r2 E T
r3 T T / D
r4 T D
r5 D a | b | ( E )

/ Shift
T

E

JJ J I II G O B ACK F ULL S CREEN C LOSE 242 OF 600


PL April 2, 2020
Parsing: UB10

r1. E E − T
r2 E T
r3 T T / D
r4 T D
r5 D a | b | ( E )

b
Shift
/

T

E

JJ J I II G O B ACK F ULL S CREEN C LOSE 243 OF 600


PL April 2, 2020
Parsing: UB11

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

JJ J I II G O B ACK F ULL S CREEN C LOSE 244 OF 600


PL April 2, 2020
Parsing: UB12

r1. E E − T
r2 E T
r3 T T / D
r4 T D
r5 D a | b | ( E )

T Reduce by r3

JJ J I II G O B ACK F ULL S CREEN C LOSE 245 OF 600


PL April 2, 2020
Parsing: UB13

r1. E E − T
r2 E T
r3 T T / D
r4 T D
r5 D a | b | ( E )

E Reduce by r1

JJ J I II G O B ACK F ULL S CREEN C LOSE 246 OF 600


PL April 2, 2020
JJ J I II G O B ACK F ULL S CREEN C LOSE 247 OF 600
PL April 2, 2020
JJ J I II G O B ACK F ULL S CREEN C LOSE 248 OF 600
PL April 2, 2020
6.5. Bottom-Up Parsing

Bottom-Up Parsing

JJ J I II G O B ACK F ULL S CREEN C LOSE 249 OF 600


PL April 2, 2020
Parse Trees: 0

r1. E E − T
r4 T D
r2 E T
r3 T T / D r5 D a | b | ( E )

a − a / b

JJ J I II G O B ACK F ULL S CREEN C LOSE shift-reduce


250parsing:0
OF 600
PL April 2, 2020
Parse Trees: 1

r1. E E − T
r4 T D
r2 E T
r3 T T / D r5 D a | b | ( E )

a − a / b

JJ J I II G O B ACK F ULL S CREEN C LOSE shift-reduce


251parsing:1
OF 600
PL April 2, 2020
Parse Trees: 2

r1. E E − T
r4 T D
r2 E T
r3 T T / D r5 D a | b | ( E )

a − a / b

JJ J I II G O B ACK F ULL S CREEN C LOSE shift-reduce


252parsing:2
OF 600
PL April 2, 2020
Parse Trees: 3

r1. E E − T
r4 T D
r2 E T
r3 T T / D r5 D a | b | ( E )

a − a / b

JJ J I II G O B ACK F ULL S CREEN C LOSE shift-reduce


253parsing:3
OF 600
PL April 2, 2020
Parse Trees: 3a

r1. E E − T
r4 T D
r2 E T
r3 T T / D r5 D a | b | ( E )

a − a / b

JJ J I II G O B ACK F ULL S CREEN C LOSE shift-reduce


254 OFparsing
600
PL April 2, 2020
Parse Trees: 3b

r1. E E − T
r4 T D
r2 E T
r3 T T / D r5 D a | b | ( E )

a − a / b

JJ J I II G O B ACK F ULL S CREEN C LOSE shift-reduce


255 OFparsing
600
PL April 2, 2020
Parse Trees: 4

r1. E E − T
r4 T D
r2 E T
r3 T T / D r5 D a | b | ( E )

D D

a − a / b

JJ J I II G O B ACK F ULL S CREEN C LOSE shift-reduce


256 OFparsing
600
PL April 2, 2020
Parse Trees: 5

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

JJ J I II G O B ACK F ULL S CREEN C LOSE shift-reduce


257 OFparsing
600
PL April 2, 2020
Parse Trees: 5a

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

JJ J I II G O B ACK F ULL S CREEN C LOSE shift-reduce


258 OFparsing
600
PL April 2, 2020
Parse Trees: 5b

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

JJ J I II G O B ACK F ULL S CREEN C LOSE shift-reduce


259 OFparsing
600
PL April 2, 2020
Parse Trees: 6

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

JJ J I II G O B ACK F ULL S CREEN C LOSE shift-reduce


260 OFparsing
600
PL April 2, 2020
Parse Trees: 7

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

JJ J I II G O B ACK F ULL S CREEN C LOSE shift-reduce


261 OFparsing
600
PL April 2, 2020
Parse Trees: 8

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

JJ J I II G O B ACK F ULL S CREEN C LOSE shift-reduce


262 OFparsing
600
PL April 2, 2020
Parsing: Summary: 1
• All high-level languages are designed so that they may be parsed in
this fashion with only a single token look-ahead.
• Parsers for a language can be automatically constructed by parser-
generators such as Yacc, Bison, ML-Yacc and CUP in the case of
Java.
• Shift-reduce conflicts if any, are automatically detected and reported
by the parser-generator.
• Shift-reduce conflicts may be avoided by suitably redesigning the
context-free grammar.

JJ J I II G O B ACK F ULL S CREEN C LOSE 263 OF 600


PL April 2, 2020
Parsing: Summary: 2
• Very often shift-reduce conflicts may occur because of the prefix prob-
lem. In such cases many parser-generators resolve the conflict in
favour of shifting.
• There is also a possiblility of reduce-reduce conflicts. This usually
happens when there is more than one nonterminal symbol to which
the contents of the stack may reduce.
• A minor reworking of the grammar to avoid redundant non-terminal
symbols will get rid of reduce-reduce conflicts.
The Big Picture

JJ J I II G O B ACK F ULL S CREEN C LOSE 264 OF 600


PL April 2, 2020
JJ J I II G O B ACK F ULL S CREEN C LOSE 265 OF 600
PL April 2, 2020
JJ J I II G O B ACK F ULL S CREEN C LOSE 266 OF 600
PL April 2, 2020
6.6. Simple LR Parsing

Parsing Problems 1
The main question in shift-reduce parsing is:

When to shift and when to reduce?


To answer this question we require
• more information from the input token stream,
• to look at the rest of the input token stream and then take a decision.
But the decision has to be automatic. So the parser requires some rules.
Once given the rules we may construct the parser to follow the rules.

JJ J I II G O B ACK F ULL S CREEN C LOSE 267 OF 600


PL April 2, 2020
Parsing Problems 2
But for a very large program it may be impossible to look at all the input
before taking a decision. So clearly the parser can look at only a limited
amount of the input to take a decision. So
The next question:

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.

JJ J I II G O B ACK F ULL S CREEN C LOSE 268 OF 600


PL April 2, 2020
Parsing Problems 3
Once all the input has been read, the parser should be able to decide
in case of a valid sentence that it should only apply reduction rules and at-
tempt to reach the start symbol of the grammar only through reduc-
tions and
in case of an invalid sentence that a grammatical error has occurred in the
parsing process
To solve this problem we augment every grammar with a new start sym-
bol S and a new terminal token $ and augment the grammar with a new
special rule. For our previous grammar we have the new rule

S → E$

JJ J I II G O B ACK F ULL S CREEN C LOSE 269 OF 600


PL April 2, 2020
Augmented Grammar
Consider the following (simplified) augmented grammar with a single bi-
nary operator − and parenthesis. We also number the rules.
1. S → E$
2. E → E−T
3. E → T
4. T → a
5. T → (E)

JJ J I II G O B ACK F ULL S CREEN C LOSE 270 OF 600


PL April 2, 2020
LR(0) Languages
LR(0) languages are those context-free languages that may be parsed
by taking deterministic shift-reduce decisions only based on the contents
of the parsing stack and without viewing any lookahead.
• “L” refers to reading the input from left to right,
• “R” refers to the (reverse) of rightmost derivation
• “0” refers to no-lookahead..
• Many simple CFLs are LR(0). But the LR(0) parsing method is too
weak for most high-level programming languages.
• But understanding the LR(0) parsing method is most crucial for un-
derstanding other more powerful LR-parsing methods which require
lookaheads for deterministic shift-reduce decision-making

JJ J I II G O B ACK F ULL S CREEN C LOSE 271 OF 600


PL April 2, 2020
LR-Parsing Invariant
In any LR-parsing technique the following invariant holds.
For any syntactically valid sentence generated by the augmented gram-
mar, the concatenation of the stack contents with the rest of the input
gives a sentential form of a rightmost derivation.
Hence given at any stage of the parsing if α ∈ (N ∪ T )∗ is the contents
of the parsing stack and x ∈ T ∗$ is the rest of the input that has not yet
been read, then αx is a sentential form of a right-most derivation.

JJ J I II G O B ACK F ULL S CREEN C LOSE 272 OF 600


PL April 2, 2020
LR(0) Item
An LR(0) item consists of an LR(0) production rule with a special marker
N on the right hand side of rule.
• The marker is different from any of the terminal or nonterminal symbols
of the grammar.
• The marker separates the contents of the stack from the expected
form of some prefix of the rest of the input.
• Given a rule X → α, where X is a nonterminal symbol and α is a
string consisting of terminal and non-terminal symbols, an LR(0) item
is of the form
X → β Nγ
where α = βγ.
• For each rule X → α, there are |α| + 1 distinct LR(0) items – one for
each position in α.

JJ J I II G O B ACK F ULL S CREEN C LOSE 273 OF 600


PL April 2, 2020
What does an LR(0) item signify?
The LR(0) item
X → β Nγ
signifies that at some stage of parsing
• β is the string (of terminals and nonterminals) on the top of the stack
and
• some prefix of the rest of the input can be generated by γ
so that whenever βγ appears on the stack, βγ may be reduced immedi-
ately to X.

JJ J I II G O B ACK F ULL S CREEN C LOSE 274 OF 600


PL April 2, 2020
LR0 Parsing Strategy
The LR0 parsing strategy is to
1. construct a DFA whose alphabet is N ∪ T ∪ {$}
2. use the parsing stack to perform reductions at appropriate points
The LR0 parsing table is hence a DFA with 3 kinds of entries.
shift i in which a terminal symbol is shifted on to the parsing stack and
the DFA moves to state i.
reduce j a reduction using the production rule j is performed
goto k Based on the contents of the stack, the DFA moves to state k.

JJ J I II G O B ACK F ULL S CREEN C LOSE 275 OF 600


PL April 2, 2020
Favourite Example
Consider our favourite augmented grammar
1. S → E$
2. E → E−T
3. E → T
4. T → a
5. T → (E)

JJ J I II G O B ACK F ULL S CREEN C LOSE 276 OF 600


PL April 2, 2020
Rule 1: Items
Rule 1
R1. S → E$
has the following three items
I1.1 S → NE$
I1.2 S → E N$
I1.3 S → E$N
one for each position on the right hand side of the rule.

JJ J I II G O B ACK F ULL S CREEN C LOSE 277 OF 600


PL April 2, 2020
Rule 2: Items
Rule 2
R2. E → E−T
has the following items
I2.1 E → NE−T
I2.2 E → E N−T
I2.3 E → E−NT
I2.4 E → E−T N

JJ J I II G O B ACK F ULL S CREEN C LOSE 278 OF 600


PL April 2, 2020
Rule 3: Items
Rule 3
R3. E → T
has just the items
I3.1 E → NT
I3.2 E → T N

JJ J I II G O B ACK F ULL S CREEN C LOSE 279 OF 600


PL April 2, 2020
Rule 4: Items
Rule 4
R4. T → a
has the items
I4.1 T → Na
I4.2 T → aN

JJ J I II G O B ACK F ULL S CREEN C LOSE 280 OF 600


PL April 2, 2020
Rule 5: Items
Rule 5
R5. T → (E)
has the items
I5.1 T → N(E)
I5.2 T → (NE)
I5.3 T → (E N)
I5.4 T → (E)N

JJ J I II G O B ACK F ULL S CREEN C LOSE 281 OF 600


PL April 2, 2020
Significance of I1.*
I1.1 S → NE$. Hence
1. The parsing stack is empty and
2. the entire input (which has not been read yet) should be reducible to
E followed by the $.
I1.2 S → E N$. Hence
1. E is the only symbol on the parsing stack and
2. the rest of the input consists of the terminating symbol $.
I1.3 S → E$N. Hence
1. There is no input left to be read and
2. the stack contents may be reduced to the start symbol

JJ J I II G O B ACK F ULL S CREEN C LOSE 282 OF 600


PL April 2, 2020
DFA States: Initial and Final
• Clearly the initial state S1 of the DFA will correspond to item I1.1.
• There should be a state corresponding to item I1.2.
• There should be a goto transition on the nonterminal symbol E from
the initial state (corresponding to item I1.1) to the state corresponding
to item I1.2.
• The accepting state of the DFA will correspond to item item I1.3.
• There would also be a shift transition on $ from the state corresponding
to item I1.2 to the accepting state corresponding to item I1.3.
• There should be a reduce action using rule 1 when the DFA reaches
the state corresponding to item I1.3.

JJ J I II G O B ACK F ULL S CREEN C LOSE 283 OF 600


PL April 2, 2020
Input Possibilities
Consider item I1.1.
1. How will a grammatically valid sentence input reduce to E$? From the
grammar it is obvious that this can happen only if the input is of a form
such that
(a) it can be reduced to E−T (recursively) or
(b) it can be reduced to T
2. How can the input be reduced to the form T ?
(a) If the enire input consists of only a then it could be reduced to T or
(b) If the entire input could be reduced to the form (E) then it could be
reduced to T .
3. How can the input be reduced to the form E−T ?
(a) If the entire input could be split into 3 parts α, β and γ such that
i. α is a prefix that can be reduced to E, and
ii. β = −, and
iii. γ is a suffix that can be reduced to T
JJ
then it could be reduced to E−T
J I II
PL April 2, 2020
G O B ACK F ULL S CREEN C LOSE 284 OF 600
Closures of Items
Theoretically each item is a state of a NFA. The above reasoning leads
to forming closures of items to obtain DFA states, in a manner similar to
the the subset construction. Essentially all NFA states with similar initial
behaviours are grouped together to form a single DFA state.
NFA to DFA construction
Algorithm 4 Closures of Items
Require: Set I of LR(0) items of a CFG with rule set P
Ensure: Closure of I for a subset I ⊆ I of items
1. repeat
2. for all A → αN Xβ ∈ I do
3. for all X → γ ∈ P do
4. I := I ∪ {X → Nγ}
5. end for
6. end for
7. until no more changes occur in I

JJ J I II G O B ACK F ULL S CREEN C LOSE 285 OF 600


PL April 2, 2020
State Changes on Nonterminals
As in the case of the NFA to DFA construction with each state transition
we also need to compute closures on the target states.
Algorithm 5 Goto for a set of I of items
Require: I ⊆ I and X ∈ N
Ensure: States of the DFA
1. J := ∅
2. for all A → αN Xβ ∈ I do
3. J := J ∪ {A → αX Nβ}
4. end for
5. return C LOSURE(J )

JJ J I II G O B ACK F ULL S CREEN C LOSE 286 OF 600


PL April 2, 2020
State S1
S1 = C LOSURE({S → NE$})
= {S → NE$, E → NE−T, E → NT,
T → Na, T → N(E)}
(
S1 −→ C LOSURE({T → (NE)}) = S2
E
S1 −→ C LOSURE({S → E N$, E → E N−T }) = S3
T
S1 −→ C LOSURE({E → T N}) = S7
a
S1 −→ C LOSURE({T → aN}) = S8

JJ J I II G O B ACK F ULL S CREEN C LOSE 287 OF 600


PL April 2, 2020
State S2
S2 = C LOSURE({T → (NE)})
= {T → N(E), E → NE−T, E → NT,
T → Na, T → N(E)}
(
S2 −→ C LOSURE({T → (NE)}) = S2
E
S2 −→ C LOSURE({T → (E N), E → E N−T }) = S9
T
S2 −→ C LOSURE({E → T N}) = S7
a
S2 −→ C LOSURE({T → aN}) = S8

JJ J I II G O B ACK F ULL S CREEN C LOSE 288 OF 600


PL April 2, 2020
Other States
S3 = C LOSURE({S → E N$, E → E N−T })
= {S → E N$, E → E N−T }
However,

S3 −→ C LOSURE({E → E−NT })
and
C LOSURE({E → E−NT })
= {E → E−NT, T → (NE), T → Na}
= S4
The closures of the other reachable sets of items are themselves.
• S5 = {E → E−T N}
• S6 = {S → E$N}
• S7 = {E → T N}
• S8 = {T → aN}
• S9 = {T → (E N), E → E N−T }
• S10 = {T → (E)N}
JJ J I II G O B ACK F ULL S CREEN C LOSE 289 OF 600
PL April 2, 2020
Example: DFA
Parsing Table

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)

JJ J I II G O B ACK F ULL S CREEN C LOSE 290 OF 600


PL April 2, 2020
Example: Parsing Table
DFA

States Input Nonterminals


a ( ) $ − S E T
S1 S8 S2 G3 G7
S2 S8 S2 G9 G7
S3 ACC S4
S4 S8 S2 G5
S5 R2 R2
R2 R2 R2
S6 R1 R1
R1 R1 R1
S7 R3 R3
R3 R3 R3
S8 R4 R4
R4 R4 R4
S9 S10 S4
S10 R5 R5 R5 R5 R5
Note: All empty entries denote errors

JJ J I II G O B ACK F ULL S CREEN C LOSE 291 OF 600


PL April 2, 2020
Example 6.11 Consider the following simple input viz. a$. Here are the parsing steps.
DFA Parsing Table

JJ J I II G O B ACK F ULL S CREEN C LOSE 292 OF 600


PL April 2, 2020
Example 6.11 Consider the following simple input viz. a$. Here are the parsing steps.
DFA Parsing Table

S1 a$ Shift S8

JJ J I II G O B ACK F ULL S CREEN C LOSE 292 OF 600


PL April 2, 2020
Example 6.11 Consider the following simple input viz. a$. Here are the parsing steps.
DFA Parsing Table

S1 a$ Shift S8

S1 a S8 $ Reduce Rule 4

JJ J I II G O B ACK F ULL S CREEN C LOSE 292 OF 600


PL April 2, 2020
Example 6.11 Consider the following simple input viz. a$. Here are the parsing steps.
DFA Parsing Table

S1 a$ Shift S8

S1 a S8 $ Reduce Rule 4

S1 T $ Goto S7

JJ J I II G O B ACK F ULL S CREEN C LOSE 292 OF 600


PL April 2, 2020
Example 6.11 Consider the following simple input viz. a$. Here are the parsing steps.
DFA Parsing Table

S1 a$ Shift S8

S1 a S8 $ Reduce Rule 4

S1 T $ Goto S7

S1 T S7 $ Reduce Rule 3

JJ J I II G O B ACK F ULL S CREEN C LOSE 292 OF 600


PL April 2, 2020
Example 6.11 Consider the following simple input viz. a$. Here are the parsing steps.
DFA Parsing Table

S1 a$ Shift S8

S1 a S8 $ Reduce Rule 4

S1 T $ Goto S7

S1 T S7 $ Reduce Rule 3

S1 E $ Goto S3

JJ J I II G O B ACK F ULL S CREEN C LOSE 292 OF 600


PL April 2, 2020
Example 6.11 Consider the following simple input viz. a$. Here are the parsing steps.
DFA Parsing Table

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

JJ J I II G O B ACK F ULL S CREEN C LOSE 292 OF 600


PL April 2, 2020
Example 6.12 Here is a slightly more complex input a − (a − a)$.
DFA Parsing Table

JJ J I II G O B ACK F ULL S CREEN C LOSE 293 OF 600


PL April 2, 2020
Example 6.12 Here is a slightly more complex input a − (a − a)$.
DFA Parsing Table

S1 a − (a − a)$ Shift S8

JJ J I II G O B ACK F ULL S CREEN C LOSE 293 OF 600


PL April 2, 2020
Example 6.12 Here is a slightly more complex input a − (a − a)$.
DFA Parsing Table

S1 a − (a − a)$ Shift S8

S1 a S8 −(a − a)$ Reduce Rule 4

JJ J I II G O B ACK F ULL S CREEN C LOSE 293 OF 600


PL April 2, 2020
Example 6.12 Here is a slightly more complex input a − (a − a)$.
DFA Parsing Table

S1 a − (a − a)$ Shift S8

S1 a S8 −(a − a)$ Reduce Rule 4

S1 T −(a − a)$ Go to S7

JJ J I II G O B ACK F ULL S CREEN C LOSE 293 OF 600


PL April 2, 2020
Example 6.12 Here is a slightly more complex input a − (a − a)$.
DFA Parsing Table

S1 a − (a − a)$ Shift S8

S1 a S8 −(a − a)$ Reduce Rule 4

S1 T −(a − a)$ Go to S7

S1 T S7 −(a − a)$ Reduce Rule 3

JJ J I II G O B ACK F ULL S CREEN C LOSE 293 OF 600


PL April 2, 2020
Example 6.12 Here is a slightly more complex input a − (a − a)$.
DFA Parsing Table

S1 a − (a − a)$ Shift S8

S1 a S8 −(a − a)$ Reduce Rule 4

S1 T −(a − a)$ Go to S7

S1 T S7 −(a − a)$ Reduce Rule 3

S1 E −(a − a)$ Go to S3

JJ J I II G O B ACK F ULL S CREEN C LOSE 293 OF 600


PL April 2, 2020
Example 6.12 Here is a slightly more complex input a − (a − a)$.
DFA Parsing Table

S1 a − (a − a)$ Shift S8

S1 a S8 −(a − a)$ Reduce Rule 4

S1 T −(a − a)$ Go to S7

S1 T S7 −(a − a)$ Reduce Rule 3

S1 E −(a − a)$ Go to S3

S1 E S3 −(a − a)$ Shift S4

JJ J I II G O B ACK F ULL S CREEN C LOSE 293 OF 600


PL April 2, 2020
Example 6.12 Here is a slightly more complex input a − (a − a)$.
DFA Parsing Table

S1 a − (a − a)$ Shift S8

S1 a S8 −(a − a)$ Reduce Rule 4

S1 T −(a − a)$ Go to S7

S1 T S7 −(a − a)$ Reduce Rule 3

S1 E −(a − a)$ Go to S3

S1 E S3 −(a − a)$ Shift S4

S1 E S3 − S4 (a − a)$ Shift S2

JJ J I II G O B ACK F ULL S CREEN C LOSE 293 OF 600


PL April 2, 2020
Example 6.12 Here is a slightly more complex input a − (a − a)$.
DFA Parsing Table

S1 a − (a − a)$ Shift S8

S1 a S8 −(a − a)$ Reduce Rule 4

S1 T −(a − a)$ Go to S7

S1 T S7 −(a − a)$ Reduce Rule 3

S1 E −(a − a)$ Go to S3

S1 E S3 −(a − a)$ Shift S4

S1 E S3 − S4 (a − a)$ Shift S2

S1 E S3 − S4 ( S2 a − a)$ Shift S8

JJ J I II G O B ACK F ULL S CREEN C LOSE 293 OF 600


PL April 2, 2020
Example 6.12 Here is a slightly more complex input a − (a − a)$.
DFA Parsing Table

S1 a − (a − a)$ Shift S8

S1 a S8 −(a − a)$ Reduce Rule 4

S1 T −(a − a)$ Go to S7

S1 T S7 −(a − a)$ Reduce Rule 3

S1 E −(a − a)$ Go to S3

S1 E S3 −(a − a)$ Shift S4

S1 E S3 − S4 (a − a)$ Shift S2

S1 E S3 − S4 ( S2 a − a)$ Shift S8

S1 E S3 − S4 ( S2 a S8 −a)$ Reduce Rule 4

JJ J I II G O B ACK F ULL S CREEN C LOSE 293 OF 600


PL April 2, 2020
Example 6.12 Here is a slightly more complex input a − (a − a)$.
DFA Parsing Table

S1 a − (a − a)$ Shift S8

S1 a S8 −(a − a)$ Reduce Rule 4

S1 T −(a − a)$ Go to S7

S1 T S7 −(a − a)$ Reduce Rule 3

S1 E −(a − a)$ Go to S3

S1 E S3 −(a − a)$ Shift S4

S1 E S3 − S4 (a − a)$ Shift S2

S1 E S3 − S4 ( S2 a − a)$ Shift S8

S1 E S3 − S4 ( S2 a S8 −a)$ Reduce Rule 4

S1 E S3 − S4 ( S2 T −a)$ Go to S7

JJ J I II G O B ACK F ULL S CREEN C LOSE 293 OF 600


PL April 2, 2020
Example 6.12 Here is a slightly more complex input a − (a − a)$.
DFA Parsing Table

S1 a − (a − a)$ Shift S8

S1 a S8 −(a − a)$ Reduce Rule 4

S1 T −(a − a)$ Go to S7

S1 T S7 −(a − a)$ Reduce Rule 3

S1 E −(a − a)$ Go to S3

S1 E S3 −(a − a)$ Shift S4

S1 E S3 − S4 (a − a)$ Shift S2

S1 E S3 − S4 ( S2 a − a)$ Shift S8

S1 E S3 − S4 ( S2 a S8 −a)$ Reduce Rule 4

S1 E S3 − S4 ( S2 T −a)$ Go to S7

S1 E S3 − S4 ( S2 T S7 −a)$ Reduce Rule 3

JJ J I II G O B ACK F ULL S CREEN C LOSE 293 OF 600


PL April 2, 2020
Example 6.12 Here is a slightly more complex input a − (a − a)$.
DFA Parsing Table

S1 a − (a − a)$ Shift S8

S1 a S8 −(a − a)$ Reduce Rule 4

S1 T −(a − a)$ Go to S7

S1 T S7 −(a − a)$ Reduce Rule 3

S1 E −(a − a)$ Go to S3

S1 E S3 −(a − a)$ Shift S4

S1 E S3 − S4 (a − a)$ Shift S2

S1 E S3 − S4 ( S2 a − a)$ Shift S8

S1 E S3 − S4 ( S2 a S8 −a)$ Reduce Rule 4

S1 E S3 − S4 ( S2 T −a)$ Go to S7

S1 E S3 − S4 ( S2 T S7 −a)$ Reduce Rule 3

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

JJ J I II G O B ACK F ULL S CREEN C LOSE 294 OF 600


PL April 2, 2020
DFA Parsing Table

JJ J I II G O B ACK F ULL S CREEN C LOSE 295 OF 600


PL April 2, 2020
DFA Parsing Table

S1 E S3 − S4 ( S2 E S9 −a)$ Shift S4

JJ J I II G O B ACK F ULL S CREEN C LOSE 295 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 − S4 a)$ Shift S8

JJ J I II G O B ACK F ULL S CREEN C LOSE 295 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 − S4 a)$ Shift S8

S1 E S3 − S4 ( S2 E S9 − S4 a S8 )$ Reduce Rule 4

JJ J I II G O B ACK F ULL S CREEN C LOSE 295 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 − 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

JJ J I II G O B ACK F ULL S CREEN C LOSE 295 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 − 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

JJ J I II G O B ACK F ULL S CREEN C LOSE 295 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 − 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

JJ J I II G O B ACK F ULL S CREEN C LOSE 295 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 − 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

JJ J I II G O B ACK F ULL S CREEN C LOSE 295 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 − 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 ) S10 $ Reduce Rule 5

JJ J I II G O B ACK F ULL S CREEN C LOSE 295 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 − 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 ) S10 $ Reduce Rule 5

S1 E S3 − S4 T $ Go to S5

JJ J I II G O B ACK F ULL S CREEN C LOSE 295 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 − 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 ) S10 $ Reduce Rule 5

S1 E S3 − S4 T $ Go to S5

S1 E S3 − S4 T S5 $ Reduce Rule 2

JJ J I II G O B ACK F ULL S CREEN C LOSE 295 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 − 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 ) S10 $ Reduce Rule 5

S1 E S3 − S4 T $ Go to S5

S1 E S3 − S4 T S5 $ Reduce Rule 2

S1 E $ Go to S3

JJ J I II G O B ACK F ULL S CREEN C LOSE 295 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 − 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 ) S10 $ Reduce Rule 5

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

JJ J I II G O B ACK F ULL S CREEN C LOSE 295 OF 600


PL April 2, 2020
Exercise 6.2

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.

JJ J I II G O B ACK F ULL S CREEN C LOSE 296 OF 600


PL April 2, 2020
CFG = RLG + Bracket Matching
We use the idea that a context-free grammar is essentially a regular
grammar with parentheses matching to arbitrary depths. Hence a DFA
with some reductions introduced may work.
We modify the grammar to have a special terminal symbol called the
end-marker (denoted by $). Now consider the following simple grammar
with a single right-associative binary operator ˆ and bracket-matching.
We create a DFA of “items” which also have a special marker called the
“cursor” (N).

JJ J I II G O B ACK F ULL S CREEN C LOSE 297 OF 600


PL April 2, 2020
LR(0) with Right-Association
Consider the following grammar
1. S → E$
2. E → P ˆE
3. E → P
4. P → a
5. P → (E)
The following items make up the initial state S1 of the DFA
I1.1 S → NE$
I2.1 E → NP ˆE
I3.1 E → NP
I4.1 P → Na
I5.1 P → N(E)

JJ J I II G O B ACK F ULL S CREEN C LOSE 298 OF 600


PL April 2, 2020
Shift-Reduce Conflicts in LR(0)
There is a transition on the nonterminal P to the state S2 which is made
up of the following items.
I2.2 E → P NˆE
I3.2 E → P N
Then clearly the LR(0) parser suffers a shift-reduce conflict because
• item I2.2 indicates a shift action,
• item I3.2 produces a reduce action
This in contrast to the parsing table produced earlier where reduce ac-
tions took place regardless of the input symbol. Clearly now that princi-
ple will have to be modified.
The parsing table in this case would have a shift action if the input in
state S2 is a ˆ and a reduce action for all other input symbols.

JJ J I II G O B ACK F ULL S CREEN C LOSE 299 OF 600


PL April 2, 2020
FOLLOW Sets
We construct for each non-terminal symbol a set of terminal symbols that
can follow this non-terminal in any rightmost derivation. In the previous
grammar we have
FOLLOW(E) = {$, )}
FOLLOW(P ) = {ˆ}
Depending upon the input symbol and whether it appears in the FOL-
LOW set of the non-terminal under question we resolve the shift-reduce
conflict.
This modification to LR(0) is called Simple LR (SLR) parsing method.
However SLR is not powerful enough for many useful grammar construc-
tions that are encountered in many programming languages.

JJ J I II G O B ACK F ULL S CREEN C LOSE 300 OF 600


PL April 2, 2020
Computing FIRST Sets
In order to compute FOLLOW sets we require FIRST sets of sentential
forms to be constructed too.
1. FIRST (a) = {a} for every terminal symbol a.
2. ε ∈ FIRST(X) if X → ε ∈ P .
3. If X → Y1Y2 · · · Yk ∈ P then FIRST(Y1) ⊆ FIRST(X)
4. If X → Y1Y2 · · · Yk ∈ P then for each i : i < k such that Y1Y2 · · · Yi ⇒ ε,
FIRST(Yi+1) ⊆ FIRST(X).

JJ J I II G O B ACK F ULL S CREEN C LOSE 301 OF 600


PL April 2, 2020
Computing FOLLOW Sets
Once FIRST has been computed, computing FOLLOW for each non-
terminal symbol is quite easy.
1. $ ∈ FOLLOW(S) where S is the start symbol of the augmented gram-
mar.
2. For each production rule of the form A → αBβ, FIRST(β) − {ε} ⊆
FOLLOW(B).
3. For each production rule of the form A → αBβ, if ε ∈ FIRST(β) then
FOLLOW(A) ⊆ FOLLOW(B).
4. For each production of the form A → αB, FOLLOW(A) ⊆
FOLLOW(B).

JJ J I II G O B ACK F ULL S CREEN C LOSE 302 OF 600


PL April 2, 2020
if-then-else vs. if-then
Most programming languages have two separate constructs if-then
and if-then-else. We abbreviate the keywords and use the following
symbols
Tokens Symbols
if i
then t
else e
booleans b
other expressions a
and construct the following two augmented grammars G1 and G2.
11 . S → I$ 12 . S → I$
21 . I → U 22 . I → ibtIE
31 . I → M 32 . I → a
41 . U → ibtI 42 . E → eI
51 . U → ibtM eU 52 . E → ε
61 . M → ibtM eM
JJ J
71 . M
I
→ aII
PL April 2, 2020
G O B ACK F ULL S CREEN C LOSE 303 OF 600
Problems in LR parsing
1. Prove that grammar G2 is ambiguous.
2. Construct the LR(0) parsing tables for both G1 and G2 and find all
shift-reduce conflicts in the parsing table.
3. Construct the FOLLOW sets in each case and try to resolve the con-
flicts.
4. Show that the following augmented grammar cannot be parsed (i.e.
there are conflicts that cannot be resolved by FOLLOW sets) either by
LR(0) or SLR parsers. (Hint First construct the LR(0) DFA).
1. S → E$
2. E → L=R
3. E → R
4. L → ∗R
5. L → a
6. R → L
JJ J I II G O B ACK F ULL S CREEN C LOSE 304 OF 600
PL April 2, 2020
Nullable
A nonterminal symbol X is nullable if it can derive the empty string, i.e.
X ⇒∗ ε.
Algorithm 6 Nullable
Require: CFG G = hN, T, P, Si
Ensure: N U LLABLE(N ∪ T )
1. for all α ∈ N ∪ T do
2. if α → ε ∈ P then
3. N U LLABLE(α) := true
4. else
5. N U LLABLE(α) := f alse
6. end if
7. end for
8. repeat
9. for all X → α1 . . . αk ∈ P do
10. if ∀i : 1 ≤ i ≤ k : N U LLABLE(αi ) then
11. N U LLABLE(X) := true
12. end if
13. end for
14. until N U LLABLE(N ∪ T ) is unchanged

JJ J I II G O B ACK F ULL S CREEN C LOSE 305 OF 600


PL April 2, 2020
First
F IRST (α) is the set of terminal symbols that can be the first symbol of
any string that α can derive, i.e. a ∈ F IRST (α) if and only if there exists
a derivation α ⇒∗ ax for any string of terminals x.
Algorithm 7 First
Require: CFG G = hN, T, P, Si
Ensure: F IRST (N ∪ T )
1. for all a ∈ T do
2. F IRST (a) := {a}
3. end for
4. for all X ∈ N do
5. F IRST (X) := ∅
6. end for
7. repeat
8. for all X → α1 . . . αk ∈ P do
9. for i := 1 . . . k do
10. if ∀i0 : 1 ≤ i0 < i : N U LLABLE(αi0 ) then
11. F IRST (X) := F IRST (X) ∪ F IRST (αi)
12. end if
13. end for
14. end for
15. until F IRST (N ∪ T ) sets are all unchanged

JJ J I II G O B ACK F ULL S CREEN C LOSE 306 OF 600


PL April 2, 2020
First And Follow
Notice that if X → αZβ is a production then one cannot ignore the
F IRST (Z) in computing F IRST (X) especially if α ⇒∗ ε. Further if
Z is also nullable then F IRST (β) ⊆ F IRST (X).
F OLLOW (X) for any nonterminal symbol X is the set of terminal sym-
bols a such that there exists a rightmost derivation of the form
S ⇒∗ · · · Xa · · · ⇒∗
i.e. F OLLOW (X) is the set of all terminal symbols that can occur to the
right of X in a rightmost derivation.
Notice that if there exists a a rightmost derivation of the form
S ⇒∗ · · · Xα1 . . . αk a · · · ⇒∗
such that α1, . . . , αk are all nullable then again we have
S ⇒∗ · · · Xα1 . . . αk a · · · ⇒∗ · · · Xa · · · ⇒∗

JJ J I II G O B ACK F ULL S CREEN C LOSE 307 OF 600


PL April 2, 2020
Computing Follow
Algorithm 8 Follow
Require: CFG G = hN, T, P, Si
Ensure: F OLLOW (N )
1. for all α ∈ N ∪ T do
2. F OLLOW (α) := ∅
3. end for
4. repeat
5. for all X → α1 . . . αk ∈ P do
6. for i := 1 . . . k do
7. if ∀i0 : i + 1 ≤ i0 ≤ k : N U LLABLE(αi0 ) then
8. F OLLOW (αi) := F OLLOW (αi) ∪ F OLLOW (X)
9. end if
10. for j := i + 1 . . . k do
11. if ∀i0 : i + 1 ≤ i0 ≤ j − 1 : N U LLABLE(αi0 ) then
12. F OLLOW (αi) := F OLLOW (αi) ∪ F IRST (αj )
13. end if
14. end for
15. end for
16. end for
17. until F OLLOW (N ∪ T ) sets are all unchanged

JJ J I II G O B ACK F ULL S CREEN C LOSE 308 OF 600


PL April 2, 2020
JJ J I II G O B ACK F ULL S CREEN C LOSE 309 OF 600
PL April 2, 2020
JJ J I II G O B ACK F ULL S CREEN C LOSE 310 OF 600
PL April 2, 2020
6.7. Recursive Descent Parsing

Recursive Descent Parsing


• Suitable for grammars that are LL(1)
• A set of (mutually) recursive procedures
• Has a single procedure/function for each non-terminal symbol
• Allows for syntax errors to be pinpointed more accurately than most
other parsing methods

JJ J I II G O B ACK F ULL S CREEN C LOSE 311 OF 600


PL April 2, 2020
Caveats with RDP: Left Recursion
Any direct or indirect left-recursion in the grammar can lead to infinite
recursive calls during which no input token is consumed and there is no
return from the recursion. In particular,
• Production rules cannot be left-recursive i.e. they should not be of the
form A −→ Aα. This would result in an infinite recursion with no input
token consumed.
• A production cannot even be indirectly left recursive. For instance the
following is indirect left-recursion of cycle length 2.
Example 6.13
A −→ Bβ
B −→ Aα
where α, β ∈ (N ∪ T )∗.
• In general it should be impossible to have derivation sequences of
the form A ⇒ A1α1 · · · ⇒ An−1αn−1 ⇒ Aαn for nonterminal symbols
A, A1, . . . , An−1 for any n > 0.
JJ J I II G O B ACK F ULL S CREEN C LOSE 312 OF 600
PL April 2, 2020
Caveats with RDP: Left Factoring
For RDP to succeed without backtracking, for each input token and each
non-terminal symbol there should be only one rule applicable;
Example 6.14 A set of productions of the form
A −→ aBβ | aCγ
where B and C stand for different phrases would lead to non-
determinism. The normal practice then would be to left-factor the two
productions by introducing a new non-terminal symbol A0 and rewrite
the rule as
A −→ aA0
A0 −→ Bβ | Cγ
provided B and C generate terminal strings with different first symbols
(otherwise more left-factoring needs to be performed).

JJ J I II G O B ACK F ULL S CREEN C LOSE 313 OF 600


PL April 2, 2020
Left Recursion
The grammar used in shift-reduce parsing is clearly left-recursive in both
the nonterminals E and T and hence is not amenable to recursive-
descent parsing.
The grammar may then have to be modified as follows:
E → T E0
E0 → −T E 0 | ε
T → DT 0
T0 → /DT 0 | ε
D → a | b | (E)
Now this grammar is no longer left-recursive and may then be parsed by
a recursive-descent parser.

JJ J I II G O B ACK F ULL S CREEN C LOSE 314 OF 600


PL April 2, 2020
JJ J I II G O B ACK F ULL S CREEN C LOSE 315 OF 600
PL April 2, 2020
JJ J I II G O B ACK F ULL S CREEN C LOSE 316 OF 600
PL April 2, 2020
6.8. Specification of Syntax: Extended Backus-Naur Form

Specification of Syntax: EBNF

JJ J I II G O B ACK F ULL S CREEN C LOSE 317 OF 600


PL April 2, 2020
6.8.1. The Extended Backus-Naur Form (EBNF)

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).

JJ J I II G O B ACK F ULL S CREEN C LOSE 318 OF 600


PL April 2, 2020
Balanced Parentheses: CFG
Example 6.15 A context-free grammar for balanced parentheses (in-
cluding the empty string) over the terminal alphabet {(, ), [, ], {, }} could
be given as BP3 = h{S}, {(, ), [, ], {, }}, P, {S}i, where P consists of the
productions
S → ,
S → (S)S,
S → [S]S,
S → {S}S

JJ J I II G O B ACK F ULL S CREEN C LOSE 319 OF 600


PL April 2, 2020
Balanced Parentheses: EBNF
Example 6.16 BP3 may be expressed in EBNF as follows:
BracketSeq ::= {Bracket} .
Bracket ::= Lef tP aren BracketSeq RightP aren |
Lef tSqbracket BracketSeq RightSqbracket |
Lef tBrace BracketSeq RightBrace .
Lef tP aren ::= “(” .
RightP aren ::= “)” .
Lef tSqbracket ::= “[” .
RightSqbracket ::= “]” .
Lef tBrace ::= “{” .
RightBrace ::= “}” .

JJ J I II G O B ACK F ULL S CREEN C LOSE 320 OF 600


PL April 2, 2020
EBNF in EBNF
EBNF has its own grammar which is again context-free. Hence EBNF
(6.8.1) may be used to define EBNF in its own syntax as follows:
Syntax ::= {P roduction} .
P roduction ::= N onT erminal “::=” P ossibleRewrites “.” .
P ossibleRewrites ::= Rewrite {“|” Rewrite} .
Rewrite ::= Symbol {Symbol} .
Symbol ::= N onT erminal | T erminal | GroupRewrites .
GroupRewrites ::= “{” P ossibleRewrites “}” |
“[” P ossibleRewrites “]” |
“(” P ossibleRewrites “)” .
N onT erminal ::= Letter {Letter | Digit} .
T erminal ::= Character {Character} .

JJ J I II G O B ACK F ULL S CREEN C LOSE 321 OF 600


PL April 2, 2020
EBNF: Character Set
The character set used in EBNF is described below.
Character ::= Letter | Digit | SpecialChar
Letter ::= U pperCase | LowerCase
U pperCase ::= “A” | “B” | “C” | “D” | “E” | “F ” | “G” | “H” |
“I” | “J” | “K” | “L” | “M ” | “N ” | “O” | “P ” | “Q” |
“R” | “S” | “T ” | “U ” | “V ” | “W ” | “X” | “Y ” | “Z”
LowerCase ::= “a” | “b” | “c” | “d” | “e” | “f ” | “g” | “h” |
“i” | “j” | “k” | “l” | “m” | “n” | “o” | “p” | “q” |
“r” | “s” | “t” | “u” | “v” | “w” | “x” | “y” | “z”
Digit ::= “0” | “1” | “2” | “3” | “4” | “5” | “6” | “7” | “8” | “9”
SpecialChar ::= “!” | “”” | “#” | “$” | “%” | “&” | “0” | “(” | “)” | “∗” |
“+” | “,” | “−” | “.” | “/” | “:” | “;” | “<” | “=” | “>” | “?”
“@” | “[” | “\” | “]” | “ˆ” | “ ” | “‘” | “{” | “|” | “}” | “˜”

JJ J I II G O B ACK F ULL S CREEN C LOSE 322 OF 600


PL April 2, 2020
JJ J I II G O B ACK F ULL S CREEN C LOSE 323 OF 600
PL April 2, 2020
6.9. The WHILE Programming Language: Syntax

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” .

JJ J I II G O B ACK F ULL S CREEN C LOSE 324 OF 600


PL April 2, 2020
Expression ::= IntExpression | BoolExpression .
IntExpression ::= IntExpression AddOp IntT erm | IntT erm .
IntT erm ::= IntT erm M ultOp IntF actor | IntF actor .
IntF actor ::= N umeral | V ariable |
“(”IntExpression“)” | “˜”IntF actor .
BoolExpression ::= BoolExpression “||” BoolT erm | BoolT erm .
BoolT erm ::= BoolT erm “&&” BoolF actor | BoolF actor .
BoolF actor ::= “tt” | “ff ” | V ariable | Comparison |
“(”BoolExpression“)” | “!”BoolF actor .
Comparison ::= IntExpression RelOp IntExpression .
V ariable ::= Identif ier .
RelOp ::= “<” | “<=” | “=” | “>” | “>=” | “<>” .
AddOp ::= “+” | “−” .
M ultOp ::= “∗” | “/” | “%” .
Identif ier ::= Letter{Letter | Digit} .
N umeral ::= [“+” | “˜”]Digit{Digit} .
Note

1. “;” acts as a terminator for both Declarations and Commands.


2. “,” acts as a separator in V ariableList
3. Comparison has a higher precedence than BoolT erm and BoolExpression.
4. RelOps have lower precedence than any of the integer operations specified in M ultOp and AddOp.
5. The nonterminals Letter and Digit are as specified earlier in the EBNF character set
JJ J I II G O B ACK F ULL S CREEN C LOSE 325 OF 600
PL April 2, 2020
Syntax Diagrams
• EBNF was first used to define the grammar of ALGOL-60 and the
syntax was used to design the parser for the language.
• EBNF also has a diagrammatic rendering called syntax diagrams or
railroad diagrams. The grammar of SML has been rendered by a set
of syntax diagrams.
• Pascal has been defined using both the text-version of EBNF and
through syntax diagrams.
• While the text form of EBNF helps in parsing, the diagrammatic ren-
dering is only for the purpose of readability.
• EBNF is a specification language that almost all modern programming
languages use to define the grammar of the programming language

JJ J I II G O B ACK F ULL S CREEN C LOSE 326 OF 600


PL April 2, 2020
Syntax Specifications
• BNF of C
• BNF of Java
• EBNF of Pascal
• Pascal Syntax diagrams
• BNF of Standard ML
• BNF of Datalog
• BNF of Prolog

JJ J I II G O B ACK F ULL S CREEN C LOSE 327 OF 600


PL April 2, 2020
Syntax Diagrams of SML: 1

Syntax of Standard ML
Tobias Nipkow and Larry Paulson

PROGRAMS AND MODULES

 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

 Speci cation
:

 ObjectDeclaration 
Structure

  
 CompoundIdent
struct end


 Ident  Structure 

 ObjectDeclaration
( )

 ObjectDeclaration  Structure 
 let in
end

 Speci cation  
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
Speci cation
val : 
 
   TypeVarList Ident

and

 type

   
 DatatypeBinding

eqtype and

 
datatype

  Ident  Type 
 and


exception of

  

  Ident  Signature
and


 
structure
:


  CompoundIdent
and

  
sharing

 
type
 =

 Speci cation  Speci cation  and


 
  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 In xOperator AtomicPattern

 )



 AtomicPattern
 AtomicPattern In xOperator AtomicPattern
 TypeVarList Ident  Type
TypeBinding


=

 and

 TypeVarList Ident   Ident  Type 


DatatypeBinding


= of

 
 

|

and

TypeVarList

 TypeVar

  TypeVar 

( )


,

JJ J I II G O B ACK F ULL S CREEN C LOSE 332 OF 600


PL April 2, 2020
EXPRESSIONS
Expression
In xExpression 
 Expression  Type
 Expression   Expression
:


andalso


Expression  Match
orelse



 Expression 
handle


 Expression   Expression
raise


 Expression 
Expression

if then else


while


 Expression  Match Expression
do


 Match
case of

fn
In xExpression
 AtomicExpression 

 In xExpression In xOperator In xExpression

JJ J I II G O B ACK F ULL S CREEN C LOSE 333 OF 600


PL April 2, 2020
AtomicExpression
CompoundName 
 Constant
  Expression  

(

  )


,

  Expression

 


 
[ ]


  Label 
f

Expression 
 g


=

 ,



 Label
  Expression 
#


 
( )

 Declaration 
  
 
;

let Expression
in end

  ;

MATCHES AND PATTERNS


Match
 Pattern  Expression
 
=>


 |

Pattern
AtomicPattern 
 CompoundName AtomicPattern
 Pattern In xOperator 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
,

JJ J I II G O B ACK F ULL S CREEN C LOSE 335 OF 600


PL April 2, 2020
TYPES
Type
TypeVar 
 CompoundIdent
 Type 
  Type  

 
( )

 Type  Type


,


*

Type  Type 
  Label  Type 
->

f
  : g

  ,


 Type 
( )
LEXICAL MATTERS: IDENTIFIERS, CONSTANTS, COMMENTS
CompoundIdent
 Ident

  .

CompoundName
CompoundIdent 
 In xOperator
op

Name
Ident 
 In xOperator
op

JJ Jany Ident that has been declared to be in x


In xOperator
I II
PL April 2, 2020
G O B ACK F ULL S CREEN C LOSE 336 OF 600
 
 

 
   

"

any printable character


except and 
"

 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

JJ J I II G O B ACK F ULL S CREEN C LOSE 338 OF 600


PL April 2, 2020
Exercise 6.3

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.

JJ J I II G O B ACK F ULL S CREEN C LOSE 339 OF 600


PL April 2, 2020
JJ J I II G O B ACK F ULL S CREEN C LOSE 340 OF 600
PL April 2, 2020
7. Attributes & Semantic Analysis

Attributes & Semantic Analysis

JJ J I II G O B ACK F ULL S CREEN C LOSE 341 OF 600


PL April 2, 2020
7.1. Context-sensitive analysis and Semantics

The Big Picture

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 )

Figure 1: Deerivation trees or Concrete parse trees

The (possibly modified grammar) required for parsing


1 The term “parse tree” is a much abused term used to refer to anything from a derivation tree to an abstract syntax tree (AST).

JJ J I II G O B ACK F ULL S CREEN C LOSE 342 OF 600


PL April 2, 2020
• treats all tokens uniformly since the phrase structure of the grammar is all-important during the parsing process,
• introduces bracketing and punctuation marks for
– disambiguation and
– to facilitate easy parsing
But these symbols do not by themselves carry any semantic information.
• also has many more non-terminal symbols that are required for parsing, but which carry no semantic signifi-
cance
– either for the end-user of the language
– or for the later phases of the compilation process.

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

Figure 2: Abstract syntax tree (AST) for the sentences in fig. 1

Notice that the AST in figure 2


JJ J I II G O B ACK F ULL S CREEN C LOSE 343 OF 600
PL April 2, 2020
• abstracts away from non-terminals which have significance only for the parsing of the expression and have no
semantic significance whatsoever,
• abstracts away from bracketing and punctuation mechanisms and provides a hierarchical structure containing
only the essential operators and operands.
• clearly distinguishes the operators (based on their arity) from the operands (which are leaves of the AST).

JJ J I II G O B ACK F ULL S CREEN C LOSE 344 OF 600


PL April 2, 2020
Semantic Analysis: 0
The Big Picture

1. Every programming language can be used to program any computable


function, assuming of course, it has
• unbounded memory, and
• unbounded time
2. Context-free grammars are not powerful enough to represent all com-
putable functions.
Example 7.2 The language {anbncn|n > 0} is not context-free.
3. Semantic analysis is an essential step to generating IR-code, since
it requires the computation of certain bits and pieces of information
called attributes (which include information to be entered into the sym-
bol table or useful for error-handling)
4. Many of these attributes are context-sensitive in nature. They need to
be computed and if necessary propagated during parsing from wher-
ever they are available.
JJ J I II G O B ACK F ULL S CREEN C LOSE 345 OF 600
PL April 2, 2020
Semantic Analysis: 1
The Big Picture

• The parser of a programming language provides the framework within


which the IR-code or even the target code is to be generated.
• The parser also provides a structuring mechanism that divides the task
of code generation into bits and pieces determined by the individual
nonterminals and production rules.
• The parser provides the framework from within which the semantic
analysis (which includes the bits and pieces of information that are
required for code generation) is performed

JJ J I II G O B ACK F ULL S CREEN C LOSE 346 OF 600


PL April 2, 2020
Semantic Analysis: 2
• There are context-sensitive aspects of a program that cannot be rep-
resented/enforced by a context-free grammar definition. Examples in-
clude
– type consistency between declaration and use.
– correspondence between formal and actual parameters (example
7.2 is an abstraction where an represents a function or procedure
declaration with n formal parameters and bn and cn represent two
calls to the same procedure in which the number of actual parame-
ters should equal n).
– scope and visibility issues with respect to identifiers in a program.

JJ J I II G O B ACK F ULL S CREEN C LOSE 347 OF 600


PL April 2, 2020
JJ J I II G O B ACK F ULL S CREEN C LOSE 348 OF 600
PL April 2, 2020
JJ J I II G O B ACK F ULL S CREEN C LOSE 349 OF 600
PL April 2, 2020
7.2. Syntax-Directed Translation

Syntax-directed Translation

JJ J I II G O B ACK F ULL S CREEN C LOSE 350 OF 600


PL April 2, 2020
Attributes
An attribute can represent anything we choose e.g.
• a string
• a number (e.g. size of an array or the number of formal parameters of
a function)
• a type
• a memory location
• a procedure to be executed
• an error message to be displayed
The value of an attribute at a parse-tree node is defined by the semantic
rule associated with the production used at that node.

JJ J I II G O B ACK F ULL S CREEN C LOSE 351 OF 600


PL April 2, 2020
Syntax-Directed Definitions (SDD)
Syntax-Directed definitions are high-level specifications which specify
the evaluation of
1. various attributes
2. various procedures such as
• transformations
• generating code
• saving information
• issuing error messages
They hide various implementation details and free the compiler writer
from explicitly defining the order in which translation, transformations,
and code generation take place.

JJ J I II G O B ACK F ULL S CREEN C LOSE 352 OF 600


PL April 2, 2020
Kinds of Attributes
There are two kinds of attributes that one can envisage.
Synthesized attributes A synthesized attribute is one whose value de-
pends upon the values of its immediate children in the concrete parse
tree.
A syntax-directed definition that uses only synthesized attributes is
called an S-attributed definition. See example
Inherited attributes An inherited attribute is one whose value depends
upon the values of the attributes of its parents or siblings in the parse
tree.
Inherited attributes are convenient for expressing the dependence of
a language construct on the context in which it appears.

JJ J I II G O B ACK F ULL S CREEN C LOSE 353 OF 600


PL April 2, 2020
What is Syntax-directed?
• A syntax-directed definition is a generalisation of a context-free gram-
mar in which each grammar symbol has an associated set of at-
tributes, partitioned into two subsets called synthesized and inherited
attributes.
• The various attributes are computed by so-called semantic rules as-
sociated with each production of the grammar which allows the com-
putation of the various attributes.
• These semantic rules are in general executed during
bottom-up (SR) parsing at the stage when a reduction needs to be per-
formed by the given rule and
top-down (RDP) parsing in the procedure before the next call or return
from the procedure.
• A parse tree showing the various attributes at each node is called an
annotated parse tree.
JJ J I II G O B ACK F ULL S CREEN C LOSE 354 OF 600
PL April 2, 2020
Forms of SDDs
In a syntax-directed definition, each grammar production rule X → α
has associated with it a set of semantic rules of the form b = f (a1, . . . , ak )
where a1, · · · , ak are attributes belonging to X and/or the grammar sym-
bols of α.
Definition 7.3 Given a production X → α, an attribute a is
synthesized: a synthesized attribute of X (denoted X.a) or
inherited: an inherited attribute of one of the grammar symbols of α (de-
noted B.a if a is an attribute of B).
In each case the attribute a is said to depend upon the attributes
a1, · · · , ak .

JJ J I II G O B ACK F ULL S CREEN C LOSE 355 OF 600


PL April 2, 2020
Attribute Grammars
• An attribute grammar is a syntax-directed definition in which the func-
tions in semantic rules can have no side-effects.
• The attribute grammar also specifies how the attributes are propa-
gated through the grammar, by using graph dependency between the
productions.
• In general different occurrences of the same non-terminal symbol in each
production will be distinguished by appropriate subscripts when defin-
ing the semantic rules associated with the rule.

The following example illustrates the concept of a syntax-directed defini-


tion using synthesized attributes.

JJ J I II G O B ACK F ULL S CREEN C LOSE 356 OF 600


PL April 2, 2020
Attribute Grammars: Example
Determining the values of arithmetic expressions. Consider a simple
attribute val associated with an expression
E0 → E1−T  E0.val := E1.val − T.val

E → T  E.val := T.val

T0 → T1/F  T0.val := T1.val/F.val

T → F  T.val := F.val

F → (E)  F.val := E.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.

JJ J I II G O B ACK F ULL S CREEN C LOSE 358 OF 600


PL April 2, 2020
JJ J I II G O B ACK F ULL S CREEN C LOSE 359 OF 600
PL April 2, 2020
JJ J I II G O B ACK F ULL S CREEN C LOSE 360 OF 600
PL April 2, 2020
7.2.1. Synthesized Attributes

Synthesized Attributes
Evaluating the expression (4 − 1)/2 generated by the grammar for sub-
traction and division

JJ J I II G O B ACK F ULL S CREEN C LOSE 361 OF 600


PL April 2, 2020
Synthesized Attributes: 0

E T F E

/ ( ) n

T

T F
/

n
F

( )
E

E T

T
F

F
n

JJ J I II G O B ACK F ULL S CREEN C LOSE 362 OF 600


PL April 2, 2020
Synthesized Attributes: 1

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

JJ J I II G O B ACK F ULL S CREEN C LOSE 363 OF 600


PL April 2, 2020
Synthesized Attributes: 2

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

JJ J I II G O B ACK F ULL S CREEN C LOSE 364 OF 600


PL April 2, 2020
Synthesized Attributes: 3

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

JJ J I II G O B ACK F ULL S CREEN C LOSE 365 OF 600


PL April 2, 2020
Synthesized Attributes: 4

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

JJ J I II G O B ACK F ULL S CREEN C LOSE 366 OF 600


PL April 2, 2020
Synthesized Attributes: 5

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

JJ J I II G O B ACK F ULL S CREEN C LOSE 367 OF 600


PL April 2, 2020
Synthesized Attributes: 6

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

JJ J I II G O B ACK F ULL S CREEN C LOSE 368 OF 600


PL April 2, 2020
Synthesized Attributes: 7

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

JJ J I II G O B ACK F ULL S CREEN C LOSE 369 OF 600


PL April 2, 2020
Synthesized Attributes: 8

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

JJ J I II G O B ACK F ULL S CREEN C LOSE 370 OF 600


PL April 2, 2020
Synthesized Attributes: 9

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

JJ J I II G O B ACK F ULL S CREEN C LOSE 371 OF 600


PL April 2, 2020
Synthesized Attributes: 10

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

JJ J I II G O B ACK F ULL S CREEN C LOSE 372 OF 600


PL April 2, 2020
Synthesized Attributes: 11

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

JJ J I II G O B ACK F ULL S CREEN C LOSE 373 OF 600


PL April 2, 2020
Synthesized Attributes: 12

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

JJ J I II G O B ACK F ULL S CREEN C LOSE 374 OF 600


PL April 2, 2020
Synthesized Attributes: 13

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

JJ J I II G O B ACK F ULL S CREEN C LOSE 375 OF 600


PL April 2, 2020
Synthesized Attributes: 14

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

JJ J I II G O B ACK F ULL S CREEN C LOSE 376 OF 600


PL April 2, 2020
An Attribute Grammar
E 1 E0 → E1−T  E0.val := sub(E1.val, T.val)
T 1

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

JJ J I II G O B ACK F ULL S CREEN C LOSE 377 OF 600


PL April 2, 2020
Synthesized Attributes Evaluation:
Bottom-up
During bottom-up parsing synthesized attributes are evaluated as fol-
lows:
Bottom-up Parsers
1. Keep an attribute value stack along with the parsing stack.
2. Just before applying a reduction of the form Z → Y1 . . . Yk compute
the attribute values of Z from the attribute values of Y1, · · · , Yk and
place them in the same position on the attribute value stack corre-
sponding to the one where the symbol Z will appear on the parsing
stack as a result of the reduction.

JJ J I II G O B ACK F ULL S CREEN C LOSE 378 OF 600


PL April 2, 2020
Synthesized Attributes Evaluation:
Top-down
During top-down parsing synthesized attributes are evaluated as follows:
Top-down Parsers In any production of the form Z → Y1 . . . Yk , the parser
makes recursive calls to procedures corresponding to the symbols
Y1 . . . Yk . In each case the attributes of the non-terminal symbols
Y1 . . . Yk are computed and returned to the procedure for Z. Com-
pute the synthesized attributes of Z from the attribute values returned
from the recursive calls.

JJ J I II G O B ACK F ULL S CREEN C LOSE 379 OF 600


PL April 2, 2020
JJ J I II G O B ACK F ULL S CREEN C LOSE 380 OF 600
PL April 2, 2020
JJ J I II G O B ACK F ULL S CREEN C LOSE 381 OF 600
PL April 2, 2020
7.2.2. Inherited Attributes

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

JJ J I II G O B ACK F ULL S CREEN C LOSE 382 OF 600


PL April 2, 2020
Inherited Attributes: 1
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 int L
z

I
L ,

I
D L T I

x y z int x

, int

JJ J I II G O B ACK F ULL S CREEN C LOSE 383 OF 600


PL April 2, 2020
Inherited Attributes: 2
C-style declarations generating int x, y, z.
D → T L T → int | float
L → L,I | I I → x | y | z
D

int T L

I
,
int int L
z

I
L ,

I
D L T I

x y z int x

, int int

JJ J I II G O B ACK F ULL S CREEN C LOSE 384 OF 600


PL April 2, 2020
Inherited Attributes: 3
C-style declarations generating int x, y, z.
D → T L T → int | float
L → L,I | I I → x | y | z
D

int T L int

I
,
int int L
z

I
L ,

I
D L T I

x y z int x

, int int

JJ J I II G O B ACK F ULL S CREEN C LOSE 385 OF 600


PL April 2, 2020
Inherited Attributes: 4
C-style declarations generating int x, y, z.
D → T L T → int | float
L → L,I | I I → x | y | z
D

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

JJ J I II G O B ACK F ULL S CREEN C LOSE 386 OF 600


PL April 2, 2020
Inherited Attributes: 5
C-style declarations generating int x, y, z.
D → T L T → int | float
L → L,I | I I → x | y | z
D

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

JJ J I II G O B ACK F ULL S CREEN C LOSE 387 OF 600


PL April 2, 2020
Inherited Attributes: 6
C-style declarations generating int x, y, z.
D → T L T → int | float
L → L,I | I I → x | y | z
D

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

JJ J I II G O B ACK F ULL S CREEN C LOSE 388 OF 600


PL April 2, 2020
Inherited Attributes: 7
C-style declarations generating int x, y, z.
D → T L T → int | float
L → L,I | I I → x | y | z
D

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

JJ J I II G O B ACK F ULL S CREEN C LOSE 389 OF 600


PL April 2, 2020
Attribute Grammar: Inherited
D

int T L int
D → TL  L.in := T.type
I int

T → int  T.type := int.int


,
int int L int
z int
I int
L 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

JJ J I II G O B ACK F ULL S CREEN C LOSE 390 OF 600


PL April 2, 2020
L-attributed Definitions
Definition 7.4 A grammar is L-attributed if for each production of the
form Y → X1 . . . Xk , each inherited attribute of the symbol Xj , 1 ≤ j ≤ k
depends only on
1. the inherited attributes of the symbol Y and
2. the synthesized or inherited attributes of X1, · · · , Xj−1.

JJ J I II G O B ACK F ULL S CREEN C LOSE 391 OF 600


PL April 2, 2020
Why L-attributedness?
Intuitively, if Xj .inh is an inherited attribute then
• it cannot depend on any synthesized attribute Y.syn of Y because it
is possible that the computation of Y.syn requires the value of Xj .inh
leading to circularity in the definition.
• if the value of Xj .inh depends upon the attributes of one or more of
the symbols Xj+1, · · · , Xk then the computation of Xj .inh cannot be
performed just before the reduction by the rule Y → X1 . . . Xk during
parsing. Instead it may have to be postponed till the end of parsing.
• it could depend on the synthesized or inherited attributes of any of
the symbols X1 . . . Xj−1 since they would already be available on the
attribute value stack.
• it could depend upon the inherited attributes of Y because these in-
herited attributes can be computed from the attributes of the symbols
lying below X1 on the stack, provided these inherited attributes of Y
are also L-attributed.
JJ J I II G O B ACK F ULL S CREEN C LOSE 392 OF 600
PL April 2, 2020
A Non L-attributed Definition
Our attribute grammar for C-style declarations is definitely L-attributed.
However consider the following grammar for declarations in Pascal and
ML.

D → L:T  L.in := T.type


T → int  T.type := int.int
T → real  T.type := real.real
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.

JJ J I II G O B ACK F ULL S CREEN C LOSE 393 OF 600


PL April 2, 2020
Evaluating Non-L-attributed Definitions
In many languages like ML which allow higher order functions as values,
a definition not being L-attributed may not be of serious concern.
But in most other languages it is serious enough to warrant changing
the grammar of the language so as to replace inherited attributes by cor-
responding synthesized ones. The language of the grammar of Pascal
and ML declarations can be generated as follows:

D → idL  addtype(id, L.type)


L → :T  L.in := T.type
L → ,id L  L0.type := L1.type;
addtype(id.L1.type)
T → int  T.type := int.int
T → real  T.type := real.real

JJ J I II G O B ACK F ULL S CREEN C LOSE 394 OF 600


PL April 2, 2020
Dependency Graphs
In general, the attributes required to be computed during parsing could
be synthesized or inherited and further it is possible that some synthe-
sized attributes of some symbols may depend on the inherited attributes
of some other symbols. In such a scenario it is necessary to construct a
dependency graph of the attributes of each node of the parse tree.

JJ J I II G O B ACK F ULL S CREEN C LOSE 395 OF 600


PL April 2, 2020
Dependency Graph Construction
Algorithm 9 Attribute Dependency Graph Construction
Require: A parse tree of a CFG and the list of attributes
Ensure: A dependency graph
for all nodes n of the parse tree do
for all attributes a of node n do
Create an attribute node n.a
end for
end for
for all nodes n of the parse tree do
for all semantic rules a := f (b1 , . . . , bk ) do
for all i : 1 ≤ i ≤ k do
Create a directed edge bi → a
end for
end for
end for

JJ J I II G O B ACK F ULL S CREEN C LOSE 396 OF 600


PL April 2, 2020
8. Abstract Syntax

JJ J I II G O B ACK F ULL S CREEN C LOSE 397 OF 600


PL April 2, 2020
Abstract Syntax Trees
The construction of ASTs from concrete parse trees is another exam-
ple of a transformation that can be performed using a syntax-directed
definition that has no side-effects.
Hence we define it using an attribute grammar.

JJ J I II G O B ACK F ULL S CREEN C LOSE 398 OF 600


PL April 2, 2020
Abstract Syntax: 0
E → E−T | T
T → T /F | F
F → n | (E)
Suppose we want to evaluate an expression (4 − 1)/2. What we actually
want is a tree that looks like this:

−− 2

4 1

JJ J I II G O B ACK F ULL S CREEN C LOSE 399 OF 600


PL April 2, 2020
Evaluation: 0

−− 2

4 1

JJ J I II G O B ACK F ULL S CREEN C LOSE 400 OF 600


PL April 2, 2020
Evaluation: 1

−− 2

4 1

JJ J I II G O B ACK F ULL S CREEN C LOSE 401 OF 600


PL April 2, 2020
Evaluation: 2

3 2

JJ J I II G O B ACK F ULL S CREEN C LOSE 402 OF 600


PL April 2, 2020
Evaluation: 3

3 2

JJ J I II G O B ACK F ULL S CREEN C LOSE 403 OF 600


PL April 2, 2020
Evaluation: 4

But what we actually get during parsing is a tree that looks like . . .

JJ J I II G O B ACK F ULL S CREEN C LOSE 404 OF 600


PL April 2, 2020
Abstract Syntax: 1
. . . THIS! E

T F
/

n
F

( )
E

/
E T

−− n
T
F

n n
F
n

JJ J I II G O B ACK F ULL S CREEN C LOSE 405 OF 600


PL April 2, 2020
Abstract Syntax
Shift-reduce parsing produces a concrete syntax tree from the rightmost
derivation. The syntax tree is concrete in the sense that
• It contains a lot of redundant symbols that are important or useful only
during the parsing stage.
– punctuation marks
– brackets of various kinds
• It makes no distinction between operators, operands, and punctuation
symbols
On the other hand the abstract syntax tree (AST) contains no punctua-
tions and makes a clear distinction between an operand and an operator.

JJ J I II G O B ACK F ULL S CREEN C LOSE 406 OF 600


PL April 2, 2020
Abstract Syntax: Imperative Approach
We use attribute grammar rules to construct the abstract syntax tree
(AST) from the parse tree.
But in order to do that we first require two procedures for tree construc-
tion.
makeLeaf(literal) : Creates a node with label literal and returns a pointer
or a reference to it.
makeBinaryNode(opr, opd1, opd2) : Creates a node with label opr (with
fields which point to opd1 and opd2) and returns a pointer or a refer-
ence to the newly created node.
Now we may associate a synthesized attribute called ptr with each ter-
minal and nonterminal symbol which points to the root of the subtree
created for it.

JJ J I II G O B ACK F ULL S CREEN C LOSE 407 OF 600


PL April 2, 2020
Abstract Syntax Trees: Imperative
E0 → E1−T  E0.ptr := makeBinaryN ode(−, E1.ptr, T.ptr)

E → T  E.ptr := T.ptr

T0 → T1/F  T0.ptr := makeBinaryN ode(/, T1.ptr, F.ptr)

T → F  T.ptr := F.ptr

F → (E)  F.ptr := E.ptr

F → n  F.ptr := makeLeaf (n.val)

The Big Picture

JJ J I II G O B ACK F ULL S CREEN C LOSE 408 OF 600


PL April 2, 2020
Abstract Syntax: Functional Approach
We use attribute grammar rules to construct the abstract syntax tree
(AST) functionally from the parse tree.
But in order to do that we first require two functions/constructors for tree
construction.
makeLeaf(literal) : Creates a node with label literal and returns the AST.
makeBinaryNode(opr, opd1, opd2) : Creates a tree with root label opr
(with sub-trees opd1 and opd2).
Now we may associate a synthesized attribute called ast with each ter-
minal and nonterminal symbol which points to the root of the subtree
created for it.

JJ J I II G O B ACK F ULL S CREEN C LOSE 409 OF 600


PL April 2, 2020
Abstract Syntax: Functional
E0 → E1−T  E0.ast := makeBinaryN ode(−, E1.ast, T.ast)

E → T  E.ast := T.ast

T0 → T1/F  T0.ast := makeBinaryN ode(/, T1.ast, F.ast)

T → F  T.ast := F.ast

F → (E)  F.ast := E.ast

F → n  F.ast := makeLeaf (n.val)

The Big Picture

JJ J I II G O B ACK F ULL S CREEN C LOSE 410 OF 600


PL April 2, 2020
JJ J I II G O B ACK F ULL S CREEN C LOSE 411 OF 600
PL April 2, 2020
JJ J I II G O B ACK F ULL S CREEN C LOSE 412 OF 600
PL April 2, 2020
9. Symbol Table

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.

Lewis Carroll, Through the Looking-Glass

JJ J I II G O B ACK F ULL S CREEN C LOSE 413 OF 600


PL April 2, 2020
Symbol Table:1
• The store house of context-sensitive and run-time information about
every identifier in the source program.
• All accesses relating to an identifier require to first find the attributes
of the identifier from the symbol table
• Usually organized as a hash table – provides fast access.
• Compiler-generated temporaries may also be stored in the symbol ta-
ble

JJ J I II G O B ACK F ULL S CREEN C LOSE 414 OF 600


PL April 2, 2020
Symbol Table:2
Attributes stored in a symbol table for each identifier:
• type
• size
• scope/visibility information
• base address
• addresses to location of auxiliary symbol tables (in case of records,
procedures, classes)
• address of the location containing the string which actually names the
identifier and its length in the string pool

JJ J I II G O B ACK F ULL S CREEN C LOSE 415 OF 600


PL April 2, 2020
Symbol Table:3
• A symbol table exists through out the compilation and run-time.
• Major operations required of a symbol table:
– insertion
– search
– deletions are purely logical (depending on scope and visibility) and
not physical
• Keywords are often stored in the symbol table before the compilation
process begins.

JJ J I II G O B ACK F ULL S CREEN C LOSE 416 OF 600


PL April 2, 2020
Symbol Table:4
Accesses to the symbol table at every stage of the compilation process,
Scanning: Insertion of new identifiers.
Parsing: Access to the symbol table to ensure that an operand exists
(declaration before use).
Semantic analysis:
• Determination of types of identifiers from declarations
• type checking to ensure that operands are used in type-valid con-
texts.
• Checking scope, visibility violations.

JJ J I II G O B ACK F ULL S CREEN C LOSE 417 OF 600


PL April 2, 2020
Symbol Table:5
IR generation: . Memory allocation and relativea address calculation.
Optimization: All memory accesses through symbol table
Target code: Translation of relative addresses to absolute addresses in
terms of word length, word boundary etc.
The Big picture
a
i.e.relative to a base address that is known only at run-time

JJ J I II G O B ACK F ULL S CREEN C LOSE 418 OF 600


PL April 2, 2020
JJ J I II G O B ACK F ULL S CREEN C LOSE 419 OF 600
PL April 2, 2020
10. Intermediate Representation

Intermediate Representation

JJ J I II G O B ACK F ULL S CREEN C LOSE 420 OF 600


PL April 2, 2020
Intermediate Representation
Intermediate representations are important for reasons of portability i.e.
platform (hardware and OS) independence.
• (more or less) independent of specific features of the high-level lan-
guage.
Example. Java byte-code which is the instruction set of the Java Vir-
tual Machine (JVM).
• (more or less) independent of specific features of any particular target
architecture (e.g. number of registers, memory size)
– number of registers
– memory size
– word length

JJ J I II G O B ACK F ULL S CREEN C LOSE 421 OF 600


PL April 2, 2020
IR Properties: 1
1. It is fairly low-level containing instructions common to all target archi-
tectures and assembly languages.
How low can you stoop? . . .
2. It contains some fairly high-level instructions that are common to most
high-level programming languages.
How high can you rise?
3. To ensure portability
• an unbounded number of variables and memory locations
• no commitment to Representational Issues
4. To ensure type-safety
• memory locations are also typed according to the data they may
contain,
• no commitment is made regarding word boundaries, and the struc-
ture of individual data items.
Next
JJ J I II G O B ACK F ULL S CREEN C LOSE 422 OF 600
PL April 2, 2020
IR: Representation?
• No commitment to word boundaries or byte boundaries
• No commitment to representation of
– int vs. float,
– float vs. double,
– packed vs. unpacked,
– strings – where and how?.
Back to IR Properties:1

JJ J I II G O B ACK F ULL S CREEN C LOSE 423 OF 600


PL April 2, 2020
IR: How low can you stoop?
• most arithmetic and logical operations, load and store instructions etc.
• so as to be interpreted easily,
• the interpreter is fairly small,
• execution speeds are high,
• to have fixed length instructions (where each operand position has a
specific meaning).
Back to IR Properties:1

JJ J I II G O B ACK F ULL S CREEN C LOSE 424 OF 600


PL April 2, 2020
IR: How high can you rise?
• typed variables,
• temporary variables instead of registers.
• array-indexing,
• random access to record fields,
• parameter-passing,
• pointers and pointer management
• no limits on memory addresses
Back to IR Properties:1

JJ J I II G O B ACK F ULL S CREEN C LOSE 425 OF 600


PL April 2, 2020
A typical instruction set: 1
Three address code: A suite of instructions. Each instruction has at most
3 operands.
• an opcode representing an operation with at most 2 operands
• two operands on which the binary operation is performed
• a target operand, which accumulates the result of the (binary) opera-
tion.
If an operation requires less than 3 operands then one or more of the
operands is made null.

JJ J I II G O B ACK F ULL S CREEN C LOSE 426 OF 600


PL April 2, 2020
A typical instruction set: 2
• Assignments (LOAD-STORE)
• Jumps (conditional and unconditional)
• Procedures and parameters
• Arrays and array-indexing
• Pointer Referencing and Dereferencing
c.f. Java byte-code

JJ J I II G O B ACK F ULL S CREEN C LOSE 427 OF 600


PL April 2, 2020
A typical instruction set: 2.1
• Assignments (LOAD-STORE)
– x := y bop z, where bop is a binary operation
– x := uop y, where uop is a unary operation
– x := y, load, store, copy or register transfer
• Jumps (conditional and unconditional)
• Procedures and parameters
• Arrays and array-indexing
• Pointer Referencing and Dereferencing

JJ J I II G O B ACK F ULL S CREEN C LOSE 428 OF 600


PL April 2, 2020
A typical instruction set: 2.2
• Assignments (LOAD-STORE)
• Jumps (conditional and unconditional)
– goto L – Unconditional jump,
– x relop y goto L – Conditional jump, where relop is a rela-
tional operator
• Procedures and parameters
• Arrays and array-indexing
• Pointer Referencing and Dereferencing

JJ J I II G O B ACK F ULL S CREEN C LOSE 429 OF 600


PL April 2, 2020
A typical instruction set: 2.3
• Assignments (LOAD-STORE)
• Jumps (conditional and unconditional)
• Procedures and parameters
– call p n, where n is the number of parameters
– return y, return value from a procedures call
– param x, parameter declaration
• Arrays and array-indexing
• Pointer Referencing and Dereferencing

JJ J I II G O B ACK F ULL S CREEN C LOSE 430 OF 600


PL April 2, 2020
A typical instruction set: 2.4
• Assignments (LOAD-STORE)
• Jumps (conditional and unconditional)
• Procedures and parameters
• Arrays and array-indexing
– x := a[i] – array indexing for r-value
– a[j] := y – array indexing for l-value
Note: The two opcodes are different depending on whether l-value or
r-value is desired. x and y are always simple variables
• Pointer Referencing and Dereferencing

JJ J I II G O B ACK F ULL S CREEN C LOSE 431 OF 600


PL April 2, 2020
A typical instruction set: 2.5
• Assignments (LOAD-STORE)
• Jumps (conditional and unconditional)
• Procedures and parameters
• Arrays and array-indexing
• Pointer Referencing and Dereferencing
– x := ˆy – referencing: set x to point to y
– x := *y – dereferencing: copy contents of location pointed to by y
into x
– *x := y – dereferencing: copy r-value of y into the location pointed
to by x
Picture

JJ J I II G O B ACK F ULL S CREEN C LOSE 432 OF 600


PL April 2, 2020
Pointers

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.

JJ J I II G O B ACK F ULL S CREEN C LOSE 434 OF 600


PL April 2, 2020
IR: Infrastructure 1
Given an abstract syntax tree T, with T also denoting its root node.
T.place address of temporary variable where result of execution of the T
is stored.
newtemp returns a fresh variable name and also installs it in the symbol
table along with relevant information
T.code the actual sequence of instructions generated for the tree T.
newlabel returns a label to mark an instruction in the generated code
which may be the target of a jump.
emit emits an instructions (regarded as a string).

JJ J I II G O B ACK F ULL S CREEN C LOSE 435 OF 600


PL April 2, 2020
IR: Infrastructure 2
Colour and font coding of IR code generation process.
• Green: Nodes of the Abstract Syntax Tree
• Brown: Intermediate Representation i.e. the language of the
“virtual machine”
• Red: Variables and data structures of the language in which the IR code
generator is written
• Blue: Names of relevant procedures used in IR code generation.
• Black: All other stuff.

JJ J I II G O B ACK F ULL S CREEN C LOSE 436 OF 600


PL April 2, 2020
IR: Expressions
E → id 

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)

JJ J I II G O B ACK F ULL S CREEN C LOSE 437 OF 600


PL April 2, 2020
The WHILE Language
Assume there is a language of expressions (with start symbol E) over
which the statements are defined. For simplicity assume these are the
only constructs of the language.
S → id := E Assignment
| S; S Sequencing
| if E then S else Sf i Conditional
| while E do S end Iteration

JJ J I II G O B ACK F ULL S CREEN C LOSE 438 OF 600


PL April 2, 2020
IR: Assignment and Sequencing
S → id := E 

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:)

JJ J I II G O B ACK F ULL S CREEN C LOSE 439 OF 600


PL April 2, 2020
IR: Conditional
S0 → if E then S1 else S2f i 

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:)

JJ J I II G O B ACK F ULL S CREEN C LOSE 440 OF 600


PL April 2, 2020
IR: Iteration
S0 → while E do S1 end 

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:)

JJ J I II G O B ACK F ULL S CREEN C LOSE 441 OF 600


PL April 2, 2020
IR: Generation End
While generating the intermediate representation, it is sometimes nec-
essary to generate jumps into code that has not been generated as yet
(hence the address of the label is unknown). This usually happens while
processing
• forward jumps
• short-circuit evaluation of boolean expressions
It is usual in such circumstances to either fill up the empty label entries
in a second pass over the the code or through a process of backpatching
(which is the maintenance of lists of jumps to the same instruction num-
ber), wherein the blank entries are filled in once the sequence number
of the target instruction becomes known.

JJ J I II G O B ACK F ULL S CREEN C LOSE 442 OF 600


PL April 2, 2020
JJ J I II G O B ACK F ULL S CREEN C LOSE 443 OF 600
PL April 2, 2020
JJ J I II G O B ACK F ULL S CREEN C LOSE 444 OF 600
PL April 2, 2020
11. The Pure Untyped Lambda Calculus: Basics

Pure Untyped λ-Calculus: Syntax


The language Λ of pure untyped λ-terms is the smallest set of terms
built up from an infinite set V of variables and closed under the following
productions

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).

JJ J I II G O B ACK F ULL S CREEN C LOSE 446 OF 600


PL April 2, 2020
Notational Conventions
To minimize use of brackets unambiguously
1. λx1x2 . . . xm[L] denotes λx1[λx2[. . . λxm[L] · · · ]] i.e. L is the scope of
each of the variables x1, x2, . . . xm.
2. (L1 L2 · · · Lm) denotes (· · · (L1 L2) · · · Lm) i.e. application is left-
associative.

JJ J I II G O B ACK F ULL S CREEN C LOSE 447 OF 600


PL April 2, 2020
Substitution
Definition 11.2 For any terms L, M and N and any variable x, the sub-
stitution of the term N for a variable x is defined as follows:
{N /x}x ≡ N
{N /x}y ≡ y if y 6≡ x
{N /x}λx[L] ≡ λx[L]
{N /x}λy[L] ≡ λy[{N /x}L] if y 6≡ x and y 6∈ F V (N )
{N /x}λy[L] ≡ λz[{N /x}{z/y}L] if y 6≡ x and y ∈ F V (N ) and z is ’fres
{N /x}(L M ) ≡ ({N /x}L {N /x}M )
• In the above definition it is necessary to ensure that the free variables
of N continue to remain free after substitution i.e. none of the free
variables of N should be ”captured” as a result of the substitution.
• The phrase “z is ’fresh”’ may be taken to mean z 6∈ F V (N ) ∪ V ar(L).
• z could be fresh even if z ∈ BV (N )

JJ J I II G O B ACK F ULL S CREEN C LOSE 448 OF 600


PL April 2, 2020
Compatibility
Definition 11.3 A binary relation ρ ⊆ Λ × Λ is said to be compatible if
L ρ M implies
1. for all variables x, λx[L] ρ λx[M ] and
2. for all terms N , (L N ) ρ (M N ) and (N L) ρ (N M ).

JJ J I II G O B ACK F ULL S CREEN C LOSE 449 OF 600


PL April 2, 2020
Compatible Closure
Definition 11.4 The compatible closure of a relation ρ ⊆ Λ × Λ is the
smallest (under the ⊆ ordering) relation ρc ⊆ Λ × Λ such that
LρM L ρc M
ρ ρAbs
L ρc M λx[L] ρc λx[M ]

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.

JJ J I II G O B ACK F ULL S CREEN C LOSE 451 OF 600


PL April 2, 2020
Untyped λ-Calculus: Basic β -Reduction
Definition 11.8
• Any (sub-)term of the form (λx[L] M ) is called a β-redex
• Basic β-reduction is the relation on Λ
df
→β = {((λx[L] M ), {M /x}L0) | L0 ≡α L, L0, L, M ∈ Λ}
• It is usually represented by the axiom
(λx[L] M ) →β {M /x}L0 (4)
where L0 ≡α L.

JJ J I II G O B ACK F ULL S CREEN C LOSE 452 OF 600


PL April 2, 2020
Untyped λ-Calculus: 1-step β -Reduction
Definition 11.9 A 1-step β-reduction →1β is the smallest relation (under
the ⊆ ordering) on Λ such that
L →β M L →1β M
β1 1 β1Abs
L →β M λx[L] →1β λx[M ]

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 II G O B ACK F ULL S CREEN C LOSE 453 OF 600


PL April 2, 2020
Untyped λ-Calculus: β -Reduction
Definition 11.10
• For all integers n ≥ 0, n-step β-reduction →nβ is defined by induction
on 1-step β-reduction
L →mβ M → 1 N
β
βnBasis βnInduction (m ≥ 0)
L →0β L L →βm+1 N
• β-reduction →∗β is the reflexive-transitive closure of 1-step β-reduction.
That is,
L →nβ M
β∗ ∗ (n ≥ 0)
L →β M

JJ J I II G O B ACK F ULL S CREEN C LOSE 454 OF 600


PL April 2, 2020
Untyped λ-Calculus: Normalization
Definition 11.11
• A term is called a β-normal form (β-nf) if it has no β-redexes.
• A term is weakly normalising (β-WN) if it can reduce to a β-normal
form.
• A term L is strongly normalising (β-SN) if it has no infinite reduction
sequence L →1β L1 →1β L2 →1β · · ·

JJ J I II G O B ACK F ULL S CREEN C LOSE 455 OF 600


PL April 2, 2020
Untyped λ-Calculus: Examples
Example 11.12
df df df df
1. K = λx y[x], I = λx[x], S = λx y z[((x z) (y z))], ω = λx[(x x)] are all
β-nfs.
df
2. Ω = (ω ω) has no β-nf. Hence it is neither weakly nor strongly normal-
ising.
3. (K (ω ω)) cannot reduce to any normal form because it has no finite
reduction sequences. All its reductions are of the form
(K (ω ω)) →1β (K (ω ω)) →1β (K (ω ω)) →1β · · ·
or at some point it could transform to
(K (ω ω)) →1β λy[(ω ω)] →1β λy[(ω ω)] →1β · · ·

4. ((K ω) Ω) is weakly normalising because it can reduce to the normal


form ω but it is not strongly normalising because it also has an infinite
reduction sequence

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

JJ J I II G O B ACK F ULL S CREEN C LOSE 457 OF 600


PL April 2, 2020
JJ J I II G O B ACK F ULL S CREEN C LOSE 458 OF 600
PL April 2, 2020
JJ J I II G O B ACK F ULL S CREEN C LOSE 459 OF 600
PL April 2, 2020
12. Notions of Reduction

Notions of Reduction

JJ J I II G O B ACK F ULL S CREEN C LOSE 460 OF 600


PL April 2, 2020
Reduction
For any function such as p = λx[3.x.x + 4.x + 1],
(p 2) = 3.2.2 + 4.2 + 1 = 21
However there is something asymmetric about the identity, in the sense
that while (p 2) deterministically produces 3.2.2 + 4.2 + 1 which in turn
simplifies deterministically to 21, it is not possible to deterministically in-
fer that 21 came from (p 2). It would be more accurate to refer to this
sequence as a reduction sequence and capture the asymmetry as follows:
(p 2) ; 3.2.2 + 4.2 + 1 ; 21
And yet they are behaviourally equivalent and mutually substitutable in all
contexts (referentially transparent).
1. Reduction (specifically β-reduction) captures this asymmetry.
2. Since reduction produces behaviourally equal terms we have the fol-
lowing notion of equality.
JJ J I II G O B ACK F ULL S CREEN C LOSE 461 OF 600
PL April 2, 2020
Untyped λ-Calculus: β -Equality
Definition 12.1 β-equality or β-conversion (denoted =β ) is the smallest
equivalence relation containing β-reduction (→∗β ).
The following are equivalent definitions.
1. =β is the reflexive-symmetric-transitive closure of 1-step β-reduction.
2. =β is the smallest relation defined by the following rules.
L →∗β M
=β Basis =β Reflexivity
L =β M L =β L
L =β M L =β M , M =β N
=β Symmetry =β Transitivity
M =β L L =β N

JJ J I II G O B ACK F ULL S CREEN C LOSE 462 OF 600


PL April 2, 2020
The Paradoxical Combinator
Example 12.2 Consider Curry’s paradoxical combinator
df
YC = λf [(C C)]
where
df
C = λx[(f (x x))]
For any term L we have
(YC L) →1β (λx[(L (x x))] λx[(L (x x))])
≡α (λy[(L (y y))] λx[(L (x x))])
→1β (L (λx[(L
| (x x))]{zλx[(L (x x))])})

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

(YC L) =β (L (YC L)) =β (L (L (YC L))) =β (L (L (L (YC L)))) =β · · ·

Many other researchers have defined other combinators which mimic the behaviour of the combinator YC. Of

JJ J I II G O B ACK F ULL S CREEN C LOSE 464 OF 600


PL April 2, 2020
df df
particular interest is Turing’s combinator YT = (T T) where T = λx y[(y ((x x) y))]. Notice that
(T T)
≡ (λx y[(y ((x x) y))] T)
→1β λy[(y ((T T) y))]
≡ λy[(y (YT y))]
from which, by compatible closure, for any term L we get
(YT L)
≡ ((A A) L)
→∗β (λy[(y (YT y))] L)
→1β (L (YT L))
Thus YT is also a recursion unfolding combinator yielding

(YT L) =β (L (YT L)) =β (L (L (YT L))) =β (L (L (L (YT L)))) =β · · ·

JJ J I II G O B ACK F ULL S CREEN C LOSE 465 OF 600


PL April 2, 2020
Compatibility of Beta-reduction and
Beta-Equality
Theorem 12.3 β-reduction →∗β and β-equality =β are both compatible
relations.
2

JJ J I II G O B ACK F ULL S CREEN C LOSE 466 OF 600


PL April 2, 2020
Proof of theorem 12.3
Proof: (→∗β ) Assume L →∗β M . By definition of β-reduction L →nβ M for some n ≥ 0. The proof proceeds by
induction on n

Basis. n = 0. Then L ≡ M and there is nothing to prove.


Induction Hypothesis (IH).

The proof holds for all k, 0 ≤ k ≤ m for some m ≥ 0.

Induction Step. For n = m + 1, let L ≡ L0 →m 1


β Lm →β M . Then by the induction hypothesis and the compati-
bility of →1β we have

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

JJ J I II G O B ACK F ULL S CREEN C LOSE 468 OF 600


PL April 2, 2020
Eta reduction
Given any term M and a variable x 6∈ F V (M ), the syntax allows us to
construct the term λx[(M x)] such that for every term N we have
(λx[(M x)] N ) →1β (M N )
In other words,
(λx[(M x)] N ) =β (M N ) for all terms N
We say that the two terms λx[(M x)] and M are extensionally equiva-
lent i.e. they are syntactically distinct but there is no way to distinguish
between their behaviours.
So we define basic η-reduction as the relation
λx[(L x)] →η L provided x 6∈ F V (L) (5)

JJ J I II G O B ACK F ULL S CREEN C LOSE 469 OF 600


PL April 2, 2020
Eta-Reduction and Eta-Equality
The following notions are then defined similar to the corresponding no-
tions for β-reduction.
• 1-step η-reduction →1η is the closure of basic η-reduction to all con-
texts,
• →nη is defined by induction on 1-step η-reduction
• η-reduction →∗η is the reflexive-transitive closure of 1-step η-reduction.
• the notions of strong and weak η normal forms η-nf.
• the notion of η-equality or η-conversion denoted by =η .

JJ J I II G O B ACK F ULL S CREEN C LOSE 470 OF 600


PL April 2, 2020
Exercise 12.1

1. Prove that η-reduction and η-equality are both compatible relations.


2. Prove that η-reduction is strongly normalising.
3. Define basic βη-reduction as the application of either (4) or (5). Now prove that →1βη , →∗βη and =βη are all compatible relations.

JJ J I II G O B ACK F ULL S CREEN C LOSE 471 OF 600


PL April 2, 2020
JJ J I II G O B ACK F ULL S CREEN C LOSE 472 OF 600
PL April 2, 2020
13. Confluence Definitions

Confluence: Definitions

JJ J I II G O B ACK F ULL S CREEN C LOSE 473 OF 600


PL April 2, 2020
Reduction Relations
Definition 13.1 For any binary relation ρ on Λ
1. ρ1 is the compatible closure of ρ
2. ρ+ is the transitive closure of ρ1
3. ρ∗ is the reflexive-transitive-closure of ρ1 and is a preorder
4. ((ρ1) ∪ (ρ1)−1)∗ (denoted =ρ) is the reflexive-symmetric-transitive clo-
sure of ρ1 and is an equivalence relation.
5. =ρ is also called the equivalence generated by ρ.
We will often use −→ (suitably decorated) as a reduction relation instead

of ρ. Then −→1, −→+, −→∗ and ←→ denote respectively the compat-
icble closure, the transitive closure, the reflexive transitive closure and
the equivalence generated by −→

JJ J I II G O B ACK F ULL S CREEN C LOSE 474 OF 600


PL April 2, 2020
The Diamond Property
Definition 13.2 Let ρ be any relation on terms. ρ has the diamond prop-
erty if for all L, M , N ,
M M
ρ ρ
L ⇒ ∃P : P
ρ ρ
N N
We often use a decorated version of the symbol −→ for a reduction re-
lation and depict the diamond property as
M
−→

−→
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

JJ J I II G O B ACK F ULL S CREEN C LOSE 476 OF 600


PL April 2, 2020
13.1. Why confluence?

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.

JJ J I II G O B ACK F ULL S CREEN C LOSE 477 OF 600


PL April 2, 2020
Reduction: Local Confluence
Definition 13.5 −→ is locally confluent if for all L, M , N ,
N ←− L −→ M ⇒ ∃P : N −→∗ P ∗←− M
which we denote by
M

−→
−→


L ⇒∃ P


−→

−→
N

JJ J I II G O B ACK F ULL S CREEN C LOSE 478 OF 600


PL April 2, 2020
Reduction: Semi-confluence
Definition 13.6 −→ is semi-confluent if for all L, M , N ,
N ←− L −→∗ M ⇒ ∃P : N −→∗ P ∗←− M
which we denote by
M

−→
−→


L ⇒∃ P


−→

−→

JJ J I II G O B ACK F ULL S CREEN C LOSE 479 OF 600


PL April 2, 2020
Reduction: Confluence
Definition 13.7 −→ is confluent if for all L, M , N ,
N ∗←− L −→∗ M ⇒ ∃P : N −→∗ P ∗←− M
which we denote as
M

−→
−→


L ⇒∃ P


−→

−→

N
Fact 13.8 Any confluent relation is also semi-confluent.


JJ J I II G O B ACK F ULL S CREEN C LOSE 480 OF 600


PL April 2, 2020
Reduction: Church-Rosser
Definition 13.9 −→ is Church-Rosser if for all L, M ,

L ←→ M ⇒ ∃P : L −→∗ P ∗←− M
which we denote by

L ←→ M

−→
−→


∃P

JJ J I II G O B ACK F ULL S CREEN C LOSE 481 OF 600


PL April 2, 2020
Equivalence Characterization
Lemma 13.10

1. ←→ is the least equivalence containing −→.

2. ←→ is the least equivalence containing −→∗.

3. L ←→ M if and only if there exists a finite sequence L ≡
M0, M1, . . . Mm ≡ M , m ≥ 0 such that for each i, 0 ≤ i < m,
Mi −→ Mi+1 or Mi+1 −→ Mi. We represent this fact more succinctly
as
L ≡α M0 −→ / ←− M1 −→ / ←− · · · −→ / ←− Mm ≡α M (6)

JJ J I II G O B ACK F ULL S CREEN C LOSE 482 OF 600


PL April 2, 2020
JJ J I II G O B ACK F ULL S CREEN C LOSE 483 OF 600
PL April 2, 2020
Proof of lemma 13.10
Proof:

1. Just prove that ←→ is a subset of every equivalence that contains −→.
2. Use induction on the length of proofs to prove this part

3. For the last part it is easy to see that the existence of the “chain equation” (6) implies L ←→ M by transitivity. For the other part use
induction on the length of the proof.

QED

JJ J I II G O B ACK F ULL S CREEN C LOSE 484 OF 600


PL April 2, 2020
JJ J I II G O B ACK F ULL S CREEN C LOSE 485 OF 600
PL April 2, 2020
14. Confluence: Church-Rosser

The Church-Rosser Property

JJ J I II G O B ACK F ULL S CREEN C LOSE 486 OF 600


PL April 2, 2020
Confluence and Church-Rosser
Lemma 14.1 Every confluent relation is also semi-confluent

Theorem 14.2 The following statements are equivalent for any reduction
relation −→.
1. −→ is Church-Rosser.
2. −→ is confluent.

JJ J I II G O B ACK F ULL S CREEN C LOSE 487 OF 600


PL April 2, 2020
Proof of theorem 14.2
Proof: (1 ⇒ 2) Assume −→ is Church-Rosser and let
N ∗←− L −→∗ M

Clearly then N ←→ M . If −→ is Church-Rosser then
∃P : N −→∗ P ∗←− M
which implies that it is confluent.

(2 ⇒ 1) Assume −→ is confluent and let L ←→ M . We proceed by induction on the length of the chain (6).
L ≡α M0 −→ / ←− M1 −→ / ←− · · · −→ / ←− Mm ≡α M

Basis. m = 0. This case is trivial since for any P , L −→∗ P iff M −→∗ P
Induction Hypothesis (IH).

The claim is true for all chains of length k, 0 ≤ k < m.

Induction Step. Assume the chain is of length m = k + 1. i.e.


L ≡α M0 −→ / ←− M1 −→ / ←− · · · −→ / ←− Mk −→ / ←− Mk+1 ≡α M
Case Mk −→ M . Then by the induction hypothesis and semi-confluence we have

L ←→ Mk
−→
−→


−→

∃Q M
−→
−→


∃P

which proves the claim.


JJ J I II G O B ACK F ULL S CREEN C LOSE 488 OF 600
PL April 2, 2020
Case Mk ←− M . Then the claim follows from the induction hypothesis and the following diagram

L ←→ Mk ←− M

−→
−→


∃P

QED

Lemma 14.3 If a terminating relation is locally confluent then it is semi-confluent.

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.

Theorem 14.4 If a terminating relation is locally confluent then it is confluent.

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

Fact 14.5 The graph G(L) is acyclic for any term L.

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.

Fact 14.6 For every M ∈ Γ(L),

1. G(M ) is a sub-graph of G(L) and


2. For every M ∈ Γ(L) − {L}, the length of the longest reduction sequence of M is less than p.

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

N ∗←− N1 1←− L −→1 M1 −→∗ M (9)

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

Theorem 14.7 If a terminating relation is locally confluent then it is Church-Rosser.

Proof: Follows from theorem 14.4 and theorem 14.2 QED

JJ J I II G O B ACK F ULL S CREEN C LOSE 491 OF 600


PL April 2, 2020
JJ J I II G O B ACK F ULL S CREEN C LOSE 492 OF 600
PL April 2, 2020
15. The Church-Rosser Property

The Church-Rosser Property

JJ J I II G O B ACK F ULL S CREEN C LOSE 493 OF 600


PL April 2, 2020
Parallel Beta Reduction
Definition 15.1 The parallel-β or ||β reduction is the smallest relation for
which the following rules hold.

L −→1||β L0
||β1 L −→1 L ||β1Abs1
||β λx[L] −→1||β λx[L0]

L −→1||β L0, M −→1||β M 0 L −→1||β L0, M −→1||β M


||β1App ||β1Abs2
(L M ) −→1||β (L0 M 0) (λx[L] M ) −→1||β {M 0/x

JJ J I II G O B ACK F ULL S CREEN C LOSE 494 OF 600


PL April 2, 2020
Parallel Beta: The Diamond Property
Lemma 15.2
1. L −→1β L0 ⇒ L −→1||β L0.
2. L −→1||β L0 ⇒ L −→∗β L0.
3. The smallest preorder containing −→1||β is −→∗||β =−→∗β .
4. If L −→1β L0 and M −→1||β M 0 then {M /x}L −→1||β {M 0/x}L0.
Proof: By induction on the structure of terms or by induction on the
number of steps in any proof. QED
Theorem 15.3 −→1||β has the diamond property.

JJ J I II G O B ACK F ULL S CREEN C LOSE 495 OF 600


PL April 2, 2020
Proof of theorem 15.3
Proof: We need to prove for all L
N 1||β←− L −→1||β M ⇒ ∃P : N −→1||β P 1
||β←− M
We prove this by induction on the structure of L and a case analysis of the rule applied in definition 15.1.
Case L ≡ x ∈ V . Then L ≡ M ≡ N ≡ P .
Before dealing with the other inductive cases we dispose of some trivial sub-cases that arise in some or all of them.
Case L ≡α M . Choose P ≡α N to complete the diamond.
Case L ≡α N . Then choose P ≡α M .
Case M ≡α N . Then there is nothing to prove.
In the sequel we assume N 6≡α L 6≡α M 6≡α N and proceed by induction on the structure of L.
Case L ≡ λx[L1]. Then clearly M and N were both obtained in proofs whose last step was an application of rule
||β1Abs1 and so M ≡ λx[M1] and N ≡ λx[N1] for some M1 and N1 respectively and hence N1 1||β←− L1 −→1||β M1.
By the induction hypothesis we have
∃P1 : N1 −→1||β P1 1||β←− M1
Hence by choosing P ≡ λx[P1] we obtain the required result.
Case L ≡ (L1 L2) and L1 is not an abstraction.
The rule ||β1App is the only rule that must have been applicable in the last step of the proofs of N 1||β←− L −→1||β
M . Clearly then there exist M1, M2, N1, N2 such that N1 1||β←− L1 −→1||β M1 and N2 1||β←− L2 −→1||β M2. Again
by the induction hypothesis, we have
∃P1 : N1 −→1||β P1 1||β←− M1
JJ J I II G O B ACK F ULL S CREEN C LOSE 496 OF 600
PL April 2, 2020
and
∃P2 : N2 −→1||β P2 1||β←− M2
By choosing P ≡ (P1 P2) we obtain the desired result.
Case L ≡ (λx[L1] L2).
Here we have four sub-cases depending upon whether each of M and N were obtained by an application of ||β1App
or ||β1Abs2. Of these the sub-case when both M and N were obtained by applying ||β1App is easy and similar to
the previous case. That leaves us with three subscases.

Sub-case: Both M and N were obtained by applying rule ||β1Abs2.


Then we have
{N2/x}N1 ≡ N 1||β←− L ≡ (λx[L1] L2) −→1||β M ≡ {M2/x}M1
for some M1, M2, N1, N2 such that
N1 1||β←− L1 −→1||β M1
and
N2 1||β←− L2 −→1||β M2
By the induction hypothesis
∃P1 : N1 −→1||β P1 1||β←− M1
and
∃P2 : N2 −→1||β P2 1||β←− M2
and the last part of lemma 15.2 we have
∃P ≡ {P2/x}P1 : N −→1||β P 1
||β←− M
JJ J I II G O B ACK F ULL S CREEN C LOSE 497 OF 600
PL April 2, 2020
completing the proof.
Sub-case: M was obtained by applying rule ||β1Abs2 and N by ||β1App.
Then we have the form
(λx[N1] N2) ≡ N 1||β←− L ≡ (λx[L1] L2) −→1||β M ≡ {M2/x}M1
where again
N1 1||β←− L1 −→1||β M1
and
N2 1||β←− L2 −→1||β M2
By the induction hypothesis
∃P1 : N1 −→1||β P1 1||β←− M1
and
∃P2 : N2 −→1||β P2 1||β←− M2
and finally we have
∃P ≡ {P2/x}P1 : N −→1||β P 1
||β←− M
completing the proof.
Sub-case: M was obtained by applying rule ||β1App and N by ||β1Abs2.
Similar to the previous sub-case.

QED

Theorem 15.4 −→1||β is confluent.

JJ J I II G O B ACK F ULL S CREEN C LOSE 498 OF 600


PL April 2, 2020
Proof: We need to show that for all L, M , N ,
N ∗||β←− L −→∗||β M ⇒ ∃P : N −→∗||β P ∗
||β←− M
We prove this by induction on the length of the sequences
L −→1||β M1 −→1||β M2 −→1||β · · · −→1||β Mm ≡ M
and
L −→1||β N1 −→1||β N2 −→1||β · · · −→1||β Nn ≡ N
where m, n ≥ 0. More specifically we prove this by induction on the pairs of integers (j, i) bounded by (n, m),
where (j, i) < (j 0, i0) if and only if either j < j 0 or (j = j 0) and i < i0. The interesting cases are those where both
m, n > 0. So we repeatedly apply theorem 15.3 to complete the rectangle
L −→1||β M1 −→1||β M2 −→1||β · · · −→1||β Mm ≡ M
||β↓1 ||β↓1 ||β↓1 ··· ||β↓1
N1 −→||β P11 −→1||β
1
P12 −→1||β · · · −→1||β P1m
||β↓1 ||β↓1 ||β↓1 ··· ||β↓1
... ... ... ··· ...
||β↓1 ||β↓1 ||β↓1 ··· ||β↓1
Nn −→1||β Pn1 −→1||β Pn2 −→1||β · · · −→1||β Pnm ≡ P
QED 

Corollary 15.5 −→1β is confluent.

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

Corollary 15.7 −→1β is Church-Rosser.

Proof: Follows from corollary 15.5 and theorem 14.2. QED

JJ J I II G O B ACK F ULL S CREEN C LOSE 500 OF 600


PL April 2, 2020
JJ J I II G O B ACK F ULL S CREEN C LOSE 501 OF 600
PL April 2, 2020
16. An Applied Lambda-Calculus

16.1. FL with recursion

An Applied Lambda-Calculus

JJ J I II G O B ACK F ULL S CREEN C LOSE 502 OF 600


PL April 2, 2020
A Simple Language of Terms: FL0
Let X be an infinite collection of variables (names). Consider the lan-
guage (actually a collection of abstract syntax trees) of terms TΩ(X) de-
fined by the following constructors (along with their intended meanings).
Construct Arity Informal Meaning
Z 0 The number 0
T 0 The truth value true
F 0 The truth value false
P 1 The predecessor function on numbers
S 1 The successor function on numbers
ITE 3 The if-then-else construct (on numbers and truth values)
IZ 1 The is-zero predicate on numbers
GTZ 1 The greater-than-zero predicate on numbers

JJ J I II G O B ACK F ULL S CREEN C LOSE 503 OF 600


PL April 2, 2020
FL(X): Language, Datatype or Instruction
Set?
The set of terms TΩ(X) may be alternatively defined by the BNF:
t ::= x ∈ X Z (P t) (S t) T F (ITE ht, t1, t0i) (IZ t) (GTZ t)
(14)
• It could be thought of as a user-defined data-type
• It could be thought of as the instruction-set of a particularly simple
hardware machine.
• It could be thought of as a simple functional programming language
without recursion.
• It is a language with two simple types of data: integers and booleans
• Notice that the constructor (ITE ht, t1, t0i) is overloaded.

JJ J I II G O B ACK F ULL S CREEN C LOSE 504 OF 600


PL April 2, 2020
Extending the language
To make this simple language safe we require
Type-checking : to ensure that arbitrary expressions are not mixed in
ways they are not “intended” to be used. For example
• t cannot be a boolean expression in S(t), P(t), IZ(t) and GTZ(t)
• ITE(t, t1, t0) may be used as a conditional expression for both inte-
gers and booleans, but t needs to be a boolean and either both t1
and t0 are integer expressions or both are boolean expressions.
Functions : To be a useful programming language we need to be able to
define functions.
Recursion : to be able to define complex functions in a well-typed fashion.
Recursion should also be well-typed

JJ J I II G O B ACK F ULL S CREEN C LOSE 505 OF 600


PL April 2, 2020
Typing FL Expressions
We have only two types of objects – integers and booleans which we
represent by int and bool respectively. We then have the following el-
ementary typing annotations for the expressions, which may be obtained
by pattern matching.
1. Z : int
2. T : bool
3. F : bool
4. S : int → int
5. P : int → int
6. IZ : int → bool
7. GTZ : int → bool
8. ITEI : bool ∗ int ∗ int → int
9. ITEB : bool ∗ bool ∗ bool → bool
JJ J I II G O B ACK F ULL S CREEN C LOSE 506 OF 600
PL April 2, 2020
Λ +FL(X): The Power of Functions
To make the language powerful we require the ability to define functions,
both non-recursive and recursive. We define an applied lambda-calculus
of lambda terms ΛΩ(X) over this set of terms as follows:

L, M , N ::= t ∈ TΩ(X) λx[L] (L M ) (15)


This is two-level grammar combining the term grammar (14) with λ-
abstraction and λ-application.
While this makes it possible to use the operators of TΩ(X) as part of
functions (λ-expressions), it does not allow us to use the operators of
TΩ(X) outside of λ-abstractions and λ-applications.

JJ J I II G O B ACK F ULL S CREEN C LOSE 507 OF 600


PL April 2, 2020
Λ+FL(X): Lack of Higher-order Power?
Example 16.1 The grammar (15) does not allow us to define expressions
such as the following:
1. the successor of the result of an application (S (L M )) where (L M )
yields an integer value.
2. higher order conditionals e.g. λx[(ITE h(L x), (M x), (N x)i)] where
(L x) yields a boolean value for an argument of the appropriate type.
3. In general, it does not allow the constructors to be applied to λ-
expressions.
So we extend the language by allowing a free intermixing of λ-terms and
terms of the sub-language TΩ(X).

JJ J I II G O B ACK F ULL S CREEN C LOSE 508 OF 600


PL April 2, 2020
ΛF L(X) : Higher order functions
We need to flatten the grammar of (15) to allow λ-terms also to be used
as arguments of the constructors of the term-grammar (14). The lan-
guage of applied λ-terms (viz. ΛΩ(X)) now is defined by the grammar.
L, M , N ::= x ∈ X Z
(P L) (S L)
T F
(16)
(IZ L) (GTZ L)
(ITE hL, M, N i)
λx[L] (L M )

JJ J I II G O B ACK F ULL S CREEN C LOSE 509 OF 600


PL April 2, 2020
Unfortunately the result of flattening the grammar leads to an even larger number of meaningless expressions (in
particular, we may be able to generate self-referential ones or ones that may not even be interpretable as functions
which yield integer or boolean values.
It is therefore imperative that we define a type-checking mechanism to rule out meaningless expressions. As men-
tioned before, type-checking is not context-free and hence cannot be done through mechanisms such as scanning
and parsing and will have to be done separately before any code-generation takes place.
We will in fact, go a step further and design a type-inferencing mechanism that will prevent meaningless expressions
from being allowed.
Further, given a well-typed expression we need to be able to define a meaning for each expression that is somehow
compatible with our intuitive understanding of what λ-expressions involving integer and boolean operations mean.
This meaning is defined through an operational semantics i.e. a system of transitions on how computation actually
takes place for each expression. We define this through a reduction mechanism that is consistent with reduction
relations that we have earlier studied for the untyped λ-calculus.
In order for it to be compatible with the notions of reduction in the λ-calculus we require to define a notion of
reduction first for expressions that do not involve either λ abstraction or λ application. We refer to this notion of
reduction as δ-reduction. Furthermore we need to be able to define δ-normal forms for these expressions. Since the
language is completely symbolic, these normal forms would serve as the final answers obtained in the evaluation
of these expressions.

JJ J I II G O B ACK F ULL S CREEN C LOSE 510 OF 600


PL April 2, 2020
The Normal forms for Integers
We need reduction rules to simplify (non-recursive) expressions.
Zero . Z is the unique representation of the number 0 and every integer
expression that is equal to 0 must be reducible to Z.
Positive integers . Each positive integer k is uniquely represented by the
expression Sk (Z) where the super-script k denotes a k-fold application
of S.
Negative integers . Each negative integer −k is uniquely represented by
the expression Pk (Z) where the super-script k denotes a k-fold appli-
cation of P.
δ-rules

(P (S x)) −→δ x (17)


(S (P x)) −→δ x (18)

JJ J I II G O B ACK F ULL S CREEN C LOSE 511 OF 600


PL April 2, 2020
Reduction Rules for Boolean Expressions
Pure Boolean Reductions . The constructs T and F are the normal forms
for boolean values.

(ITE hb, x, xi) −→δ x (19)


(ITE hT, x, yi) −→δ x (20)
(ITE hF, x, yi) −→δ y (21)

Testing for zero .

(IZ Z) −→δ T (22)


(IZ (S n)) −→δ F, where (S n) is a δ-nf (23)
(IZ (P n)) −→δ F, where (P n) is a δ-nf (24)

JJ J I II G O B ACK F ULL S CREEN C LOSE 512 OF 600


PL April 2, 2020
Testing for Positivity

(GTZ Z) −→δ F (25)


(GTZ (S n)) −→δ T, where (S n) is a δ-nf (26)
(GTZ (P n)) −→δ F, where (P n) is a δ-nf (27)

JJ J I II G O B ACK F ULL S CREEN C LOSE 513 OF 600


PL April 2, 2020
Other Non-recursive Operators
We may “program” the other boolean operations as follows:
df
NOT = λx[ITE hx, F, Ti]
df
AND = λhx, yi[ITE hx, y, Fi]
df
OR = λhx, yi[ITE hx, T, yi]
We may also “program” the other integer comparison operations as fol-
lows:
df
GEZ = λx[OR h(IZ x), (GTZ x)i]
df
LTZ = λx[NOT (GEZ x)]
df
LEZ = λx[OR h(IZx), (LTZ x)i]

JJ J I II G O B ACK F ULL S CREEN C LOSE 514 OF 600


PL April 2, 2020
Exercise 16.1

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)?

JJ J I II G O B ACK F ULL S CREEN C LOSE 515 OF 600


PL April 2, 2020
Recursion in the Applied Lambda-calculus
The full power of a programming language will not be realised without
a recursion mechanism. The untyped lambda-calculus has “paradoxical
combinators” which behave like recursion operators upto =β .
Definition 16.2 A combinator Y is called a fixed-point combinator if for
every lambda term L, (Y L) =β (L (Y L))
Curry’s Y combinator (YC)
df df
YC = λf [(C C)] where C = λx[(f (x x))]
Turing’s Y combinator (YT)
df df
YT = (T T) where T = λy x[(x (y y x ))]
But the various Y combinators unfortunately will not satisfy any typing
rules that we may define for the language, because they are all “self-
applicative” in nature.
JJ J I II G O B ACK F ULL S CREEN C LOSE 516 OF 600
PL April 2, 2020
ΛRecF L(X): Adding Recursion
Instead it is more convenient to use the fixed-point property and define
a new constructor with a δ-rule which satisfies the fixed-point property
(definition 16.2).
We extend the language FL with a new constructor
L ::= . . . (REC L)
and add the fixed point property as a δ-rule

(REC L) −→δ (L (REC L)) (28)

JJ J I II G O B ACK F ULL S CREEN C LOSE 517 OF 600


PL April 2, 2020
Recursion Example: Addition
Consider addition on integers as a binary operation to be defined in this
language. We use the following properties of addition on the integers to
define it by induction on the first argument.

y if x = 0
x + y = (x − 1) + (y + 1) if x > 0 (29)
(x + 1) + (y − 1) if x < 0

JJ J I II G O B ACK F ULL S CREEN C LOSE 518 OF 600


PL April 2, 2020
Using the constructors of ΛRecF L(X) we require that any (curried) definition of addition on numbers should be a
solution to the following equation in ΛRecF L(X) for all (integer) expression values of x and y.
(plusc x y) =βδ ITE h(IZ x), y, ITE h(GTZ x), (plusc (P x) (S y)), (plusc (S x) (P y))ii (30)

Equation (30) may be rewritten using abstraction as follows:

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)

JJ J I II G O B ACK F ULL S CREEN C LOSE 519 OF 600


PL April 2, 2020
Hence we may regard (REC addc) where addc is defined by right-hand-side of definition (32) as the required solution
to the equation (30) in which plusc is an unknown.
The abstraction shown in (32) and the claims (16.3) and (16.4) simply go to show that M ≡α λf [{f /z}L] is a
solution to the equation z =βδ L, whenever such a solution does exist. Further, the claims also show that we may
“unfold” the recursion (on demand) by simply performing the substitution {L/z}L for each free occurrence of z
within L.

JJ J I II G O B ACK F ULL S CREEN C LOSE 520 OF 600


PL April 2, 2020
Exercise 16.2

1. Prove that the relation −→δ is confluent.


2. The language FL does not have any operators that take boolean arguments and yields integer values. Define a standard conversion
function B2I which maps the value F to Z and T to (S Z).
3. Prove that YC and YT are both fixed-point combinators.
4. Using the combinator add and the other constructs of ΛΣ (X) to
(a) define the equation for products of numbers in the language.
(b) define the multiplication operation mult on integers and prove that it satisfies the equation(s) for products.
5. The equation (29) is defined conditionally. However the following is equally valid for all integer values x and y.
x + y = (x − 1) + (y + 1) (36)
(a) Follow the steps used in the construction of addc to define a new applied addc0 that instead uses equation (36).
(b) Is (REC addc0 ) =βδ (addc0 (REC addc0 ))?
(c) Is addc =βδ addc0 ?
(d) Is (REC addc) =βδ (REC addc0 )?
6. The function addc was defined in curried form. Use the pairing function in the untyped λ-calculus, to define
(a) addition and multiplication as binary functions independently of the existing functions.
(b) the binary ’curry’ function which takes a binary function and its arguments and creates a curried version of the binary function.

JJ J I II G O B ACK F ULL S CREEN C LOSE 521 OF 600


PL April 2, 2020
JJ J I II G O B ACK F ULL S CREEN C LOSE 522 OF 600
PL April 2, 2020
16.2. FL with type rules

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.

JJ J I II G O B ACK F ULL S CREEN C LOSE 523 OF 600


PL April 2, 2020
The Need for typing in FL
• A type is an important attribute of any variable, constant or expression,
since every such object can only be used in certain kinds of expres-
sions.
• Besides the need for type-checking rules on TΩ(X) to prevent illegal
constructor operations,
– rules are necessary to ensure that λ-applications occur only be-
tween terms of appropriate types in order to remain meaningful.
– rules are necessary to ensure that all terms have clearly defined
types at compile-time so that there are no run-time type violations.

JJ J I II G O B ACK F ULL S CREEN C LOSE 524 OF 600


PL April 2, 2020
TL: A Simple Language of Types
Consider the following language of types (in fully parenthesized form)
defined over an infinite collection 0a ∈ T V of type variables, disjoint fromt
he set of variables. We also have two type constants int and bool.
σ, τ ::= int bool 0a ∈ T V (σ∗τ ) (σ→τ )
Notes.
• int and bool are type constants.
• In any type expression τ , T V ar(τ ) is the set of type variables
• ∗ is the product operation on types and
• → is the function operator on types.
• We require ∗ because of the possibility of defining functions of various
kinds of arities in ΛΩ(X).

JJ J I II G O B ACK F ULL S CREEN C LOSE 525 OF 600


PL April 2, 2020
TL: Precedence and Associativity
• Precedence. We assume ∗ has a higher precedence than →.
• Associativity.
– ∗ is left associative whereas
– → is right associative

JJ J I II G O B ACK F ULL S CREEN C LOSE 526 OF 600


PL April 2, 2020
Type-inference Rules: Infrastructure
The question of assigning types to complicated expressions which may
have variables in them still remains to be addressed.
Type inferencing. Can be done using type assignment rules, by a recur-
sive travel of the abstract syntax tree.
Free variables (names) are already present in the environment (symbol
table).
Constants and Constructors. May have their types either pre-defined or
there may be axioms assigning them types.
Bound variables. May be necessary to introduce “fresh” type variables in
the environment.

JJ J I II G O B ACK F ULL S CREEN C LOSE 527 OF 600


PL April 2, 2020
Type Inferencing: Infrastructure
The elementary typing previously defined for the elementary expressions
of FL does not suffice
1. in the presence of λ abstraction and application, which allow for
higher-order functions to be defined
2. in the presence of polymorphism, especially when we do not want to
unnecessarily decorate expressions with their types.

JJ J I II G O B ACK F ULL S CREEN C LOSE 528 OF 600


PL April 2, 2020
Type Assignment: Infrastructure
• Assume Γ is the environmenta (an association list) which may be
looked up to determine the types of individual names. For each vari-
able x ∈ X, Γ(x) yields the type of x i.e. Γ(x) = σ if x : σ ∈ Γ.
• For each (sub-)expression in FL we define a set C of type constraints
of the form σ = τ , where T is the set of type variables used in C.
• The type constraints are defined by induction on the structure of the
expressions in the language FL.
• The expressions of FL could have free variables. The type of the ex-
pression would then depend on the types assigned to the free vari-
ables. This is a simple kind of polymorphism.
• It may be necessary to generate new type variables as and when re-
quired during the process of inferencing and assignment.
a
usually a part of the symbol table

JJ J I II G O B ACK F ULL S CREEN C LOSE 529 OF 600


PL April 2, 2020
Constraint Typing Relation
Definition 16.5 For each term L ∈ ΛΣ(X) the constraint typing relation
is of the form
Γ ` L : τ T C
where
• Γ is called the contexta and defines the stack of assumptionsb that may
be needed to assign a type (expression) to the (sub-)expression L.
• τ is the type(-expression) assigned to L
• C is the set of constraints
• T is the set of “fresh” type variables used in the (sub-)derivations
a
usually in the symbol table
b
including new type variables

JJ J I II G O B ACK F ULL S CREEN C LOSE 530 OF 600


PL April 2, 2020
Typing axioms: Basic 1
The following axioms (c.f Typing FL Expressions) may be applied during
the scanning and parsing phases of the compiler to assign types to the
individual tokens.
Z T F
Γ ` Z : int ∅ ∅ Γ ` T : bool ∅ ∅ Γ ` F : bool ∅ ∅

S P
Γ ` S : int→int ∅ ∅ Γ ` P : int→int ∅ ∅

IZ GTZ
Γ ` IZ : int→bool ∅ ∅ Γ ` GTZ : int→bool ∅ ∅

JJ J I II G O B ACK F ULL S CREEN C LOSE 531 OF 600


PL April 2, 2020
Typing axioms: Basic 2
ITEI
Γ ` ITE : bool∗int∗int→int ∅ ∅

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.

JJ J I II G O B ACK F ULL S CREEN C LOSE 532 OF 600


PL April 2, 2020
Type Rules for FL: 3
Var
Γ ` x : Γ(x) ∅ ∅

Γ, 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.

1. x : 0a, y : 0b, z : 0c ` x : 0a ∅ ∅ (Var)


2. x : 0a, y : 0b, z : 0c ` y : 0b ∅ ∅ (Var)
3. x : 0a, y : 0b, z : 0c ` z : 0c ∅ ∅ (Var)
4. x : 0a, y : 0b, z : 0c ` (y z) : 0d {0d} {0b = 0c→0d} (App)
5. x : 0a, y : 0b, z : 0c ` (x (y z)) : 0e {0d,0e} {0b = 0c→0d, 0a = 0d→0e} (App)
6. x : 0a, y : 0b ` λz[(x (y z))] : 0c→0e {0d,0e} {0b = 0c→0d, 0a = 0d→0e} (Abs)
7. x : 0a ` λx[λy[λz[(x (y z))]]] : 0b→0c→0e {0d,0e} {0b = 0c→0d, 0a = 0d→0e} (Abs)
8. ` λx[λy[λz[(x (y z))]]] : 0a→0b→0c→0e {0d,0e} {0b = 0c→0d, 0a = 0d→0e} (Abs)

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

JJ J I II G O B ACK F ULL S CREEN C LOSE 534 OF 600


PL April 2, 2020
Principal Type Schemes
Definition 16.7 A solution for Γ ` L : τ T C is a pair hS, σi where S is
a substitution of type variables in τ such that S(τ ) = σ.
• The rules yield a principal type scheme for each well-typed applied
λ-term.
• The term is ill-typed if there is no solution that satisfies the constraints.
• Any substitution of the type variables which satisfies the constraints
C is an instance of the most general polymorphic type that may be
assigned to the term.

JJ J I II G O B ACK F ULL S CREEN C LOSE 535 OF 600


PL April 2, 2020
Exercise 16.3

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)

JJ J I II G O B ACK F ULL S CREEN C LOSE 537 OF 600


PL April 2, 2020
JJ J I II G O B ACK F ULL S CREEN C LOSE 538 OF 600
PL April 2, 2020
17. An Imperative Language

An Imperative Language

JJ J I II G O B ACK F ULL S CREEN C LOSE 539 OF 600


PL April 2, 2020
JJ J I II G O B ACK F ULL S CREEN C LOSE 540 OF 600
PL April 2, 2020
17.1. The Operational Semantics of Expressions

The Concept of State


• Any imperative language indirectly exposes the memory (or store) to
the user for manipulation.
• Memory (or store) is a set Loc of locations used to store the values of
variables.
• We define the store to be a (partial) function from Loc to values. σ :
Loc * (int ∪ bool). Stores = {σ | σ : Loc * (int ∪ bool)} is the set
of possible stores.
• Each variable in an imperative program is assigned a location.
• The environment is an association γ of variable (names) to locations
i.e. γ : X → Loc.
• The (dynamic) state of a program is defined by the pair (γ, σ).

JJ J I II G O B ACK F ULL S CREEN C LOSE 541 OF 600


PL April 2, 2020
State: Illustration
i
T
j
132456

k
−87567

x y z

• l-values. γ(x) = i : bool, γ(y) = j : int, γ(z) = k : int


• r-values. σ(i) = T : bool, σ(j) = 132456 : int, σ(k) = −87567 : int

JJ J I II G O B ACK F ULL S CREEN C LOSE 542 OF 600


PL April 2, 2020
References in Languages
ML-like impure functional languages
• have an explicit polymorphic 0a ref type constructor. Hence x :
bool ref, y, z : int ref and x is a named reference to the location i
• have an explicit unary dereferencing operator ! to read the value
contained in the location referenced by x, i.e. !x = σ(i).
• The actual locations however are not directly visible.
C-like imperative languages are not as fussy as the ML-like languages.
C (and C++) even treats locations only as integers and allows integer
operations to be preformed on them!

JJ J I II G O B ACK F ULL S CREEN C LOSE 543 OF 600


PL April 2, 2020
l-values and r-values
i
T
j
132456

k
−87567

l m
m
78663

x y z w

• l is the l-value of w i.e γ(w) = l ∈ Loc


• m is the r-value of w i.e. σ(γ(w)) = !w = m ∈ Loc
• m is also an l-value since !w : int ref
• !(!w) = 78663 : int is the r-value of !w

JJ J I II G O B ACK F ULL S CREEN C LOSE 544 OF 600


PL April 2, 2020
7.1.1. l-values, r-values, aliasing and indirect addressing

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.

Standard ML of New Jersey v110.76 [built: Tue Oct 22 14:04:11 2013]


- val u = ref 1;
val u = ref 1 : int ref
- val v = u; (* u and v are aliases for the same location *)
val v = ref 1 : int ref
- v := !v+1;
val it = () : unit
- !u;
val it = 2 : int
- !v;
val it = 2 : int
- v := !v+1;
val it = () : unit
- !u;.val it = 3 : int
- !v;
val it = 3 : int
-

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).

Standard ML of New Jersey v110.76 [built: Tue Oct 22 14:04:11 2013]


- val x = ref (ref 0);
val x = ref (ref 0) : int ref ref
- val y = !x;
val y = ref 0 : int ref
- val z = ref y;
val z = ref (ref 0) : int ref ref
- y := !y+1;
val it = () : unit
- !y;
val it = 1 : int
- !z;
val it = ref 1 : int ref
- !(!z);
val it = 1 : int
- !(!x);
val it = 1 : int
-

JJ J I II G O B ACK F ULL S CREEN C LOSE 547 OF 600


PL April 2, 2020
Operational Semantics of Expressions
• Consider the language of terms defined FL0. Instead of the δ-rules
defined earlier, we assume that these terms are evaluated on a hard-
ware which can represent int and bool.
• Assume int is the hardware representation of the integers and bool =
{T, F}.
• We assume that every (sub-)expression in the language has been
typed with a unique type attribute.
• We define an expression evaluation relation −→e such that
−→e⊆ (Stores × TΣ(X)) × (Stores × (TΣ(X) ∪ int ∪ bool))
in a given environment γ.

JJ J I II G O B ACK F ULL S CREEN C LOSE 548 OF 600


PL April 2, 2020
JJ J I II G O B ACK F ULL S CREEN C LOSE 549 OF 600
PL April 2, 2020
JJ J I II G O B ACK F ULL S CREEN C LOSE 550 OF 600
PL April 2, 2020
17.2. The Operational Semantics of Commands

WHILE: Big-Step Semantics

JJ J I II G O B ACK F ULL S CREEN C LOSE 551 OF 600


PL April 2, 2020
The WHILE language
• We initially define a simple language of commands.
• The expressions of the language are those of any term algebra TΣ(X).
• We simply assume there is a well-defined relation −→e for evaluating
expressions in
• We defer defining the relation −→e for FL0.

JJ J I II G O B ACK F ULL S CREEN C LOSE 552 OF 600


PL April 2, 2020
State Changes or Side-Effects
• State changes are usually programmed by assignment commands
which occur one location at a time.
• In the simple WHILE language side-effects do not occur except by
explicit assignment commands.

JJ J I II G O B ACK F ULL S CREEN C LOSE 553 OF 600


PL April 2, 2020
Modelling a Side-Effect
Given a store σ, a variable x such that γ(x) = ` and σ(`) = a, the state
change effected by the assignment x := b is a new store that is identical
to σ except at the location γ(x) which now contains the value b
σ 0 = [γ(x) 7→ b]σ
i.e. 
σ(`) if ` 6= γ(x)
σ 0(`) =
b otherwise

JJ J I II G O B ACK F ULL S CREEN C LOSE 554 OF 600


PL April 2, 2020
Aliases
Definition 17.1 Two (or more) variables are called aliases if they denote
the same location (y and u in the figure below).

i
T
j
132456

k
−87567

l m
m
78663

z
x y u w

JJ J I II G O B ACK F ULL S CREEN C LOSE 555 OF 600


PL April 2, 2020
The Commands of the WHILE Language
c0, c1, c ::= skip Skip
x := e Assgn
{c0} Block
c0; c1 Seq
if e then c1 else c0 Cond
while e do c While
where e is either an integer or boolean expression in the language FL
with operational semantics as given before.

JJ J I II G O B ACK F ULL S CREEN C LOSE 556 OF 600


PL April 2, 2020
Operational Semantics: Basic Commands
Skip
γ ` hσ, skipi −→1c σ

γ ` 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)

JJ J I II G O B ACK F ULL S CREEN C LOSE 557 OF 600


PL April 2, 2020
Operational Semantics: Blocks
We have defined a block as simply a command enclosed in braces. It
is meant to delimit a (new) scope. Later we will see that there could be
local declarations as well, in which case the semantics changes slightly
to include a new scope
γ ` hσ, ci −→1c σ 0
Block
γ ` hσ, {c}i −→1c σ 0

JJ J I II G O B ACK F ULL S CREEN C LOSE 558 OF 600


PL April 2, 2020
Operational Semantics: Sequencing
γ ` hσ, c0i −→1c σ 0,
Seq γ ` hσ 0, c1i −→1c σ 00
γ ` hσ, c0; c1i −→1c σ 00
Notice that sequencing is precisely the composition of relations. If the
relation −→1c is a function (in the case of our language it actually is a
function), sequencing would then be a composition of functions

JJ J I II G O B ACK F ULL S CREEN C LOSE 559 OF 600


PL April 2, 2020
Operational Semantics: Conditionals
γ ` hσ, ei −→e F ,
Cond0 γ ` hσ, c0i −→1c σ0
γ ` hσ, if e then c1 else c0i −→1c σ0

γ ` hσ, ei −→e T ,
Cond1 γ ` hσ, c1i −→1c σ1
γ ` hσ, if e then c1 else c1i −→1c σ1

JJ J I II G O B ACK F ULL S CREEN C LOSE 560 OF 600


PL April 2, 2020
Operational Semantics: While loop
We use the fact that the while e do c is really a form of recursion – ac-
tually it is a form of “tail recursion”. Hence the execution behaviour of
while e do c is exactly that of
if e then {c; while e do c} else skip (37)
The following rules may be derived from (37) using the rules for condi-
tional, sequencing and skip (though the number of steps may not exactly
correspond).
γ ` hσ, ei −→e F
While0
γ ` hσ, while e do ci −→1c σ

γ ` hσ, ei −→e T,
γ ` hσ, ci −→1c σ 0,
While1
γ ` hσ 0, while e do ci −→1c σ 00
γ ` hσ, while e do ci −→1c σ 00

JJ J I II G O B ACK F ULL S CREEN C LOSE 561 OF 600


PL April 2, 2020
JJ J I II G O B ACK F ULL S CREEN C LOSE 562 OF 600
PL April 2, 2020
JJ J I II G O B ACK F ULL S CREEN C LOSE 563 OF 600
PL April 2, 2020
17.3. The Semantics of Expressions in FL

Operational Semantics for FL

JJ J I II G O B ACK F ULL S CREEN C LOSE 564 OF 600


PL April 2, 2020
Evaluating FL on a machine
• We previously treated FL as simply a data-type and gave δ-rules.
• Here we define a deterministic evaluation mechanism −→e on a more
realistic hardware which supports integers and booleans
• The normal forms on this machine would have to be appropriate inte-
ger and boolean constants as represented in the machine.

JJ J I II G O B ACK F ULL S CREEN C LOSE 565 OF 600


PL April 2, 2020
Operational Semantics: Constants and
Variables
Let σ ∈ States be any state.

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

JJ J I II G O B ACK F ULL S CREEN C LOSE 566 OF 600


PL April 2, 2020
Operational Semantics: Integer Expressions
γ ` hσ, ei −→e hσ, mi
P (e, m : int)
γ ` hσ, (P e)i −→e hσ, m − 1i

γ ` hσ, ei −→e hσ, mi


S (e, m : int)
γ ` hσ, (P e)i −→e hσ, m + 1i

γ ` hσ, ei −→e hσ, mi


IZ0 (e, m : int, m <> 0)
γ ` hσ, (IZ e)i −→e hσ, Fi

γ ` hσ, ei −→e hσ, 0i


IZ1 (e : int)
γ ` hσ, (IZ e)i −→e hσ, Ti

γ ` hσ, ei −→e hσ, mi


GTZ0 (e, m : int, m <= 0)
γ ` hσ, (GT Z e)i −→e hσ, Fi

γ ` hσ, ei −→e hσ, mi


GTZ1 (e, m : int, m > 0)
γ ` hσ, (GT Z e)i −→e hσ, Ti
JJ J I II G O B ACK F ULL S CREEN C LOSE 567 OF 600
PL April 2, 2020
Operational Semantics: Conditional
Expressions
γ ` hσ, ei −→e hσ, Fi
ITEI0 (e1, e0 : int)
γ ` hσ, (IT E he, e1, e0i)i −→e hσ, e0i

γ ` hσ, ei −→e hσ, Ti


ITEI1 (e1, e0 : int)
γ ` hσ, (IT E he, e1, e0i)i −→e hσ, e1i

γ ` hσ, ei −→e hσ, Fi


ITEB0 (e1, e0 : bool)
γ ` hσ, (IT E he, e1, e0i)i −→e hσ, e0i

γ ` hσ, ei −→e hσ, Ti


ITEB1 (e1, e0 : bool)
γ ` hσ, (IT E he, e1, e0i)i −→e hσ, e1i

JJ J I II G O B ACK F ULL S CREEN C LOSE 568 OF 600


PL April 2, 2020
JJ J I II G O B ACK F ULL S CREEN C LOSE 569 OF 600
PL April 2, 2020
JJ J I II G O B ACK F ULL S CREEN C LOSE 570 OF 600
PL April 2, 2020
17.4. The Operational Semantics of Declarations

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.

JJ J I II G O B ACK F ULL S CREEN C LOSE 571 OF 600


PL April 2, 2020
Some changed rules
• We use the symbol ⊥ to denote the undefined.
• We use z 6= ⊥ to denote that z is well-defined.

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

JJ J I II G O B ACK F ULL S CREEN C LOSE 572 OF 600


PL April 2, 2020
Declarations: Little Environments
The effect of a declaration is to create a little environment which is
pushed onto the existing environment. The transition relation
−→d⊆ ((Env × Stores × Decls) × (Env × Stores))

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

JJ J I II G O B ACK F ULL S CREEN C LOSE 573 OF 600


PL April 2, 2020
Scope
• The scope of a name begins from its definition and ends where the
corresponding scope ends
• Scopes end with definitions of functions
• Scopes end with the keyword end in any let ... in ...end or
local ... in ...end
• Scopes are delimited by brackets “[. . .]” in (fully-bracketed) λ-
abstractions.
• We simply use {} to delimit scope

JJ J I II G O B ACK F ULL S CREEN C LOSE 574 OF 600


PL April 2, 2020
Scope Rules
• Scopes may be disjoint
• Scopes may be nested one completely within another
• A scope cannot span two disjoint scopes
• Two scopes cannot (partly) overlap
forward

JJ J I II G O B ACK F ULL S CREEN C LOSE 575 OF 600


PL April 2, 2020
Disjoint Scopes
let
val x = 10;
fun fun1 y =
let
...
in
...
end
fun fun2 z =
let
...
in
in ...
end
fun1 (fun2 x)
end

JJ J I II G O B ACK F ULL S CREEN C LOSE 576 OF 600


PL April 2, 2020
Nested Scopes
let
val x = 10;
fun fun1 y =

let
val x = 15

in
x + y
end
in
fun1 x
end

JJ J I II G O B ACK F ULL S CREEN C LOSE 577 OF 600


PL April 2, 2020
Overlapping Scopes
let
val x = 10;
fun fun1 y =
...

...

...
in ...

fun1 (fun2 x)
end

JJ J I II G O B ACK F ULL S CREEN C LOSE 578 OF 600


PL April 2, 2020
Spanning
let
val x = 10;
fun fun1 y =
...

...

fun fun2 z =
...
in ...

fun1 (fun2 x)
end

JJ J I II G O B ACK F ULL S CREEN C LOSE 579 OF 600


PL April 2, 2020
Scope & Names
• A name may occur either as being defined or as a use of a previously
defined name
• The same name may be used to refer to different objects.
• The use of a name refers to the textually most recent definition in the
innermost enclosing scope
diagram

JJ J I II G O B ACK F ULL S CREEN C LOSE 580 OF 600


PL April 2, 2020
Names & References
let
val x = 10; val z = 5;
fun fun1 y =

let
val x = 15

in
x + y * z
end
in
fun1 x
end

Back to scope names

JJ J I II G O B ACK F ULL S CREEN C LOSE 581 OF 600


PL April 2, 2020
Names & References
let
val x = 10; val z = 5;
fun fun1 y =

let
val x = 15

in
x + y * z
end
in
fun1 x
end

Back to scope names

JJ J I II G O B ACK F ULL S CREEN C LOSE 582 OF 600


PL April 2, 2020
Names & References
let
val x = 10; val z = 5;
fun fun1 y =

let
val x = 15

in
x + y * z
end
in
fun1 x
end

Back to scope names

JJ J I II G O B ACK F ULL S CREEN C LOSE 583 OF 600


PL April 2, 2020
Names & References
let
val x = 10; val z = 5;
fun fun1 y =

let
val x = 15

in
x + y * z
end
in
fun1 x
end

Back to scope names

JJ J I II G O B ACK F ULL S CREEN C LOSE 584 OF 600


PL April 2, 2020
Names & References
let
val x = 10; val z = 5;
fun fun1 y =

let
val x = 15

in
x + y * z
end
in
fun1 x
end

Back to scope names

JJ J I II G O B ACK F ULL S CREEN C LOSE 585 OF 600


PL April 2, 2020
Names & References
let
val x = 10; val z = 5;
fun fun1 y =

let
val x = 15

in
x + y * z
end
in
fun1 x
end

Back to scope names

JJ J I II G O B ACK F ULL S CREEN C LOSE 586 OF 600


PL April 2, 2020
Names & References
let
val x = 10; val z = 5;
fun fun1 y =

let
val x = 15

in
x + y * z
end
in
fun1 x
end

Back to scope names

JJ J I II G O B ACK F ULL S CREEN C LOSE 587 OF 600


PL April 2, 2020
Names & References
let
val x = 10; val x = x − 5;
fun fun1 y =
let
...
in
...
end
fun fun2 z =
let
...
in
...
end
in fun1 (fun2 x)
end

Back to scope names

JJ J I II G O B ACK F ULL S CREEN C LOSE 588 OF 600


PL April 2, 2020
Names & References
let
val x = 10; val x = x − 5;
fun fun1 y =
let
...
in
...
end
fun fun2 z =
let
...
in
...
end
in fun1 (fun2 x)
end

Back to scope names

JJ J I II G O B ACK F ULL S CREEN C LOSE 589 OF 600


PL April 2, 2020
Names & References
let
val x = 10; val x = x − 5;
fun fun1 y =
let
...
in
...
end
fun fun2 z =
let
...
in
...
end
in fun1 (fun2 x)
end

Back to scope names

JJ J I II G O B ACK F ULL S CREEN C LOSE 590 OF 600


PL April 2, 2020
Definition of Names
Definitions are of the form
qualifier name . . . = body
• val name =
• fun name ( argnames ) =
• local def initions
in def inition
end

JJ J I II G O B ACK F ULL S CREEN C LOSE 591 OF 600


PL April 2, 2020
Use of Names
Names are used in expressions.
Expressions may occur
• by themselves – to be evaluated
• as the body of a definition
• as the body of a let-expression
let def initions
in expression
end
• as the body of a local-declaration
local def initions
in def inition
end

JJ J I II G O B ACK F ULL S CREEN C LOSE 592 OF 600


PL April 2, 2020
Example 17.2 local. Consider the following example ML program which uses local declarations in the devel-
opment of the algorithm to determine whether a positive integer is perfect.

local
exception invalidArg;

fun ifdivisor3 (n, k) =


if n <= 0 orelse
k <= 0 orelse
n < k
then raise invalidArg
else if n mod k = 0
then k
else 0;
fun sum_div2 (n, l, u) =
if n <= 0 orelse
l <= 0 orelse
l > n orelse
u <= 0 orelse
u > n
then raise invalidArg
else if l > u
then 0
else ifdivisor3 (n, l)

JJ J I II G O B ACK F ULL S CREEN C LOSE 593 OF 600


PL April 2, 2020
+ sum_div2 (n, l+1, u)
in

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

JJ J I II G O B ACK F ULL S CREEN C LOSE 594 OF 600


PL April 2, 2020
Scope & local
local
fun fun1 y =
...

fun fun2 z = ...


fun1

in
fun fun3 x = ...
fun2 ...
fun1 ...
end

JJ J I II G O B ACK F ULL S CREEN C LOSE 595 OF 600


PL April 2, 2020
Execution in the Modified Environment
Once a declaration has been processed a new scope γ 0 is created in
which the new variables are available for use in addition to everything
else that was previously present in the environment γ (unless it has been
“hidden” by the use of the same name in the new scope). γ 0 is pushed
onto γ to create a new environment γ[γ 0]. For any variable x,
 0
 γ (x) if x ∈ Dom(γ 0)
γ[γ 0](x) = γ(x) if x ∈ Dom(γ) − Dom(γ 0)
⊥ otherwise

γ ` hσ, d1i −→d hγ1, σ1i


D − Seq γ[γ1] ` hσ1, d2i −→d hγ2, σ2i
γ ` hσ, d1; d2i −→d hγ1[γ2], σ2i

JJ J I II G O B ACK F ULL S CREEN C LOSE 596 OF 600


PL April 2, 2020
Semantics of Anonymous Blocks
γ ` hσ, di −→∗d hγ 0, σ 0i
Block γ[γ 0] ` hσ 0, ci −→∗c σ 00
γ ` hσ, {d; c}i −→c σ 00  Dom(σ)
Note.
• Note the use of the multi-step transitions on both declarations and
commands
• We have given up on single-step movements, since taking these “big”-
steps in the semantics is more convenient and less cumbersome
• Note that the “little” environment γ 0 which was produced by the decla-
ration d is no longer present on exiting the block.
• On exiting the block the domain of the state returns to Dom(σ), shed-
ding the new locations that were created for the “little” environment.

JJ J I II G O B ACK F ULL S CREEN C LOSE 597 OF 600


PL April 2, 2020
JJ J I II G O B ACK F ULL S CREEN C LOSE 598 OF 600
PL April 2, 2020
JJ J I II G O B ACK F ULL S CREEN C LOSE 599 OF 600
PL April 2, 2020
18. Logic Programming and Prolog

Prolog0: Abstract Interpreter


Algorithm 10 A simple abstract interpreter for propositional Prolog
Require: A Prolog program P and ground goal G
Ensure: yes if P ` G else no
1: resolvent := {G}
2: while notempty(resolvent) do
3: Choose goal A from resolvent
4: Choose a ground instance of some clause A0 ← B1 , . . . , Bk from P such that A ≡ A0
5: if A0 does not exist then
6: exit loop
7: end if
8: resolvent := (resolvent − {A}) ∪ {B1, . . . , Bk }
9: end while
10: if empty(resolvent) then
11: return yes
12: else
13: return no
14: end if

JJ J I II G O B ACK F ULL S CREEN C LOSE 600 OF 600


PL April 2, 2020
Prolog1: Abstract Interpreter
Algorithm 11 An abstract interpreter for general Prolog programs
Require: A Prolog program P and goal G
Ensure: if P ` G then θ else no
1: Standardize variables apart in P ∪ {G}
2: resolventStack := empty
3: θ := 1
4: push(resolventStack, θG)
5: while ¬empty(resolventStack) do
6: A0 := pop(resolventStack)
7: if ∃ clause A0 ← B1 , . . . , Bk ∈ P : unif iable(A0 , A0 ) then
8: τ := mgu(A0, A0)
9: θ := τ ◦ θ
10: else
11: exit loop
12: end if
13: if k > 0 then
14: push(resolventStack, θBk , . . . , θB1)
15: end if
16: end while
17: if empty(resolventStack) then
18: return θ
19: else
20: return no
21: end if
Figure 3: Case m > 0 and n > 0

M
*

*
M1
*Q

1
* *
L *P S
*

*
1

N1 R
*

You might also like