Home Page JJ J I II PL May 4, 2021 Go Back Full Screen Close 1 of 721 Quit Programming Languages http://www.cse.iitd.ac.in/ s̃ak/courses/pl/2020-21/index.html S. Arun-Kumar & Subodh Sharma Department...

1 answer below »
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 1 of 721 Quit
Programming Languages
http://www.cse.iitd.ac.in/ s̃ak/courses/pl/2020-21/index.html
S. Arun-Kumar & Subodh Sharma
Department of Computer Science and Engineering
I. I. T. Delhi, Hauz Khas, New Delhi XXXXXXXXXX.
May 4, 2021
http://www.cse.iitd.ac.in/~sak/courses/pl/2020-21/index.html
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 2 of 721 Quit
Contents
1 The Programming Languages Overview 3
2 Introduction to Compiling 25
3 Scanning or Lexical Analysis 45
3.1 Regular Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . XXXXXXXXXX58
3.2 Nondeterministic Finite Automata (NFA) . . . . . . . . . . . . . . . . . . XXXXXXXXXX80
3.3 Deterministic Finite Automata (DFA) . . . . . . . . . . . . . . . . . . . . XXXXXXXXXX114
4 Parsing or Syntax Analysis 135
4.1 Grammars . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . XXXXXXXXXX135
4.2 Context-Free Grammars . . . . . . . . . . . . . . . . . . . . . . . . . . . . XXXXXXXXXX142
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 3 of 721 Quit
4.3 Ambiguity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . XXXXXXXXXX156
4.4 The “dangling else” problem . . . . . . . . . . . . . . . . . . . . . . . . . . XXXXXXXXXX176
4.5 Specification of Syntax: Extended Backus-Naur Form . . . . . . . . . . . . XXXXXXXXXX187
4.6 The WHILE Programming Language: Syntax . . . . . . . . . . . . . . . . XXXXXXXXXX195
4.7 Parsing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . XXXXXXXXXX213
4.8 Recursive Descent Parsing . . . . . . . . . . . . . . . . . . . . . . . . . . . XXXXXXXXXX218
4.9 Shift-Reduce Parsing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . XXXXXXXXXX235
4.10 Bottom-Up Parsing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . XXXXXXXXXX299
4.11 Simple LR Parsing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . XXXXXXXXXX317
5 Attributes & Semantic Analysis 355
5.1 Context-sensitive analysis and Semantics . . . . . . . . . . . . . . . . . . . XXXXXXXXXX358
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 4 of 721 Quit
6 Static Scope Rules 367
7 Runtime Structure 385
8 Abstract Syntax 396
9 Syntax-Directed Translation 412
9.1 Synthesized Attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . XXXXXXXXXX424
9.2 Inherited Attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . XXXXXXXXXX445
10 Symbol Table 466
11 Intermediate Representation 475
12 The Pure Untyped Lambda Calculus: Basics 500
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 5 of 721 Quit
12.1 Motivation for λ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . XXXXXXXXXX500
12.2 The λ-notation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . XXXXXXXXXX505
13 Notions of Reduction 537
14 Confluence Definitions 552
14.1 Why confluence? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . XXXXXXXXXX558
14.2 Confluence: Church-Rosser . . . . . . . . . . . . . . . . . . . . . . . . . . XXXXXXXXXX567
14.3 The Church-Rosser Property . . . . . . . . . . . . . . . . . . . . . . . . . XXXXXXXXXX575
15 An Applied Lambda-Calculus 584
15.1 FL with recursion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . XXXXXXXXXX584
15.2 Motivation and Organization . . . . . . . . . . . . . . . . . . . . . . . . . XXXXXXXXXX585
15.3 Static Semantics of FL(X) . . . . . . . . . . . . . . . . . . . . . . . . . . . XXXXXXXXXX592
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 6 of 721 Quit
15.3.1 Type-checking FL(X) terms . . . . . . . . . . . . . . . . . . . . . . XXXXXXXXXX593
15.3.2 The Typing Rules . . . . . . . . . . . . . . . . . . . . . . . . . . . XXXXXXXXXX594
15.4 Equational Reasoning in FL(X) . . . . . . . . . . . . . . . . . . . . . . . . XXXXXXXXXX601
15.5 Type-checking FL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . XXXXXXXXXX627
15.6 ΛRecFL(X) with type rules . . . . . . . . . . . . . . . . . . . . . . . . . . . XXXXXXXXXX627
16 An Imperative Language 644
16.0.1 l-values, r-values, aliasing and indirect addressing . . . . . . . . . . . XXXXXXXXXX649
16.1 The Operational Semantics of Commands . . . . . . . . . . . . . . . . . . XXXXXXXXXX656
16.2 The Semantics of Expressions in FL . . . . . . . . . . . . . . . . . . . . . . XXXXXXXXXX670
16.3 The Operational Semantics of Declarations . . . . . . . . . . . . . . . . . . XXXXXXXXXX678
16.4 The Operational Semantics of Subroutines . . . . . . . . . . . . . . . . . . XXXXXXXXXX691
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 7 of 721 Quit
17 Logic Programming and Prolog 698
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 8 of 721 Quit
1. The Programming Languages Overview
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 9 of 721 Quit
What is a Programming Language?
• A (linear) notation for the accurate and precise description of algorithms
and data-structures implementable on a digital computer.
• In contrast,
– the usual mathematical notation is accurate and precise enough for human
beings but is not necessarily implementable on a digital computer,
– and often the usual mathematical notation is not linear.
– pseudo-code for algorithms and data-structures is too abstracta to be
directly executed on a digital computer or even a virtual computer.
• A program is a description of an algorithm written in a programming lan-
guage.
aToo many implementation details are either left unspecified or implicit.
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 10 of 721 Quit
The World of PLs
• There are just too many actual programming languages and more are being
designed every year. Impossible to master every new PL that is released.
• Often impossible to master every feature of even the PLs that are currently
in use.
• Often not necessary to master all features of a PL.
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 11 of 721 Quit
Why Study the subject of PL? - 1
To understand the various major paradigms of programming.
• The same algorithm requires different design considerations in different
paradigms.
• Different data-structures as part of the language,
• Different libraries provided along with the language implementation.
• Different styles of thought involved in the implementation.
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 12 of 721 Quit
Why Study the subject of PL? - 2
To understand the major features and their implementation common to large
numbers of PLs.
• The same feature may be implemented differently in different PLs.
• The same algorithm is written differently in different PLs of the same
paradigm depending upon
– the data-structures available,
– the control structures available,
– the libraries available.
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 13 of 721 Quit
Why Study the subject of PL? - 3
To understand the major design and architectural considerations common to
most PLs.
• Whether a data- or control-structure is part of the programming language
itself.
• Whether certain complex (data- and control-)structures are provided as li-
braries of the programming language.
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 14 of 721 Quit
Architectural Considerations in PL
• Compilation vs Interpretation
• Portability considerations across hardware architecures
• Virtual machines or target architectures.
• Stack architecture vs. register architecure.
• Representation and typing.
• The set of intermediate languages/representation required for the implemen-
tation.
• Support for parallelism.
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 15 of 721 Quit
Programs: Source to Runs
Compiler/
Interpreter Linker Loader
Runtime
System
Macro−
processor
IR
Target
code Results
Source
Compilation
Errors
Linking
Errors
Loading
Errors
Runtime
Errors
Errors
Macro−translation
Translated
Source
IR
Linked
Pre−processor
Pre−processing
Errors
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 16 of 721 Quit
Programs: Source to Runs-2
Compiler/
Interpreter Linker Loader
Runtime
System
Macro−
processor
IR Results
Source
Compilation
Errors
Linking
Errors
Runtime
Errors
Errors
Macro−translation
IR
Linked
Pre−processor
Pre−processing
Errors
Loading
Source
Translated
Errors
code
Assembly
Assembler
Errors
Assembling
code
Machine
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 17 of 721 Quit
Programs: Source to Runs-1: LATEX
Compiler/
Interpreter Linker
processor
IR
Source
Compilation
Errors
Linking
Errors
Errors
Macro−translation
Source
IR
Linked
Pre−processor
Pre−processing
Errors
nw
.texlatex
noweave
notangle
Source Program
Macro−
LaTeX
nw source
latex source
latex source
Translated TeX
TeX
TeX dvi
dvi2pdf
dvi2ps
ps
pdf
or
Display
Printer
Output
Postscript
Processor
Target
printer
code
Print
Render
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 18 of 721 Quit
Programs: Source to Runs-2: LATEX
Compiler/
Interpreter Linker
processor
IR
Source
Compilation
Errors
Linking
Errors
Errors
Macro−translation
Source
IR
Linked
Pre−processor
Pre−processing
Errors
nw
.texlatex
noweave
notangle
Source Program
Macro−
LaTeX
nw source
latex source
latex source
Translated TeX
TeX
TeX dvi
dvi2pdf
dvi2ps
ps
pdf
or
Display
Printer
Output
Postscript
Processor
Target
printer
code
Print
Render
Printer
Display
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 19 of 721 Quit
Programs: Source to Runs: Java
Compiler/
Interpreter Linker Loader
Runtime
System
IR
Target
code Results
Source
Compilation
Errors
Linking
Errors
Loading
Errors
Runtime
Errors
IR
Linked
.java
.class
bytecode
Java Java Virtual Machine (JVM)
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 20 of 721 Quit
The Landscape of General PLs
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 21 of 721 Quit
The Usage of General PLs
Scientific & Numerical Computations
Business Data Processing
Structured Programming
Symbolic computation
Theorem Proving
Teaching
Systems
Programming
Symbolic
Symbol processing
Expert
Systems
Simulation
Reactive
Systems
Rendering &
Printing
Reporting
Web programming
Modular
Progrmg
Theorem pro
Model-checking
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 22 of 721 Quit
The Major Features of General PLs
7XEXMG�1IQSV]�EPPSG�
7XEXMG�WGSTMRK
9RX]TIH
(]REQMG�QIQSV]�EPPSG�
(]REQMG�WGSTI
(]REQMG�1IQSV]�EPPSG�
7XEXMGEPP]�WGSTIH
(]REQMG�WGSTI
(]REQMG�1IQSV]�EPPSGEXMSR
7XEXMG�WGSTI
7XEXMG�WGSTI
,IET�EPPSG
7XVSRK�X]TMRK
6YRXMQI�WXEGO
6YRXMQI�WXEGO
,IET�EPPSG�
(]REQMG�EPPSG
,IET�EPPSG
7XEXMG�WGSTI
7XEXMG�X]TIW
6YRXMQI�WXEGO
,IET�EPPSG�
7XEXMG�WGSTI
6YRXMQI�WXEGO
,IET�EPPSG�
YRX]TIH
7XEXMG�X]TI
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 23 of 721 Quit
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
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 24 of 721 Quit
COBOL
• A business oriented language
• Extremely verbose
• Very highly input-oriented
• Meant to manage large amounts of data on disks and tapes and generate
reports
• Not computationally friendly
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 25 of 721 Quit
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.
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 26 of 721 Quit
ALGOL-60
• Introduced the Backus-Naur Form (BNF) for specifying syntax of a pro-
gramming langauge
• Formal syntax defined by BNF (an extension of context-free grammars)
• First language to implement recursion
• Introduction of block-structure and nested scoping
• Dynamic memory allocation
• Introduced the call-by-name parameter mechanism
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 27 of 721 Quit
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 algorithms in
pseudo-code
• First language to be ported across a variety of hardware and OS platforms –
introduced the concepts of virtual machine and intermediate code (bytecode)
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 28 of 721 Quit
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 except in
special cases
• Its module facility is inspired by the algebraic theory of abstract data types
• The first language to introduce functorial programming between algebraic
structures and modules
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 29 of 721 Quit
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 pro-
gramming, higher-order functions etc.
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 30 of 721 Quit
2. Introduction to Compiling
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 31 of 721 Quit
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 designing
most large pieces of software.
• Compiler design uses techniques from theory, data structures and algorithms.
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 32 of 721 Quit
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 techniques.
• 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 techniques
• 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.
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 33 of 721 Quit
Books and References
1. Appel A W. Modern Compiler Implementation in Java Cambridge
University Press, Revised Indian Paperback edition 2001
2. Aho A V, Sethi R, Ullman J D. Compilers: Principles, Tech-
niques, and Tools, Addison-Wesley 1986.
3. Muchnick S S. Advanced Compiler Design and Implementation, Aca-
demic Press 1997.
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 34 of 721 Quit
A Plethora of Languages: Compiling
In general a compiler/interpreter for a a source language S written in
some language C translates code written in S to a target language T .
Source S
Target T
Language of the compiler/interpreter C
Our primary concern. Compiling from a high-level source programming
language to a target language using a high-level language C.
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 35 of 721 Quit
A Plethora of Languages: Source
The Source language S could be
• a programming language, or
• a description language (e.g. Verilog, VHDL), or
• a markup language (e.g. XML, HTML, SGML, LATEX) or
• even a “mark-down” language to simplify writing code.
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 36 of 721 Quit
A Plethora of Languages: Target
The Target language T could be
• an intermediate language (e.g. ASTs, IR, bytecode etc.)
• another programming language, assembly language or machine language, or
• a language for describing various objects (circuits etc.), or
• a low level language for execution, display, rendering etc. or
• even another high-level language.
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 37 of 721 Quit
The Compiling Process
Besides S, C and T there could be several other intermediate languages
I1, I2, XXXXXXXXXXalso called intermediate representations) into which the
source program could be translated in the process of compiling or interpret-
ing 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.
Some of these intermediate representations could just be data-types of a mod-
ern functional or object-oriented programming language.
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 38 of 721 Quit
Compiling as Translation
Except in the case of a source to source translation (for example, a Pascal 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 languages 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.
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 39 of 721 Quit
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 compilation
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 40 of 721 Quit
The Big Picture: 1
SCANNER
stream of
characters
stream of
tokens
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 41 of 721 Quit
The Big Picture: 2
SCANNER
PARSER
stream of
characters
stream of
tokens
parse tree
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 42 of 721 Quit
The Big Picture: 3
SCANNER
PARSER
SEMANTIC ANALYZER
stream of
characters
stream of
tokens
parse tree
abstract
syntax tree
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 43 of 721 Quit
The Big Picture: 4
SCANNER
PARSER
SEMANTIC ANALYZER
I.R. CODE GENERATOR
stream of
characters
stream of
tokens
parse tree
intermediate
representation
abstract
syntax tree
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 44 of 721 Quit
The Big Picture: 5
SCANNER
PARSER
SEMANTIC ANALYZER
I.R. CODE GENERATOR
OPTIMIZER
stream of
characters
stream of
tokens
parse tree
intermediate
representation
optimized
intermediate
representation
abstract
syntax tree
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 45 of 721 Quit
The Big Picture: 6
SCANNER
PARSER
SEMANTIC ANALYZER
I.R. CODE GENERATOR
OPTIMIZER
CODE GENERATOR
stream of
characters
stream of
tokens
parse tree
intermediate
representation
optimized
intermediate
representation
target code
abstract
syntax tree
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 46 of 721 Quit
The Big Picture: 7
SCANNER
PARSER
SEMANTIC ANALYZER
I.R. CODE GENERATOR
OPTIMIZER
CODE GENERATOR
ERROR−
HANDLER
SYMBOL
TABLE
MANAGER
stream of
characters
stream of
tokens
parse tree
intermediate
representation
optimized
intermediate
representation
target code
abstract
syntax tree
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 47 of 721 Quit
The Big Picture: 8
SCANNER
PARSER
SEMANTIC ANALYZER
I.R. CODE GENERATOR
OPTIMIZER
CODE GENERATOR
ERROR−
HANDLER
SYMBOL
TABLE
MANAGER
stream of
characters
stream of
tokens
parse tree
intermediate
representation
optimized
intermediate
representation
target code
abstract
syntax tree
Scanner Parser Semantic Analysis Symbol Table
IR Run-time structure
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 48 of 721 Quit
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 49 of 721 Quit
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 50 of 721 Quit
3. Scanning or Lexical Analysis
Lexical Analysis
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 51 of 721 Quit
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 expres-
sions, commands, functions, procedures and programs.
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 52 of 721 Quit
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 compiler
must first break up this stream into a sequence of “lexemes”, and other
symbols.
• Each such lexeme is then classified as belonging to a certain token type.
• Certain lexemes may violate the pattern rules for tokens and are considered
erroneous.
• Certain sequences of characters are not tokens and are completely ignored
(or skipped) by the compiler.
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 53 of 721 Quit
Erroneous lexemes
Some lexemes violate all rules of tokens. Some examples common to most
programming languages
• 12ab would not be an identifier or a number in most programming languages.
If it were an integer in Hex code it would be written 0x12ab.
• XXXXXXXXXXis usually not any kind of number. However XXXXXXXXXXmay be a
valid token representing an IP address.
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 54 of 721 Quit
Tokens and Non-tokens: 1
• Tokens
• Non-tokens
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 55 of 721 Quit
Tokens and Non-tokens: 2
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
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 56 of 721 Quit
Tokens and Non-tokens: 3
Tokens
Non-tokens Typical non-tokens are
• whitespace: sequences of tabs, spaces, new-line characters,
• comments: compiler ignores comments
• preprocessor directives: #include ..., #define ...
• macros in the beginning of C programs
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 57 of 721 Quit
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 pa-
rameters to the other phases to enable error-reporting and handling to the
user.
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 58 of 721 Quit
Scanning: 2
Definition 3.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 symbols)
• Examples of lexemes.
– reserved words, keywords, identifiers etc.
– Each comment is usually a single lexeme
– preprocessor directives
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 59 of 721 Quit
Scanning: 3
Definition 3.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.)
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 60 of 721 Quit
Scanning: 4
• Identification of tokens is usually done by a Deterministic Finite-state au-
tomaton (DFA).
• The set of tokens of a language is represented by a large regular expression.
• 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.
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 61 of 721 Quit
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 following
rules.
1. An identifier consists of a sequence of of letters (A . . . Z, a . . . z), digits
XXXXXXXXXXand 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.
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 62 of 721 Quit
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 63 of 721 Quit
3.1. Regular Expressions
Regular Expressions
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 64 of 721 Quit
Consider the string 11/12/2021? What does this string of characters represent? There are at least the following different
possibilities.
• In a school mathematics text it might represent the operation of division (11/12)/2021, i.e the fraction 11/12 divided
by 2021, yielding the value . XXXXXXXXXX.
• To a student who is confused, it may also represent the operation of division 11/(12/2021) i.e. the result of dividing 11
by the fraction 12/2021 yielding the value 1852. XXXXXXXXXX.
• In some official document from India it might represent a date (in dd/mm/yyyy format) viz. 11 December 20211.
• In some official document from America it might represent a different date (in mm/dd/yyyy format) viz. November 12,
20212.
The ambiguity inherent in such representations requires that (especially if the school mathematics text also uses some date
format in some problems) a clearer specification of the individual elements be provided. These specifications of individual
elements in programming languages are provided by lexical rules. These lexical rules specify “patterns” which are legal for
use in the language.
1Have you heard of the 26/11 attack?
2Have you heard of the 9/11 attack?
https://www.youtube.com/watch?v=lAoTnw416Lo
https://www.youtube.com/watch?v=GySgEL4NRFY
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 65 of 721 Quit
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.
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 66 of 721 Quit
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 ]∗
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 67 of 721 Quit
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 con-
catenation 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}
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 68 of 721 Quit
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.
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 69 of 721 Quit
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.
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 programming 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.
https://www.google.co.in/search?q=meaning+dialect&oq=meaning+dialect&aqs=chrome.0.0l6.3590j1j7&sourceid=chrome&ie=UTF-8
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 70 of 721 Quit
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.
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 71 of 721 Quit
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)
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 72 of 721 Quit
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, ””}.
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 73 of 721 Quit
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}
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 74 of 721 Quit
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
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 75 of 721 Quit
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
...
Xn+1 = X.Xn = Xn.X
...
Then
X∗ =

n≥0
Xn
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 76 of 721 Quit
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}
...
Xn+1 = X.Xn
...
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 77 of 721 Quit
Kleene Closure
Given a regular expression r, rn specifies the n-fold iteration of the language
specified by r.
Given any regular expression r, the Kleene closure of r, denoted r∗ specifies
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)
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 78 of 721 Quit
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
X+ =

n>0
Xn
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+
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 79 of 721 Quit
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.
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 80 of 721 Quit
Equivalence of Regular Expressions
Definition 3.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 equivalence
between different regular expressions.
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 81 of 721 Quit
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 3.4 The language of arithmetic expressions over numbers uses the “BDMAS” convention
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 82 of 721 Quit
that brackets have the highest precedence, followed by division and multiplication and the operations of
addition and subtraction have the lowest precedence.
Example 3.5 The regular expression r.s|t.u is ambiguous because we do not know beforehand whether
it represents (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 3.6 The language of arithmetic expressions can also be extended to include the unary post-fix
operation 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 3.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
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 83 of 721 Quit
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 3.8 Another non-associative operator in arithmetic which often leaves students confused is
the exponentiation operator. Consider the arithmetic expression ab
c
. For a = 2, b = 3, c = 4 is this
expression to be interpreted as a(b
c) or as (ab)c?
Exercise 3.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+.
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 84 of 721 Quit
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 85 of 721 Quit
3.2. Nondeterministic Finite Automata (NFA)
Nondeterministic Finite Automata (NFA)
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 86 of 721 Quit
Nondeterministic Finite Automata
A regular expression is useful in defining a finite state automaton. An automa-
ton 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 consists 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.
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 87 of 721 Quit
What is nondeterminstic?
• The transition relation may be equivalently represented as a function
−→: Q× (A ∪ {ε})→ 2Q
that for each source state q ∈ Q and symbol a ∈ A associates a set of
target states.
• It is non-deterministic because for a given source state and input symbol,
– there may not be a unique target state, there may be more than one, or
– the set of target states could be empty.
• Another source of non-determinism is the empty string ε.
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 88 of 721 Quit
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 transition
on ε (and going to different states).
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 89 of 721 Quit
Acceptance of NFA
• For any alphabet A, A∗ denotes the set of all (finite-length) strings of symbols
from A.
• Given a string x = a1a XXXXXXXXXXan ∈ A∗, an accepting sequence is a se-
quence of transitions
q0
ε−→ · · · a1−→ ε−→ · · · q1 ε−→ · · ·
a2−→ · · · ε−→ an−→ ε−→ · · · qn
where qn ∈ F is an accepting state.
• Since the automaton is nondeterministic, it is also possible that there exists
another sequence of transitions
q0
ε−→ · · · a1−→ ε−→ · · · q′1
ε−→ · · · a2−→ · · · ε−→ an−→ ε−→ · · · q′n
where q′n is not a final state.
• The automaton accepts x, if there is an accepting sequence for x.
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 90 of 721 Quit
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 .
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 91 of 721 Quit
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 regular
expression. That is, for each regular expression operator, we show how to
construct the corresponding automaton assuming that the NFAs correspond-
ing 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 au-
tomaton. 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.
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 92 of 721 Quit
Constructing NFA
• We show the construction only for the most basic operators on regular ex-
pressions.
• 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 depiction
of NFAs.
Initial state
Accepting state
a Typical transition
0 fr rrN
Typical NFA for r
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 93 of 721 Quit
Regular Expressions to NFAs:1
a
Na
0
a
f
a
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.
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 94 of 721 Quit
Regular Expressions to NFAs:2
ε
Nεεε0 f
Nε Input Symbol
State a · · · ε
ε0 ∅ ∅ · · · ∅ {εf}
εf ∅ ∅ · · · ∅ ∅
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 95 of 721 Quit
Regular Expressions to NFAs:3
ε
ε
ε
ε
N
r|s
N
N
r
s
r
s
r
s
0 f
0 f
r|s r|s
0 f
Nr|s Input Symbol
State a · · · ε
r|s0 ∅ · · · {r0, s0}
r0 · · · · · · · · ·
...
...
...
...
rf · · · · · · {r|sf}
s0 · · · · · · · · ·
...
...
...
...
sf · · · · · · {r|sf}
r|sf ∅ · · · ∅
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 96 of 721 Quit
Regular Expressions to NFAs:4
N
ε
r.s
N
rr Ns
s
0 0 f
r
f
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.
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 97 of 721 Quit
Regular Expressions to NFAs:5
ε ε
ε
ε
N
r*
N
rr rr* f
r*
0 0 f
Nr∗ Input Symbol
State a · · · ε
r∗0 ∅ · · · {r0, r∗f}
r0 · · · · · · · · ·
...
...
...
...
rf · · · · · · {r0, r∗f}
r∗f ∅ ∅ ∅
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 98 of 721 Quit
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.
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 99 of 721 Quit
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 (subscripted
by 0 and f respectively) to name the two new states created by the regular
expression operator.
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 100 of 721 Quit
Example:-6
aa
0 f
a N
a
Steps in NFA for (a|b)∗abb
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 101 of 721 Quit
Example:-5
aa
0 f
b
0
b
f
b
a N
a
N
b
Steps in NFA for (a|b)∗abb
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 102 of 721 Quit
Example:-4
ε
ε
ε
ε
a
0
a
0 f
b
0
b
f
b
a
a|b a|b
f
a|b
N
Steps in NFA for (a|b)∗abb
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 103 of 721 Quit
Example:-3
ε
ε
ε
ε
ε
a
0
a
0 f
b
0
b
f
b
a
a|b a|bf
ε
(a|b)*
f
ε
0
(a|b)*
ε
(a|b)*N
Steps in NFA for (a|b)∗abb
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 104 of 721 Quit
Example:-2
ε
(a|b)* a
(a|b)* a
ε
ε
ε
ε
a
0
a
0 f
b
0
b
f
b
a
a|b a|bf
ε
(a|b)*
f
ε
a
0
(a|b)*
ε
N
Steps in NFA for (a|b)∗abb
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 105 of 721 Quit
Example:-1
ε
(a|b)* a(a|b)* ab
b
(a|b)* ab
ε
ε
ε
ε
a
0
a
0 f
b
0
b
f
b
a
a|b a|bf
ε
(a|b)*
f
ε
a
0
(a|b)*
ε
N
Steps in NFA for (a|b)∗abb
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 106 of 721 Quit
Example-final
(a|b)* a(a|b)* ab
b(a|b)* abb
(a|b)* abb
ε
ε
ε
ε
ε
a
0
a
0 f
b
0
b
f
b
a
a|b a|bf
ε
(a|b)*
f
ε
a
b
f
0
(a|b)*
ε
N
Steps in NFA for (a|b)∗abb
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 107 of 721 Quit
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 symbols
2. Assuming Nr is a NFA for the regular expression r, how will you construct
the NFA Nr+.
3. Certain languages like Perl allow an operator like r{k, n}, where
L(r{k, n}) =

k≤m≤n
L(rm)
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?
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 108 of 721 Quit
Scanning Using NFAs
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 109 of 721 Quit
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 efficiently.
• Accuracy is achieved by regular expression specification of the tokens
• Efficiency implies that the input should not be read more than once.
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 110 of 721 Quit
Nondeterminism and Token Recognition
• The three kinds of nondeterminism in the NFA construction are depicted in
the figure below.
ε
ε ε
a a
a
(i) (ii) (iii)
(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.
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 111 of 721 Quit
Nondeterministic Features
• In general it is impossible to recognize tokens in the presence of nondeter-
minism without backtracking.
• Hence NFAs are not directly useful for scanning because of the presence of
nondeterminism.
• The nondeterministic feature of the construction of Nr for any regular ex-
pression r is in the ε transitions.
• The ε transitions in any automaton refer to the fact that no input character
is consumed in the transition.
• Backtracking usually means algorithms involving them are very complex and
hence inefficient.
• To avoid backtracking, the automaton should be made deterministic
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 112 of 721 Quit
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′ are equivalent if there are only ε transitions between them.
This is called the ε-closure of states.
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 113 of 721 Quit
ε-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 ε-Closure
Require: T a set of states of the NFA N = 〈Q, A ∪ {ε}, q0, F,−→〉
Ensure: Tε = ε-Closure(T ).
1. U := T
2. repeat
3. Uold := U
4. U := Uold ∪ {q′ | q′ 6∈ U,∃q ∈ Uold : q ε−→ q′}
5. until U = Uold
6. Tε = U
7. return Tε
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 114 of 721 Quit
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|).
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 115 of 721 Quit
Recognition using NFA
The following algorithm may be used to recognize a string using a NFA.
Algorithm 2 Recognition using NFA
Require: NFA N = 〈Q, A ∪ {ε}, q0, F,−→〉, a lexeme x ∈ A∗.
Ensure: Boolean
S := ε-Closure({q0}).
a := nextchar(x)
while a 6= end of string do
S := ε-Closure(S
a−→)
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
S
a−→= {q′ | ∃q ∈ S : q a−→ q′}
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 116 of 721 Quit
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.
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 117 of 721 Quit
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 118 of 721 Quit
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 119 of 721 Quit
3.3. Deterministic Finite Automata (DFA)
Conversion of NFAs to DFAs
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 120 of 721 Quit
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 symbol
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 lan-
guage, all backtracking could be avoided.
aAlso in the case of the NFA the relation −→ may not define a transition from every state on every letter
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 121 of 721 Quit
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
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 122 of 721 Quit
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′.
The recognition problem for the same language of strings becomes simpler
and would work faster (it would have no back-tracking) if the NFA could be
converted into a DFA accepting the same language.
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 123 of 721 Quit
NFA to DFA
Let N = 〈QN , A∪{ε}, sN , FN ,−→N〉 be a NFA . We would like to construct
a DFA D = 〈QD, A, sD, FD,−→D〉 where
•QD the set of states of the DFA
• A the alphabet (notice there is no ε),
• 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)
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 124 of 721 Quit
The Subset Construction
Non-determinism .
ε-closure .
Subsets of NFA states .
Acceptance .
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 125 of 721 Quit
The Subset Construction: Non-determinism
Non-determinism A major source of non-determinism in NFAs is the pres-
ence of ε transitions. The use of ε-Closure creates a cluster of “similar”
statesa.
ε-closure .
Subsets of NFA states .
Acceptance .
aTwo states are “similar” if they are reached from the start state by the same string of symbols from the alphabet
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 126 of 721 Quit
The Subset Construction: ε-closure
Non-determinism .
ε-closure . 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.
Subsets of NFA states .
Acceptance .
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 127 of 721 Quit
The Subset Construction: Subsets of NFA states
Non-determinism .
ε-closure .
Subsets of NFA states . 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.
Acceptance
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 128 of 721 Quit
The Subset Construction: Acceptance
Non-determinism .
ε-closure .
Subsets of NFA states .
Acceptance . 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 sequence of symbols may be
also thought to be similar.
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 129 of 721 Quit
NFA to DFA construction
Algorithm 3 Construction of DFA from NFA
Require: NFA N = 〈QN , A ∪ {ε}, sN , FN ,−→N〉
Ensure: DFA D = 〈QD, A, sD, FD, δD〉 with L(N) = L(D)
1. sD := ε-Closure({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
7. q′D := ε-Closure(qD
a−→N) {Note: qD ⊆ QN}
8. δD(qD, a) := q

D
9. if q′D ∩ FN 6= ∅ then
10. FD := FD ∪ {q′D}
11. end if
12. if q′D 6∈ QD then
13. QD := QD ∪ {q′D}
14. U := U ∪ {q′D}
15. end if
16. end for
17. end while
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 130 of 721 Quit
Example-NFA
Consider the NFA constructed for the regular expression (a|b)∗abb.
ε
b
ε
ε
ε
ε
b
a
ε
ε
a
b
ε
5
6 70 1
2 4
3
8910
N
(a|b)*abb
and apply the NFA to DFA construction algorithm
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 131 of 721 Quit
Determinising
N(a|b)∗abb D(a|b)∗abb
EC0 = ε-Closure(0) = {0, 1, 2, 3, 7}
2
a−→N 4 and 7 a−→N 8. So EC0 a−→D ε-Closure(4, 8) = EC4,8. Similarly
EC0
b−→D ε-Closure(5) = EC5
EC4,8 = ε-Closure(4, 8) = {4, 6, 7, 1, 2, 3, 8}
EC5 = ε-Closure(5) = {5, 6, 7, 1, 2, 3}
EC5
a−→D ε-Closure(4, 8) = EC4,8 and EC5 b−→D ε-Closure(5)
EC4,8
a−→D ε-Closure(4, 8) = EC4,8 and EC4,8 b−→D ε-Closure(5, 9) = EC5,9
EC5,9 = ε-Closure(5, 9) = {5, 6, 7, 1, 2, 3, 9}
EC5,9
a−→D ε-Closure(4, 8) = EC4,8 and EC5,9 b−→D ε-Closure(5, 10) = EC5,10
EC5,10 = ε-Closure(5, 10) = {5, 6, 7, 1, 2, 3, 10}
EC5,10
a−→D ε-Closure(4, 8) and EC5,10 b−→ ε-Closure(5)
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 132 of 721 Quit
Final DFA
EC4,8
a
EC
5,9
D
(a|b)*abb
b
0EC
EC5 EC5,10
a
b
b
a a
b
a
b
D(a|b)∗abb Input Symbol
State a 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
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 133 of 721 Quit
Recognition using DFA
The following algorithm may be used to recognize a string using a DFA. Com-
pare it with the algorithm for recognition using an NFA.
Algorithm 4 Recognition using DFA
Require: DFA D = 〈Q, A, q0, F, δ〉, a lexeme x ∈ A∗.
Ensure: Boolean
S := q0;
a := nextchar(x)
while a 6= end of string do
S := δ(S, a)
a := nextchar(x)
end while
return S ∈ F
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 134 of 721 Quit
Analysis of Recognition using DFA
• The running time of the algorithm is O(|x|).
• The space required for the automaton is O(|Q|.|A|).
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 135 of 721 Quit
Scanning With output
1
2 3 4
5 6
7 8
913
i
f
a−
h,
j−
z
a−e,g−z
0−9,a−z 0−9,a−z
0−9
0−9
0−9
0−9 0−9
(
* XXXXXXXXXX
14
*
if identifier
real
real
error comment
white
space
error
int
identifier
)
*~*
~* ~)
o
th
er
c
h
a
rs
The Big Picture
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 136 of 721 Quit
DFA vis-a-vis Scanner
A scanner differs from a simple DFA in the following ways.
• A DFA may simply reach a non-accepting state in case of an unrecognizable
lexeme. A scanner on the other hand needs to accept the lexeme and raise
an error and proceed to the next lexeme.
• A DFA simply accepts a token or rejects a lexeme. A scanner needs to
recognize and classify every token and lexeme.
• Where a token is allowed as a prefix of another (see the case of “if”) scanners
choose the longest lexeme that is an identifiable token.
• DFAs are often “minimised” to collapse all accepting states into one accept-
ing state. This is not desirable in the case of scanner since tokens need to
be classified separately based on the accepting states.
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 137 of 721 Quit
Exercise 3.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
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 138 of 721 Quit
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.
(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.
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 139 of 721 Quit
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 140 of 721 Quit
4. Parsing or Syntax Analysis
4.1. Grammars
Parsing Or Syntax Analysis
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 141 of 721 Quit
Generating a Language
Consider the DFA constructed earlier to accept the language defined by the
regular expression (a|b)∗abb. We rename the states for convenience.
D(a|b)∗abb Input
State a b
S A B
A A C
B A B
C A D
D A B
We begin by rewriting each of the transitions as follows.
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 142 of 721 Quit
Production rules
S −→ aA | bB
A −→ aA | bC
B −→ aA | bB
C −→ aA | bD
D −→ aA | bB | ε
and think of each of the symbols S,A,B,C,D as generating symbols and thus
producing (rather than consuming strings). For example, the strings abb and
aabbabb are generated by the above production rules as follows.
S ⇒ aA⇒ abC ⇒ abbD ⇒ abb
S ⇒ aA⇒ aaA⇒ aabC ⇒ aabbD
⇒ aabbaA⇒ aabbabC ⇒ aabbabbD ⇒ aabbabb
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 143 of 721 Quit
Formal languages: Definition, Recognition, Generation
There are three different processes used in dealing with a formal language.
Definition : Regular expressions is a formal (functional programming) lan-
guage 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 determine
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.
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 144 of 721 Quit
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 ex-
pressions which require parentheses to be nested and matched to arbitrary
depth.
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 145 of 721 Quit
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 146 of 721 Quit
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 147 of 721 Quit
4.2. Context-Free Grammars
Grammars
Definition 4.1 A grammar G = 〈N, T , P, S〉 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 4.2 Given a grammar G = 〈N, T , P, S〉, any α ∈ (N ∪ T )∗ is
called a sentential form. Any x ∈ T ∗ is called a sentencea.
Note. Every sentence is also a sentential form.
asome authors call it a word. However we will reserve the term word to denote the tokens of a programming language.
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 148 of 721 Quit
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 sen-
tences).
• 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.
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 149 of 721 Quit
Context-Free Grammars: Definition
Definition 4.3 A grammar G = 〈N, T , P, S〉 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
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 150 of 721 Quit
CFG: Example 1
G = 〈{S}, {a, b}, P, S〉, 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
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 151 of 721 Quit
Derivations
Definition 4.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 4.5 The derivation (3) is complete if αn ∈ T ∗ i.e. αn is a
sentence. Then αn is said to be a sentence generated by the grammar.
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 152 of 721 Quit
Language Generation
Definition 4.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 4.7L(G), the language generated by the grammar G is
{anbn|n > 0}. Prove using induction on the length of derivations.
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 153 of 721 Quit
Regular Grammars
Definition 4.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 4.9 A regular grammar is a context-free grammar whose produc-
tions are either only right linear or only left linear.
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 154 of 721 Quit
DFA to Regular Grammar
b
a
b
b
a a
b
a
bS
a D (a|b)*abb
CA
B E
D(a|b)∗abb Input RLG
State a b Rules
S A B S → aA|bB
A A C A→ aA|bC
B A B B → aA|bB
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 eas-
ily convert the DFA to a right linear grammar which generates the language
accepted by the DFA.
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 155 of 721 Quit
CFG: Empty word
G = 〈{S}, {a, b}, P, S〉, 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!
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 156 of 721 Quit
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 productions
as being fired in parallel.
• There is then no highlighting in red to determine which copy of a nonterminal
was used to get the next member of the sequence.
• Of course, generation of the empty word ε must be shown explicitly in the
tree.
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 157 of 721 Quit
CFG: Derivation trees 2
S
S
S
S
S S
S
S
a b a b
a bε
ε
ε
Derivation tree of
abaabb
ε
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 158 of 721 Quit
CFG: Derivation trees 3
S
S
S
S
S
S
S
a b
a b
ε
ε
S
a b
ε
Another
Derivation tree of
abaabb
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 159 of 721 Quit
CFG: Derivation trees 4
S
S
S
S
S
S
a b
a b
ε
Sa b
ε
S
ε
Yet another
Derivation tree of
abaabb
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 160 of 721 Quit
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 161 of 721 Quit
4.3. Ambiguity
Ambiguity Disambiguation
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 162 of 721 Quit
Ambiguity: 1
G1 = 〈{E, I, C}, {y, z,4, ∗,+}, P1, {E}〉 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.
EE
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 163 of 721 Quit
Ambiguity: 2
G1 = 〈{E, I, C}, {y, z,4, ∗,+}, P1, {E}〉 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
EE +
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 164 of 721 Quit
Ambiguity: 3
G1 = 〈{E, I, C}, {y, z,4, ∗,+}, P1, {E}〉 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+
I
E
E
E
E +
EI
*
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 165 of 721 Quit
Ambiguity: 4
G1 = 〈{E, I, C}, {y, z,4, ∗,+}, P1, {E}〉 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+
I C
I
z
E
E
E
E +
E
C
I
y
I
*
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 166 of 721 Quit
Ambiguity: 5
G1 = 〈{E, I, C}, {y, z,4, ∗,+}, P1, {E}〉 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+
I
y
C
4
I
z
E
E
E
E +
E
C
I
y
4
I
z
*
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 167 of 721 Quit
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
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 168 of 721 Quit
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
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 169 of 721 Quit
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
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 170 of 721 Quit
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
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 171 of 721 Quit
Characterizing Ambiguity
The following statements are equivalent.
• A CFG is ambiguous if some sentence it generates has more than one deriva-
tion 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
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 172 of 721 Quit
Ambiguity in CFLs
see Wikipedia
• Some ambiguities result from incorrect grammars, i.e. there may exist a
grammar which generates the same language with unique derivation trees.
• There may be some languages which are inherently ambiguous i.e. there is
no context-free grammar for the language with only unique derivation trees
for every sentence of the language.
• Whether a given CFG is ambiguous is undecidable i.e. there is no algorithm
which can decide whether a given context-free grammar is ambiguous.
• Whether a given context-free language is inherently ambiguous is also unde-
cidable since there is no algorithm which can decide whether any CFG that
generates the language is ambiguous.
https://en.wikipedia.org/wiki/Ambiguous_grammar
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 173 of 721 Quit
Removing ambiguity
There are essentially three ways adopted by programming language designers
or compiler designers to remove ambiguity
• Change the language generated by introducing new bracketing tokens, (e.g.
new reserved keywords begin...end).
• Introduce new precedence or associativity rules to disambiguate – this will
invalidate certain derivation trees and may guarantee uniqueness, (e.g. the
dangling-else problem see section 4.4).
• Change the grammar of the language (without changing the language gen-
erated)
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 174 of 721 Quit
Disambiguation
The only way to remove ambiguity (without changing the language generated
for a language which is not ambiguous) is to change the grammar by introducing
some more non-terminal symbols and changing the production rulesa. Consider
the grammar G′1 = 〈N ′, {y, z,4, ∗,+}, P ′, {E}〉 where N ′ = N ∪ {T, F}
with the following production rules P ′.
E → E+T | T
T → T∗F | F
F → I | C
I → y | z
C → 4
and compare it with the grammar G1
aHowever the introduction of fresh non-terminals and rules may introduce new ambiguities, if th edesigner is not careful!
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 175 of 721 Quit
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
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 176 of 721 Quit
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
G′1. 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 G′1 corresponding to Left-most Derivation 2 (Why
not?).
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 177 of 721 Quit
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.
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 178 of 721 Quit
Disambiguation by Parenthesization
Another method of disambiguating a language is to change the language gen-
erated, by introducing suitable bracketing mechanisms.
Example 4.10 Compare the following fully parenthesized grammar G2 (which
has the extra terminal symbols ( and )) with the grammar G1 without paren-
theses
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.
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 179 of 721 Quit
Associativity and Precedence
The grammar G′1 implements
Precedence. ∗ has higher precedence than +.
Associativity. ∗ and + are both left associative operators.
but is parentheses-free, whereas grammar G2 generates a different language
which is unambiguous. We may combine the two with the benefits of both.
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 180 of 721 Quit
Parenthesization, Associativity and Precedence
Example 4.11 Compare the following parenthesized grammar G′2 which com-
bines the benefits of both G′1 and G2 (parenthesization wherever required
by implementing the bodmas rule). G′2 = 〈N ′, {y, z,4, ∗,+, (, )}, P ′2, {E}〉
where N ′ = N ∪ {T, F} with the following production rules P ′2.
E → E+T | T
T → T∗F | F
F → I | C | (E)
I → y | z
C → 4
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 181 of 721 Quit
4.4. The “dangling else” problem
• Some programming languages like FORTRAN and Assembly (conditional jumps) have a single if. . . then construct. We
write it(b, C) to denote if b then C.
• Some programming languages like ML, OCAML have a single if. . . then. . . else construct and we write ite(b, C, C ′) to
denote if b then C else C ′.
• Many programming languages have both if. . . then and if. . . then. . . else constructs which potentially may lead to a
dangling-else problem.
The dangling-else problem potentially is an ambiguity associated with a compound construct such as
if b1 then if b2 then C1 else C2 (4)
where b1 and b2 are boolean expressions and C1 and C2 are appropriate constructs (expressions or commands) that are
allowed by the language.
The ambiguity arises because the construct (4) may be interpreted as denoting either it(b1, ite(b2, C1, C2)) or ite(b1, it(b2, C1), C2).
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 182 of 721 Quit
if then if then begin endbegin else begin end end
$C’_2$
b1 b2 C1 C2
C ′1
C ′0
C ′0
C0
Figure 1: it(b1, ite(b2, C1, C2))
Disambiguation
1. Disambiguation may be achieved in the language by introducing new bracketing symbols (e.g. begin. . . end) for all
constructs of the kind that C belongs to. If the use of these brackets is made mandatory in the language then the
construct (4) itself would be syntactically illegal and would have to be replaced by one of the following depending upon
the programmer’s intention.
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 183 of 721 Quit
• If the programmer’s intention corresponds to it(b1, ite(b2, C1, C2)) (see the parse tree in figure 1) then
if b1 then
begin
if b2 then
begin
C1
end
else
begin
C2
end
end
• If programmer intended ite(b1, it(b2, C1), C2) (see the parse tree in figure 2).
if b1 then
begin
if b2 then
begin
C1
end
end
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 184 of 721 Quit
else
begin
C2
end
if then if then begin end end else begin endbegin
$C’_2$
b1 b2 C1 C2
C ′1
C ′0
C ′0
C0
Figure 2: ite(b1, it(b2, C1), C2)
2. While the use of begin. . . end is general-purpose enough for all constructs of the kind that C is, it tends to introduce
too many tokens in an actual program. Some languages (e.g. Bash) instead introduce a unique closing token for
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 185 of 721 Quit
each construct. That is, the constructs come with pairs of unique opening and closing tokens (e.g. if. . . then. . . fi,
if. . . then. . . else. . . fi, case. . . esac etc.) In such a language the constructs corresponding to it(b1, ite(b2, C1, C2)) would
then be written as follows (see also the parse tree in figure 3).
if then else
fi
if
$b_1$
then fi
b2 C1 C2
C ′0
C0
Figure 3: it(b1, ite(b2, C1, C2))
if b1 then
if b2 then
C1
else
C2
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 186 of 721 Quit
fi
while ite(b1, it(b2, C1), C2) would be written as (see also the parse tree in figure 4)
thenif fithenif else fi
b2
C1
C ′1
b1 C2
C0
Figure 4: it(b1, ite(b2, C1, C2))
if b1 then
if b2 then
C1
fi
else
C2
fi
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 187 of 721 Quit
In general this solution leads to a larger number of reserved words in the language but a smaller number of tokens
(produced after scanning) per syntactically valid program as opposed to the previous solution. Languages like C and
Perl also dispense with the reserved word then by insisting that all conditions in such statements be enclosed in
parentheses.
3. Languages like Pascal which use a single bracketing mechanism for command constructs, often try to reduce the number
of tokens produced per program by relaxing the mandatory requirement of bracketing, by stipulating that bracketing is
required only for compound commands. Thus for atomic commands c1 and c2 the ambiguity in
if b1 then if b2 then c1 else c2 (5)
is resolved by introducing an associativety rule that each else is associated with the nearest enclosing condition. That
is the construct (5) is interpreted as referring to it(b1, ite(b2, C1, C2)).
4. There are other means of achieving disambiguation of which the most ingenious is the use of white-space indentation in
Python to keep it unambiguous. Hence in Python it(b1, ite(b2, C1, C2)) would be written as
if b1:
if b2:
C1
else:
C2
and ite(b1, it(b2, C1), C2) would be written as
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 188 of 721 Quit
if b1:
if b2:
C1
else:
C2
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 189 of 721 Quit
Exercise 4.1
1. Two context-free grammars are considered equivalent if they generate the same language. Prove that
G1 and G

1 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 termi-
nals consists 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.
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 190 of 721 Quit
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.
(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 G′1 is indeed unambiguous.
9. Prove that the grammar of fully parenthesized expressions is unambiguous.
10. Explain how the grammar G′1 implements left associativity and precedence.
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 191 of 721 Quit
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 192 of 721 Quit
4.5. Specification of Syntax: Extended Backus-Naur Form
Specification of Syntax: EBNF
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 193 of 721 Quit
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. In order
to reduce the number of rules unambiguously regular expression operators such as alternation, Kleene closure and +-closure
are also used. (Con)catenation is represented by juxtaposition. In addition, a period is used to terminate a rule. The rules
are written usually in a “top-down fashion”.
Start symbol. 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 suggestive 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.
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 194 of 721 Quit
Terminals. Terminal symbol strings are sometimes enclosed in double-quotes when written in monochrome (we shall addi-
tionally colour-code them).
Note.
We have chosen to colour-code the EBNF specification in order to clearly separate the colours of the EBNF operators from
those of the language that is being specified. Further we have chosen to use different colours for the Nonterminal symbols
and the terminal symbols. In the bad old days when the world was only black-and-white and the only font available was
the type-writer font, the symbols were usually enclosed in “<>” while the terminal symbols were written
directly (optionally enclosed in double-quotes (")).
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 195 of 721 Quit
Balanced Parentheses: CFG
Example 4.12 A context-free grammar for balanced parentheses (including
the empty string) over the terminal alphabet {(, ), [, ], {, }} could be given as
BP3 = 〈{S}, {(, ), [, ], {, }}, P, {S}〉, where P consists of the productions
S → �,
S → (S)S,
S → [S]S,
S → {S}S
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 196 of 721 Quit
Balanced Parentheses: EBNF
Example 4.13BP3 may be expressed in EBNF as follows:
BracketSeq ::= {Bracket} .
Bracket ::= LeftParen BracketSeq RightParen |
LeftSqbracket BracketSeq RightSqbracket |
LeftBrace BracketSeq RightBrace .
LeftParen ::= “(” .
RightParen ::= “)” .
LeftSqbracket ::= “[” .
RightSqbracket ::= “]” .
LeftBrace ::= “{” .
RightBrace ::= “}” .
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 197 of 721 Quit
EBNF in EBNF
EBNF has its own grammar which is again context-free. Hence EBNF (4.5)
may be used to define EBNF in its own syntax as follows:
Syntax ::= {Production} .
P roduction ::= NonTerminal “::=” PossibleRewrites “.” .
PossibleRewrites ::= Rewrite {“|” Rewrite} .
Rewrite ::= Symbol {Symbol} .
Symbol ::= NonTerminal | Terminal | GroupRewrites .
GroupRewrites ::= “{” PossibleRewrites “}” |
“[” PossibleRewrites “]” |
“(” PossibleRewrites “)” .
NonTerminal ::= Letter {Letter | Digit} .
T erminal ::= Character {Character} .
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 198 of 721 Quit
EBNF: Character Set
The character set used in EBNF is described below.
Character ::= Letter | Digit | SpecialChar
Letter ::= UpperCase | LowerCase
UpperCase ::= “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 ::= “!” | “”” | “#” | “$” | “%” | “&” | “′” | “(” | “)” | “∗” |
“+” | “,” | “−” | “.” | “/” | “:” | “;” | “<” | “=” | “>” | “?”
“@” | “[” | “\” | “]” | “ˆ” | “ ” | “‘” | “{” | “|” | “}” | “˜”
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 199 of 721 Quit
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 200 of 721 Quit
4.6. The WHILE Programming Language: Syntax
All words written in bold font are reserved words and cannot be used as identifiers in any program.
Program ::= “program” Identifier “::”Block .
Block ::= DeclarationSeq CommandSeq .
DeclarationSeq ::= {Declaration} .
Declaration ::= “var” V ariableList“:”Type“;” .
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” .
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 201 of 721 Quit
Expression ::= IntExpression | BoolExpression .
IntExpression ::= IntExpression AddOp IntTerm | IntTerm .
IntTerm ::= IntTerm MultOp IntFactor | IntFactor .
IntFactor ::= Numeral | V ariable |
“(”IntExpression“)” | “˜”IntFactor .
BoolExpression ::= BoolExpression “||” BoolTerm | BoolTerm .
BoolTerm ::= BoolTerm “&&” BoolFactor | BoolFactor .
BoolFactor ::= “tt” | “ff” | V ariable | Comparison |
“(”BoolExpression“)” | “!”BoolFactor .
Comparison ::= IntExpression RelOp IntExpression .
V ariable ::= Identifier .
RelOp ::= “<” | “<=” | “=” | “>” | “>=” | “<>” .
AddOp ::= “+” | “−” .
MultOp ::= “∗” | “/” | “%” .
Identifier ::= Letter{Letter | Digit} .
Numeral ::= [“+” | “˜”]Digit{Digit} .
Note
1. “;” acts as a terminator for both Declarations and Commands.
2. “,” acts as a separator in V ariableList
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 202 of 721 Quit
3. Comparison has a higher precedence than BoolTerm and BoolExpression.
4. RelOps have lower precedence than any of the integer operations specified in MultOp and AddOp.
5. The nonterminals Letter and Digit are as specified earlier in the EBNF character set
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 203 of 721 Quit
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 rendering
is only for the purpose of readability.
• EBNF is a specification language that almost all modern programming lan-
guages use to define the grammar of the programming language
http://darcy.rsgc.on.ca/ACES/ICS4U/Calculus/SyntaxDiagrams.pdf
http://darcy.rsgc.on.ca/ACES/ICS4U/Calculus/SyntaxDiagrams.pdf
https://www.cse.buffalo.edu//~regan/cse305/MLBNF.pdf
http://www.fit.vutbr.cz/study/courses/APR/public/ebnf.html
http://primepuzzle.com/tp2/syntax-diagrams.html
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 204 of 721 Quit
Syntax Specifications
• BNF of C
• BNF of Java
• EBNF of Pascal
• Pascal Syntax diagrams
• BNF of Standard ML
• BNF of Datalog
• BNF of Prolog
https://cs.wmich.edu/~gupta/teaching/cs4850/sumII06/The%20syntax%20of%20C%20in%20Backus-Naur%20form.htm
https://users-cs.au.dk/amoeller/RegAut/JavaBNF.html
http://www.fit.vutbr.cz/study/courses/APR/public/ebnf.html
http://primepuzzle.com/tp2/syntax-diagrams.html
https://www.cse.buffalo.edu//~regan/cse305/MLBNF.pdf
https://docs.racket-lang.org/datalog/datalog.html
https://github.com/simonkrenger/ch.bfh.bti7064.w2013.PrologParser/blob/master/doc/prolog-bnf-grammar.txt
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 205 of 721 Quit
Syntax Diagrams of SML: 1
Syntax of Standard ML
Tobias Nipkow and Larry Paulson
PROGRAMS AND MODULES
Program
TopLevelDeclaration
;






TopLevelDeclaration
Expression


ObjectDeclaration

SignatureDeclaration

FunctorDeclaration

ObjectDeclaration
Declaration


structure



Ident
:



Signature



=



Structure


and





local



ObjectDeclaration
in



ObjectDeclaration
end







;







1
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 206 of 721 Quit
Syntax Diagrams of SML: 2
SignatureDeclaration
signature



Ident
=



Signature


and









;







FunctorDeclaration
functor



FunctorBinding


and









;







FunctorBinding
Ident
(



FunctorArguments
)



:



Signature



=



Structure
FunctorArguments
Ident
:



Signature


Speci�cation

Structure
struct



ObjectDeclaration
end





CompoundIdent

Ident
(



Structure


ObjectDeclaration

)




let



ObjectDeclaration
in



Structure
end




Signature
sig



Speci�cation
end





Ident

2
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 207 of 721 Quit
Syntax Diagrams of SML: 3
Speci�cation
val



Ident
:



Type


and






type





eqtype




TypeVarList Ident


and





datatype



DatatypeBinding


and





exception



Ident
of



Type





and





structure



Ident
:



Signature


and





sharing





type




CompoundIdent


=






and





local



Speci�cation
in



Speci�cation
end




open



CompoundIdent




include



Ident







;







3
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 208 of 721 Quit
Syntax Diagrams of SML: 4
1.
DECLARATIONS
Declaration
val





rec




Pattern
=



Expression


and






fun



FunHeading


:



Type

=



Expression


|






and





type



TypeBinding

datatype



DatatypeBinding


withtype



TypeBinding


abstype



DatatypeBinding


withtype



TypeBinding




with



Declaration
end




exception



Name
of



Type


=



CompoundName




and





local



Declaration
in



Declaration
end




open



CompoundIdent




infix





infixr






Digit



nonfix




Ident







;







4
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 209 of 721 Quit
Syntax Diagrams of SML: 5
1.
FunHeading
Name AtomicPattern





(



AtomicPattern In�xOperator AtomicPattern )





��

AtomicPattern


AtomicPattern In�xOperator AtomicPattern

TypeBinding
TypeVarList Ident
=



Type


and




DatatypeBinding
TypeVarList Ident
=



Ident
of



Type





|






and




TypeVarList


TypeVar

(



TypeVar


,




)




5
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 210 of 721 Quit
Syntax Diagrams of SML: 6
1.
EXPRESSIONS
Expression
In�xExpression


Expression
:



Type

Expression andalso





orelse




Expression

Expression
handle



Match

raise



Expression

if



Expression
then



Expression
else



Expression

while



Expression
do



Expression

case



Expression
of



Match

fn



Match

In�xExpression
AtomicExpression





In�xExpression In�xOperator In�xExpression

6
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 211 of 721 Quit
Syntax Diagrams of SML: 7
1.
AtomicExpression
CompoundName


Constant

(



Expression


,







)




[



Expression


,







]




f



Label
=



Expression


,







g




#



Label

(



Expression


;




)




let



Declaration in



Expression


;




end




MATCHES AND PATTERNS
Match
Pattern
=>



Expression


|




Pattern
AtomicPattern


CompoundName AtomicPattern

Pattern In�xOperator Pattern

Pattern
:



Type

Name


:



Type

as



Pattern

7
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 212 of 721 Quit
Syntax Diagrams of SML: 8
1.
AtomicPattern
_





CompoundName

Constant

(



Pattern


,







)




[



Pattern


,







]




f



FieldPattern



g




FieldPattern
...





Label =



Pattern


Ident


:



Type



as



Pattern




��

,



FieldPattern


8
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 213 of 721 Quit
Syntax Diagrams of SML: 9
1.
TYPES
Type
TypeVar

��

Type


(



Type


,




)





CompoundIdent

Type
*



Type




Type
->



Type

f



Label :



Type


,







g




(



Type
)




LEXICAL MATTERS: IDENTIFIERS, CONSTANTS, COMMENTS
CompoundIdent
Ident


.




CompoundName
CompoundIdent


op



In�xOperator

Name
Ident


op



In�xOperator

In�xOperator
any Ident that has been declared to be in�x



9
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 214 of 721 Quit
Syntax Diagrams of SML: 10
1.
Constant
Numeral
.



Digit






E



Numeral





"





any printable character except \ and "





StringEscape
\





"




StringEscape
n





t




^



one of @ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_




Digit Digit Digit

"




\




Space


Tab

Newline

Formfeed




\




Numeral


~




Digit



TypeVar
'








_




AlphanumericIdent
10
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 215 of 721 Quit
Syntax Diagrams of SML: 11
1.
Ident
AlphanumericIdent


one of !%&$#+-/:<=>[email protected]\~`^|*







Label
Ident


Digit




AlphanumericIdent
Letter


Letter


Digit

_




'





Digit
one of XXXXXXXXXX



Letter
one of ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz



Comment
(*



any text that does not include (* or *) as a substring





Comment

*)



11
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 216 of 721 Quit
Exercise 4.2
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.
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 217 of 721 Quit
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 218 of 721 Quit
4.7. Parsing
Introduction to Parsing
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 219 of 721 Quit
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 different
strings
any parsing method should be guided by the given input (token) string, so that
a deterministic strategy may be evolved.
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 220 of 721 Quit
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 determin-
istic guess from the given input token stream. On the other hand bottom-up
parsing yields better results and can be automated by software tools.
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 221 of 721 Quit
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 222 of 721 Quit
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 223 of 721 Quit
4.8. Recursive Descent Parsing
Top-down Parsing
• Try to generate the given input sentence from the start symbol of the
grammar by applying the production rules.
• Not the most general.
• But most modern high-level programming languages are designed to be ef-
ficiently parsed by this method.
• Recursive-descent is the most frequently employed technique when language
C in which the compiler is written, supports recursion.
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 224 of 721 Quit
Recursive Descent Parsing
• Suitable for grammars that are LL(1)a parseable.
• 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
aLeft-to-right Left-most derivations with 1 look-ahead
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 225 of 721 Quit
Caveats with RDP: Direct Left Recursion
Any 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.
That is, they should not be of the form
A −→ Aα
This would result in an infinite recursion with no input token consumed.
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 226 of 721 Quit
Caveats with RDP: Indirect Left Recursion
• A production cannot even be indirectly left recursive. For instance the fol-
lowing is indirect left-recursion of cycle length 2.
Example 4.14
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.
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 227 of 721 Quit
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 4.15 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 A′ and rewrite the rule as
A −→ aA′
A′ −→ Bβ | Cγ
provided B and C generate terminal strings with different first symbols (oth-
erwise more left-factoring needs to be performed).
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 228 of 721 Quit
A Simple Left-recursive Grammar
The following grammar is unambiguous and implements both left-associativity
and precedence of operators. G = 〈{E, T,D}, {a, b,−, /(, )}, P, E〉 whose
productions are
E → E−T | T
T → T/D| D
D → a | b | (E)
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 229 of 721 Quit
Left Recursion Removal
The grammar G 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 → TE′
E′ → −TE′ | ε
T → DT ′
T ′ → /DT ′ | ε
D → a | b | (E)
Now this grammar is no longer left-recursive and may then be parsed by a
recursive-descent parser.
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 230 of 721 Quit
Recursive Descent Parsing: Determinization
RDP can be deterministic only if
• the input token lookahead uniquely determines the production to be applied.
• We need to define the FIRST symbols that will be generated by each pro-
duction.
• In the presence of ε productions, symbols that can FOLLOW a given non-
terminal symbol also need to be specified.
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 231 of 721 Quit
Nullable
A nonterminal symbol X is nullable if it can derive the empty string, i.e.
X ⇒∗ ε.
Algorithm 5 Nullable
Require: CFG G = 〈N, T, P, S〉
Ensure: NULLABLE(N ∪ T )
1. for all X ∈ N do
2. if X → ε ∈ P then
3. NULLABLE(X) := true
4. else
5. NULLABLE(X) := false
6. end if
7. end for
8. repeat
9. for all X → α XXXXXXXXXXαk ∈ P do
10. if ∀i : 1 ≤ i ≤ k : NULLABLE(αi) then
11. NULLABLE(X) := true
12. end if
13. end for
14. until NULLABLE(N) is unchanged
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 232 of 721 Quit
First
FIRST (α) is the set of terminal symbols that can be the first symbol of any
string that α can derive, i.e. a ∈ FIRST (α) if and only if there exists a
derivation α⇒∗ ax for any string of terminals x.
Algorithm 6 First
Require: CFG G = 〈N, T, P, S〉
Ensure: FIRST (N ∪ T )
1. for all a ∈ T do
2. FIRST (a) := {a}
3. end for
4. for all X ∈ N do
5. FIRST (X) := ∅
6. end for
7. repeat
8. for all X → α XXXXXXXXXXαk ∈ P do
9. for i := XXXXXXXXXXk do
10. if ∀i′ : 1 ≤ i′ < i : NULLABLE(αi′) then
11. FIRST (X) := FIRST (X) ∪ FIRST (αi)
12. end if
13. end for
14. end for
15. until FIRST (N ∪ T ) sets are all unchanged
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 233 of 721 Quit
First And Follow
Notice that if X → αZβ is a production then one cannot ignore the
FIRST (Z) in computing FIRST (X) especially if α ⇒∗ ε. Further if Z
is also nullable then FIRST (β) ⊆ FIRST (X).
FOLLOW (X) for any nonterminal symbol X is the set of terminal symbols
a such that there exists a rightmost derivation of the form
S ⇒∗ · · ·Xa · · · ⇒∗
i.e. FOLLOW (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α XXXXXXXXXXαka · · · ⇒∗
such that α1, . . . , αk are all nullable then again we have
S ⇒∗ · · ·Xα XXXXXXXXXXαka · · · ⇒∗ · · ·Xa · · · ⇒∗
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 234 of 721 Quit
Computing Follow
Algorithm 7 Follow
Require: CFG G = 〈N, T, P, S〉
Ensure: FOLLOW (N)
1. for all α ∈ N ∪ T do
2. FOLLOW (α) := ∅
3. end for
4. repeat
5. for all X → α XXXXXXXXXXαk ∈ P do
6. for i := XXXXXXXXXXk do
7. if ∀i′ : i + 1 ≤ i′ ≤ k : NULLABLE(αi′) then
8. FOLLOW (αi) := FOLLOW (αi) ∪ FOLLOW (X)
9. end if
10. for j := i XXXXXXXXXXk do
11. if ∀i′ : i + 1 ≤ i′ ≤ j − 1 : NULLABLE(αi′) then
12. FOLLOW (αi) := FOLLOW (αi) ∪ FIRST (αj)
13. end if
14. end for
15. end for
16. end for
17. until FOLLOW (N ∪ T ) sets are all unchanged
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 235 of 721 Quit
Recursive Descent Parsing: Pragmatics
• In any collection of (possibly) mutually recursive procedures, it is necessary
to clearly specify the entry point into the collection and the exits. So we
add a new start symbol S and a new end-of-file token EOF (represented by
a new terminal symbol $) with the unique production S → E$.
• Tokens are in upper case and the correspondence between the lexemes and
tokens is as follows:
ID(a) ↔ a , ID(b) ↔ b
LPAREN ↔ ( , RPAREN ↔ )
MINUS ↔ − , DIV IDE ↔ /
EOF ↔ $
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 236 of 721 Quit
A recursive descent parser
program Parse ;
var input token : token ;
func t i on get token : token ;
begin
(∗ l e x ∗)
end ;
procedure match ( expected ) ;
l a b e l 99 ;
begin
i f input token = expected then
begin
consume ( input token ) ;
i f input token <> EOF then input token := get token
e l s e goto 99
end
e l s e p a r s e e r r o r
99 :
end ;
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 237 of 721 Quit
(∗ The system of mutually r e c u r s i v e procedures beg ins here ∗)
procedure Express ion ; Forward ;
procedure D iv i s i on (∗ D −> a | b | (E) ∗)
begin
case input token o f
ID : match ( ID ) ;
LPAREN: Express ion ; match (RPAREN) ;
e l s e p a r s e e r r o r
end ;
procedure Term ta i l (∗ T’ −> /DT’ | ∗)
begin
case input token o f
DIVIDE : D iv i s i on ; Term ta i l ;
MINUS: Term ta i l ; (∗ e p s i l o n product ion ∗)
RPAREN, ID : ; (∗ sk ip e p s i l o n product ion ∗)
e l s e p a r s e e r r o r
end ;
procedure Term (∗ T −> DT’ ∗)
begin
case input token o f
ID( a ) , ID(b ) , LPAREN: Div i s i on ; Term ta i l ;
e l s e p a r s e e r r o r
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 238 of 721 Quit
end ;
procedure E x p r e s s i o n t a i l (∗ E’ −> −TE’ | ∗)
begin
case input token o f
MINUS: Term ; E x p r e s s i o n t a i l ;
RPAREN, ID : ; (∗ sk ip e p s i l o n product ion ∗)
e l s e p a r s e e r r o r
end ;
procedure Express ion (∗ E −> TE’ ∗)
begin
case input token o f
ID( a ) , ID(b ) , LPAREN: Term ; E x p r e s s i o n t a i l ;
e l s e p a r s e e r r o r
end ;
begin (∗ main S −> E$ ∗)
input token := get token ;
Express ion ; match (EOF)
end .
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 239 of 721 Quit
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 240 of 721 Quit
4.9. Shift-Reduce Parsing
Bottom-Up Parsing Strategy
The main problem is to match parentheses of arbitrary nesting depths. This
requires a stacka 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 production
X → α we reduce any occurrence of α in the parsing stack to X .
aIn the case of recursive-descent parsing the stack is provided by the recursion facility in the language of implementation.
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 241 of 721 Quit
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 ⇐
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 242 of 721 Quit
Fully Bracketed Expression
Consider an example of a fully bracketed expression generated by the simple
left-recursive grammar defined earlier.
The main questions are
• When to shift and when to reduce?
• If reduce then what production to use?
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 243 of 721 Quit
Shift-reduce parsing: Invariant
Given a sentence generated by the grammar, at any stage of the pars-
ing, the contents of the stack concatenated with the rest of the input
token stream should be a sentential form of a right-most derivation of
the sentence.
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 244 of 721 Quit
Parsing: FB0

/
b ( )a
/a )b )(−(
r1. E E T
r2 E T
r3 T T D
r4 T D
r5 D | | E
Shift
Principle:
Reduce
whenever possible.
Shift only when
reduce is
impossible
a
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 245 of 721 Quit
Parsing: FB1

/
b ( )a
/a )b )(−
(
r1. E E T
r2 E T
r3 T T D
r4 T D
r5 D | | E
Shift
Principle:
Reduce
whenever possible.
Shift only when
reduce is
impossible
a
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 246 of 721 Quit
Parsing: FB2

/
b ( )a
/a )b )(
(
a

r1. E E T
r2 E T
r3 T T D
r4 T D
r5 D | | E
Shift
Principle:
Reduce
whenever possible.
Shift only when
reduce is
impossible
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 247 of 721 Quit
Parsing: FB3

/
b ( )a
/a )b )(
(
a

r1. E E T
r2 E T
r3 T T D
r4 T D
r5 D | | E
Principle:
Reduce
whenever possible.
Shift only when
reduce is
impossible
Reduce
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 248 of 721 Quit
Parsing: FB4

/
b ( )a
/a )b )(
(

r1. E E T
r2 E T
r3 T T D
r4 T D
r5 D | | E
Principle:
Reduce
whenever possible.
Shift only when
reduce is
impossible
D
Reduce
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 249 of 721 Quit
Parsing: FB5

/
b ( )a
/a )b )(
(

r1. E E T
r2 E T
r3 T T D
r4 T D
r5 D | | E
Principle:
Reduce
whenever possible.
Shift only when
reduce is
impossible
Reduce
T
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 250 of 721 Quit
Parsing: FB6

/
b ( )a
/a )b )(
(

r1. E E T
r2 E T
r3 T T D
r4 T D
r5 D | | E
Principle:
Reduce
whenever possible.
Shift only when
reduce is
impossible
E
Shift
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 251 of 721 Quit
Parsing: FB7

/
b ( )a
/a )b )(
(

r1. E E T
r2 E T
r3 T T D
r4 T D
r5 D | | E
Principle:
Reduce
whenever possible.
Shift only when
reduce is
impossible
E
Shift
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 252 of 721 Quit
Parsing: FB8

/
b ( )a
/ )b )
(

(
a
r1. E E T
r2 E T
r3 T T D
r4 T D
r5 D | | E
Principle:
Reduce
whenever possible.
Shift only when
reduce is
impossible
E
Reduce
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 253 of 721 Quit
Parsing: FB9

/
b ( )a
/ )b )
(

(
r1. E E T
r2 E T
r3 T T D
r4 T D
r5 D | | E
Principle:
Reduce
whenever possible.
Shift only when
reduce is
impossible
E
Reduce
D
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 254 of 721 Quit
Parsing: FB10

/
b ( )a
/ )b )
(

(
r1. E E T
r2 E T
r3 T T D
r4 T D
r5 D | | E
Principle:
Reduce
whenever possible.
Shift only when
reduce is
impossible
E
Reduce?
T
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 255 of 721 Quit
Parsing: FB11

/
b ( )a
/ )b )
(

(
r1. E E T
r2 E T
r3 T T D
r4 T D
r5 D | | E
Principle:
Reduce
whenever possible.
Shift only when
reduce is
impossible
E
T
Reduce? Shift
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 256 of 721 Quit
Parsing: FB12

/
b ( )a
)b )
(

(
/
r1. E E T
r2 E T
r3 T T D
r4 T D
r5 D | | E
Principle:
Reduce
whenever possible.
Shift only when
reduce is
impossible
E
T
Shift
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 257 of 721 Quit
Parsing: FB13

/
b ( )a
))
(

(
/
b
r1. E E T
r2 E T
r3 T T D
r4 T D
r5 D | | E
Principle:
Reduce
whenever possible.
Shift only when
reduce is
impossible
E
T
Reduce
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 258 of 721 Quit
Parsing: FB14

/
b ( )a
))
(

(
/
r1. E E T
r2 E T
r3 T T D
r4 T D
r5 D | | E
Principle:
Reduce
whenever possible.
Shift only when
reduce is
impossible
E
T
Reduce
D
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 259 of 721 Quit
Parsing: FB15

/
b ( )a
))
(

(
/
r1. E E T
r2 E T
r3 T T D
r4 T D
r5 D | | E
Principle:
Reduce
whenever possible.
Shift only when
reduce is
impossible
E
T
D
Reduce?
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 260 of 721 Quit
Parsing: FB16

/
b ( )a
))
(

(
/
r1. E E T
r2 E T
r3 T T D
r4 T D
r5 D | | E
Principle:
Reduce
whenever possible.
Shift only when
reduce is
impossible
E
T
D
No, REDUCE!
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 261 of 721 Quit
Parsing: FB17

/
b ( )a
))
(

(
r1. E E T
r2 E T
r3 T T D
r4 T D
r5 D | | E
Principle:
Reduce
whenever possible.
Shift only when
reduce is
impossible
E
T
Reduce?
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 262 of 721 Quit
Parsing: FB18

/
b ( )a
))
(

(
r1. E E T
r2 E T
r3 T T D
r4 T D
r5 D | | E
Principle:
Reduce
whenever possible.
Shift only when
reduce is
impossible
E
Shift
E
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 263 of 721 Quit
Parsing: FB19

/
b ( )a
)
(

(
)
r1. E E T
r2 E T
r3 T T D
r4 T D
r5 D | | E
Principle:
Reduce
whenever possible.
Shift only when
reduce is
impossible
E
E
Reduce
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 264 of 721 Quit
Parsing: FB20

/
b ( )a
)
(

r1. E E T
r2 E T
r3 T T D
r4 T D
r5 D | | E
Principle:
Reduce
whenever possible.
Shift only when
reduce is
impossible
E
Reduce
D
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 265 of 721 Quit
Parsing: FB21

/
b ( )a
)
(

r1. E E T
r2 E T
r3 T T D
r4 T D
r5 D | | E
Principle:
Reduce
whenever possible.
Shift only when
reduce is
impossible
E
T
Reduce?
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 266 of 721 Quit
Parsing: FB22

/
b ( )a
)
(

r1. E E T
r2 E T
r3 T T D
r4 T D
r5 D | | E
Principle:
Reduce
whenever possible.
Shift only when
reduce is
impossible
E
T
No, REDUCE!
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 267 of 721 Quit
Parsing: FB23

/
b ( )a
)
(
r1. E E T
r2 E T
r3 T T D
r4 T D
r5 D | | E
Principle:
Reduce
whenever possible.
Shift only when
reduce is
impossible
E
Shift
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 268 of 721 Quit
Parsing: FB24

/
b ( )a
(
)
r1. E E T
r2 E T
r3 T T D
r4 T D
r5 D | | E
Principle:
Reduce
whenever possible.
Shift only when
reduce is
impossible
Reduce
E
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 269 of 721 Quit
Parsing: FB25

/
b ( )a
r1. E E T
r2 E T
r3 T T D
r4 T D
r5 D | | E
Principle:
Reduce
whenever possible.
Shift only when
reduce is
impossible
ReduceD
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 270 of 721 Quit
Parsing: FB26

/
b ( )a
r1. E E T
r2 E T
r3 T T D
r4 T D
r5 D | | E
Principle:
Reduce
whenever possible.
Shift only when
reduce is
impossible
ReduceT
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 271 of 721 Quit
Parsing: FB27

/
b ( )a
r1. E E T
r2 T
r3 T T D
r4 T D
r5 D | | E
Principle:
Reduce
whenever possible.
Shift only when
reduce is
impossible
Reduce
E
E
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 272 of 721 Quit
Unbracketed Expression
Consider an example of an unbracketed expression which relies on the prece-
dence rules as defined in the grammar.
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 273 of 721 Quit
Parsing: UB0

/
b ( )
a −
a
a / b
r1. E E T
r2 E T
r3 T T D
r4 T D
r5 D | | E
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 274 of 721 Quit
Parsing: UB1

/
b ( )a
− a / b
r1. E E T
r2 E T
r3 T T D
r4 T D
r5 D | | E
Shifta
Principle:
Reduce
whenever possible.
Shift only when
reduce is
impossible
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 275 of 721 Quit
Parsing: UB2

/
b ( )a
− a / b
r1. E E T
r2 E T
r3 T T D
r4 T D
r5 D | | E
D Reduce by r5
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 276 of 721 Quit
Parsing: UB3

/
b ( )a
− a / b
r1. E E T
r2 E T
r3 T T D
r4 T D
r5 D | | E
T Reduce by r4
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 277 of 721 Quit
Parsing: UB4

/
b ( )a
− a / b
r1. E E T
r2 E T
r3 T T D
r4 T D
r5 D | | E
E Reduce by r2
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 278 of 721 Quit
Parsing: UB5

/
b ( )a

a / b
r1. E E T
r2 E T
r3 T T D
r4 T D
r5 D | | E
E
Shift
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 279 of 721 Quit
Parsing: UB6

/
b ( )a

/ b
r1. E E T
r2 E T
r3 T T D
r4 T D
r5 D | | E
E
Shifta
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 280 of 721 Quit
Parsing: UB7

/
b ( )a

/ b
r1. E E T
r2 E T
r3 T T D
r4 T D
r5 D | | E
E
D
E
Reduce by r5
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 281 of 721 Quit
Parsing: UB8

/
b ( )a

/ b
r1. E E T
r2 E T
r3 T T D
r4 D
r5 D | | E
EE
T
T Reduce by r4
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 282 of 721 Quit
Parsing: UB8a

/
b ( )a
/ b
r1. E E T
r2 E T
r3 T T D
r4 D
r5 D | | E
EE
T
T Reduce by r4

Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 283 of 721 Quit
Parsing: UB9a

/
b ( )a
/ b
r1. E E T
r2 E T
r3 T T D
r4 D
r5 D | | E
EE
T
Reduce by r1
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 284 of 721 Quit
Parsing: UB10a

/
b ( )a
b
/
r1. E E T
r2 E T
r3 T T D
r4 D
r5 D | | E
EE
T
Shift
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 285 of 721 Quit
Parsing: UB11a

/
b ( )a
/
r1. E E T
r2 E T
r3 T T D
r4 D
r5 D | | E
EE
T
Shiftb
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 286 of 721 Quit
Parsing: UB12a

/
b ( )a
/
r1. E E T
r2 E T
r3 T T D
r4 D
r5 D | | E
EE
T
D Reduce by r5
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 287 of 721 Quit
Parsing: UB13a

/
b ( )a
/
r1. E E T
r2 E T
r3 T T D
r4 D
r5 D | | E
EE
T
T Reduce by r4
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 288 of 721 Quit
Parsing: UB14a

/
b ( )a
/
r1. E E T
r2 E T
r3 T T D
r4 D
r5 D | | E
EE
T
E Reduce by r2
St
uc
k!
Get back!
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 289 of 721 Quit
Parsing: UB14b

/
b ( )a
/
r1. E E T
r2 E T
r3 T T D
r4 D
r5 D | | E
EE
T
E Reduce by r2
Get back!
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 290 of 721 Quit
Parsing: UB13b

/
b ( )a
/
r1. E E T
r2 E T
r3 T T D
r4 D
r5 D | | E
EE
T
T Reduce by r4
Get back!
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 291 of 721 Quit
Parsing: UB12b

/
b ( )a
/
r1. E E T
r2 E T
r3 T T D
r4 D
r5 D | | E
EE
T
D Reduce by r5
Get back!
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 292 of 721 Quit
Parsing: UB11b

/
b ( )a
/
r1. E E T
r2 E T
r3 T T D
r4 D
r5 D | | E
EE
T
ShiftbGet back!
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 293 of 721 Quit
Parsing: UB10b

/
b ( )a
b
/
r1. E E T
r2 E T
r3 T T D
r4 D
r5 D | | E
EE
T
ShiftGet back!
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 294 of 721 Quit
Parsing: UB9b

/
b ( )a
/ b
r1. E E T
r2 E T
r3 T T D
r4 D
r5 D | | E
EE
T
Reduce by r1
Get back to
where you
once belonged!
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 295 of 721 Quit
Parsing: UB8b

/
b ( )a
/ b
r1. E E T
r2 E T
r3 T T D
r4 D
r5 D | | E
EE
T
T Reduce by r4

Shift instead
of reduce here!
Principle:m
odifie
d
Reduce whenever possible, but
but depending upon
lookahead
Shift−
reduc
e
confli
ct
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 296 of 721 Quit
Parsing: UB8

/
b ( )a

/ b
r1. E E T
r2 E T
r3 T T D
r4 D
r5 D | | E
EE
T
T Reduce by r4
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 297 of 721 Quit
Parsing: UB9

/
b ( )a

/
b
r1. E E T
r2 E T
r3 T T D
r4 D
r5 D | | E
EE
T
T
Shift
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 298 of 721 Quit
Parsing: UB10

/
( )a

/
r1. E E T
r2 E T
r3 T T D
r4 D
r5 D | | E
EE
T
T
Shift
b
b
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 299 of 721 Quit
Parsing: UB11

/
b ( )a

r1. E E T
r2 E T
r3 T T D
r4 D
r5 D | | E
EE
T
T
/
D Reduce by r5
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 300 of 721 Quit
Parsing: UB12

/
b ( )a
r1. E E T
r2 E T
r3 T T D
r4 D
r5 D | | E
EE
T
T Reduce by r3

Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 301 of 721 Quit
Parsing: UB13

/
b ( )a
r1. E E T
r2 E T
r3 T T D
r4 D
r5 D | | E
EE
T
Reduce by r1
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 302 of 721 Quit
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 303 of 721 Quit
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 304 of 721 Quit
4.10. Bottom-Up Parsing
Bottom-Up Parsing
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 305 of 721 Quit
Parse Trees: 0
−r1. E E T
r2 E T
/r3 T T D b ( )aD | | Er5
r4 T D
a− / ba
shift-reduce parsing:0
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 306 of 721 Quit
Parse Trees: 1
a − a / b
−r1. E E T
r2 E T
/r3 T T D b ( )aD | | Er5
r4 T D
D
shift-reduce parsing:1
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 307 of 721 Quit
Parse Trees: 2
a − a / b
T
−r1. E E T
r2 E T
/r3 T T D b ( )aD | | Er5
r4 T D
D
shift-reduce parsing:2
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 308 of 721 Quit
Parse Trees: 3
a − a / b
T
−r1. E E T
r2 E T
/r3 T T D b ( )aD | | Er5
r4 T D
E
D
shift-reduce parsing:3
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 309 of 721 Quit
Parse Trees: 3a
a − a / b
T
−r1. E E T
r2 E T
/r3 T T D b ( )aD | | Er5
r4 T D
E
D
shift-reduce parsing
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 310 of 721 Quit
Parse Trees: 3b
a − a / b
T
−r1. E E T
r2 E T
/r3 T T D b ( )aD | | Er5
r4 T D
E
D
shift-reduce parsing
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 311 of 721 Quit
Parse Trees: 4
a − a
D
/ b
T
−r1. E E T
r2 E T
/r3 T T D b ( )aD | | Er5
r4 T D
E
D
shift-reduce parsing
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 312 of 721 Quit
Parse Trees: 5
a − a
D
/ b
TT
−r1. E E T
r2 E T
/r3 T T D b ( )aD | | Er5
r4 T D
E
D
shift-reduce parsing
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 313 of 721 Quit
Parse Trees: 5a
a − a
D
/ b
TT
−r1. E E T
r2 E T
/r3 T T D b ( )aD | | Er5
r4 T D
E
D
shift-reduce parsing
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 314 of 721 Quit
Parse Trees: 5b
a − a
D
/ b
TT
−r1. E E T
r2 E T
/r3 T T D b ( )aD | | Er5
r4 T D
E
D
shift-reduce parsing
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 315 of 721 Quit
Parse Trees: 6
a − a
D
/ b
D
TT
−r1. E E T
r2 E T
/r3 T T D b ( )aD | | Er5
r4 T D
E
D
shift-reduce parsing
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 316 of 721 Quit
Parse Trees: 7
a − a
D
/ b
D
T
T
T
−r1. E E T
r2 E T
/r3 T T D b ( )aD | | Er5
r4 T D
E
D
shift-reduce parsing
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 317 of 721 Quit
Parse Trees: 8
a − a
D
/ b
D
T
T
T
−r1. E E T
r2 E T
/r3 T T D b ( )aD | | Er5
r4 T D
E
E
D
shift-reduce parsing
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 318 of 721 Quit
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.
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 319 of 721 Quit
Parsing: Summary: 2
• Very often shift-reduce conflicts may occur because of the prefix problem. 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
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 320 of 721 Quit
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 321 of 721 Quit
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 322 of 721 Quit
4.11. 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.
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 323 of 721 Quit
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 looka-
head.
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 324 of 721 Quit
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
attempt to reach the start symbol of the grammar only through reductions
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 symbol 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$
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 325 of 721 Quit
Augmented Grammar
Consider the following (simplified) augmented grammar with a single binary
operator − and parenthesis. We also number the rules.
1. S → E$
2. E → E−T
3. E → T
4. T → a
5. T → (E)
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 326 of 721 Quit
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 understand-
ing other more powerful LR-parsing methods which require lookaheads for
deterministic shift-reduce decision-making
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 327 of 721 Quit
LR-Parsing Invariant
In any LR-parsing technique the following invariant holds.
For any syntactically valid sentence generated by the augmented grammar, 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.
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 328 of 721 Quit
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 α.
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 329 of 721 Quit
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 immediately
to X .
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 330 of 721 Quit
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.
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 331 of 721 Quit
Favourite Example
Consider our favourite augmented grammar
1. S → E$
2. E → E−T
3. E → T
4. T → a
5. T → (E)
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 332 of 721 Quit
Rule 1: Items
Rule 1
R1. S → E$
has the following three items
I1.1 S → NE$
I1.2 S → EN$
I1.3 S → E$N
one for each position on the right hand side of the rule.
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 333 of 721 Quit
Rule 2: Items
Rule 2
R2. E → E−T
has the following items
I2.1 E → NE−T
I2.2 E → EN−T
I2.3 E → E−NT
I2.4 E → E−TN
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 334 of 721 Quit
Rule 3: Items
Rule 3
R3. E → T
has just the items
I3.1 E → NT
I3.2 E → TN
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 335 of 721 Quit
Rule 4: Items
Rule 4
R4. T → a
has the items
I4.1 T → Na
I4.2 T → aN
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 336 of 721 Quit
Rule 5: Items
Rule 5
R5. T → (E)
has the items
I5.1 T → N(E)
I5.2 T → (NE)
I5.3 T → (EN)
I5.4 T → (E)N
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 337 of 721 Quit
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 → EN$. 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
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 338 of 721 Quit
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.
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 339 of 721 Quit
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
then it could be reduced to E−T
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 340 of 721 Quit
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 8 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→ αNXβ ∈ 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
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 341 of 721 Quit
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 9 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→ αNXβ ∈ I do
3. J := J ∪ {A→ αXNβ}
4. end for
5. return Closure(J)
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 342 of 721 Quit
State S1
S1 = Closure({S → NE$})
= {S → NE$, E → NE−T,E → NT,
T → Na, T → N(E)}
S1
(−→ Closure({T → (NE)}) = S2
S1
E−→ Closure({S → EN$, E → EN−T}) = S3
S1
T−→ Closure({E → TN}) = S7
S1
a−→ Closure({T → aN}) = S8
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 343 of 721 Quit
State S2
S2 = Closure({T → (NE)})
= {T → N(E), E → NE−T,E → NT,
T → Na, T → N(E)}
S2
(−→ Closure({T → (NE)}) = S2
S2
E−→ Closure({T → (EN), E → EN−T}) = S9
S2
T−→ Closure({E → TN}) = S7
S2
a−→ Closure({T → aN}) = S8
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 344 of 721 Quit
Other States
S3 = Closure({S → EN$, E → EN−T})
= {S → EN$, E → EN−T}
However,
S3
−−→ Closure({E → E−NT})
and
Closure({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−TN}
• S6 = {S → E$N}
• S7 = {E → TN}
• S8 = {T → aN}
• S9 = {T → (EN), E → EN−T}
• S10 = {T → (E)N}
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 345 of 721 Quit
Example: DFA
Parsing Table

S1
S E$
E E T
E T
T a
T ( E)

E E T
E T
T a
T ( E)
S2
T ( E)
S $
E T
E
E
S3
S6
S E$
S5
E E T
S4
E E T
T a
T (E)
S7
E T
S8
T a
S10
T (E)
S9
T )
E E
E(
T
(
E
$
−−
T
(
E )
a
T
a
T
a
−−
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 346 of 721 Quit
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
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 347 of 721 Quit
Example 4.16 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
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 348 of 721 Quit
Example 4.17 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
DFA Parsing Table
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 349 of 721 Quit
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
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 350 of 721 Quit
Exercise 4.3
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.
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 351 of 721 Quit
CFG = RLG + Bracket Matching
We use the idea that a context-free grammar is essentially a regular gram-
mar 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 “cur-
sor” (N).
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 352 of 721 Quit
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)
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 353 of 721 Quit
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 → PNˆE
I3.2 E → PN
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 actions took
place regardless of the input symbol. Clearly now that principle 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.
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 354 of 721 Quit
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 FOLLOW 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 constructions that are
encountered in many programming languages.
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 355 of 721 Quit
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).
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 356 of 721 Quit
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 grammar.
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).
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 357 of 721 Quit
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
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 358 of 721 Quit
if-then-else vs. if-then (Contd.)
and construct the following two augmented grammars G1 and G2.
11. S → I $ 12. S → I $
21. I → U 22. I → i b t I E
31. I → M 32. I → a
41. U → i b t I 42. E → e I
51. U → i b t M e U 52. E → ε
61. M → i b t M e M
71. M → a
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 359 of 721 Quit
Exercise 4.4
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 conflicts.
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
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 360 of 721 Quit
5. Attributes & Semantic Analysis
Introduction to Semantics
Context-free grammars (actually EBNF) are used to describe the rules that define the grammatical structure of phrases
and sentences in the language. However a manual for a programming language also needs to describe the meaning of each
construct in the language both alone and in conjunction with other constructs. This is to enable users of the language to
write correct programs and to be able to predict the effect of each construct. Implementors of the language need correct
definitions of the meanings to be able to construct correct implementations of the language.
Syntax defines a well-formed program. Semantics defines the meaning of a syntactically correct program. However not
all well-formed programs have well defined meanings. Thus semantics also separates meaningful programs from merely
syntactically correct ones.
“Meaning” in the case of programming languages often refers to the execution behaviour of the program or the individual
constructs. This is useful from an implementation point of view. From a user programmer’s point of view It is possible to
view a programming language as a precise description mechanism that is independent of execution behaviour and restrict
meaning to the “effect” that a program or a construct has on some input (state).
While there are precise means of defining the syntax af the language, most language manuals describe the meanings of the
constructs in natural language prose. This unfortunately is not very desirable as natural language tends to be too verbose,
imprecise and very often ambiguous. On the other hand, if users and implementors have to be on the same page as regards
the behaviour of programs and individual programming constructs a precise and unambiguous definition is required for
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 361 of 721 Quit
this description. Typically a user programmer may misunderstand what a program or a construct will do when executed.
Implementors may interpret the meaning differently and hence different implementaions of the language may yield different
results on the same program.
While there are several formalisms for defining meanings of the constructs of a programming language, they all share the
following characteristics in order to maintain a certain uniformity and applicability for any program written in the language
• Meanings should be syntax-directed i.e. meanings should be based on the syntactical definition of the language in the
sense that it follows the hierarchy of the non-terminals in the grammar. The syntax (grammar) of the language therefore
provides the framework for the semantics of the language.
• The meaning should be compositional i.e. the meaning of a compound construct should be expressed in terms of the
meanings of the individual components in the construct. Hence it is important that the meanings of the most basic
constructs be defined first so that the meanings of the compound constructs may be expressed in terms of the meanings
of the individual components of the compound construct.
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 362 of 721 Quit
Attributes & Semantic Analysis
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 363 of 721 Quit
5.1. Context-sensitive analysis and Semantics
The Big Picture
a − a
D
/ b
D
T
T
T
−r1. E E T
r2 E T
/r3 T T D
r4 T D
E
E
a −
b ( )a
a / b
D
D | | Er5
a −
T
−r1. E E T
r2 E T
/r3 T T D
E
E
a −
b ( )a
a / b )(
( )a
T
b/
T
E
T
D
D | | Er5
r4 T D
D
D
D
Figure 5: Derivation trees or Concrete parse trees example 5.1
The parser for a context-free grammar transforms the token stream into a derivation tree (which we also call a concrete
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 364 of 721 Quit
parse tree)3. What we actually require in order to perform a computation is really an abstract syntax tree.
Example 5.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.
The (possibly modified grammar) required for parsing
• 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 override associativity when needed,
– to facilitate easy parsing
But these symbols do not by themselves carry any semantic4 information.
• also has many more non-terminal symbols that are required for parsing, but which carry no semantic significance
– either for the end-user of the language
– or for the later phases of the compilation process.
Both expressions in example 5.1 have the same meaning (semantics) if we assume that the operations are subtraction
and division over integers respectively, and that division has higher precedence than subtraction. But the sentences are
3The term parse tree is a much abused term used to refer to anything from a derivation tree to an abstract syntax tree (AST).
4Semantic analysis is another much abused term, often used by compiler writers to included even merely context-sensitive information.
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 365 of 721 Quit
syntactically different and correspondingly have different parse trees (see fig. 5). Both the expressions may be represented
by the following abstract syntax tree (AST) which gives the hierarchical structure of the expression.

b
a
a
/
Figure 6: Abstract syntax tree (AST) for the sentences in fig. 5
Notice that in figure 6
• Every node in the AST is labelled by a token.
• The AST abstracts away from non-terminals which have significance only for the parsing of the expression and have no
semantic significance whatsoever,
• The AST abstracts away from bracketing and punctuation mechanisms and provides a hierarchical structure containing
only the essential operators and operands.
• The AST clearly distinguishes the operators (based on their arity) from the operands (which are leaves of the AST).
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 366 of 721 Quit
Context-sensitive Analysis: Preamble
The Big Picture
• Every programming language can be used to program any computable func-
tion, assuming of course, it has
– unbounded memory, and
– unbounded time
• Context-free grammars are used to specify the phrase structure of a language
in a manner that is free of all context.
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 367 of 721 Quit
Semantic Analysis
The Big Picture
1. Context-free grammars are not powerful enough to represent all computable
functions.
Example 5.2 The language {anbncn|n > 0} is not context-free but can
be generated by a context-sensitive grammar.
2. Semantic analysis is an essential step to
• producing the abstract syntax trees (AST)
• 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 symbol table or useful for error-handling)
• allocating memory for individual “objects” (variables, constants, struc-
tures, arrays etc.)
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 368 of 721 Quit
Context-sensitive Analysis: 1
The Big Picture
• There are aspects of a program that cannot be represented/enforced by a
context-free grammar definition. Examples include
– scope and visibility issues with respect to identifiers in a program.
– type consistency between declaration and use.
– correspondence between formal and actual parameters (example 5.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 parameters should equal n).
• Many of these attributes are context-sensitive in nature. They need to be
computed and if necessary propagated during parsing from wherever they
are available.
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 369 of 721 Quit
Context-sensitive Analysis: 2
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 nontermi-
nals 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
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 370 of 721 Quit
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 371 of 721 Quit
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 372 of 721 Quit
6. Static Scope Rules
Disjoint Scopes
let
in
end
val x = 10;
fun fun1 y =
let
...
in
...
end
fun fun2 z =
let ...
in ...
end
fun1 (fun2 x)
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 373 of 721 Quit
Nested Scopes
let
in
end
fun1 x
val x = 10;
fun fun1 y =
let
val x = 15
in
x + y
end
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 374 of 721 Quit
Overlapping Scopes
let
in
end
val x = 10;
fun fun1 y =
...
...
...
...
fun1 (fun2 x)
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 375 of 721 Quit
Spannning
let
in
end
val x = 10;
fun fun1 y =
...
...
fun fun2 z =
...
...
fun1 (fun2 x)
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 376 of 721 Quit
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
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 377 of 721 Quit
Names & References: 0
let
in
end
fun1 x
val x = 10;
fun fun1 y =
let
val x = 15
in
x + y
end
val z = 5;
* z
Back to Scope & Names
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 378 of 721 Quit
Names & References: 1
let
in
end
fun1 x
val x = 10;
fun fun1 y =
let
val x = 15
in
x + y
end
val z = 5;
* z
Back to Scope & Names
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 379 of 721 Quit
Names & References: 2
let
in
end
fun1 x
val x = 10;
fun fun1 y =
let
val x = 15
in
x + y
end
val z = 5;
* z
Back to Scope & Names
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 380 of 721 Quit
Names & References: 3
let
in
end
fun1 x
val x = 10;
fun fun1 y =
let
val x = 15
in
x + y
end
val z = 5;
* z
Back to Scope & Names
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 381 of 721 Quit
Names & References: 4
let
in
end
fun1 x
val x = 10;
fun fun1 y =
let
val x = 15
in
x + y
end
val z = 5;
* z
Back to Scope & Names
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 382 of 721 Quit
Names & References: 5
let
in
end
fun1 x
val x = 10;
fun fun1 y =
let
val x = 15
in
x + y
end
val z = 5;
* z
Back to Scope & Names
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 383 of 721 Quit
Names & References: 6
let
in
end
fun1 x
val x = 10;
fun fun1 y =
let
val x = 15
in
x + y
end
val z = 5;
* z
Back to Scope & Names
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 384 of 721 Quit
Names & References: 7
let
end
val x = 10;
fun fun1 y =
let
...
in
...
end
fun fun2 z =
let ...
in ...
end
fun1 (fun2 x)
val x = x − 5;
in
Back to Scope & Names
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 385 of 721 Quit
Names & References: 8
let
end
val x = 10;
fun fun1 y =
let
...
in
...
end
fun fun2 z =
let ...
in ...
end
fun1 (fun2 x)
val x = x − 5;
in
Back to Scope & Names
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 386 of 721 Quit
Names & References: 9
let
end
val x = 10;
fun fun1 y =
let
...
in
...
end
fun fun2 z =
let ...
in ...
end
fun1 (fun2 x)
val x = x − 5;
in
Back to Scope & Names
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 387 of 721 Quit
Definition of Names
Definitions are of the form
qualifier name . . . = body
• val name =
• fun name ( argnames ) =
• local definitions
in definition
end
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 388 of 721 Quit
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 definitions
in expression
end
use of local
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 389 of 721 Quit
Scope & local
end
local
fun fun1 y =
fun fun2 z =
in
fun fun3 x =
...
fun2 ...
fun1 ...
...
...
fun1
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 390 of 721 Quit
7. Runtime Structure
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 391 of 721 Quit
Run-time Structure
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 392 of 721 Quit
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.
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 393 of 721 Quit
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
Locals of P1
Procedure P1
Body of P1
Call P2
Main body
Call P1
Main → P1 → P2 → P21 → P21
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 394 of 721 Quit
Run-time Structure: 1
Main program
Globals
Main body
Procedure P2
Locals of P2
Procedure P21
Locals of P21
Body of P2
Procedure P1
Locals of P1
Body of P1
Globals
Body of P21
Main
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 395 of 721 Quit
Run-time Structure: 2
Main program
Globals
Main body
Procedure P2
Locals of P2
Procedure P21
Locals of P21
Body of P2
Procedure P1
Locals of P1
Body of P1
Globals
Formal par of P1
Locals of P1
Return address to Main
Dynamic link to Main
Static link to Main
Body of P21
Main → P1
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 396 of 721 Quit
Run-time Structure: 3
Main program
Globals
Main body
Procedure P2
Locals of P2
Procedure P21
Locals of P21
Body of P2
Procedure P1
Locals of P1
Body of P1
Globals
Formal par of P1
Locals of P1
Return address to Main
Formal par P2
Locals of P2
Return address to last of P1
Dynamic link to Main
Dynamic link to last P1
Static link to Main
Static link to last P1
Body of P21
Main → P1 → P2
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 397 of 721 Quit
Run-time Structure: 4
Main program
Globals
Main body
Procedure P2
Locals of P2
Procedure P21
Locals of P21
Body of P2
Procedure P1
Locals of P1
Body of P1
Globals
Formal par of P1
Locals of P1
Return address to Main
Formal par P2
Locals of P2
Return address to last of P1
Formal par P21
Locals of P21
Return address to last of P2
Dynamic link to Main
Dynamic link to last P1
Dynamic link to last P2
Static link to Main
Static link to last P1
Static link last P2
Body of P21
Main → P1 → P2 → P21
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 398 of 721 Quit
Run-time Structure: 5
Main program
Globals
Main body
Procedure P2
Locals of P2
Procedure P21
Locals of P21
Body of P2
Procedure P1
Locals of P1
Body of P1
Globals
Formal par of P1
Locals of P1
Return address to Main
Formal par P2
Locals of P2
Return address to last of P1
Formal par P21
Locals of P21
Return address to last of P2
Formal par P21
Locals of P21
Return address to last of P21
Dynamic link to Main
Dynamic link to last P1
Dynamic link to last P2
Dynamic link to last P21
Static link to Main
Static link to last P1
Static link last P2
Static link to last P2
Body of P21
Main → P1 → P2 → P21 → P21
Back to the Big Picture
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 399 of 721 Quit
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 400 of 721 Quit
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 401 of 721 Quit
8. Abstract Syntax
Abstract Syntax Trees
The construction of ASTs from concrete parse trees is an example of a trans-
formation that can be performed using a syntax-directed definition that has no
side-effects.
Hence we define it using an attribute grammar.
Definition 8.1 An attribute grammar is a formal way to define semantic rules
and context-sensitive aspects of the language. Each production of the grammar
is associated with a set of values or semantic rules. These values and semantic
rules are collectively referred to as attributes.
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 402 of 721 Quit
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:
4 1
2−−
/
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 403 of 721 Quit
Evaluation: 0
4 1
2−−
/
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 404 of 721 Quit
Evaluation: 1
4 1
2−−
/
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 405 of 721 Quit
Evaluation: 2
2
/
3
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 406 of 721 Quit
Evaluation: 3
2
/
3
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 407 of 721 Quit
Evaluation: 4
1
But what we actually get during parsing is a tree that looks like . . .
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 408 of 721 Quit
Abstract Syntax: 1
. . . THIS! E
T
T F
/
n
F
( )
E
E T

T
F
F
n
n
n n
/
n−−
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 409 of 721 Quit
Abstract Syntax
Shift-reduce parsing produces a concrete syntax tree from the rightmost deriva-
tion. 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 sym-
bols
On the other hand the abstract syntax tree (AST) contains no punctuations
and makes a clear distinction between an operand and an operator.
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 410 of 721 Quit
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 construction.
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 terminal
and nonterminal symbol which points to the root of the subtree created for it.
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 411 of 721 Quit
Abstract Syntax Trees: Imperative
E0 → E1−T B E0.ptr := makeBinaryNode(−, E1.ptr, T.ptr)
E → T B E.ptr := T.ptr
T0 → T1/F B T0.ptr := makeBinaryNode(/, T1.ptr, F.ptr)
T → F B T.ptr := F.ptr
F → (E) B F.ptr := E.ptr
F → n B F.ptr := makeLeaf (n.val)
The Big Picture
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 412 of 721 Quit
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 terminal
and nonterminal symbol which points to the root of the subtree created for it.
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 413 of 721 Quit
Abstract Syntax: Functional
E0 → E1−T B E0.ast := makeBinaryNode(−, E1.ast, T.ast)
E → T B E.ast := T.ast
T0 → T1/F B T0.ast := makeBinaryNode(/, T1.ast, F.ast)
T → F B T.ast := F.ast
F → (E) B F.ast := E.ast
F → n B F.ast := makeLeaf (n.val)
The Big Picture
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 414 of 721 Quit
Abstract Syntax: Alternative Functional
In languages like SML which support algebraic (abstract) datatypes, the func-
tions makeLeaf(literal) and makeBinaryNode(opr, opd1, opd2)
may be replaced by the constructors of an appropriate recursively defined
datatype AST.
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 415 of 721 Quit
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 416 of 721 Quit
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 417 of 721 Quit
9. Syntax-Directed Translation
Syntax-directed Translation
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 418 of 721 Quit
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.
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 419 of 721 Quit
The Structure of a Compiler
Divide and conquer. A large-scale structure and organization of a compiler
or translator is defined by the structure of the parser in terms of the individual
productions of the context-free grammar that is used in parsing.
Syntax-directed definitions. The problem of context-sensitive and se-
mantic analysis is split up into the computation of individual attributes and
semantic rules in such a way that each production is associated with the
(partial) computation of one or more attributes.
Glue code. Finally it may require some “glue-code” to put together these
computations to obtain the final compiler/translator. The glue-code may
also be split into some that occurs in the beginning through global declara-
tions/definitions and some which need to be performed in the end.
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 420 of 721 Quit
Syntax-Directed Definitions (SDD)
Syntax-Directed definitions are high-level specifications which specify the eval-
uation 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.
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 421 of 721 Quit
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 lan-
guage construct on the context in which it appears.
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 422 of 721 Quit
What is Syntax-directed?
• A syntax-directed definition is a generalisation of a context-free grammar in
which each grammar symbol has an associated set of attributes, partitioned
into two subsets called synthesized and inherited attributes.
• The various attributes are computed by so-called semantic rules associated
with each production of the grammar which allows the computation 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
performed by the given rule and
top-down (RDP) parsing in the procedure before the next call or re-
turn from the procedure.
• A parse tree showing the various attributes at each node is called an anno-
tated parse tree.
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 423 of 721 Quit
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 symbols of α.
Definition 9.1 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.
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 424 of 721 Quit
Attribute Grammars
• An attribute grammar is a syntax-directed definition in which the functions
in semantic rules can have no side-effects.
• The attribute grammar also specifies how the attributes are propagated
through the grammar, by using graph dependency between the produc-
tions.
• In general different occurrences of the same non-terminal symbol in each
production will be distinguished by appropriate subscripts when defining the
semantic rules associated with the rule.
The following example illustrates the concept of a syntax-directed definition
using synthesized attributes.
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 425 of 721 Quit
Attribute Grammars: Example
Determining the values of arithmetic expressions. Consider a simple attribute
val associated with an expression
E0 → E1−T B E0.val := E1.val − T.val
E → T B E.val := T.val
T0 → T1/F B T0.val := T1.val/F.val
T → F B T.val := F.val
F → (E) B F.val := E.val
F → n B F.val := n.val
Note: The attribute n.val is the value of the numeral n computed during
scanning (lexical analysis).
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 426 of 721 Quit
Attributes: Basic Assumptions
• Terminal symbols are assumed to have only synthesized attributes. Their
attributes are all supplied by the lexical analyser during scanning.
• 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.
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 427 of 721 Quit
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 428 of 721 Quit
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 429 of 721 Quit
9.1. Synthesized Attributes
Synthesized Attributes
Evaluating the expression (4− 1)/2 generated by the grammar for subtraction
and division
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 430 of 721 Quit
Synthesized Attributes: 0
ET F
T
T F
/
/
(
n
F
( )
E
E
) n
E T


T
F
F
n
n
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 431 of 721 Quit
Synthesized Attributes: 1
ET F
T
T F
/
/
(
n
F
( )
E
E
) n
E T


T
F
F
n
n
4
4
3 2 1
Synthesized Attributes
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 432 of 721 Quit
Synthesized Attributes: 2
ET F
T
T F
/
/
(
n
F
( )
E
E
) n
E T


T
F
F
n
n
4
4
3 2 1
4
Synthesized Attributes
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 433 of 721 Quit
Synthesized Attributes: 3
ET F
T
T F
/
/
(
n
F
( )
E
E
) n
E T


T
F
F
n
n
4
4
4
4
3 2 1
Synthesized Attributes
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 434 of 721 Quit
Synthesized Attributes: 4
ET F
T
T F
/
/
(
n
F
( )
E
E
) n
E T


T
F
F
n
n
4
4
4
4
4
3 2 1
Synthesized Attributes
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 435 of 721 Quit
Synthesized Attributes: 5
ET F
T
T F
/
/
(
n
F
( )
E
E
) n
E T


T
F
F
n
n
4
4
4
4
4
1
3 2 1
Synthesized Attributes
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 436 of 721 Quit
Synthesized Attributes: 6
ET F
T
T F
/
/
(
n
F
( )
E
E
) n
E T


T
F
F
n
n
4
4
4
4
4
1
1
3 2 1
Synthesized Attributes
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 437 of 721 Quit
Synthesized Attributes: 7
ET F
T
T F
/
/
(
n
F
( )
E
E
) n
E T


T
F
F
n
n
4
4
4
4
4
1
1
1
3 2 1
Synthesized Attributes
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 438 of 721 Quit
Synthesized Attributes: 8
ET F
T
T F
/
/
(
n
F
( )
E
E
) n
E T


T
F
F
n
n
4
4
4
4
4
1
1
1
3 2 1
3
Synthesized Attributes
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 439 of 721 Quit
Synthesized Attributes: 9
ET F
T
T F
/
/
(
n
F
( )
E
E
) n
E T


T
F
F
n
n
4
4
4
4
4
1
1
1
3
3 2 1
3
Synthesized Attributes
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 440 of 721 Quit
Synthesized Attributes: 10
ET F
T
T F
/
/
(
n
F
( )
E
E
) n
E T


T
F
F
n
n
4
4
4
4
4
1
1
1
3
3
3 2 1
3
Synthesized Attributes
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 441 of 721 Quit
Synthesized Attributes: 11
ET F
T
T F
/
/
(
n
F
( )
E
E
) n
E T


T
F
F
n
n
4
4
4
4
4
1
1
1
3 2
3
3 2 1
3
Synthesized Attributes
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 442 of 721 Quit
Synthesized Attributes: 12
ET F
T
T F
/
/
(
n
F
( )
E
E
) n
E T


T
F
F
n
n
4
4
4
4
4
1
1
1
3 2
23
3 2 1
3
Synthesized Attributes
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 443 of 721 Quit
Synthesized Attributes: 13
ET F
T
T F
/
/
(
n
F
( )
E
E
) n
E T


T
F
F
n
n
4
4
4
4
4
1
1
1
3 2
23
3 2 1
1
3
Synthesized Attributes
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 444 of 721 Quit
Synthesized Attributes: 14
ET F
T
T F
/
/
(
n
F
( )
E
E
) n
E T


T
F
F
n
n
4
4
4
4
4
1
1
1
3 2
23
3 2 1
1
1
3
Synthesized Attributes
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 445 of 721 Quit
An Attribute Grammar
E
T
T F
/
n
F
( )
E
E T

T
F
F
n
n4
4
4
4
1
1
1
3 2
23
1
1
3
E0 → E1−T B E0.val := sub(E1.val, T.val)
E → T B E.val := T.val
T0 → T1/F B T0.val := div(T1.val, F.val)
T → F B T.val := F.val
F → (E) B F.val := E.val
F → n B F.val := n.val
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 446 of 721 Quit
Synthesized Attributes Evaluation: Bottom-up
During bottom-up parsing synthesized attributes are evaluated as follows:
Bottom-up Parsers
1. Keep an attribute value stack along with the parsing stack.
2. Just before applying a reduction of the form Z → Y XXXXXXXXXXYk 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 corresponding to
the one where the symbol Z will appear on the parsing stack as a result
of the reduction.
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 447 of 721 Quit
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 → Y XXXXXXXXXXYk, the parser
makes recursive calls to procedures corresponding to the symbols Y XXXXXXXXXXYk.
In each case the attributes of the non-terminal symbols Y XXXXXXXXXXYk are com-
puted and returned to the procedure for Z. Compute the synthesized at-
tributes of Z from the attribute values returned from the recursive calls.
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 448 of 721 Quit
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 449 of 721 Quit
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 450 of 721 Quit
9.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
T
int
L
z
L
L
x
I
y
I
D
I
,
,
x
D L T I
y z int
,
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 451 of 721 Quit
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
T
int
L
z
L
L
x
I
y
I
D
I
,
,
x
D L T I
y z int
int
int
,
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 452 of 721 Quit
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
T
int
L
z
L
L
x
I
y
I
D
I
,
,
x
D L T I
y z int
int int
int
int
,
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 453 of 721 Quit
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
T
int
L
z
L
L
x
I
y
I
D
I
,
,
x
D L T I
y z int
int int
int
int
,
int
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 454 of 721 Quit
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
T
int
L
z
L
L
x
I
y
I
D
I
,
,
x
D L T I
y z int
int int
int
int
,
int
int
int
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 455 of 721 Quit
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
T
int
L
z
L
L
x
I
y
I
D
I
,
,
x
D L T I
y z int
int int
int
int
,
int
int
int
int
int
int
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 456 of 721 Quit
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
T
int
L
z
L
L
x
I
y
I
D
I
,
,
x
D L T I
y z int
int int
int
int
,
int
int
int
int
int
int
int
int
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 457 of 721 Quit
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
T
int
L
z
L
L
x
I
y
I
D
I
,
,
x
D L T I
y z int
int int
int
int
,
int
int
int
int
int
int
int
int
int
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 458 of 721 Quit
Attribute Grammar: Inherited
T
int
L
z
L
L
x
I
y
I
D
I
,
int
int
,
int
int
int
int
int
int
int
int
int
D → TL B L.in := T.type
T → int B T.type := int.int
T → float B T.type := float.f loat
L0 → L1,I B L1 := L0.in
L → I B I.in := L.in
I → id B id.type := I.in
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 459 of 721 Quit
L-attributed Definitions
Definition 9.2 A grammar is L-attributed if for each production of the form
Y → X XXXXXXXXXXXk, 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.
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 460 of 721 Quit
Why L-attributedness? 1
Y → X XXXXXXXXXXXk
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.
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 461 of 721 Quit
Why L-attributedness? 2
Y → X XXXXXXXXXXXk
Intuitively, if Xj.inh is an inherited attribute then
• 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 per-
formed just before the reduction by the rule Y → X XXXXXXXXXXXk during parsing.
Instead it may have to be postponed till the end of parsing.
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 462 of 721 Quit
Why L-attributedness? 3
Y → X XXXXXXXXXXXk
Intuitively, if Xj.inh is an inherited attribute then
• it could depend on the synthesized or inherited attributes of any of the
symbols X XXXXXXXXXXXj−1 since they would already be available on the attribute
value stack.
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 463 of 721 Quit
Why L-attributedness? 4
Y → X XXXXXXXXXXXk
Intuitively, if Xj.inh is an inherited attribute then
• it could depend upon the inherited attributes of Y because these inher-
ited 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.
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 464 of 721 Quit
A Non L-attributed Definition
Our attribute grammar for C-style declarations is definitely L-attributed. How-
ever consider the following grammar for declarations in Pascal and ML.
D → L:T B L.in := T.type
T → int B T.type := int.int
T → real B T.type := real.real
L0 → L1,I B L1 := L0.in
L → I B I.in := L.in
I → id B 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.
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 465 of 721 Quit
Evaluating Non-L-attributed Definitions 1
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 corresponding
synthesized ones.
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 466 of 721 Quit
Evaluating Non-L-attributed Definitions 2
The language of the grammar of Pascal and ML declarations can be generated
as follows:
D → idL B addtype(id, L.type)
L → :T B L.in := T.type
L → ,id L B L0.type := L1.type;
addtype(id.L1.type)
T → int B T.type := int.int
T → real B T.type := real.real
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 467 of 721 Quit
Dependency Graphs
In general, the attributes required to be computed during parsing could be syn-
thesized or inherited and further it is possible that some synthesized attributes
of some symbols may depend on the inherited attributes of some other sym-
bols. In such a scenario it is necessary to construct a dependency graph of the
attributes of each node of the parse tree.
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 468 of 721 Quit
Dependency Graph Construction
Algorithm 10 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
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 469 of 721 Quit
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 470 of 721 Quit
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 471 of 721 Quit
10. 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
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 472 of 721 Quit
Symbol Table:1
The Big picture
• 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 table
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 473 of 721 Quit
Symbol Table:2
The Big picture
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, proce-
dures, classes)
• address of the location containing the string which actually names the iden-
tifier and its length in the string pool
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 474 of 721 Quit
Symbol Table:3
The Big picture
• A symbol table exists through out the compilation (and run-time for debug-
ging purposes).
• 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.
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 475 of 721 Quit
Symbol Table:4
The Big picture
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 (decla-
ration before use).
Semantic analysis:
• Determination of types of identifiers from declarations
• type checking to ensure that operands are used in type-valid contexts.
• Checking scope, visibility violations.
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 476 of 721 Quit
Symbol Table:5
The Big picture
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.
ai.e.relative to a base address that is known only at run-time
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 477 of 721 Quit
The hash table
Each name is hashed to an index of the hash table whose entry points to a
chain of records where each record contains
• a possible link to the next record on the chain (in case of collisions)
• the name of the identifier
• category (e.g. module, procedure, function, block, record, formal parameter
etc.)
• scope number of the identifier
• type information
• number of parameters (in case of functions, procedures, modules classes
etc.)
• visibility information (derived from qualifiers such as public, private)
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 478 of 721 Quit
Symbol Table: Scope Stack
Scope rules
• In addition to the hash table a scope stack is maintained for resolving non-
local references.
• The new scope number is pushed onto the scope stack when the compiler
enters a new scope and popped when exiting a scope.
• There could be unnamed scopes too (e.g. unnamed blocks with local dec-
larations, for-loops where the counting variable is local to the loop etc).
• Each (static) scope may be assigned a number in sequential order as it
is encountered in the program text starting with 0 assigned for the global
scope.
• The scope number of a nested scope is always greater than that of its parent.
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 479 of 721 Quit
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 480 of 721 Quit
11. Intermediate Representation
Intermediate Representation
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 481 of 721 Quit
Intermediate Representation
Intermediate representations are important for reasons of portability i.e. plat-
form (hardware and OS) independence.
• (more or less) independent of specific features of the high-level language.
Example. Java byte-code which is the instruction set of the Java Virtual
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
Typical Instruction set
https://www.javatpoint.com/java-bytecode
https://www.javatpoint.com/java-bytecode
https://en.wikipedia.org/wiki/Java_bytecode
https://www.javatpoint.com/internal-details-of-jvm
https://www.javatpoint.com/internal-details-of-jvm
https://en.wikipedia.org/wiki/Java_bytecode_instruction_listings
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 482 of 721 Quit
IR Properties: Low vs high
1. It is fairly low-level containing instructions common to all target architectures
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 across architectures and OSs.
Portability
4. To ensure type-safety
Type safety
Typical Instruction set
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 483 of 721 Quit
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
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 484 of 721 Quit
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
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 485 of 721 Quit
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
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 486 of 721 Quit
IR Properties: Portability
1. How low can you stoop? . . .
2. How high can you rise?
3. To ensure portability across architectures and OSs.
• an unbounded number of variables and memory locations
• no commitment to Representational Issues
4. Type safety
Back to IR Properties
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 487 of 721 Quit
IR Properties: Type Safety
1. How low can you stoop? . . .
2. How high can you rise?
3. Portability
4. To ensure type-safety despite the hardware instruction set architectures.
• Memory locations are also typed according to the data they may contain,
• No commitment is made regarding word boundaries, and the structure of
individual data items.
Back to IR Properties Typical Instruction set
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 488 of 721 Quit
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) operation.
If an operation requires less than 3 operands then one or more of the operands
is made null.
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 489 of 721 Quit
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
https://en.wikipedia.org/wiki/Java_bytecode
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 490 of 721 Quit
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
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 491 of 721 Quit
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 relational
operator
• Procedures and parameters
• Arrays and array-indexing
• Pointer Referencing and Dereferencing
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 492 of 721 Quit
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
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 493 of 721 Quit
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
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 494 of 721 Quit
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
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 495 of 721 Quit
Pointers
x
x
*y
y
@z
*x
*x @z
*z
z
*z
z
x := ^y
x := *y
*x := y
*[email protected]
@z
*z
*z
x y
yx
x y
z
x
yy
*y
*y*y
@z
z
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 496 of 721 Quit
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 language
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 maintained as
part of the generation process.
• It is independent of the source language but can express target machine
operations without committing to too much detail.
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 497 of 721 Quit
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).
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 498 of 721 Quit
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.
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 499 of 721 Quit
IR: Expressions
E → id B
E.place := id.place;
E.code := emit()
E0 → E1 − E2 B
E0.place := newtemp;
E0.code := E1.code;
E2.code;
emit(E0.place := E1.place − E2.place)
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 500 of 721 Quit
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 Sfi Conditional
| while E do S end Iteration
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 501 of 721 Quit
IR: Assignment and Sequencing
S → id := E B
S.code := E.code
emit(id.place:=E.place)
S0 → S1; S2 B
S0.begin := S1.begin;
S0.after := S2.after;
S0.code := emit(S0.begin:)
S1.code
S2.code
emit(S0.after:)
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 502 of 721 Quit
IR: Conditional
S0 → if E then S1 else S2fi B
S0.begin := newlabel;
S0.after := S2.after;
S0.code := emit(S0.begin:)
E.code;
emit(if E.place= 0 goto S2.begin);
S1.code;
emit(goto S0.after);
S2.code;
emit(S0.after:)
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 503 of 721 Quit
IR: Iteration
S0 → while E do S1 end B
S0.begin := newlabel;
S0.after := newlabel;
S0.code := emit(S0.begin:)
E.code
emit(if E.place= 0 goto S0.after);
S1.code;
emit(goto S0.begin);
emit(S0.after:)
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 504 of 721 Quit
IR: Generation End
While generating the intermediate representation, it is sometimes necessary
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 number), wherein the
blank entries are filled in once the sequence number of the target instruction
becomes known.
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 505 of 721 Quit
12. The Pure Untyped Lambda Calculus: Basics
12.1. Motivation for λ
As Curry points out in his classic work on Combinatory Logic,
Curiously a systematic notation for functions is lacking in ordinary mathematics. The usual
notation ’f (x)’ does not distinguish between the function itself and the value of this function for
an undetermined value of the argument.
Let us consider the nature of functions, higher-order functions (functionals) and the use of naming in mathe-
matics, through some examples.
Example 12.1 Let y = x2 be the squaring function on the reals. Here it is commonly understood that
x is the “independent” variable and y is the “dependent” variable when we look on it as plotting the
function f (x) = x2 on the x− y axis.
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 506 of 721 Quit
Example 12.2 Often a function may be named and written as f (x) = xn to indicate that x is the
independent variable and n is understood (somehow!) to be some constant. Here f , x and n are all
names with different connotations. Similarly in the quadratic polynomial ax2 + bx + c it is somehow
understood that a, b and c denote constants and that x is the independent variable. Implicitly by using
the names like a, b and c we are endeavouring to convey the impression that we consider the class
{ax2 + bx + c | a, b, c ∈ R} of all quadratic polynomials of the given form.
Example 12.3 As another example, consider the uni-variate polynomial p(x) = x2 + 2x + 3. Is this
polynomial the same as p(y) = y2 + 2y + 3? Clearly they cannot be the same since the product p(x).p(y)
is a polynomial in two variables whereas p(x).p(x) yields a uni-variate polynomial of degree 4. However,
in the case of the function f in example 12.1 it does not matter whether we define the squaring function
as f (x) = x2 or as f (y) = y2.
Example 12.4 The squaring function 12.1 is a continuous and differentiable real-valued function (in
the variable x) and its derivative is f ′(x) = 2x. Whether we regard f ′ as the name of a new function or
we regard the ′ as an operation on f which yields its derivative seems to make no difference.
Example 12.5 Referring again to the functions f (x) and f ′(x) in example 12.4, it is commonly un-
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 507 of 721 Quit
derstood that f ′(0) refers to the value of the derivative of f at 0 which is also the value the function f ′
takes at 0. Now let us consider f ′(x + 1). Going by the commonly understood notion, since f ′(x) = 2x,
we would have f ′(x + 1) = 2(x + 1). Then for x = 0 we have f ′(x + 1) = f ′(0 + 1) = f ′(1) = 2 × 1 = 2.
We could also think of it as the function f ′(g(0)) where g is the function defined by g(x) = x + 1, then
f ′(g(0)) = 2g(0) = 2 which yields the same result.
The examples above give us some idea of why there is no systematic notation for functions which distinguishes
between a function definition and the application of the same function to some argument. It simply did not
matter!
However, this ambiguity in mathematical notation could lead to differing interpretationas and results in the
context of mathematical theories involving higher-order functions (or “functionals” as they are often referred
to). One common higher order function is the derivative (the differentiation operation) and another is the
indefinite integral. Most mathematical texts emphasize the higher-order nature of a function by enclosing their
arguments in (square) brackets. Hence if O is a functional which transforms a function f (x) into a function
g(x), this fact is usually written O[f (x)] = g(x).
Example 12.6 Consider the functional E (on continuous real-valued functions of one real variable x)
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 508 of 721 Quit
defined as follows.
E[f (x)] =
 f
′(0) if x = 0
f (x)− f (0)
x
if x 6= 0
The main question we ask now is “What does E[f (x + 1)] mean?”
It turns out that there are at least two ways of interpreting E[f (x + 1)] and unlike the case of example
12.5, the two interpretations actually yield different results!.
1. We may interpret E[f (x+ 1)] to mean that we first apply the transformation E to the function f (x)
and then substitute x + 1 for x in the resulting expression. We then have the following.
E[f (x)]
=
 f
′(0) if x = 0
f (x)− f (0)
x
if x 6= 0
=
{
0 if x = 0
x if x 6= 0
= x
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 509 of 721 Quit
Since E[f (x)] = x, E[f (x + 1)] = x + 1.
2. Since f (x+1) = f (g(x)) where g(x) = x+1, we may interpret E[f (x+1)] as applying the operator E to
the function h(x) = f (g(x)). Hence E[f (x+1)] = E[h(x)] where h(x) = f (g(x)) = (x+1)2 = x2+2x+1.
Noting that h′(x) = 2x + 2, h(0) = 1 and h′(0) = 2, we get
E[h(x)]
=
 h
′(0) if x = 0
h(x)− h(0)
x
if x 6= 0
=
{
2 if x = 0
x + 2 if x 6= 0
= x + 2
The last example should clearly convince the reader that there is a need to disambiguate between a function
definition and its application.
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 510 of 721 Quit
12.2. The λ-notation
In function definitions the independent variables are “bound” by a λ which acts as a pre-declaration of the
name that is going to be used in the expression that defines a function.
The notation f (x), which is interpreted to refer to “the value of function f at x”, will be replaced by (f x) to
denote an application of a function f to the (known or unknown) value x.
In our notation of the untyped applied λ-calculus the functions and their applications in the examples in
subsection 12.1 would be rewritten as follows.
Squaring . λ x[x2] is the squaring function.
Example XXXXXXXXXXq
df
= λ a b c x[ax2 + bx+ c] refers to any quadratic polynomial with coefficients unknown or
symbolic. To obtain a particular member of this family such as 1x2 + 2x + 3, one would have to evaluate
(((q XXXXXXXXXXwhich would yield λ x[1x2 + 2x + 3].
Example XXXXXXXXXXp
df
= λ x[x2 + 2x + 3]. Then p(x) would be written as (p x) i.e. as the function p applied
to the argument x to yield the expression x2 + 2x + 3. Likewise p(y) would be (p y) which would yield
y2 + 2y + 3. The products (p x).(p x) and (p x).(p y) are indeed different and distinct.
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 511 of 721 Quit
Example 12.5 Let us denote the operation of obtaining the derivative of a real-valued function f of one
independent variable x by the simple symbol D (instead of the more confusing
d
dx
). Then for any function
f , (D f ) would yield the derivative. In particular (D λ x[x2]) = λ x[2x] and the value of the derivative at
0 would be obtained by the application (λ x[2x] 0) which would yield 0. Likewise the value of the derivative
at x + 1 would be expressed as the application (λ x[2x] (x XXXXXXXXXXThus for any function f the value of its
derivative at x + 1 is simply the application ((D f ) (x + 1)).
The function g(x) = x + 1 would be defined as g
df
= λ x[x + 1] and (g x) = x + 1. Thus the alternative
definition of the derivative of f at x + 1 is simply the application ((D f ) (g x)).
Example 12.6 The two interpretations of the expression E[f (x + 1)] are respectively the following.
1. ((E f ) (x + 1)) and
2. ((E h) x) where h
df
= λ x[(f (g x))]
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 512 of 721 Quit
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 parameter.
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 513 of 721 Quit
The language Λ
• The language Λ is “pure” (as opposed to being “applied”) in the sense
that it is minimal and symbolic and does not involve any operators other
abstraction and application.
• When used in the context of some algebraic system (e.g. the algebra of
integers or reals) it becomes applied. Hence the example of using the λ-
notation in the differential calculus is one of an applied λ-calculus.
• It is purely symbolic and no types have been specified which put restrictions
on the use of variables in contexts. We will look at typing much later.
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 514 of 721 Quit
Free Variables
Definition 12.7 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 FV (N) V ar(N)
x {x} {x}
λx[L] FV (L)− {x} V ar(L) ∪ {x}
(L M) FV (L) ∪ FV (M) V ar(L) ∪ V ar(M)
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 515 of 721 Quit
Bound Variables
• The set of bound variables BV (N) = V ar(N)− FV (N).
• The same variable name may be used with different bindings in a single term
(e.g. (λx[x] λx[(x y)]))
• The brackets “[” and “]” delimit the scope of the bound variable x in the
term λx[L].
• The usual rules of static scope apply to λ-terms.
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 516 of 721 Quit
Closed Terms and Combinators
Definition 12.8
• Λ0 ⊆ Λ is the set of closed λ-terms (i.e. terms with no free variables).
• A λ abstraction with no free variables is called a combinatora.
The λ-terms D (see equation (??)) and E (see equation (??)) are combinators.
aCombinators represent function definitions
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 517 of 721 Quit
Notational Conventions
To minimize use of brackets and parentheses unambiguously
1. λx1x XXXXXXXXXXxm[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.
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 518 of 721 Quit
Substitution
Definition 12.9 For any terms L, M and N and any variable x, the substi-
tution 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∈ FV (N)
{N/x}λy[L] ≡ λz[{N/x}{z/y}L] if y 6≡ x and y ∈ FV (N) and
z is ’fresh’
{N/x}(L M) ≡ ({N/x}L {N/x}M)
Lemma 12.10 If L and N are pure λ-terms and x is a variable symbol then
{N/x}L is a pure λ-term.
Proof: By induction on the structure of the λ-term L. QED
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 519 of 721 Quit
Notes on Substitution
• 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∈ FV (N) ∪ V ar(L).
• Λ is closed under the syntactic operation of substitution.
• Substitution is the only operation required for function application in the
pure λ-calculus.
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 520 of 721 Quit
Compatibility
Definition 12.11 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).
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 521 of 721 Quit
Compatible Closure
Definition 12.12 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 ]
ρAppL
L ρc M
(L N) ρc (M N)
ρAppR
L ρc M
(N L) ρc (N M)
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 522 of 721 Quit
Compatible Closure: Properties
Lemma 12.13
1. ρc ⊇ ρ.
2. The compatible closure of any relation is compatible.
3. If ρ is compatible then ρc = ρ.
Example 12.14
1.≡α is a compatible relation
2.→1β is by definition a compatible relation.
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 523 of 721 Quit
α-equivalence
Definition XXXXXXXXXXα-equivalence) ≡α⊆ Λ × Λ is the compatible closure
of the relation {(λx[L] ≡α λy[{y/x}L]) | y 6∈ FV (L)}.
• α-equivalence implies that the the name(s) of the bound (called “indepen-
dent” in normal mathematics) variable(s) in a function definition is unim-
portanta. Hence λx[x2] ≡α λy[y2]b.
• As long as distinct bound variable names do not clash within the same or
nested scopes (where they need to be kept visible)c one can substitute the
other.
• Condition y 6∈ FV (L) is necessary to ensure that a “free” y is not captured
by the new “bound” variable y.
ait corresponds exactly to uniformly replacing a variable name in a local context in a program by another variable name throughout the block
provided there is no clash of variable names.
bSee also ??
cWhenever they need to be ’collapsed’ e.g. when we need the value of f(x, x) as an instance of a function f(x, y), we need to explicitly apply f
to the argument pair (x, x).
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 524 of 721 Quit
Function application: Basic β-Reduction
Definition 12.16
• 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}L′) | L′ ≡α L,L′, L,M ∈ Λ}
• It is usually represented by the axiom
(λx[L] M)→β {M/x}L′ (6)
where L′ ≡α L.
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 525 of 721 Quit
Function application: 1-step β-Reduction
Definition 12.17 A 1-step β-reduction→1β is the smallest relation (under the
⊆ ordering) on Λ such that
β1
L→β M
L→1β M
β1Abs
L→1β M
λx[L]→1β λx[M ]
β1AppL
L→1β M
(L N)→1β (M N)
β1AppR
L→1β M
(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.
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 526 of 721 Quit
Untyped λ-Calculus: β-Reduction
Definition 12.18
• For all integers n ≥ 0, n-step β-reduction →nβ is defined by induction on
1-step β-reduction
βnBasis L→0β L
βnInduction
L→mβ M →1β N
L→m+1β N
(m ≥ 0)
• β-reduction →∗β is the reflexive-transitive closure of 1-step β-reduction.
That is,
β∗
L→nβ M
L→∗β M
(n ≥ 0)
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 527 of 721 Quit
Computations and Normal Forms
• Loosely speaking, by a normal form we mean a term that cannot be “sim-
plified” further. In some sense it is like a “final answer”.
• We use β-reduction as the only way to “compute” final answers by simpli-
fication.
• There may be more than one β-redex in a term – this may lead to different
ways of computing the final answer.
Main Question: Do all the different ways of computing yield the same
answer?
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 528 of 721 Quit
Function Calls
Let
f = λx[x2 + 1]
g = λy[3.y + 2]
Consider two different evaluations of the function call (f (g 4))
Call-by-value Call-by-name/text
(f (g 4)) (f (g 4))
= (f XXXXXXXXXX)) = (g XXXXXXXXXX
= (f 14) = XXXXXXXXXX
= XXXXXXXXXX
= XXXXXXXXXX = XXXXXXXXXX
= XXXXXXXXXX = XXXXXXXXXX
= 197 = 197
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 529 of 721 Quit
Function Composition
• Let F ≡ x2 + 1 be an expression involving one independent variable x and
let f
df
= λx[F ]
• Let G ≡ 3.y + 2 be an expression involving one independent variable y and
let g
df
= λy[G].
• Let h = λf [λg[λz[(f (g z))]. Then h is the composition of f and g i.e.
h = (f o g).
The function call (f (g a)) for some value a is (h a) which is exactly ((f o g) a).
Hence There are at least two different ways of evaluating the composition of
functions.
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 530 of 721 Quit
Evaluating Function Composition
Call-by-value.
1. First evaluate (g a) = {a/y}G yielding a value b.
2. Then evaluate (f b) = {b/x}F yielding a value c.
Call-by-text.
1. First evaluate (f (g y)) = {(g y)/x}F = {G/x}F yielding expres-
sion H which contains only y as independent variable. This expres-
sion represents a function h
df
= λy[H ].
2. Evaluate (h a) = {a/y}H yielding a value d.
Main Question: Is c = d always?
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 531 of 721 Quit
Untyped λ-Calculus: Normalization
Definition 12.19
• 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 se-
quence L→1β L1→1β L2→1β · · ·
Intuitively speaking a β-normal form is one that cannot be “reduced” further.
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 532 of 721 Quit
Some Combinators
Example 12.20
1. K
df
= λx y[x] a projection function.
2. I
df
= λx[x], the identity function.
3. S
df
= λx y z[((x z) (y z))], a generalized composition function
4. ω
df
= λx[(x x)]
are all β-nfs.
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 533 of 721 Quit
Examples of Strong Normalization
Example 12.21
1. ((K ω) ω) is strongly normalising because it reduces to the normal form
ω in two β-reduction steps.
((K ω) ω)→1β (λy[ω] ω)→1β ω
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
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 534 of 721 Quit
Unnormalized Terms
Example 12.22
1. Ω
df
= (ω ω) has no β-nf. Hence it is neither weakly nor strongly normalising.
2. (K (ω ω)) cannot reduce to any normal form because it has no finite re-
duction 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β · · ·
3. ((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
((K ω) Ω)→1β ((K ω) Ω)→1β · · ·
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 535 of 721 Quit
Parameter Passing Mechanisms
• Call-by-name/text defines a Leftmost-outermost-computation, i.e. the
leftmost-outermost β-redex is chosen for application.
• Call-by-value defines a Leftmost-innermost-computation, i.e. the
leftmost-innermost β-redex is chosen for application.
To study these computation rules with regard to computing β-normal forms
we consider the following examples.
Example 12.23 Let
• L→lβ P 6→β yield a normal form P in l steps of β-reduction,
•M →mβ Q 6→β yield a normal form Q in m steps and
•N →nβ Q 6→β yield a normal form R in n steps
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 536 of 721 Quit
Deterministic Computation Mechanisms: K
Example 12.24 For the term ((K L) Ω) we have the following reduction
sequences.
Call-by-name/text. Choose the leftmost-outermost β-redex
• ((K L) Ω)→1β (λy[L] Ω)→1β L→lβ P
Call-by-value. Choose the leftmost-innermost β-redex
• ((K L) Ω)→lβ ((K P ) Ω)→1β (λy[P ] Ω)→∗β (λy[P ] Ω)→∗β · · ·
Here Call-by-value fails to produce the normal form even when it exists.
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 537 of 721 Quit
Deterministic Computation Mechanisms: S
Example 12.25 For the term (((S L) M) N) we have the following reduc-
tion sequences if ((P R) (Q R)) is in β normal form.
Call-by-name/text. Choose the leftmost-outermost β-redex
• (((S L) M) N) →3β ((L N) (M N)) →lβ ((P N) (M N)) →nβ
((P R) (M N))→mβ ((P R) (Q N))→nβ ((P R) (Q R))
Call-by-value. Choose the leftmost-innermost β-redex
• (((S L) M) N) →lβ (((S P ) M) N) →mβ (((S P ) Q) N) →nβ
(((S P ) Q) R)→3β ((P R) (Q R))
Call-by-value takes fewer steps to reduce to the normal form because there is
no duplication of the argument N .
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 538 of 721 Quit
Contrariwise
Example 12.26 However consider the term ((K L) M).
Call-by-name/text. In l + 2 steps we get the normal form.
• ((K L) M)→1β (λy[L] M)→1β L→lβ P
Call-by-value. We get the normal form in l + m + 2 steps.
• ((K L) M) →lβ ((K P ) M) →mβ ((K P ) Q) →1β (λy[P ] Q) →1β
L→lβ P
Here Call-by-value takes an extra m steps reducing an argument M that
has no influence on the computation!
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 539 of 721 Quit
Some Morals, Some Practice
In general,
• Call-by-value (example XXXXXXXXXXmay fail to terminate even if there is a possi-
bility of termination.
• If Call-by-value terminates, then Call-by-name will also terminate. More
precisely, if a normal form exists then Call-by-name will definitely find it.
• However, Call-by-value when it does terminate may terminate faster (ex-
ample XXXXXXXXXXthan Call-by-name/text provided all arguments need to be
evaluated in both cases.
• It is also easier to implement Call-by-value rather than Call-by-name under
static scope rules in the presence of non-local references.
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 540 of 721 Quit
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 541 of 721 Quit
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 542 of 721 Quit
13. Notions of Reduction
Notions of Reduction
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 543 of 721 Quit
Reduction
For any function such as p = λx[3.x.x + 4.x + 1],
(p 2) = XXXXXXXXXX = 21
However there is something asymmetric about the identity,
• While (p 2) deterministically produces XXXXXXXXXXwhich in turn
• simplifies deterministically to 21,
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 544 of 721 Quit
Reduction Induced Equality
• It is not possible to deterministically infer 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 XXXXXXXXXX21
• And yet they are behaviourally equivalent and mutually substitutable in all
contexts (referentially transparent).
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 545 of 721 Quit
Reduction Vs. Equality
1. Reduction (specifically β-reduction) captures this asymmetry.
2. Since reduction produces behaviourally equal terms we have the following
notion of equality.
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 546 of 721 Quit
Untyped λ-Calculus: β-Equality
Definition 13.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.
=β Basis
L→∗β M
L =β M
=β Reflexivity L =β L
=β Symmetry
L =β M
M =β L
=β Transitivity
L =β M, M =β N
L =β N
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 547 of 721 Quit
The Paradoxical Combinator
Example 13.2 Consider Curry’s paradoxical combinator
YC
df
= λf [(C C)] where C
df
= λ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))] λx[(L (x x))])︸ ︷︷ ︸)
=β (L
︷ ︸︸ ︷
(YC L))
Hence (YC L) =β (L (YC L)). However (L (YC L)) will never β-reduce
to (YC L).
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 548 of 721 Quit
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 particular
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 549 of 721 Quit
interest is Turing’s combinator YT
df
= (T T) where T
df
= λ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)
≡ ((T T) 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)))) =β · · ·
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 550 of 721 Quit
Compatibility of Beta-reduction and Beta-Equality
Theorem 13.3 β-reduction→∗β and β-equality =β are both compatible rela-
tions.

Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 551 of 721 Quit
Proof of theorem 13.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β Lm →1β M . Then by the induction hypothesis and the compatibility 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β (Lm N), (Lm N)→1β (M N) (L N)→nβ (M N)
for all N ∈ Λ, (N L)→mβ (N Lm), (N Lm)→1β (N M) (N L)→nβ (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).
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 552 of 721 Quit
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 =β Transitivity 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
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 553 of 721 Quit
Eta reduction
Given any term M and a variable x 6∈ FV (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 equivalent
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∈ FV (L) (7)
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 554 of 721 Quit
Eta-Reduction and Eta-Equality
The following notions are then defined similar to the corresponding notions for
β-reduction.
• 1-step η-reduction →1η is the closure of basic η-reduction to all contexts,
•→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 =η.
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 555 of 721 Quit
Exercise 13.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 (6) or (7). Now prove that →1βη, →∗βη and =βη are all compatible relations.
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 556 of 721 Quit
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 557 of 721 Quit
14. Confluence Definitions
Confluence: Definitions
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 558 of 721 Quit
Reduction Relations
Definition 14.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 closure
of ρ1 and is an equivalence relation.
5. =ρ is also called the equivalence generated by ρ.
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 559 of 721 Quit
Reduction Relations: Arrow Notation
We will often use −→ (suitably decorated) as a reduction relation instead of
ρ. Then
•−→1 denotes the compatible closure of −→,
•−→+ denotes the transitive closure of −→,
•−→∗ denotes the reflexive-transitive closure of −→, and
• ∗←→ denotes the equivalence generated by −→,
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 560 of 721 Quit
The Diamond Property
Definition 14.2 Let ρ be any relation on terms. ρ has the diamond prop-
erty if for all L, M , N ,
M
ρ
L
ρ
N
⇒ ∃P :
M
ρ
P
ρ
N
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 561 of 721 Quit
The Diamond Property: Arrow Notation
We often use a decorated version of the symbol −→ for a reduction relation
and depict the diamond property as
M
−→
−→
L ⇒ ∃ P−→ −→
N
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 562 of 721 Quit
Reduction Relations: Termination
Let −→ be a reduction relation, −→∗ the least preorder containing −→ and
∗←→ the least equivalence relation containing −→∗. Then
Definition 14.3−→ is terminating iff there is no infinite sequence of the
form
L0 −→ L1 −→ · · ·
Lemma 14.4−→η is a terminating reduction relation.
Proof: By induction on the structure of terms. QED
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 563 of 721 Quit
14.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 termination
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.
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 564 of 721 Quit
Reduction: Local Confluence
Definition 14.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
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 565 of 721 Quit
Reduction: Semi-confluence
Definition 14.6−→ is semi-confluent if for all L, M , N ,
N ←− L −→∗ M ⇒ ∃P : N −→∗ P ∗←−M
which we denote by
M
−→
−→ ∗
L ⇒ ∃ P
−→ ∗ −→

N
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 566 of 721 Quit
Reduction: Confluence
Definition 14.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 14.8 Any confluent relation is also semi-confluent.

Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 567 of 721 Quit
Reduction: Church-Rosser
Definition 14.9−→ is Church-Rosser if for all L, M ,
L
∗←→M ⇒ ∃P : L −→∗ P ∗←−M
which we denote by
L
∗←→ M
−→ ∗ ⇓
−→

∃P
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 568 of 721 Quit
Equivalence Characterization
Lemma 14.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 (8)
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 569 of 721 Quit
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 570 of 721 Quit
Proof of lemma 14.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” (8) implies L
∗←→M by transitivity. For the other part use induction
on the length of the proof.
QED
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 571 of 721 Quit
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 572 of 721 Quit
14.2. Confluence: Church-Rosser
The Church-Rosser Property
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 573 of 721 Quit
Confluence and Church-Rosser
Lemma 14.11 Every confluent relation is also semi-confluent

Theorem 14.12 The following statements are equivalent for any reduction
relation −→.
1.−→ is Church-Rosser.
2.−→ is confluent.
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 574 of 721 Quit
Proof of theorem 14.12
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 (8).
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
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 575 of 721 Quit
which proves the claim.
Case Mk ←−M . Then the claim follows from the induction hypothesis and the following diagram
L
∗←→ Mk ←−M−→ ∗ ⇓
−→

∃P
QED
Lemma 14.13 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.13 and theorem 14.12 we have the following theorem.
Theorem 14.14 If a terminating relation is locally confluent then it is confluent.
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 576 of 721 Quit
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 (9)
We need to show for any term L that
N ∗←− L −→∗ M ⇒ ∃S : M −→∗ S ∗←− N (10)
Let L be any term. Consider the graph G(L) = 〈Γ(L),−→1〉 such that Γ(L) = {M | L −→∗ M}. Since −→ is a terminating reduction
Fact 14.15 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.16 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.
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 577 of 721 Quit
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 (10) 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 .
Case m,n > 0. Then consider M1 and N1 such that
N ∗←− N1 1←− L −→1 M1 −→∗ M (11)
See figure (7). By (9), ∃P : M1 −→∗ P ∗←− N1. Clearly M1, N1, P ∈ Γ(L) − {L}. Hence by fact 14.16, 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 (12)
and
N ∗←− N1 −→∗ P ⇒ ∃R : P −→∗ R ∗←− N (13)
But by (12) and (13) and the induction hypothesis we have
R ∗←− P −→∗ Q⇒ ∃S : Q −→∗ S ∗←− R (14)
Combining (14) with (11), (12) and (13) we get
N ∗←− L −→∗ M ⇒ ∃S : M −→∗ S ∗←− N (15)
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 578 of 721 Quit
QED
Theorem 14.17 If a terminating relation is locally confluent then it is Church-Rosser.
Proof: Follows from theorem 14.14 and theorem 14.12 QED
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 579 of 721 Quit
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 580 of 721 Quit
14.3. The Church-Rosser Property
The Church-Rosser Property
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 581 of 721 Quit
Parallel Beta Reduction
Definition 14.18 The parallel-β or ||β reduction is the smallest relation for
which the following rules hold.
||β1 L −→1||β L
||β1Abs1
L −→1||β L

λx[L] −→1||β λx[L
′]
||β1App
L −→1||β L
′,M −→1||β M

(L M) −→1||β (L
′ M ′)
||β1Abs2
L −→1||β L
′,M −→1||β M

(λx[L] M) −→1||β {M
′/x}L′
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 582 of 721 Quit
Parallel Beta: The Diamond Property
Lemma 14.19
1. L −→1β L′⇒ L −→1||β L
′.
2. L −→1||β L
′⇒ L −→∗β L′.
3. The smallest preorder containing −→1||β is −→

||β=−→

β.
4. If L −→1β L′ and M −→1||β M
′ then {M/x}L −→1||β {M
′/x}L′.
Proof: By induction on the structure of terms or by induction on the number of steps in any proof. QED
Theorem 14.20−→1||β has the diamond property.
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 583 of 721 Quit
Proof of theorem 14.20
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 14.18.
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
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 584 of 721 Quit
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 14.19 we have
∃P ≡ {P2/x}P1 : N −→1||β P 1||β←−M
completing the proof.
Sub-case: M was obtained by applying rule ||β1Abs2 and N by ||β1App.
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 585 of 721 Quit
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 14.21 −→1||β is confluent.
Proof: We need to show that for all L, M , N ,
N ∗||β←− L −→∗||β M ⇒ ∃P : N −→∗||β P ∗||β←−M
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 586 of 721 Quit
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′, i′) if and only if
either j < j′ or (j = j′) and i < i′. The interesting cases are those where both m,n > 0. So we repeatedly apply theorem 14.20 to complete the
rectangle
L −→1||β M1 −→1||β M2 −→1||β · · · −→1||β Mm ≡M
||β↓1 ||β↓1 ||β↓1 · · · ||β↓1
N1 −→1||β P11 −→1||β P12 −→1||β · · · −→1||β P1m
||β↓1 ||β↓1 ||β↓1 · · · ||β↓1
...
...
... · · ·
...
||β↓1 ||β↓1 ||β↓1 · · · ||β↓1
Nn −→1||β Pn1 −→1||β Pn2 −→1||β · · · −→1||β Pnm ≡ P
QED �
Corollary 14.22 −→1β is confluent.
Proof: Since −→∗β=−→∗||β it follows from theorem 14.21 that −→1β is confluent. QED
Corollary 14.23 If a term reduces to a β-normal form then the normal form is unique (upto ≡α).
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 587 of 721 Quit
Proof: If N1 ∗β←− L −→∗β N2 and both N1 N2 are β-nfs, then by the corollary 14.22 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 14.24 −→1β is Church-Rosser.
Proof: Follows from corollary 14.22 and theorem XXXXXXXXXXQED
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 588 of 721 Quit
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 589 of 721 Quit
15. An Applied Lambda-Calculus
15.1. FL with recursion
An Applied Lambda-Calculus With Types
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 590 of 721 Quit
15.2. Motivation and Organization
In the sequel we will define, by stages a simple higher order programming language.
Stage 0. A simple expression language to represent integers and booleans. Initially we define a representation for integers
and booleans purely symbolically (16) as a data type with constant constructors.
Stage 1. FL(X) – a simple expression (functional programming) language with variables that allows expressions to be
defined on the two types of data – integers and booleans.
Static Semantics. By allowing more than one type of data we also show that there is a need for a type-checking
discipline since several meaningless constructs may be generated by the grammar. We specify the type-checking
(type-inferencing) system for this simple language as the static semantics of the language.
Functional Semantics. For the well-typed terms we also define the intended meanings of these expressions, by defining
a functional semantics.
Operational (Reduction) Semantics. We show that we can capture the intended meanings of well-typed expressions
by a dynamic semantics which specifies symbolically a notion of reduction (δ-rules (38) to (47)).
Relating Functional and Operational Semantics. The integer values and boolean values are denoted symbolically
by δ-normal forms. Lemma 15.2 shows that the intended values of integers and boolean values are obtained as
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 591 of 721 Quit
δ-normal forms and in combination with the property of confluence (see exercise 15.1 problem 3) it follows that all
integer and boolean values have unique normal form representations in the expression language.
Subject Reduction. In problem XXXXXXXXXXwe encourage the reader to show that types are preserved under δ-reductions
(5) i.e. the type of an expression cannot change arbitrarily during reduction (program execution) – an important
static property that a dynamic semantics should obey.
Referential Transparency. Further, in problem (6) the reader is encouraged to show that the language enjoys the
property of referential transparency viz. that each variable name in an expression may be substituted by its value
while preserving the meaning of the expression – a dynamic property that any functional programming language
should obey.
Stage 2. Λ+FL(X). However, the language FL(X) lacks the elementary facilities for user-defined functions. Add to that
the lack of expressiveness to define even the most common useful integer operations such as addition, subtraction and
multiplication. We rectify this by defining λ-abstraction and application to terms of the language. The new extended
language Λ+FL(X) allows us to define some (non-recursive) operators and functions over the terms of the language
FL(X). BY this addition, β-reduction has been added to the language as well. The language Λ+FL(X) is expressive
enough to define some of the common boolean operators and the most common order relations on integers. These have
been made possible due to the inclusion of the ternary if-the-else construct(or) ITE and construct(or)s for checking for
0 (IZ) and positive integer values (GTZ).
Stage 3. The addition of the λ-abstraction and application on top of FL(X) has the drawback that functions and function
applications do not have the same status as expressions. To bring function definition and application down to the
expression level it is necessary to allow an intermingling of the two. Hence we “flatten” the language to produce a
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 592 of 721 Quit
genuinely applied λ-calculus with a βδ reduction mechanism. The result is the language ΛFL(X).
Stage 4. ΛFL(X) allows the full power of the λ-calculus to be incorporated into the language. Hence it allows higher-
order functions as well. However, the power of recursion is not achieved in a type-safe manner because no paradoxical
combinator can be made type-safe. Hence even to program some elementary inductive functions like addition, a recursion
operator is absolutely required. This yields the language ΛRecFL(X).
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 593 of 721 Quit
A Simple Language of Terms: FL0
Let X be an infinite collection of variables (names). Consider the language
(actually a collection of abstract syntax trees) of terms TΩ(X) defined by the
following constructors (along with their intended meanings). TΩ denotes the
variable-free subset of TΩ(X) and is called the set of ground terms.
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
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 594 of 721 Quit
FL(X): Language, Datatype or Instruction Set?
The set of terms TΩ(X), where X is an infinite collection of variable names
(that are disjoint from all other symbols in the language) may be defined by
the BNF:
t ::= x ∈ X Z (P t) (S t) T F (ITE 〈t, t1, t0〉) (IZ t) (GTZ t)
(16)
• 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 〈t, t1, t0〉) is overloaded.
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 595 of 721 Quit
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 integers
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
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 596 of 721 Quit
Typing FL0 Expressions
We have only two types of objects in FL0 – integers and booleans which we
represent by int and bool respectively. We then have the following elemen-
tary typing annotations for the expressions, which may be obtained by pattern
matching.
Basis. Z : int, T : bool, F : bool
Int. S : int→ int, P : int→ int
Bool. IZ : int→ bool, GTZ : int→ bool
boolCond. ITEB : bool ∗ bool ∗ bool→ bool
intCond. ITEI : bool ∗ int ∗ int→ int
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 597 of 721 Quit
15.3. Static Semantics of FL(X)
While dynamic semantics refers to the run-time behaviour of a program, the static semantics refers to all the context-
sensitive information about a program that needs to be gathered during the compilation process, to enable the generation of
code both for execution as well as error-reporting and handling. Most of this information about variable symbols is stored in
the symbol table and is accessed during the code-generation process for both memory allocation and the actual generation
of target code.
The purposes of both code-generation and memory allocation aspects are more or less (i.e. except for scope and the absolute
addresses of the data-objects during execution) covered by determining the types of the various objects in a program (data
objects, functions, procedures etc.). The type of a scalar data item implicitly defines the amount of storage it requires. For
example, an integer variable needs perhaps one word of storage and a floating point variable requires two-words of storage,
booleans require just a bit (but in the case of byte-addressable or word-addressable machines machines it may be more
efficient to assign a byte or word of storage to it). Similarly characters may require a byte of storage and strings require
storage that is proportional to their length. All complex data items such as records and arrays being built of the scalar
components require correspondingly proportional amounts of storage in the run-time stack. For each of these the compiler
creates a so-called data descriptor and stores it in the symbol table and refers to it while generating code. The control
units viz. expressions, commands, functions and procedures would require storage (in the code-segment) proportional to
the length of the code that is generated for each of them; and the parameters they invoke correspondingly require data
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 598 of 721 Quit
descriptors to define the storage requirements for the parameters. Further in the process of compiling a procedure or a
function the types of input and output parameters in the definition (declaration) should correspond exactly with the types
of the actual parameters in each call (otherwise a compile-time error needs to be generated).
Much of the above process can all be captured by the simple process of assigning types to each data and control unit in a
program. Hence most compilers (with static scoping rules) actually perform static or compile-time type-checking.
XXXXXXXXXXType-checking FL(X) terms
While trying to type FL0 expressions we have had to introduce two new type operators viz. ∗ and → which allow us to
precisely capture the types of expressions involving constructors such as S, P, IZ, GTZ, ITE etc. which we intend to view as
functions of appropriate arity on appropriate types of arguments. These type operators will be required for specifying the
types of other (user-defined) functions as well. Hence it makes sense for us to define a formal language of type expressions
(with type variables!) to enable us define types of polymorphic operations (which in the particular case of FL(X) is restricted
to overloading the ITE constructor). As we shall see later, this expression language of types may be defined by the grammar
σ, τ ::= int bool ′a ∈ TV (σ∗τ) (σ→τ)
where ′a ∈ TV is a type variable and all type variables are distinct from program variables in X.
What we have specified earlier are the typing axioms for the constant expressions (without variables). For the purpose of
typing expressions involving (free) variables we require assumptions to be made about the types of the variables occurring
in an expression. In most programming languages these assumptions come from the declarations of variables. For instance,
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 599 of 721 Quit
the successor constructor S should be applied only to expressions which yield integer values. Hence for any expression
t ∈ FL(X), (S t) would be well-typed only if t : int and further (S t) : int. Similarly, given three expressions t, t1, t0, the
expression (ITE 〈t, t1, t0〉) type-checks i.e. it is well-typed only if t : bool and the types of t1 and t0 are the same – either
both bool or both int.
XXXXXXXXXXThe Typing Rules
In a language of expressions that requires the type of each variable to be declared beforehand, the list of (free) variables
and their types may be available as a type environment Γ and the rules that we give are type-checking rules. The rules for
type-checking any expression t ∈ TΩ(X) extend the earlier specification by induction on the structure of expressions. More
precisely, the earlier specification form the basis of an induction by structure of expressions. The 0-ary constructors and
variables form the basis for the structural induction rules and have the following axioms and their types are independent
of the type environment. The type-checking rules for expressions form the induction step of the type-checking algorithm
and go as follows. These rules also assign types to each individual sub-expression along the way. We begin with the unary
constructors and conclude with the conditional operator.
Alternatively, in the absence of declarations, we could derive them as constraints on the types of variables (as we shall see
later). It is then necessary to also use the concept of type variables as distinct from program variables.
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 600 of 721 Quit
Static Semantics of FL(X): Type-checking
Ft
Γ ` F : bool Tt Γ ` T : bool Zt Γ ` Z : int Var Γ ` x : Γ(x)
St
Γ ` t : int
Γ ` (S t) : int Pt
Γ ` t : int
Γ ` (P t) : int
IZt
Γ ` t : int
Γ ` (IZ t) : bool GTZt
Γ ` t : int
Γ ` (GTZ t) : bool
ITEIt
Γ ` t : bool
Γ ` t1 : int
Γ ` t0 : int
Γ ` (ITE 〈t, t1, t0〉) : int
ITEBt
Γ ` t : bool
Γ ` t1 : bool
Γ ` t0 : bool
Γ ` (ITE 〈t, t1, t0〉) : bool
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 601 of 721 Quit
Well-typed Terms
Definition 15.1 A term t ∈ TΩ(X) in a type environment Γ is well-typed if
there exists a proof of either Γ ` t : int or Γ ` t : bool (not both).
As we have seen before there are terms that are not well-typed. We consider
only the subset WTΩ(X) ⊂ TΩ(X) while describing the dynamic semantics.
While TΩ is variable-free subset of TΩ(X), WTΩ ⊂ WTΩ(X) is the variable-
free subset of WTΩ(X).
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 602 of 721 Quit
Dynamic Semantics of FL(X)
The dynamic semantics or the run-time behaviour of FL(X) expressions may
be specified in several ways.
Functional semantics. The language designer could specify the intended
meanings of the constants, constructors and operators in terms that are
useful to the user programmer as functions (as an extension of the informal
meaning specified earlier), or
Operational semantics The implementor of the language could specify the
run-time behaviour of expressions through an abstract algorithm.
But any implementation should also be consistent with the intended meanings
specified by the designer
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 603 of 721 Quit
Functional Semantics of FL(X):0
Boolean constants . The constructors T and F are interpreted as the
boolean constants true and false respectively.
Zero . Z is interpreted as the constant number 0
Positive integers . Each k-fold application, k > 0, of the constructor S to Z
viz. (S XXXXXXXXXXS Z) . . .)︸ ︷︷ ︸
k−fold
(abbreviated to (Sk Z) for convenience) is interpreted
as the positive integer k.
Negative integers . Similarly, each k-fold application, k > 0, of the con-
structor P to Z viz. (P XXXXXXXXXXP Z) . . .)︸ ︷︷ ︸
k−fold
(abbreviated as (Pk Z)) is interpreted
as −k.
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 604 of 721 Quit
Functional Semantics of FL(X):1
• Let Z and B denote the sets of integers and booleans respectively.
• Each well-typed expression in TΩ(X) denotes either an integer or boolean
value depending upon its type.
• Let V = {v | v : X → (Z∪B)} denote the set of all valuation environments
which associate with each variable a value of the appropriate type (either
integer or boolean).
• With the interpretation of the symbols in the language given earlier we
associate a meaning function
M : WTΩ(X)→ (V → (Z ∪ B))
such that for each well-typed expression t ∈ WTΩ(X), M [t] is a function
that extends each v ∈ V , inductively on the structure of expressions to a
value of the appropriate type.
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 605 of 721 Quit
Functional Semantics of FL(X):2
M [x] v
df
= v(x) (17)
M [T] v
df
= true (18)
M [F] v
df
= false (19)
M [Z] v
df
= 0 (20)
M [(P t)] v
df
= M [t] v − 1 (21)
M [(S t)] v
df
= M [t] v XXXXXXXXXX)
M [(IZ t)] v
df
= M [t] v = 0 (23)
M [(GTZ t)] v
df
= M [t] v > 0 (24)
M [(ITE 〈t, t1, t0〉)] v
df
=
{
M [t1] v if M [t] v
M [t0] v if not M [t] v
(25)
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 606 of 721 Quit
15.4. Equational Reasoning in FL(X)
From the semantics of FL(X) the following identitities are easily derived. We leave the proofs of these identities to the reader. It is also important that some of these identities
are used (oriented from left to right) in the definition of the δ-rules as rules of reduction (or “simplification”) in order to obtain normal forms. In such cases the equality is
made asymmetric (left to right).
Identities used for simplification
(P (S x)) = x (26)
(S (P x)) = x (27)
(ITE 〈T, x, y〉) = x (28)
(ITE 〈F, x, y〉) = y (29)
(IZ Z) = T (30)
(GTZ Z) = F (31)
Identities involving normal forms
(IZ (S n)) = F, where (S n) is a δ-nf (32)
(IZ (P n)) = F, where (P n) is a δ-nf (33)
(GTZ (S n)) = T, where (S n) is a δ-nf (34)
(GTZ (P n)) = F, where (P n) is a δ-nf (35)
Besides the above identities which are actually used in an oriented form for the purpose of computation we may also prove other identities from the functional semantics.
Many of these look like they could be included in the rules for computation, but we may not be because of
• the limits of computability in general and
• their inclusion might at times lead to non-determinism and
• in more extreme cases lead to non-termination even though there are deterministic ways to obtain δ-normal forms.
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 607 of 721 Quit
However they are useful for reasoning about programs written in the language. For example the following obvious identity
(ITE 〈b, x, x〉) = x, where b is a boolean (36)
(37)
is useful for simplifying a program for human reasoning. However, when included as a δ-rule, it greatly complicates the computation when equality of the two arms of the
conditional need to be checked (when they are not merely variables but complicated expressions themselves). There is a further complication of defining under what conditions
this equality checking needs to be performed.
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 608 of 721 Quit
Reduction Semantics
Just as the dynamic behaviour of a λ-term may be described by β-reduction, we
may describe the dynamic behaviour of a FL(X) expression through a notion of
reduction called δ−reduction. It is important that such a notion of reduction
produces results (values) that are consistent with the functional semantics.
We first begin with the δ-normal forms for integers and booleans.
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 609 of 721 Quit
The Normal forms for Integers
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 application
of P.
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 610 of 721 Quit
δ rules for Integers
(P (S x)) −→δ x (38)
(S (P x)) −→δ x (39)
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 611 of 721 Quit
δ Normal Forms for Integers
Lemma 15.2 The following well-typed (in any type environment Γ) terms are
exactly the δ-normal forms in WTΩ along with their respective meanings in
the functional semantics (in any dynamic environment v ∈ V).
1. Γ ` Z : int and M [Z] v = 0
2. Γ ` T : bool and M [T] v = true
3. Γ ` F : bool and M [F] v = false
4. For each positive integer k, Γ ` (Sk Z) : int and M [(Sk Z)] v = k
5. For each positive integer k, Γ ` (Pk Z) : int and M [(Pk Z)] v = −k
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 612 of 721 Quit
δ Rules for Conditional
Pure Boolean Reductions . The constructs T and F are the normal forms
for boolean values.
(ITE 〈T, x, y〉) −→δ x (40)
(ITE 〈F, x, y〉) −→δ y (41)
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 613 of 721 Quit
δ Rules: Zero Test
Testing for zero .
(IZ Z) −→δ T (42)
(IZ (S n)) −→δ F, where (S n) is a δ-nf (43)
(IZ (P n)) −→δ F, where (P n) is a δ-nf (44)
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 614 of 721 Quit
δ Rules: Positivity
(GTZ Z) −→δ F (45)
(GTZ (S n)) −→δ T, where (S n) is a δ-nf (46)
(GTZ (P n)) −→δ F, where (P n) is a δ-nf (47)
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 615 of 721 Quit
Exercise 15.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. The language FL(X) extends FL0 with variables. What are the new δ-normal forms in FL(X)?
5. Subject reduction. Prove that for any well-typed term t ∈WTΩ(X), and α ∈ {int, bool} if Γ ` t : α and t −→δ t′ then Γ ` t′ : α.
6. Referential Transparency. Let t ∈ WTΩ(X), FV (t) = {x1, . . . , xn} and let v be a valuation environment. If {t1, . . . , tn} are ground terms such that for each i,
1 ≤ i ≤ n, M [xi] v = M [ti] v then prove that
(a) M [t] v = M [{t1/x1, . . . , tn/xn}t] v and
(b) {t1/x1, . . . , tn/xn}t −→∗δ u where M [u] v = M [t] v
where {t1/x1, . . . , tn/xn}t denotes the simultaneous syntactic substitution of every occurrence of variable xi by the ground term ti for 1 ≤ i ≤ n.
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 616 of 721 Quit
Λ+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) (48)
This is a two-level grammar combining the term grammar (16) with λ-
abstraction and λ-application.
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 617 of 721 Quit
Some Non-recursive Operators
We may “program” the other boolean operations as follows:
NOT
df
= λx[ITE 〈x, F, T〉]
AND
df
= λ〈x, y〉[ITE 〈x, y, F〉]
OR
df
= λ〈x, y〉[ITE 〈x, T, y〉]
We may also “program” the other integer comparison operations as follows:
GEZ
df
= λx[OR 〈(IZ x), (GTZ x)〉]
LTZ
df
= λx[NOT (GEZ x)]
LEZ
df
= λx[OR 〈(IZ x), (LTZ x)〉]
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 618 of 721 Quit
Λ+FL(X): Lack of Higher-order Power?
Example 15.3 The grammar (48) 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 〈(L x), (M x), (N x)〉)] 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).
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 619 of 721 Quit
ΛFL(X): Higher order functions
We need to flatten the grammar of (48) to allow λ-terms also to be used as
arguments of the constructors of the term-grammar (16). The language of
applied λ-terms (viz. ΛΩ(X)) now is defined by the grammar.
L,M,N ::= x ∈ X Z T F
(P L) (S L)
(IZ L) (GTZ L)
(ITE 〈L,M,N〉)
λx[L] (L M)
(49)
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 620 of 721 Quit
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 mentioned
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.
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 621 of 721 Quit
Exercise 15.2
1. Prove that the language of (48) is properly contained in the language of (49).
2. Give examples of meaningful terms generated by the grammar (49) which cannot be generated by the grammar (48).
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 622 of 721 Quit
Recursion in the Applied Lambda-calculus
The full power of a programming language will not be realised without a recur-
sion mechanism. The untyped lambda-calculus has “paradoxical combinators”
which behave like recursion operators upto =β.
Definition 15.4 A combinator Y is called a fixed-point combinator if
for every lambda term L, Y satisfies the fixed-point property
(Y L) =β (L (Y L)) (50)
Curry’s Y combinator (YC)
YC
df
= λf [(C C)] where C
df
= λx[(f (x x))]
Turing’s Y combinator (YT)
YT
df
= (T T) where T
df
= λy x[(x (y y x ))]
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 623 of 721 Quit
The Paradoxical Combinators
Lemma 15.5 Both YC and YT satisfy the fixed-point property.
Proof: For each term L we have
(Yc L)
≡ (λf [(C C)] L)
→1β ({L/f}C {L/f}C)
≡ (λx[(L (x x))] λx[(L (x x))])
=β (L (Yc L))
Similarly for YT it may be verified that it satisfies the fixed-point property.
(YT L)
≡ ((T T) L)
≡ ((λy[λx[(x ((y y) x))]] T) L)
→2β (L ((T T) L))
=β (L (YT L))
QED
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 624 of 721 Quit
ΛRecFL(X): Recursion
• 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.
• 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
50).
• REC is assigned the type ((τ→τ )→τ ) for each type τ .
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 625 of 721 Quit
ΛRecFL(X): Adding Recursion
We extend the language ΛFL(X) with a new constructor
L ::= XXXXXXXXXXREC L)
and add the fixed point property as a δ-rule
(REC L) −→δ (L (REC L)) (51)
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 626 of 721 Quit
Typing REC
With REC : ((τ→τ )→τ ) and L : τ→τ we have that
(REC L) : τ
(L (REC L)) : τ
which
• type-checks (without recourse to self-reference) as a constructor and
• is consistent with our intuition about recursion as a syntactic unfolding
operator.
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 627 of 721 Quit
Recursion Example: Addition
Consider addition on integers as a binary operation to be defined in this lan-
guage. We use the following properties of addition on the integers to define it
by induction on the first argument.
Example 15.6
x + y =
 y if x = 0(x− 1) + (y + 1) if x > 0
(x XXXXXXXXXXy − 1) if x < 0
(52)
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 628 of 721 Quit
Using the constructors of ΛRecFL(X) we require that any (curried) definition of addition on numbers should be a solution
to the following equation in ΛRecFL(X) for all (integer) expression values of x and y.
(plusc x y) =βδ ITE 〈(IZ x), y, ITE 〈(GTZ x), (plusc (P x) (S y)), (plusc (S x) (P y))〉〉 (53)
Equation (53) may be rewritten using abstraction as follows:
plusc =βδ λx[λy[ITE 〈(IZ x), y, ITE 〈(GTZ x), (plusc (P x) (S y)), (plusc (S x) (P y))〉〉]] (54)
We may think of equation (54) as an equation to be solved in the unknown variable plusc.
Consider the (applied) λ-term obtained from the right-hand-side of equation (54) by simply abstracting the unknown plusc.
addc
df
= λf [λx y[ITE 〈(IZ x), y, ITE 〈(GTZ x), (f (P x) (S y)), (f (S x) (P y))〉〉]] (55)
Claim 15.7
(REC addc) −→δ (addc (REC addc)) (56)
and hence
(REC addc) =βδ (addc (REC addc)) (57)
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 629 of 721 Quit
Claim 15.8 (REC addc) satisfies exactly the equation (54). That is
((REC addc) x y) =βδ ITE 〈(IZ x), y, ITE 〈(GTZ x), ((REC addc) (P x) (S y)), ((REC addc) (S x) (P y))〉〉 (58)
Hence we may regard (REC addc) where addc is defined by the right-hand-side of definition (55) as the required solution to
the equation (53) in which plusc is an unknown.
The abstraction shown in (55) and the claims XXXXXXXXXXand XXXXXXXXXXsimply 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.
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 630 of 721 Quit
Exercise 15.3
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. 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.
4. The equation (52) is defined conditionally. However the following is equally valid for all integer values x and y.
x+ y = (x− 1) + (y XXXXXXXXXX)
(a) Follow the steps used in the construction of addc to define a new applied addc′ that instead uses equation (59).
(b) Is (REC addc′) =βδ (addc
′ (REC addc′))?
(c) Is addc =βδ addc
′?
(d) Is (REC addc) =βδ (REC addc
′)?
(e) Computationally speaking (in terms of β and δ reductions), what is the difference between addc and addc′?
5. 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.
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 631 of 721 Quit
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 632 of 721 Quit
15.5. Type-checking FL
15.6. ΛRecFL(X) with type rules
Typing ΛRecFL(X) expressions
We have already seen that the simple language FL has
• two kinds of expressions: integer expressions and boolean expressions,
• there are also constructors which take integer expressions as arguments and
yield boolean values
• there are also function types which allow various kinds of functions to be
defined on boolean expressions and integer expressions.
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 633 of 721 Quit
The Need for typing in ΛRecFL(X)
• A type is an important attribute of any variable, constant or expression,
since every such object can only be used in certain kinds of expressions.
• Besides the need for type-checking rules on TΩ(X) to prevent illegal con-
structor operations,
– rules are necessary to ensure that λ-applications occur only between 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.
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 634 of 721 Quit
TL: A Language of Simple Types
Consider the following language of types (in fully parenthesized form) defined
over an infinite collection ′a ∈ TV of type variables, disjoint from the set of
variables. We also have two type constants int and bool.
σ, τ ::= int bool ′a ∈ TV (σ∗τ ) (σ→τ )
Notes.
• int and bool are type constants.
• In any type expression τ , TV 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).
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 635 of 721 Quit
TL: Precedence and Associativity
• Precedence. We assume ∗ has a higher precedence than →.
• Associativity.
– ∗ is left associative whereas
–→ is right associative
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 636 of 721 Quit
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 recursive
travel of the abstract syntax tree.
Free variables (names) are already present in the environment (symbol ta-
ble).
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.
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 637 of 721 Quit
Type Inferencing: Infrastructure
The elementary typing defined previously (§ XXXXXXXXXXfor the elementary expres-
sions 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 unnec-
essarily decorate expressions with their types.
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 638 of 721 Quit
Type Assignment: Infrastructure
• Assume Γ is the environmenta (an association list) which may be looked up
to determine the types of individual names. For each variable 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 expres-
sions in the language FL.
• The expressions of FL could have free variables. The type of the expression
would then depend on the types assigned to the free variables. This is a
simple kind of polymorphism.
• It may be necessary to generate new type variables as and when required
during the process of inferencing and assignment.
ausually a part of the symbol table
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 639 of 721 Quit
Constraint Typing Relation
Definition 15.9 For each term L ∈ ΛΣ(X) the constraint typing rela-
tion is of the form
Γ ` L : τ BT 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
ausually in the symbol table
bincluding new type variables
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 640 of 721 Quit
Typing axioms: Basic 1
The following axioms (c.f Typing FL Expressions) may be either predefined or
applied during the scanning and parsing phases of the compiler to assign types
to the individual tokens and thus create an initial type environment Γ0.
Z
Γ ` Z : int B∅ ∅
T
Γ ` T : bool B∅ ∅
F
Γ ` F : bool B∅ ∅
S
Γ ` S : int→int B∅ ∅
P
Γ ` P : int→int B∅ ∅
IZ
Γ ` IZ : int→bool B∅ ∅
GTZ
Γ ` GTZ : int→bool B∅ ∅
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 641 of 721 Quit
Typing axioms: Basic 2
ITEI
Γ ` ITE : bool∗int∗int→int B∅ ∅
ITEB
Γ ` ITE : bool∗bool∗bool→bool B∅ ∅
Notice that the constructor ITE is overloaded and actually is two constructors
ITEI and ITEB. Which constructor is actually used will depend on the context
and the type-inferencing mechanism.
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 642 of 721 Quit
Type Rules: Variables and Abstraction
Var
Γ ` x : Γ(x) B∅ ∅
Abs
Γ, x : σ ` L : τ BT C
Γ ` λx[L] : σ→τ BT C
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 643 of 721 Quit
Type Rules: Application
App
Γ ` L : σ BT1 C1
Γ `M : τ BT2 C2
Γ ` (L M) : ′a BT ′ C ′
(Conditions 1. and 2.)
where
• Condition 1. T1 ∩ T2 = T1 ∩ TV ar(τ ) = T2 ∩ TV ar(σ) = ∅
Condition 2. ′a 6∈ T1 ∪ T2 ∪ TV ar(σ) ∪ TV ar(τ ) ∪ TV ar(C1) ∪
TV ar(C2).
• T ′ = T1 ∪ T2 ∪ {′a}
•C ′ = C1 ∪ C2 ∪ {σ = τ→′a}
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 644 of 721 Quit
Example 15.10 Consider the following simple combinator λx[λy[λz[(x (y z))]]] which defines the function composition
operator. Since there are three bound variables x, y and z we begin with an initial assumption Γ = x : ′a, y : ′b, z : ′c which
assign arbitrary types to the bound variables, represented by the type variables ′a, ′b and ′c respectively. 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 : ′a, y : ′b, z : ′c ` x : ′a B∅ ∅ (Var)
2. x : ′a, y : ′b, z : ′c ` y : ′b B∅ ∅ (Var)
3. x : ′a, y : ′b, z : ′c ` z : ′c B∅ ∅ (Var)
4. x : ′a, y : ′b, z : ′c ` (y z) : ′d B{′d} {′b = ′c→′d} (App)
5. x : ′a, y : ′b, z : ′c ` (x (y z)) : ′e B{′d,′e} {′b = ′c→′d, ′a = ′d→′e} (App)
6. x : ′a, y : ′b ` λz[(x (y z))] : ′c→′e B{′d,′e} {′b = ′c→′d, ′a = ′d→′e} (Abs)
7. x : ′a ` λx[λy[λz[(x (y z))]]] : ′b→′c→′e B{′d,′e} {′b = ′c→′d, ′a = ′d→′e} (Abs)
8. ` λx[λy[λz[(x (y z))]]] : ′a→′b→′c→′e B{′d,′e} {′b = ′c→′d, ′a = ′d→′e} (Abs)
Hence λx[λy[λz[(x (y z))]]] : ′a→′b→′c→′e subject to the constraints given by {′b = ′c→′d, ′a = ′d→′e} which yields
λx[λy[λz[(x (y z))]]] : (′d→′e)→(′c→′d)→′c→′e
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 645 of 721 Quit
Principal Type Schemes
Definition 15.11 A solution for Γ ` L : τ BT C is a pair 〈S, σ〉 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.
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 646 of 721 Quit
Exercise 15.4
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.
Sx
Γ ` t : τ BT C
Γ ` (S t) : int BT C ∪ {τ = int}
Px
Γ ` t : τ BT C
Γ ` (P t) : int BT C ∪ {τ = int}
IZx
Γ ` t : τ BT C
Γ ` (IZ t) : bool BT C ∪ {τ = int}
GTZx
Γ ` t : τ BT C
Γ ` (GTZ t) : bool BT C ∪ {τ = int}
ITEx
Γ ` t : σ BT C
Γ ` t1 : τ BT1 C1
Γ ` t0 : υ BT0 C0
Γ ` (ITE 〈t, t1, t0〉) : τ BT ′ C ′
(T ∩ T1 = T1 ∩ T0 = T0 ∩ T = ∅)
where T ′ = T ∪ T1 ∪ T0 and C ′ = C ∪ C1 ∪ C0 ∪ {σ = bool, τ = υ}
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 (55).
4. Prove that the terms, ω = λx[(x x)] and Ω = (ω ω) are ill-typed.
5. Are the following well-typed or ill-typed? Prove your answer.
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 647 of 721 Quit
(a) (K S)
(b) ((K S) ω)
(c) (((S K) K) ω)
(d) (ITE 〈(IZ x), T, (K x)〉)
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 648 of 721 Quit
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 649 of 721 Quit
16. An Imperative Language
An Imperative Language
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 650 of 721 Quit
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 (γ, σ).
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 651 of 721 Quit
State: Illustration
x y z
T
132456
i
j
k
−87567
• l-values. γ(x) = i : bool, γ(y) = j : int, γ(z) = k : int
• r-values. σ(i) = T : bool, σ(j) = 132456 : int, σ(k) = −87567 : int
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 652 of 721 Quit
References in Languages
ML-like impure functional languages
• have an explicit polymorphic ′a 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!
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 653 of 721 Quit
l-values and r-values
T
132456
i
j
k
−87567
wx y z
m
l m
78663
• 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
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 654 of 721 Quit
XXXXXXXXXXl-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 occurrences
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.
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 655 of 721 Quit
– 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 obained by applying
a dereferencing operation (the prefix operation !). 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;
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 656 of 721 Quit
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 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
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 657 of 721 Quit
- !y;
val it = 1 : int
- !z;
val it = ref 1 : int ref
- !(!z);
val it = 1 : int
- !(!x);
val it = 1 : int
-
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 658 of 721 Quit
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 hardware 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 γ.
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 659 of 721 Quit
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 660 of 721 Quit
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 661 of 721 Quit
16.1. The Operational Semantics of Commands
WHILE: Big-Step Semantics
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 662 of 721 Quit
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 ex-
pressions in
• We defer defining the relation −→e for FL0.
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 663 of 721 Quit
State Changes or Side-Effects
• State changes are usually programmed by assignment commands which oc-
cur one location at a time.
• In the simple WHILE language side-effects do not occur except by explicit
assignment commands.
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 664 of 721 Quit
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
σ′ = [γ(x) 7→ b]σ
i.e.
σ′(`) =
{
σ(`) if ` 6= γ(x)
b otherwise
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 665 of 721 Quit
Aliases
Definition 16.1 Two (or more) variables are called aliases if they denote the
same location (y and u in the figure below).
T
132456
i
j
k
−87567
wx y
m
l m
78663
z
u
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 666 of 721 Quit
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 endif 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.
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 667 of 721 Quit
Operational Semantics: Basic Commands
Skip
γ ` 〈σ, skip〉 −→1c σ
Assgn
γ ` 〈σ, e〉 −→e m
γ ` 〈σ, x := e〉 −→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)
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 668 of 721 Quit
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
Block
γ ` 〈σ, c〉 −→1c σ′
γ ` 〈σ, {c}〉 −→1c σ′
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 669 of 721 Quit
Operational Semantics: Sequencing
Seq
γ ` 〈σ, c0〉 −→1c σ′,
γ ` 〈σ′, c1〉 −→1c σ′′
γ ` 〈σ, c0; c1〉 −→1c σ′′
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
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 670 of 721 Quit
Operational Semantics: Conditionals
Cond0
γ ` 〈σ, e〉 −→e F,
γ ` 〈σ, c0〉 −→1c σ0
γ ` 〈σ, if e then c1 else c0 endif〉 −→1c σ0
Cond1
γ ` 〈σ, e〉 −→e T,
γ ` 〈σ, c1〉 −→1c σ1
γ ` 〈σ, if e then c1 else c1 endif〉 −→1c σ1
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 671 of 721 Quit
The 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 endif (60)
• The following rules may be derived from (60) using the rules for condi-
tional, sequencing and skip (though the number of steps may not exactly
correspond).
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 672 of 721 Quit
Operational Semantics: While loop
While0
γ ` 〈σ, e〉 −→e F
γ ` 〈σ,while e do c〉 −→1c σ
While1
γ ` 〈σ, e〉 −→e T,
γ ` 〈σ, c〉 −→1c σ′,
γ ` 〈σ′,while e do c〉 −→1c σ′′
γ ` 〈σ,while e do c〉 −→1c σ′′
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 673 of 721 Quit
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 674 of 721 Quit
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 675 of 721 Quit
16.2. The Semantics of Expressions in FL
Operational Semantics for FL
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 676 of 721 Quit
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 real-
istic hardware which supports integers and booleans
• The normal forms on this machine would have to be appropriate integer and
boolean constants as represented in the machine.
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 677 of 721 Quit
Operational Semantics: Constants and Variables
Let σ ∈ States be any state.
T
γ ` 〈σ, T〉 −→e 〈σ, T〉 F γ ` 〈σ, F〉 −→e 〈σ, F〉
Z
γ ` 〈σ, Z〉 −→e 〈σ, 0〉 x γ ` 〈σ, x〉 −→e 〈σ, σ(γ(x))〉
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 678 of 721 Quit
Operational Semantics: Integer-valued Expressions
P
γ ` 〈σ, e〉 −→e 〈σ, m〉
γ ` 〈σ, (P e)〉 −→e 〈σ, m− 1〉 (e, m : int)
S
γ ` 〈σ, e〉 −→e 〈σ, m〉
γ ` 〈σ, (S e)〉 −→e 〈σ, m + 1〉 (e, m : int)
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 679 of 721 Quit
Operational Semantics: Boolean-valued Expressions
IZ0
γ ` 〈σ, e〉 −→e 〈σ, m〉
γ ` 〈σ, (IZ e)〉 −→e 〈σ, F〉 (e, m : int, m <> 0)
IZ1
γ ` 〈σ, e〉 −→e 〈σ, 0〉
γ ` 〈σ, (IZ e)〉 −→e 〈σ, T〉 (e : int)
GTZ0
γ ` 〈σ, e〉 −→e 〈σ, m〉
γ ` 〈σ, (GTZ e)〉 −→e 〈σ, F〉 (e, m : int, m <= 0)
GTZ1
γ ` 〈σ, e〉 −→e 〈σ, m〉
γ ` 〈σ, (GTZ e)〉 −→e 〈σ, T〉 (e, m : int, m > 0)
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 680 of 721 Quit
Operational Semantics: Conditional Expressions
ITEI0
γ ` 〈σ, e〉 −→e 〈σ, F〉
γ ` 〈σ, (ITE 〈e, e1, e0〉)〉 −→e 〈σ, e0〉
(e1, e0 : int)
ITEI1
γ ` 〈σ, e〉 −→e 〈σ, T〉
γ ` 〈σ, (ITE 〈e, e1, e0〉)〉 −→e 〈σ, e1〉
(e1, e0 : int)
ITEB0
γ ` 〈σ, e〉 −→e 〈σ, F〉
γ ` 〈σ, (ITE 〈e, e1, e0〉)〉 −→e 〈σ, e0〉
(e1, e0 : bool)
ITEB1
γ ` 〈σ, e〉 −→e 〈σ, T〉
γ ` 〈σ, (ITE 〈e, e1, e0〉)〉 −→e 〈σ, e1〉
(e1, e0 : bool)
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 681 of 721 Quit
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 682 of 721 Quit
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 683 of 721 Quit
16.3. The Operational Semantics of Declarations
Local Declarations
We introduce declarations through a new syntactic category Decls defined 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.
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 684 of 721 Quit
Some changed rules
• We use the symbol ⊥ to denote the undefined.
• We use z 6= ⊥ to denote that z is well-defined.
x′
γ ` 〈σ, x〉 −→e 〈σ, σ(γ(x))〉 (σ(γ(x)) 6= ⊥)
Assgn0′
γ ` 〈σ, x := m〉 −→1c [γ(x) 7→ m]σ
(γ(x) 6= ⊥)
Assgn1′
γ ` 〈σ, e〉 −→e 〈σ, e′〉
γ ` 〈σ, x := e〉 −→1c 〈σ, x := e′〉
(γ(x) 6= ⊥)
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 685 of 721 Quit
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
γ ` 〈σ, int x〉 −→d 〈[x 7→ l], [l 7→ ⊥]σ〉
(l /∈ Range(γ))
bool− x
γ ` 〈σ, bool x〉 −→d 〈[x 7→ l], [l 7→ ⊥]σ〉
(l /∈ Range(γ))
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 686 of 721 Quit
Scope
• The scope of a name begins from its definition and ends where the corre-
sponding 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
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 687 of 721 Quit
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
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 688 of 721 Quit
Example 16.2 local. Consider the following example ML program which uses local declarations in the
development 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
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 689 of 721 Quit
l > n orelse
u <= 0 orelse
u > n
then raise invalidArg
else if l > u
then 0
else ifdivisor3 (n, l)
+ sum_div2 (n, l+1, u)
in
fun perfect n =
if n <= 0
then raise invalidArg
else
let
val nby2 = n div 2
in
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 690 of 721 Quit
n = sum_div2 (n, 1, nby2)
end
end
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 691 of 721 Quit
Scope & local
end
local
fun fun1 y =
fun fun2 z =
in
fun fun3 x =
...
fun2 ...
fun1 ...
...
...
fun1
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 692 of 721 Quit
Execution in the Modified Environment
Once a declaration has been processed a new scope γ′ 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). γ′ is pushed onto γ to create a new
environment γ[γ′]. For any variable x,
γ[γ′](x) =
 γ
′(x) if x ∈ Dom(γ′)
γ(x) if x ∈ Dom(γ)−Dom(γ′)
⊥ otherwise
D− Seq
γ ` 〈σ, d1〉 −→d 〈γ1, σ1〉
γ[γ1] ` 〈σ1, d2〉 −→d 〈γ2, σ2〉
γ ` 〈σ, d1; d2〉 −→d 〈γ1[γ2], σ2〉
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 693 of 721 Quit
Semantics of Anonymous Blocks
Block
γ ` 〈σ, d〉 −→∗d 〈γ′, σ′〉
γ[γ′] ` 〈σ′, c〉 −→∗c σ′′
γ ` 〈σ, {d; c}〉 −→c σ′′ � Dom(σ)
Note.
• Note the use of the multi-step transitions on both declarations and com-
mands
• 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 γ′ which was produced by the declaration
d is no longer present on exiting the block.
• On exiting the block the domain of the state returns to Dom(σ), shedding
the new locations that were created for the “little” environment.
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 694 of 721 Quit
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 695 of 721 Quit
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 696 of 721 Quit
16.4. The Operational Semantics of Subroutines
Parameterless Subroutines: Named Blocks
The introduction of named blocks allows transfer of control from more control
points than in the case of unnamed blocks.
d1, d2, d ::= · · · sub P = c
c ::= · · · P
• The scope rules remain the same. All names in c refer to the most recent
definition in the innermost enclosing scope of the current scope.
• c may refer to variables that are visible in the static scope of P .
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 697 of 721 Quit
What does a procedure name represent?
• An anonymous block transforms a store σ to another store σ′.
• Each procedure name stands for a piece of code which effectively transforms
the store.
• Unlike an anonymous block which has a fixed position in the code, a named
procedure may be called from several points (representing many different
states).
• Each procedure represents a “state transformer”.
• However under static scope rules, the environment in which a procedure
executes remains fixed though the store may vary.
• Our environment, in addition to having locations should also be able to
associate names with state transformers.
Proc0 = Stores→ Stores
Env = {γ | γ : X → (Loc + Proc0)}
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 698 of 721 Quit
Semantics of Parameterless Subroutines
Each procedure declaration sub P = c modifies the environment γ by associat-
ing the procedure name P with an entity called a procedure closure proc0(c, γ),
which represents the body of the procedure and the environment in which it is
to be executed.
DSub0
γ ` 〈σ, sub P = c〉 −→d 〈[P 7→ proc0(c, γ)]γ, σ〉
CSub0
γ1 ` 〈σ, c〉 −→∗c σ′
γ ` 〈σ, P 〉 −→c 〈[P 7→ proc0(c, γ)]γ, σ′〉 (γ(P ) = proc0(c, γ1))
If P is recursive then we modify the last rule to
CrecSub0
γ2 ` 〈σ, c〉 −→∗c σ′
γ ` 〈σ, P 〉 −→c 〈[P 7→ proc0(c, γ)]γ, σ′〉 (γ(P ) = proc0(c, γ1))
where γ2 = [P 7→ γ(P )]γ1.
A generalization to mutual recursion with many such procedures is, in principle
easy, though notationally tedious.
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 699 of 721 Quit
Subroutines with Value Parameters
We consider the case of only a single parameter for simplicity.
d1, d2, d ::= · · · sub P (t x) = c sub P (bool x) = c
c ::= · · · P (e)
Proc0 = Stores→ Stores
Procv = (Stores× (int ∪ bool))→ Stores
Proc = Proc0 + Procv
Env = {γ | γ : X → (Loc + Proc)}
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 700 of 721 Quit
Semantics of Call-by-value
DSubv
γ ` 〈σ, sub P (t x) = c〉 −→d 〈[P 7→ procv(t x, c, γ)]γ, σ〉
where t ∈ {int, bool}
CrecSubv
γ ` 〈σ, e〉 −→∗e v
γ2 ` 〈[l 7→ v]σ, c〉 −→∗c σ′
γ ` 〈σ, P (e)〉 −→c σ′ � Dom(σ)
(γ(P ) = procv(t x, c, γ1))
where
• γ2 = [x 7→ l][P 7→ γ(P )]γ1,
• l /∈ Range(γ) ∪Dom(σ) and
• γ(P ) = procv(t x, c, γ1).
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 701 of 721 Quit
Subroutines with Reference Parameters
The Call-by-value parameter passing mechanism requires the evaluation of an
expression for the value parameter to be passed to the procedure. It requires in
addition the allocation of a location to store the value of the actual expression.
This strategy while quite efficient for scalar variables is too expensive when the
parameters are large structures such as arrays and records. In these case it is
more usual to pass merely only a reference to the parameter and ensure that all
modifications to any component of the formal parameter are instantaneously
reflected also in the actual parameter.
We consider the case of a single reference parameter for simplicity. We consider
the case of only a single parameter for simplicity.
d1, d2, d ::= · · · sub P (ref t x) = c sub P (ref bool x) = c
c ::= · · · P (x)
Notice that unlike the case of value parameters, the actual parameter in the
calling code can only pass a variable that is already present in its environment.
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 702 of 721 Quit
Semantics of Call-by-Reference
We augment the definition of Proc to include a new entity viz. Procr. We
then have
Proc0 = Stores→ Stores
Procv = (Stores× (int ∪ bool))→ Stores
Procr = (Stores× Loc)→ Stores
Proc = Proc0 + Procv + Procr
Env = {γ | γ : X → (Loc + Proc)}
DSubr
γ ` 〈σ, sub P (t x) = c〉 −→d 〈[P 7→ procr(t x, c, γ)]γ, σ〉
where t ∈ {int, bool}
CrecSubr
γ2 ` 〈[σ, c〉 −→∗c σ′
γ ` 〈σ, P (y)〉 −→c σ′ (γ(P ) = procr(t x, c, γ1))
where
• γ2 = [x 7→ γ(y)][P 7→ γ(P )]γ1,
Notice that no new location is needed in the call-by-reference mechanism.
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 703 of 721 Quit
17. Logic Programming and Prolog
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 704 of 721 Quit
Prolog: EBNF1
::= |
::= |
::= . | :- .
::= |
,
::= | ( )
::= | ,
::= | | |
::= ( )
::= ?- .
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 705 of 721 Quit
Prolog: EBNF2
::= | ’
::= |

::= |
::= a | b | c | ... | x | y | z
::= A | B | C | ... | X | Y | Z | _
::= |
::= 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9
::= | |
|
::= + | - | * | / | \ | ^ | ~ | : | . | ? | |
# | $ | &
::= |
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 706 of 721 Quit
Reversing the Arrow
Let
φ← ψ df= ψ → φ
Consider any clause C = {π1, . . . , πp}∪{¬ν1, . . . ,¬νn} where πi, 1 ≤ i ≤ p
are positive literals and ¬νj, 1 ≤ j ≤ n are the negative literals. Since
a clause in FOL with free variables represents the universal closure of the
disjunction of its literals, we have
Arrow Reversal
C ⇔ ~∀[(

1≤i≤p
πi) ∨ (

1≤j≤n
¬νj)]
⇔ ~∀[(

1≤i≤p
πi) ∨ ¬(

1≤j≤n
νj)]
⇔ ~∀[(

1≤j≤n
νj)→ (

1≤i≤p
πi)]
≡ ~∀[(

1≤i≤p
πi)← (

1≤j≤n
νj)]
df
= π1, . . . , πp← ν1, . . . , νn
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 707 of 721 Quit
Horn Clauses
Definition 17.1: Horn clauses
Given a clause
C
df
= π1, . . . , πp← ν1, . . . , νn
• Then C is a Horn clause if 0 ≤ p ≤ 1.
•C is called a
– program clause or rule clause if p = 1,
– fact or unit clause if p = 1 and n = 0,
– goal clause or query if p = 0,
• Each νj is called a sub-goal of the goal clause.
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 708 of 721 Quit
Program or Rule Clause
P
df
= π ← ν1, . . . , νn
≡ ~∀[π ∨ (

1≤j≤n
¬νj)]
≡ ~∀[π ∨ ¬(

1≤j≤n
νj)]
and is read as “π if ν1 and ν2 and . . . and νn”.
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 709 of 721 Quit
Facts: Unit Clauses
F
df
= π
≡ ~∀[π]
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 710 of 721 Quit
Goal clauses
Given a goal clause
G
df
= ← ν1, . . . , νn
⇔ ~∀[¬ν1 ∨ . . . ∨ ¬νn]
⇔ ¬~∃[ν1 ∧ . . . ∧ νn]
If ~y = FV (ν1 ∧ . . . ∧ νn) then the goal is to prove that there exists an assign-
ment to ~y which makes ν1 ∧ . . . ∧ νn true.
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 711 of 721 Quit
Logic Programs
Definition 17.2: Logic programs
A logic program is a finite set of Horn clauses, i.e. it is a set of rules
P = {h1, . . . , hk}, k ≥ 0 with hl ≡ πl ← νl1, . . . , νlnl, for 0 ≤ l ≤ k. π
l
is called the head of the rule and νl1, . . . , ν
l
nl
is the body of the rule.
Given a logic program P and a goal clause G = {ν1, . . . , νn} the basic idea is
to show that
P ∪ {G} is unsatisfiable
⇔ ~∀[¬ν1 ∨ · · · ∨ ¬νn] is a logical consequence of P
⇔ ~∃[ν1 ∧ · · · ∧ νn] is a logical consequence of P
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 712 of 721 Quit
Effectively showing P |= ~∃[ν1 ∧ · · · ∧ νn] implies that we need to find values for the variables X =
n⋃
j=1
FV (νj) which ensure
that P ∪ {G} is unsatisfiable. By Herbrand’s theorem this reduces to the problem of finding substitutions of ground terms
in the Herbrand base for variables in such a manner as to ensure unsatisfiability of P ∪{G}. This substitution is also called
a correct answer substitution.
We may regard a logic program therefore as a set of postulates of a family of models (represented by a Herbrand model)
and any correct answer substitution that may be derived (through resolution refutation) as a proof of the Goal as a logical
consequence of the postulates. Since resolution refutation is sound and complete we effectively show P `R ~∃[ν1 ∧ · · · ∧ νn]
where `R denotes a proof by resolution refutation.
A propositional logic program is one in which there are no variables either in P or in the goal clause G and the execution
is a pure application of the rule Res0 to obtain a contradiction.
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 713 of 721 Quit
Sorting in Logic
sort(x, y) ← perm(x, y), ordered(y)
ordered(nil) ←
ordered(x.nil) ←
ordered(x.y.z) ← lesseq(x, y), ordered(y.z)
lesseq(x, x) ←
lesseq(x, y) ← x < y
perm(nil, nil) ←
perm(x.y, u.v) ← delete(u, x.y, z), perm(z, v)
delete(x, x.y, y) ←
delete(x, y.z, y.w) ← delete(x, z, w)
← sort([2, 8,−1, 10, 4, 2], x)
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 714 of 721 Quit
Example 17.1
Here is the corresponding Prolog encoding.
i s o r t (X, Y) :− permutation (X, Y) ,
ordered (Y) .
ordered ( [ ] ) .
ordered (H . [ ] ) .
ordered (F . S .T) :− l e s s e q (F , S ) ,
ordered (S .T) .
l e s s e q (F , S) :− F=S .
l e s s e q (F , S) :− Fpermutation ( [ ] , [ ] ) .
permutation (H.T, F .R) :− d e l e t e (F , H.T, Z) ,
permutation (Z , R) .
d e l e t e (H, H.T, T) .
d e l e t e (X, H.T, H.U):− d e l e t e (X, T, U) .
/∗ i s o r t ( [ 2 , 8 , −1 ,10 , 4 , 2 ] , Y) . ∗/
Example 17.2
Notice that merge-sort closely follows the corresponding functional implementation.
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 715 of 721 Quit
mergeSort ( [ ] , [ ] ) .
mergeSort (H . [ ] , H . [ ] ) .
mergeSort (F . S .T, S a l l ) :− s p l i t (F . S .T, Left , Right ) ,
mergeSort ( Left , S l e f t ) ,
mergeSort ( Right , S r i gh t ) ,
merge ( S l e f t , Sr ight , S a l l ) .
s p l i t ( [ ] , [ ] , [ ] ) .
s p l i t (H . [ ] , H . [ ] , [ ] ) .
s p l i t (F . S .T, F .U, S .V) :− s p l i t (T, U, V) .
merge ( [ ] , L , L ) .
merge (L , [ ] , L ) .
merge (F .B, H.T, F .U) :− F=merge (F .B, H.T, H.V) :− HExample 17.3
And so does quick-sort.
%consu l t (+/home/ sak / pro log / ordered .P, / home/ sak / pro log / permutation .P) .
[ ’permutation.P’ ] .
[ ’ordered.P’ ] .
q u i ck so r t ( [ ] , [ ] ) .
q u i ck so r t (H . [ ] , H . [ ] ) .
q u i ck so r t (H.T, S) :− p a r t i t i o n (H, T, L , G) ,
q u i ck so r t (L , Ls ) ,
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 716 of 721 Quit
q u i ck so r t (G, Gs ) ,
append ( Ls , H . [ ] , Lsh ) ,
append ( Lsh , Gs , S ) .
p a r t i t i o n (M, [ ] , [ ] , [ ] ) .
p a r t i t i o n (M, H.T, H. Lesser , Greater ) :− H=p a r t i t i o n (M, T, Lesser , Greater ) .
p a r t i t i o n (M, H.T, Lesser , H. Greater ) :− Mp a r t i t i o n (M, T, Lesser , Greater ) .
append ( [ ] , L , L ) .
append (H.T, L , H.A) :− append (T, L , A) .
/∗ t e s t i n g
q u i ck so r t ([−2 , 10 ,3 ,5 , −3 ,8 ,22 ] ,X) .
s o r t ed (X,Y):− q u i ck so r t (X,Y) , ordered (Y) , permutation (X,Y) .
so r t ed ([−2 , 10 ,3 ,5 , −3 ,8 ,22 ] ,X) .
∗/
Example 17.4
Here is a simple problem in alphametics which is a constraint solving problem.
https://en.wikipedia.org/wiki/Verbal_arithmetic
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 717 of 721 Quit
smm :−
L = [ S ,E,N,D,M,O,R,Y] ,
D i g i t s = [ 0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 ] ,
a s s i g n d i g i t s (L , D i g i t s ) ,
M > 0 ,
S > 0 ,
1000∗S + 100∗E + 10∗N + D +
1000∗M + 100∗O + 10∗R + E =:=
10000∗M + 1000∗O + 100∗N + 10∗E + Y,
wr i t e (’ ’ ) , wr i t e (S ) , wr i t e (E) , wr i t e (N) , wr i t e (D) , nl ,
wr i t e (’ + ’ ) , wr i t e (M) , wr i t e (O) , wr i t e (R) , wr i t e (E) , nl ,
wr i t e (’ -----’ ) , nl ,
wr i t e (’= ’ ) , wr i t e (M) , wr i t e (O) , wr i t e (N) , wr i t e (E) , wr i t e (Y) , n l .
s e l e c t (Z , [ Z |R] , R) .
s e l e c t (Z , [Y| Zs ] , [Y|Ys ]) :− s e l e c t (Z , Zs , Ys ) .
a s s i g n d i g i t s ( [ ] , L i s t ) .
a s s i g n d i g i t s ( [D|Ds ] , L i s t ):−
s e l e c t (D, Lis t , NewList ) ,
a s s i g n d i g i t s (Ds , NewList ) .
Example 17.5: Prolog: Naturals
n example of a (symbolic) rewrite system to compute normal forms.
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 718 of 721 Quit
i s n f ( z ) .
i s n f ( s (X) ) :− i s n f (X) .
r e w r i t e (X, X) :− i s n f (X) .
r e w r i t e ( s (X) , s (Y) ) :− r e w r i t e (X, Y) , i s n f (Y) .
r e w r i t e ( a ( z , Y) , Y) :− i s n f (Y) .
r e w r i t e ( a (Y, z ) , Y) :− i s n f (Y) .
r e w r i t e ( a ( s (X) , Y) , s (Z ) ) :− r e w r i t e ( a (X,Y) , Z ) .
r e w r i t e ( a (X, s (Y) ) , s (Z ) ) :− r e w r i t e ( a (X,Y) , Z ) .
r e w r i t e ( a (X,Y) , Z) :− r e w r i t e (X,U) , r e w r i t e (Y,V) , r e w r i t e ( a (U,V) , Z ) .
even ( z ) .
even ( s ( s (X) ) ) :− r e w r i t e (X,Y) , even (Y) .
odd (X) :− not even (X) . % negat ion as f a i l u r e
/∗ r e w r i t e ( a ( a ( s ( z ) , s ( s ( z ) ) ) , a ( s ( z ) , s ( s ( z ) ) ) ) , X) .
X = s ( s ( s ( s ( s ( s ( z ) ) ) ) ) ) ∗/
Example 17.6
In this example we give a prolog implementation of double-ended queues of integers using constructors.
deq ( nu l l q ) .
deq ( fnq (A, D) ) :− i n t e g e r (A) , deq (D) .
deq ( rnq (B, D) ) :− i n t e g e r (B) , deq (D) .
nonnul l ( fnq (A, D) ) :− i n t e g e r (A) , deq (D) .
nonnul l ( rnq (B, D) ) :− i n t e g e r (B) , deq (D) .
deq ( fdq (D) ) :− nonnul l (D) .
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 719 of 721 Quit
deq ( rdq (D) ) :− nonnul l (D) .
nf ( nu l l q ) .
n f ( fnq (A, D) ) :− i n t e g e r (A) , nf (D) .
r e w r i t e (D, D) :− nf (D) .
%induct ion step f o r normal forms
r e w r i t e ( fnq (A, D) , fnq (A, E) ) :− i n t e g e r (A) , r e w r i t e (D, E) .
% f o r a l l forms other than normal forms
r e w r i t e ( rnq (B, nu l l q ) , fnq (B, nu l l q )):− i n t e g e r (B) . % b a s i s o f induct i on
r e w r i t e ( rdq ( fnq (A, nu l l q ) ) , nu l l q ) . % b a s i s o f induct i on
% r e w r i t e ( fdq ( fnq (A, nu l l q ) ) , nu l l q ) f o l l o w s from the more gene ra l r e w r i t e
r e w r i t e ( fdq ( fnq (A, D) ) , E):− i n t e g e r (A) , r e w r i t e (D, E) . % fdq f o r a l l nonnul l
r e w r i t e ( rnq (B, fnq (A, D) ) , fnq (A, E) ) :− % f o r rnq on normal forms
i n t e g e r (A) , i n t e g e r (B) ,
r e w r i t e (D, F) ,
r e w r i t e ( rnq (B, F) , E) , nf (E) .
r e w r i t e ( rnq (B, D) , E) :− % f o r rnq on other forms
i n t e g e r (B) ,
r e w r i t e (D, F) ,
r e w r i t e ( rnq (B, F) , E) .
r e w r i t e ( rdq ( fnq (A, D) ) , fnq (A, E) ) :− % f o r rdq on normal forms
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 720 of 721 Quit
i n t e g e r (A) ,
r e w r i t e (D, F) , nonnul l (F) ,
r e w r i t e ( rdq (F) , E) .
r e w r i t e ( rdq (D) , E) :− % f o r rdq on other forms
r e w r i t e (D, F) , r e w r i t e ( rdq (F) , E) .
% r e w r i t e ( rdq ( rnq (B, D) ) , D) f o l l o w s by induct ion from the var i ous r e w r i t e s above
fv ( fnq (A, D) , A):− i n t e g e r (A) , deq (D) . % value at the f r o n t o f the dequeue
fv (D, B) :− r e w r i t e (D,E) , fv (E,B) .
rv ( fnq (A, nu l l q ) , B) :− i n t e g e r (A) , A=B.
rv ( fnq (A, D) , B) :− i n t e g e r (A) , r e w r i t e (D, E) , rv (E, B) .
rv (D, B) :− r e w r i t e (D, E) , rv (E, B) .
/∗ Test ing
% Restor ing f i l e / usr / l o c a l / l i b /Yap/ s ta r tup
YAP ve r s i on Yap−5.1.1
?− % r e c o n s u l t i n g /home/ sak / pro log / deques .P . . .
% re con su l t ed /home/ sak / pro log / deques .P in module user , 0 msec 4096 bytes
yes
?− r e w r i t e ( fnq (2 , fnq (1 , nu l l q ) ) , X) .
X = fnq (2 , fnq (1 , nu l l q ) ) ?
yes
?− rv ( fnq (1 , fnq (2 , nu l l q ) ) , B) .
B = 2 ?
yes
?− rv ( rnq (3 , fnq (1 , fnq (2 , nu l l q ) ) ) , B) .
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 721 of 721 Quit
B = 3 ?
yes
?− r e w r i t e ( rnq (4 , fdq ( fnq (1 , fnq (2 , rnq (3 , nu l l q ) ) ) ) ) , X) .
X = fnq (2 , fnq (3 , fnq (4 , nu l l q ) ) ) ?
yes
?− rv ( rnq (4 , fdq ( fnq (1 , fnq (2 , rnq (3 , nu l l q ) ) ) ) ) , B) .
B = 4 ?
yes
?− rv ( rdq ( rnq (4 , fdq ( fnq (1 , fnq (2 , rnq (3 , nu l l q ) ) ) ) ) ) , B) .
B = 3 ?
yes
?− fv ( rnq (4 , fdq ( fnq (1 , fnq (2 , rnq (3 , nu l l q ) ) ) ) ) , B) .
B = 2 ?
yes
∗/
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 722 of 721 Quit
Prolog0: Abstract Interpreter
Algorithm 11 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 A′ ← B1, . . . , Bk from P such that A ≡ A′
5: if A′ 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
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 723 of 721 Quit
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 724 of 721 Quit
Prolog1: Abstract Interpreter
Algorithm 12 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 A′ ← B1, . . . , Bk ∈ P : unifiable(A′, A0) then
8: τ := mgu(A′, 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
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 725 of 721 Quit
Home Page JJ J I II
PL May 4, 2021
Go Back Full Screen Close 726 of 721 Quit
Figure 7: Case m > 0 and n > 0
1
1
L
M
M
N
N
P
Q
R
S
*
*
*
*
*
*
*
*
1
1
*
The Programming Languages Overview
Introduction to Compiling
Scanning or Lexical Analysis
Regular Expressions
Nondeterministic Finite Automata (NFA)
Deterministic Finite Automata (DFA)
Parsing or Syntax Analysis
Grammars
Context-Free Grammars
Ambiguity
The ``dangling else'' problem
Specification of Syntax: Extended Backus-Naur Form
The WHILE Programming Language: Syntax
Parsing
Recursive Descent Parsing
Shift-Reduce Parsing
Bottom-Up Parsing
Simple LR Parsing
Attributes & Semantic Analysis
Context-sensitive analysis and Semantics
Static Scope Rules
Runtime Structure
Abstract Syntax
Syntax-Directed Translation
Synthesized Attributes
Inherited Attributes
Symbol Table
Intermediate Representation
The Pure Untyped Lambda Calculus: Basics
Motivation for
The -notation
Notions of Reduction
Confluence Definitions
Why confluence?
Confluence: Church-Rosser
The Church-Rosser Property
An Applied Lambda-Calculus
FL with recursion
Motivation and Organization
Static Semantics of FL(X)
Type-checking FL(X) terms
The Typing Rules
Equational Reasoning in FL(X)
Type-checking FL
RecFL(X) with type rules
An Imperative Language
l-values, r-values, aliasing and indirect addressing
The Operational Semantics of Commands
The Semantics of Expressions in FL
The Operational Semantics of Declarations
The Operational Semantics of Subroutines
Logic Programming and Prolog
Answered 3 days AfterMay 12, 2021

Solution

Swapnil Powar answered on May 16 2021
22 Votes

Submit New Assignment

Copy and Paste Your Assignment Here