Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... ·...

352
Index to Lectures Introduction Lexical Analysis Syntax Analysis Semantic Analysis Symbol Table Code Generation Runtime Structure Home Page Title Page JJ II J I Page 1 of 322 Go Back Full Screen Close Quit First Prev Next Last Go Back Full Screen Close Quit ECE 726: Compiler Design Principles Dec 2009-Mar 2010 http://www.cse.iitd.ac.in/˜sak/courses/cdp/eth09-10 S. Arun-Kumar Department of Computer Science and Engineering I. I. T. Delhi, Hauz Khas, New Delhi 110 016. March 28, 2010

Transcript of Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... ·...

Page 1: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 1 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

ECE 726:Compiler Design Principles

Dec 2009-Mar 2010http://www.cse.iitd.ac.in/ s̃ak/courses/cdp/eth09-10

S. Arun-KumarDepartment of Computer Science and Engineering

I. I. T. Delhi, Hauz Khas, New Delhi 110 016.

March 28, 2010

Page 2: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 2 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

1. Index to Lectures1. Introduction

2. Lexical Analysis

3. Regular Expressions

4. Nondeterministic Finite Automata (NFA)

5. Scanning Using NFAs

6. Conversion of NFAs to DFAsLexical Analysis: Problems

7. Syntax Analysis

8. Context-Free GrammarsContext-Free Grammars: Problems

9. Ambiguity Disambiguation

10. Introduction to Parsing

11. Bottom-Up Parsing

12. LR(0) ParsingLR0 Parsing: Problems

13. LR(0) Parsing Contd.

14. LR(0) to SLR

15. Review Lecture

16. SLR to LR(1)

17. LR(1) Example

18. LR(1) Example contd

Page 3: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 3 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

19. LR(1) to LALR(1)

20. Concrete to Abstract

21.

Page 4: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 4 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Page 5: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 5 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

2. Introduction

Lecture 01Introduction

Page 6: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 6 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Introduction• Translation of programming languages into executable code

• But more generally any large piece of software requires the use ofcompiling techniques.

• The processes and techniques of designing compilers is useful indesigning most large pieces of software.

• Compiler design uses techniques from theory, data structures andalgorithms.

Page 7: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 7 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Software ExamplesSome examples of other software that use compiling techniques

• Almost all user-interfaces require scanners and parsers to be used.

• All XML-based software require interpretation that uses these tech-niques.

• All mathematical text formatting requires the use of scanning, pars-ing and code-generation techniques (e.g. LATEX).

• Model-checking and verification software are based on compilingtechniques

• Synthesis of hardware circuits requires a description language andthe final code that is generated is an implementation either at theregister-transfer level or gate-level design.

Page 8: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 8 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Books and References1. 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, Techniques,and Tools, Addison-Wesley 1986.

3. Muchnick S S. Advanced Compiler Design and Implementation,Academic Press 1997.

Page 9: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 9 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Source and TargetIn general a compiler written in some language C translates code froma source language S to a target language T . The

source language could be

• a programming language, or• a description language (e.g. Verilog, VHDL), or• a markup language (e.g. XML, HTML, SGML, LATEX)

target language could be

• another programming language, assembly language or machinelanguage, or• a language for describing various objects (circuits etc.), or• a low level language for execution, display, rendering etc.

We will be primarily concerned with compiling from a high-level pro-gramming language (source) to low-level code.

Page 10: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 10 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

The Compiling ProcessIn general the process of compiling involves at least three languages

1. The language S of source programs in which the users of the com-piler write code.

2. The language C in which the compiler itself is written. The assump-tion is that unless the compiler itself is written in machine languagethere is already a compiler or an interpreter for C.

3. The language T into which the compiler translates the user pro-grams.

Besides these three languages there could be several other intermediatelanguages I1, I2, . . . (also called intermediate representations) intowhich the source program could be translated in the process of compil-ing or interpreting the source programs written in S . In modern compil-ers, for portability, modularity and reasons of code improvement, thereis usually at least one intermediate representation.

Page 11: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 11 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

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

Page 12: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 12 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Phases of a CompilerA compiler or translator is a fairly complex piece of software that needsto be developed in terms of various independent modules.In the case of most programming languages, compilers are designed inphases.The various phases may be different from the various passes in compi-lation

Page 13: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 13 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Page 14: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 14 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

The Big Picture: 1

SCANNER

stream ofcharacters

stream oftokens

Page 15: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 15 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

The Big Picture: 2

SCANNER

PARSER

stream ofcharacters

stream oftokens

parse tree

Page 16: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 16 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

The Big Picture: 3

SCANNER

PARSER

SEMANTIC ANALYZER

stream ofcharacters

stream oftokens

parse tree

abstractsyntax tree

Page 17: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 17 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

The Big Picture: 4

SCANNER

PARSER

SEMANTIC ANALYZER

I.R. CODE GENERATOR

stream ofcharacters

stream oftokens

parse tree

intermediaterepresentation

abstractsyntax tree

Page 18: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 18 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

The Big Picture: 5

SCANNER

PARSER

SEMANTIC ANALYZER

I.R. CODE GENERATOR

OPTIMIZER

stream ofcharacters

stream oftokens

parse tree

intermediaterepresentation

optimizedintermediaterepresentation

abstractsyntax tree

Page 19: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 19 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

The Big Picture: 6

SCANNER

PARSER

SEMANTIC ANALYZER

I.R. CODE GENERATOR

OPTIMIZER

CODE GENERATOR

stream ofcharacters

stream oftokens

parse tree

intermediaterepresentation

optimizedintermediaterepresentation

target code

abstractsyntax tree

Page 20: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 20 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

The Big Picture: 7

SCANNER

PARSER

SEMANTIC ANALYZER

I.R. CODE GENERATOR

OPTIMIZER

CODE GENERATOR

ERROR−

HANDLER

SYMBOL

TABLE

MANAGER

stream ofcharacters

stream oftokens

parse tree

intermediaterepresentation

optimizedintermediaterepresentation

target code

abstractsyntax tree

Page 21: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 21 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

The Big Picture: 7SCANNER

PARSER

SEMANTIC ANALYZER

I.R. CODE GENERATOR

OPTIMIZER

CODE GENERATOR

ERROR−

HANDLER

SYMBOL

TABLE

MANAGER

stream ofcharacters

stream oftokens

parse tree

intermediaterepresentation

optimizedintermediaterepresentation

target code

abstractsyntax tree

Scanner Parser Semantic Analysis Symbol TableIR Optimization Contents

Page 22: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 22 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Page 23: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 23 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Page 24: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 24 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

3. Lexical Analysis

Lecture 02Lexical Analysis

Page 25: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 25 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Language• Every language is built from a finite alphabet of symbols. The

alphabet of a programming language consists of the symbols of theASCII set.

• Each language has a vocabulary consisting of words. Each word isa string of symbols drawn from the alphabet.

• Each language has a finite set of punctuation symbols, which sep-arate phrases, clauses and sentences.

• The phrases, clauses and sentences of a programming language areexpressions, commands, functions, procedures and programs.

Page 26: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 26 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Lexical Analysislex-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 thecompiler must first break up this stream into groups of meaningfulwords, and other symbols.

• Each such group of characters is then classified as belonging to acertain token type.

• Certain sequences of characters are not tokens and are completelyignored (or skipped) by the compiler.

Page 27: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 27 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Tokens and Non-tokensTokens Typical tokens are

• Identifiers: Names of variables, constants, procedures, func-tions etc.• Keywords/Reserved words: void, public, main• Operators:+, *, /• Punctuation: ,, :, .• Brackets: (, ), [, ], begin, end, case, esac

Non-tokens Typical non-tokens

• whitespace: sequences of tabs, spaces, new-line characters,• comments: compiler ignores comments• preprocessor directives: #include ..., #define ...

• macros in the beginning of C programs

Page 28: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 28 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Scanning: 1

• 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 themas parameters to the other phases to enable error-reporting to theuser.

Page 29: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 29 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Scanning: 2• Whitespace: A sequence of space, tab, newline, carriage-return,

form-feed characters etc.

• Lexeme: A sequence of non-whitespace characters delimited bywhitespace or special characters (e.g. operators like +, -, *).

• Examples of lexemes.

– reserved words, keywords, identifiers etc.– Each comment is usually a single lexeme– preprocessor directives

Page 30: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 30 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Scanning: 3• 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.)

Page 31: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 31 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Scanning: 4• Identification of tokens is usually done by a Deterministic Finite-

state automaton (DFA).

• The set of tokens of a language is represented by a large regularexpression.

• This regular expression is fed to a lexical-analyser generator such asLex, Flex or JLex.

• A giant DFA is created by the Lexical analyser generator.

Page 32: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 32 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •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 thefollowing rules.

1. An identifier consists of a sequence of of letters (A . . .Z, a . . .z),digits (0 . . .9) and the underscore ( ) character.

2. The first character of an identifier must be a letter.

• Any two tokens are separated by some delimiters (usually whites-pace) or non-tokens in the source program.

Page 33: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 33 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Page 34: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 34 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

3.1. Regular Expressions

Lecture 03Regular Expressions

Page 35: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 35 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Specifying Lexical RulesWe require compact and simple ways of specifying the lexical rules of alanguage. In particular,

• there are an infinite number of legally correct programs in any pro-gramming language and

• we require finite descriptions/specifications of the lexical rules sothat they can cover the infinite number of legal programs.

One way of specifying the lexical rules of a programming language isto use regular expressions.

Page 36: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 36 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Regular Expressions• Each regular expression is a finite sequence of symbols.

• A regular expression may be used to describe an infinite collectionof strings.

The regular expression used to define the set of possible identifiers asdefined by the rules is

[A−Za−z][A−Za−z0−9 ]∗

Page 37: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 37 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

ConcatenationsConsider a (finite) alphabet (of symbols) A.

• Any set of strings built up from the symbols of A is called a lan-guage.

• Given any two strings x and y in a language, x.y or simply xy isthe concatenation of the two strings.Example Given the strings x = Mengesha and y = Mamo, x.y =MengeshaMamo and y.x = MamoMengesha.

• Given two languages X and Y , then X.Y or simply XY is theconcatenation of the languages.Example Let X = {Mengesha, Gemechis} and Y ={Mamo, Bekele, Selassie}XY = {MengeshaMamo, MengeshaBekele,MengeshaSelassie, GemechisMamo,GemechisBekele, GemechisSelassie}

Page 38: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 38 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Simple Language ofRegular Expressions

We consider a simple language of regular expressions. Assume a (fi-nite) alphabet A of symbols. Each regular expression r denotes a set ofstrings L(r). L(r) is also called the language specified by the regularexpression r.

Symbol For each symbol a in A, the regular expression a denotes theset {a}.

(Con)catenation For any two regular expressions r and s, r.s or simplyrs denotes the concatenation of the languages specified by r and s.That is,

L(rs) = L(r)L(s)

Page 39: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 39 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Epsilon and AlternationEpsilon ε 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 setunion of the languages specified by the individual expressions r ands respectively.

L(r | s) = L(r) ∪ L(s)

Example L(Menelik|Selassie|ε) = {Menelik, Selassie, ””}.

Page 40: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 40 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

String RepetitionsFor any string x, we may use concatenation to create a string y with asmany repetitions of x as we want, by defining repetitions by induction.

x0 = ””x1 = xx2 = x.x

...xn+1 = x.xn = xn.x

...

Thenx∗ = {xn | n ≥ 0}

Page 41: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 41 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

String Repetitions ExampleExample. Let x = Selassie. Then

x0 = ””x1 = Selassiex2 = SelassieSelassie

...x5 = SelassieSelassieSelassieSelassieSelassie

...

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

Page 42: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 42 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Language IterationThe ∗ operator can be extended to languages in the same way. For anylanguage X , we may use concatenation to create a another language Ywith as many repetitions of the strings in X as we want, by definingrepetitions by induction.

X0 = ””X1 = XX2 = X.X

...Xn+1 = X.Xn = Xn.X

...

ThenX∗ =

⋃n≥0

Xn

Page 43: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 43 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Language Iteration ExampleExample 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

...

Page 44: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 44 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Kleene ClosureGiven a regular expression r, rn specifies the n-fold iteration of thelanguage 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.

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

Question 2. How many strings will be in the language specified by(a | b | c)n?

Question 3. Give an informal description of the language specified by(a | b | c)∗?

Question 4. Give a regular expression which specifies the language{ak | k > 100}.

Page 45: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 45 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Plus ClosureThe 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 languageX ,X+ = X.X∗ and hence for any regularexpression r we have

r+ = r.r∗

We also have the identityr∗ = ε | r+

Question 5. Simplify the expression r∗.r∗, i.e. give a simpler regularexpression which specifies the same language.

Question 6. Simplify the expression r+.r+.

Page 46: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 46 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Range SpecificationsWe may specify ranges of various kinds as follows.

• [a−c] = a | b | c. Hence the expression of Question 3 may bespecified as [a−c]∗.

• Multiple ranges: [a−c0−3] = [a−c] | [0−3]

Question 6. Try to understand what the regular expression for identi-fiers really specifies.

Question 7. Modify the regular expression so that all identifiers startonly 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 floatingpoint notation.

Page 47: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 47 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Page 48: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 48 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Page 49: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 49 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

3.2. Nondeterministic Finite Automata (NFA)

Lecture 04Nondeterministic Finite

Automata (NFA)

Page 50: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 50 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Nondeterministic FiniteAutomata

A regular expression is useful in defining a finite state automaton. Anautomaton is a machine (simple program) which can be used to recog-nize all valid lexical tokens of a language.A nondeterministic finite automaton (NFA) N over a finite alphabetA 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 acceptingstates, and

• a transition relation−→⊆ Q× (A ∪ {ε})×Q. Equivalently

−→: Q× (A ∪ {ε})→ 2Q

is a function that for each source state q ∈ Q and symbol a ∈ Aassociates a set of target states.

Page 51: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 51 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Nondeterminism andAutomata

• In general the automaton reads the input string from left to right.

• It reads each input symbol only once and executes a transition tonew state.

• The ε transitions represent going to a new target state without read-ing any input symbol.

• The NFA may be nondeterministic because of

– one or more ε transitions from the same source state differenttarget states,

– one or more transitions on the same input symbol from onesource state to two or more different target states,

– choice between executing a transition on an input symbol and atransition on ε (and going to different states).

Page 52: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 52 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •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 = a1a2 . . . an ∈ A∗, an accepting sequence is asequence 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 thereexists 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.

Page 53: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 53 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •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 .

Page 54: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 54 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •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 theregular expression. That is, for each regular expression operator,we show how to construct the corresponding automaton assumingthat the NFAs corresponding to individual components of expres-sion have already been constructed.

• For any regular expression r the corresponding NFA constructed isdenoted Nr. Hence for the regular expression r|s, we construct theNFA Nr|s using the NFAs Nr and Ns as the building blocks.

• Our method requires only one initial state and one final state foreach automaton. Hence in the construction ofNr|s fromNr andNs,the initial states and the final states of Nr and Ns are not initial orfinal unless explicitly used in that fashion.

Page 55: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 55 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Constructing NFA• We show the construction only for the most basic operators on reg-

ular expressions.

• For any regular expression r, we construct a NFA Nr whose initialstate is named r0 and final state rf .

• The following symbols show the various components used in thedepiction of NFAs.

Initial state

Accepting state

a Typical transition

0 fr rrNTypical NFA for r

Page 56: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 56 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Regular Expressions toNFAs:1

a

Na0a

fa

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

Na Input SymbolState a · · · ε

a0 {af} ∅ · · · ∅ ∅af ∅ ∅ · · · ∅ ∅

Notice that all the cells except one have empty targets.

Page 57: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 57 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Regular Expressions toNFAs:2

ε

Nεεε0 f

Nε Input SymbolState a · · · ε

ε0 ∅ ∅ · · · ∅ {εf}εf ∅ ∅ · · · ∅ ∅

Page 58: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 58 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Regular Expressions toNFAs:3

ε

ε

ε

ε

Nr|s

N

N

r

s

r

s

r

s

0 f

0 f

r|s r|s0 f

Nr|s Input SymbolState a · · · ε

r|s0 ∅ · · · {r0, s0}r0 · · · · · · · · ·... ... ... ...rf · · · · · · {r|sf}s0 · · · · · · · · ·... ... ... ...sf · · · · · · {r|sf}r|sf ∅ · · · ∅

Page 59: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 59 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Regular Expressions toNFAs:4

r.sNrr N

ss

0 0 frf

s

Nr.s Input SymbolState a · · · ε

r0 · · · · · · · · ·... ... ... ...rf · · · · · · {s0}s0 · · · · · · · · ·... ... ... ...sf · · · · · · · · ·

Notice that the initial state of Nr.s is r0 and the final state is sf in thiscase.

Page 60: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 60 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Regular Expressions toNFAs:5

ε ε

ε

ε

Nr*

Nrr rr*

fr*

0 0 f

Nr∗ Input SymbolState a · · · ε

r∗0 ∅ · · · {r0, r∗f}

r0 · · · · · · · · ·... ... ... ...rf · · · · · · {r0, r

∗f}

r∗f ∅ ∅ ∅

Page 61: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 61 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Regular expressions vs.NFAs

• It is obvious that for each regular expression r, the correspondingNFA 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.

Page 62: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 62 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

ExampleWe construct a NFA for the regular expression (a|b)∗abb.

• Assume the alphabet A = {a, b}.• We follow the steps of the construction as given in Constructing

NFA to Regular Expressions to NFAs:5

• For ease of understanding we use the regular expression itself (sub-scripted by 0 and f respectively) to name the two new states createdby the regular expression operator.

Page 63: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 63 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Example:-6

aa0 f

a Na

Page 64: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 64 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Example:-5

aa0 f

b0

bf

b

a Na

Nb

Page 65: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 65 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Example:-4

ε

ε

ε

ε

a

0

a0 f

b0

bf

b

a

a|b a|bf

a|bN

Page 66: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 66 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Example:-3

ε

ε

ε

ε

ε

a

0

a0 f

b0

bf

b

a

a|b a|bf

ε

(a|b)*f

ε

0(a|b)*

ε

(a|b)*N

Page 67: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 67 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Example:-2

ε

(a|b)* a

(a|b)* a

ε

ε

ε

ε

a

0

a0 f

b0

bf

b

a

a|b a|bf

ε

(a|b)*f

ε

a

0(a|b)*

ε

N

Page 68: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 68 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Example:-1

ε

(a|b)* a(a|b)* ab b

(a|b)* ab

ε

ε

ε

ε

a

0

a0 f

b0

bf

b

a

a|b a|bf

ε

(a|b)*f

ε

a

0(a|b)*

ε

N

Page 69: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 69 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Example-final

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

(a|b)* abb

ε

ε

ε

ε

ε

a

0

a0 f

b0

bf

b

a

a|b a|bf

ε

(a|b)*f

ε

a

bf

0(a|b)*

ε

N

Page 70: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 70 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

ExtensionsWe have provided constructions for only the most basic operators onregular expressions. Here are some extensions you can attempt

1. Show how to construct a NFA for ranges and multiple ranges ofsymbols

2. Assuming Nr is a NFA for the regular expression r, how will youconstruct 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?

Page 71: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 71 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Lecture 05Scanning Using NFAs

Page 72: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 72 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •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 andefficiently.

• Accuracy is achieved by regular expression specification of the to-kens

• Efficiency implies that the input should not be read more than once.

Page 73: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 73 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Nondeterminism and TokenRecognition

• The three kinds of nondeterminism in the NFA construction are de-picted in the figure below.

ε

ε ε

a a

a

(i) (ii) (iii)

(i) It is difficult to know which ε transition to pick without readingany further input

(ii) For two transitions on the same input symbol a it is difficult toknow which of them would reach a final state on further input.

(iii) Given an input symbol a and a ε transition on the current stateit is impossible to decide which one to take without looking atfurther input.

Page 74: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 74 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Nondeterministic Features• In general it is impossible to recognize tokens in the presence of

nondeterminism without backtracking.

• Hence NFAs are not directly useful for scanning because of the pres-ence of nondeterminism.

• The nondeterministic feature of the construction of Nr for any reg-ular expression r is in the ε transitions.

• The ε transitions in any automaton refer to the fact that no inputcharacter is consumed in the transition.

• Backtracking usually means algorithms involving them are verycomplex and hence inefficient.

• To avoid backtracking, the automaton should be made deterministic

Page 75: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 75 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

From NFA to DFA• Since the only source of nondeterminism in our construction are

the ε, we need to eliminate them without changing the languagerecognized by the automaton.

• Two consecutive ε transitions are the same as one. In fact any num-ber of ε transitions are the same as one. So as a first step we computeall finite sequences of ε transitions and collapse them into a single εtransition.

• Two states q, q′ are equivalent if there are only ε transitions betweenthem. This is called the ε-closure of states.

Page 76: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 76 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

ε-ClosureGiven a set T of states, then Tε = ε − closure(T ) is the set of stateswhich either belong to T or can be reached from states belonging to Tonly through a sequence of ε transitions.

Algorithm 1: ε-ClosureInput: A set T of states of a NFA.Output: Tε = ε− closure(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ε

Page 77: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 77 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •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 isfinite. Hence the algorithm always terminates for any NFA N .

• Time complexity: O(N)

Page 78: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 78 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Recognition using NFAThe following algorithm may be used to recognize a string using a NFA.

Algorithm 2: Recognition using NFAInput: A string x ∈ A∗.Output: BooleanRECOGNITION USING NFA(x)(1) S := ε−CLOSURE({q0}).(2) a := nextchar(x)(3) while a 6= end of string(4) S := ε−CLOSURE(S

a−→)(5) a := nextchar(x)(6) endwhile(7) return S ∩ F 6= ∅In the above algorithm we extend our notation for targets of transitionsto include sets of sources. Thus

Sa−→= {q′ | ∃q ∈ S : q

a−→ q′}

Page 79: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 79 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Analysis of Recognitionusing 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 lengthof 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 au-tomaton to reduce the space required.

Page 80: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 80 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Page 81: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 81 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Page 82: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 82 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

3.3. Deterministic Finite Automata (DFA)

Lecture 06Conversion of NFAs to

DFAs

Page 83: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 83 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Deterministic FiniteAutomata

• A deterministic finite automaton (DFA) is a NFA in which

1. there are no transitions on ε and2. −→ yields a at most one target state for each source state and

symbol from A i.e. −→ is no longer a relation but a function ofthe form

−→: Q× A→ Q

• Clearly if every regular expression had a DFA which accepts thesame language, all backtracking could be avoided.

Page 84: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 84 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Transition Tables of NFAsWe 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 eachletter of the alphabet there is a set of possible target states defined. Inthe 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 states or(b) a set consisting of a single state or(c) a set consisting of more than one state or

Page 85: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 85 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Transition Tables of DFAsIn 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(b) or there is a transition to a unique state q′.

The recognition problem for the same language of strings becomes sim-pler and would work faster (it would have no back-tracking) if the NFAcould be converted into a DFA accepting the same language.

Page 86: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 86 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

NFA to DFALet N = 〈QN , A ∪ {ε}, sN , FN ,−→N〉 be a NFA with

• QN the set of states of the NFA

• A the alphabet

• sN ∈ QN the start state of the NFA

• FN the final or accepting states of the NFA and

• −→N⊆ QN × A×QN the transition relation.

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

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

Page 87: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 87 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

The Subset Construction• The ε-closure of each NFA state is a set of NFA states with “simi-

lar” behaviour, since they make their transitions on the same inputsymbols though with different numbers of εs.

• Each state of the DFA refers to a subset of states of the NFA whichexhibit “similar” behaviour. Similarity of behaviour refers to thefact that they accept the same input symbols. The behaviour of twodifferent NFA states may not be “identical” because they may havedifferent numbers of ε transitions for the same input symbol.

• A major source of non-determinism is the presence of ε transitions.The use of ε−CLOSURE creates a cluster of similar states.

• Since the notion of acceptance of a string by an automaton, impliesfinding an accepting sequence even though there may be other non-accepting sequences, the non-accepting sequences may be ignoredand those non-accepting states may be clustered with the acceptingstates of the NFA. So two different states reachable by the samesequence of symbols may be also though to be similar.

Page 88: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 88 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

NFA to DFA constructionAlgorithm 3: Construction of DFA from NFA

Input: NFA N = 〈QN , A ∪ {ε}, sN , FN ,−→N〉Output: DFA D = 〈QD, A, sD, FD,−→D〉NFA TO DFA(N )(1) sD := ε−CLOSURE({sN});QD := {sD};FD := ∅;−→D:= ∅(2) U := {sD} // U is the set of unvisited states of the DFA(3) while U 6= ∅(4) Choose any qD ∈ U ;U := U − {qD}(5) foreach a ∈ A(6) q′D := ε−CLOSURE(qD

a−→N) // Note: qD ⊆ QN

(7) −→D:=−→D ∪{qDa−→ q′D}

(8) if q′D ∩ FN 6= ∅ then FD := FD ∪ {q′D}(9) if q′D 6∈ QD

(10) QD := QD ∪ {q′D}(11) U := U ∪ {q′D}(12) endif(13) endforeach(14) endwhile

Page 89: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 89 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Example-NFAConsider 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

Page 90: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 90 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

DeterminisingN(a|b)∗abb D(a|b)∗abb

EC0 = ε−CLOSURE(0) = {0, 1, 2, 3, 7}2

a−→N 4 and 7a−→N 8. So EC0

a−→D ε−CLOSURE(4, 8) = EC4,8.SimilarlyEC0

b−→D ε−CLOSURE(5) = EC5EC4,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 andEC5b−→D ε−CLOSURE(5)

EC4,8a−→D ε−CLOSURE(4, 8) = EC4,8 and EC4,8

b−→D

ε−CLOSURE(5, 9) = EC5,9EC5,9 = ε−CLOSURE(5, 9) = {5, 6, 7, 1, 2, 3, 9}EC5,9

a−→D ε−CLOSURE(4, 8) = EC4,8 and EC5,9b−→D

ε−CLOSURE(5, 10) = EC5,10EC5,10 = ε−CLOSURE(5, 10) = {5, 6, 7, 1, 2, 3, 10}EC5,10

a−→D ε−CLOSURE(4, 8) and EC5,10b−→ ε−CLOSURE(5)

Page 91: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 91 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Final DFA

EC4,8

a

EC5,9

D(a|b)*abb

b

0EC

EC5 EC5,10

a

b

b

a a

b

a

b

D(a|b)∗abb Input SymbolState 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,9

Page 92: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 92 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Scanning: 5

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

(

* 10 11 12

14

*

if identifier

real

real

error comment

whitespace

error

int

identifierot

her

char

s

any char

)

The Big Picture

Page 93: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 93 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Lexical Analysis: Problems1. 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 commentsii. a NFA for these modified C comments

iii. a corresponding DFA for modified C comments

4. Many systems such as Windows XP and Linux recognize commands, filenames and folder names by thetheir shortest unique prefix. Hence given the 3 commands chmod, chgrp and chown, their shortest uniqueprefixes are respectively chm, chg and cho. A user can type the shortest unique prefix of the command andthe 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 eachof the above commands.

(b) Suppose the set of commands also includes two more commands cmp and cmpdir, state how you willinclude such commands also in your DFA where one command is a prefix of another.

Page 94: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 94 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Page 95: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 95 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

4. Syntax Analysis

Lecture 07Syntax Analysis

Page 96: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 96 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Syntax AnalysisConsider 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 thoughthe actual expression is very long).

• However, P cannot actually be represented by a regular expression

• A regular expression is not powerful enough to represent languageswhich require parenthesis matching to arbitrary depths.

• All high level programming languages require an underlying lan-guage of expressions which require parentheses to be nested andmatched to arbitrary depth.

Page 97: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 97 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Page 98: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 98 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Page 99: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 99 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

4.1. Context-Free Grammars

Lecture 08Context-Free Grammars

Page 100: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 100 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

CF-Grammars: DefinitionA context-free grammar (CFG) G = 〈N, T , P, S〉 consists of

• a set N of nonterminal symbols,

• a set T of terminal symbols or the alphabet,

• a set P of productions or rewrite rules,

• each production is of the form X −→ α, where

– X ∈ N is a nonterminal and– α ∈ (N ∪ T )∗ is a string of terminals and nonterminals

• a start symbol S ∈ N .

Page 101: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 101 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

CFG: ExampleG = 〈{S}, {a, b}, P, S〉, where S −→ ab and S −→ aSb are theonly productions in P .Derivations look like this:

S ⇒ ab

S ⇒ aSb⇒ aabb

S ⇒ aSb⇒ aaSbb⇒ aaabbb

L(G), the language generated by G is {anbn|n > 0}.

Actually can be proved by induction on the length and structure ofderivations.

Page 102: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 102 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

CFG: Empty wordG = 〈{S}, {a, b}, P, S〉, where S −→ SS | aSb | εgenerates all sequences of matching nested parentheses, including theempty 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!

Page 103: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 103 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

CFG: Derivation trees 1Derivation sequences

• put an artificial order in which productions are fired.

• instead look at trees of derivations in which we may think of pro-ductions as being fired in parallel.

• There is then no highlighting in red to determine which copy of anonterminal was used to get the next member of the sequence.

• Of course, generation of the empty word εmust be shown explicitlyin the tree.

Page 104: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 104 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

CFG: Derivation trees 2

S

S

S

S

S S

S

S

a b a b

a bε

ε

ε

Derivation tree of

abaabb

ε

Page 105: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 105 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

CFG: Derivation trees 3

S

S

S

S

S

S

S

a b

a b

ε

ε

S

a b

εAnother

Derivation tree of

abaabb

Page 106: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 106 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

CFG: Derivation trees 4

S

S

S

S

S

S

a b

a b

ε

Sa b

ε

S

ε

Yet another

Derivation tree ofabaabb

Page 107: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 107 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Page 108: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 108 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Page 109: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 109 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

4.2. Ambiguity

Lecture 09Ambiguity Disambiguation

Page 110: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 110 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Ambiguity: 1E → I | C | E+E | E∗EI → y | zC → 4

Consider the sentence y + 4 ∗ z.

EE

Page 111: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 111 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Ambiguity: 2E → I | C | E+E | E∗EI → y | zC → 4

Consider the sentence y + 4 ∗ z.

E

E E*

E

EE +

Page 112: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 112 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Ambiguity: 3E → I | C | E+E | E∗EI → y | zC → 4

Consider the sentence y + 4 ∗ z.

E

E

E

E*

E+

I

E

E

E

E +

EI

*

Page 113: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 113 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Ambiguity: 4E → I | C | E+E | E∗EI → y | zC → 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

*

Page 114: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 114 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Ambiguity: 5E → I | C | E+E | E∗EI → y | zC → 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

*

Page 115: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 115 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Left-most Derivation 1Left-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

Page 116: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 116 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Left-most Derivation 2Left-most derivation of y+4*z corresponding to the second derivationtree.

E ⇒E∗E ⇒E+E∗E ⇒I+E∗E ⇒y+E∗E ⇒y+C∗E ⇒y + 4∗E ⇒y + 4∗I ⇒y + 4 ∗ z

Page 117: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 117 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Right-most Derivation 1Right-most derivation of y+4*z corresponding to the first derivationtree.

E ⇒E+E ⇒E+E∗E ⇒E+E∗I ⇒E+E∗z ⇒E+C∗z ⇒E+4 ∗ z ⇒I+4 ∗ z ⇒y + 4 ∗ z

Page 118: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 118 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Right-most Derivation 2Right-most derivation of y+4*z corresponding to the second derivationtree.

E ⇒E∗E ⇒E∗I ⇒E∗z ⇒E+E∗z ⇒E+C∗z ⇒E+4 ∗ z ⇒I+4 ∗ z ⇒y + 4 ∗ z

Page 119: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 119 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Characterizing AmbiguityThe following statements are equivalent.

• A CFG is ambiguous if some sentence it generates has more thanone derivation tree

• A CFG is ambiguous if there is a some sentence it generates withmore than one left-most derivation

• A CFG is ambiguous if there is a some sentence it generates withmore than one right-most derivation

Page 120: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 120 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

DisambiguationThe only way to remove ambiguity is to change the grammar by intro-ducing some more non-terminal sysmbols and changing the productionrules. Consider the grammar with the following production rules

E → E+T | TT → T∗F | FF → I | CI → y | zC → 4

and compare it with the original grammar

Page 121: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 121 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •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

Compare it with the Left-most Derivation 1.There is no derivation corresponding to Left-most Derivation 2

Page 122: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 122 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Right-most Derivation 1’Right-most derivation of y+4*z corresponding to the first derivationtree.

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

Page 123: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 123 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Context-Free Grammars: Problems1. 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 overthe terminal symbols a and b.

2. Matching brackets.

(a) Design a context-free grammar to generate sequences of matching brackets when the set of terminalsconsists of three pairs of brackets {(, ), [, ], {, }}.

(b) If your grammar is ambiguous give two rightmost derivations of the same string and draw the twoderivation 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.

3. Design an unambiguous grammar for the expression language on integers consisting of expressions made upof operators +, -, *, /, % and the bracketing symbols ( and ), assuming the usual rules of precedence amongoperators that you have learned in school.

4. Modify the above grammar to include the exponentiation operator ˆ which has a higher precedence than theother operators and is right-associative.

5. How will you modify the grammar above to include the unary minus operator - where the unary minus hasa higher precedence than other operators?

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

Page 124: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 124 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Page 125: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 125 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

4.3. Shift-Reduce Parsing

Lecture 10Introduction to Parsing

Page 126: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 126 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Overview of ParsingSince

• parsing requires the checking whether a given token stream con-forms to the rules of the grammar and

• since a context-free grammar may generate an infinite number ofdifferent strings

any parsing method should be guided by the given input (token) string,so that a deterministic strategy may be evolved.

Page 127: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 127 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Parsing MethodsTwo kinds of parsing methods

Top-down parsing Try to generate the given input sentence from thestart symbol of the grammar by applying the production rules.

Bottom-up parsing Try to reduce the given input sentence to the startsymbol by applying the rules in reverse

In general top-down parsing requires long look-aheads in order to doa deterministic guess from the given input token stream. On the otherhand bottom-up parsing yields better results and can be automated bysoftware tools.

Page 128: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 128 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Reverse of Right-mostDerivations

The result of a Bottom-Up Parsing technique is usually to produce areverse of the right-most derivation of a sentence.Example For the grammar and corresponding to the right-most deriva-tion 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 ⇐

Page 129: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 129 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Bottom-Up Parsing StrategyThe main problem is to match parentheses of arbitrary depths. Thisrequires a stack data structure to do the parsing so that unbounded nestedparentheses and varieties of brackets may be matched.Our basic parsing strategy is going to be based on a technique calledshift-reduce parsing.

shift. Refers to moving the next token from the input token stream intoa parsing stack.

reduce. Refers to applying a production rule in reverse i.e. given aproduction X → α we reduce any occurrence of α in the parsingstack to

Page 130: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 130 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Parsing: 0−

/

b ( )

a −

a

a / b

r1. E E T

r2 E T

r3 T T D

r4 T D

r5 D | | E

Page 131: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 131 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Parsing: 1−

/

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 whenreduce isimpossible

Page 132: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 132 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Parsing: 2−

/

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

Page 133: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 133 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Parsing: 3−

/

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

Page 134: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 134 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Parsing: 4−

/

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

Page 135: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 135 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Parsing: 5−

/

b ( )a

a / b

r1. E E T

r2 E T

r3 T T D

r4 T D

r5 D | | E

E

Shift

Page 136: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 136 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Parsing: 6−

/

b ( )a

/ b

r1. E E T

r2 E T

r3 T T D

r4 T D

r5 D | | E

E

Shifta

Page 137: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 137 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Parsing: 7−

/

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

Page 138: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 138 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Parsing: 8−

/

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

Page 139: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 139 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Parsing: 8a−

/

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−

Page 140: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 140 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Parsing: 9a−

/

b ( )a

/ b

r1. E E T

r2 E T

r3 T T D

r4 D

r5 D | | E

EE

T

Reduce by r1

Page 141: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 141 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Parsing: 10a−

/

b ( )a

b

/

r1. E E T

r2 E T

r3 T T D

r4 D

r5 D | | E

EE

T

Shift

Page 142: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 142 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Parsing: 11a−

/

b ( )a

/

r1. E E T

r2 E T

r3 T T D

r4 D

r5 D | | E

EE

T

Shiftb

Page 143: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 143 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Parsing: 12a−

/

b ( )a

/

r1. E E T

r2 E T

r3 T T D

r4 D

r5 D | | E

EE

T

D Reduce by r5

Page 144: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 144 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Parsing: 13a−

/

b ( )a

/

r1. E E T

r2 E T

r3 T T D

r4 D

r5 D | | E

EE

T

T Reduce by r4

Page 145: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 145 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Parsing: 14a−

/

b ( )a

/

r1. E E T

r2 E T

r3 T T D

r4 D

r5 D | | E

EE

T

E Reduce by r2Stu

ck!

Get back!

Page 146: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 146 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Parsing: 14b−

/

b ( )a

/

r1. E E T

r2 E T

r3 T T D

r4 D

r5 D | | E

EE

T

E Reduce by r2Get back!

Page 147: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 147 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Parsing: 13b−

/

b ( )a

/

r1. E E T

r2 E T

r3 T T D

r4 D

r5 D | | E

EE

T

T Reduce by r4Get back!

Page 148: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 148 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Parsing: 12b−

/

b ( )a

/

r1. E E T

r2 E T

r3 T T D

r4 D

r5 D | | E

EE

T

D Reduce by r5Get back!

Page 149: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 149 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Parsing: 11b−

/

b ( )a

/

r1. E E T

r2 E T

r3 T T D

r4 D

r5 D | | E

EE

T

ShiftbGet back!

Page 150: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 150 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Parsing: 10b−

/

b ( )a

b

/

r1. E E T

r2 E T

r3 T T D

r4 D

r5 D | | E

EE

T

ShiftGet back!

Page 151: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 151 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Parsing: 9b−

/

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 towhere youonce belonged!

Page 152: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 152 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Parsing: 8b−

/

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 insteadof reduce here!

Principle:modified

Reduce whenever possible, butbut depending upon

lookahead

Shift−reduce

conflict

Page 153: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 153 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Parsing: 8−

/

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

Page 154: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 154 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Parsing: 9−

/

b ( )a

/

b

r1. E E T

r2 E T

r3 T T D

r4 D

r5 D | | E

EE

T

T

Shift

Page 155: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 155 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Parsing: 10−

/

( )a

/

r1. E E T

r2 E T

r3 T T D

r4 D

r5 D | | E

EE

T

T

Shift

b

b

Page 156: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 156 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Parsing: 11−

/

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

Page 157: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 157 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Parsing: 12−

/

b ( )a

r1. E E T

r2 E T

r3 T T D

r4 D

r5 D | | E

EE

T

T Reduce by r3−

Page 158: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 158 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Parsing: 13−

/

b ( )a

r1. E E T

r2 E T

r3 T T D

r4 D

r5 D | | E

EE

T

Reduce by r1

Page 159: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 159 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Lecture 11Bottom-Up Parsing

Page 160: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 160 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Parse Trees: 0shift-reduce parsing

−r1. E E T

r2 E T

/r3 T T D b ( )aD | | Er5

r4 T D

a− / ba

Page 161: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 161 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •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

Page 162: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 162 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •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

Page 163: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 163 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •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

Page 164: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 164 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •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

Page 165: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 165 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •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

Page 166: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 166 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •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

Page 167: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 167 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •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

Page 168: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 168 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •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

Page 169: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 169 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •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

Page 170: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 170 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •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

Page 171: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 171 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •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

Page 172: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 172 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •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

Page 173: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 173 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •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 ofJava.

• Shift-reduce conflicts if any, are automatically detected and reportedby the parser-generator.

• Shift-reduce conflicts may be avoided by suitably redesigning thecontext-free grammar.

Page 174: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 174 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Parsing: Summary: 2• Very often shift-reduce conflicts may occur because of the prefix

problem. In such cases many parser-generators resolve the conflictin favour of shifting.

• There is also a possiblility of reduce-reduce conflicts. This usuallyhappens when there is more than one nonterminal symbol to whichthe contents of the stack may reduce.

• A minor reworking of the grammar to avoid redundant non-terminalsymbols will get rid of reduce-reduce conflicts.

The Big Picture

Page 175: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 175 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Page 176: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 176 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Page 177: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 177 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

4.4. LR(0) Parsing

Lecture 12LR(0) Parsing

Page 178: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 178 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Parsing Problems 1The 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.

Page 179: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 179 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Parsing Problems 2But for a very large program it may be impossible to look at all the inputbefore taking a decision. So clearly the parser can look at only a limitedamount of the input to take a decision. SoThe next question:

How much of the input token stream would the parser require?

Disregarding the very next input token as always available, the length ofthe extra amount of input required for a shift-reduce decision is calledthe lookahead.

Page 180: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 180 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Parsing Problems 3Once 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 andattempt to reach the start symbol of the grammar only through re-ductions and

in case of an invalid sentence that a grammatical error has occurred inthe parsing process

To solve this problem we augment every grammar with a new start sym-bol S and a new terminal token $ and augment the grammar with a newspecial rule. For our previous grammar we have the new rule

S → E$

Page 181: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 181 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Augmented GrammarConsider the following (simplified) augmented grammar with a singlebinary operator− and parenthesis. We also number the rules.

1. S → E$2. E → E−T3. E → T4. T → a5. T → (E)

Page 182: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 182 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

LR(0) LanguagesLR(0) languages are those context-free languages that may be parsed bytaking deterministic shift-reduce decisions only based on the contents ofthe 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 tooweak for most high-level programming languages.

• But understanding the LR(0) parsing method is most crucial for un-derstanding other more powerful LR-parsing methods which requirelookaheads for deterministic shift-reduce decision-making

Page 183: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 183 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

LR-Parsing InvariantIn any LR-parsing technique the following invariant holds.

For any syntactically valid sentence gen-erated by the augmented grammar, theconcatenation of the stack contents withthe rest of the input gives a sentential formof a rightmost derivation.Hence given at any stage of the parsing if α ∈ (N ∪ T )∗ is the contentsof the parsing stack and x ∈ T ∗$ is the rest of the input that has not yetbeen read, then αx is a sentential form of a right-most derivation.

Page 184: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 184 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

LR(0) ItemAn LR(0) item consists of an LR(0) production rule with a specialmarker N on the right hand side of rule.

• The marker is different from any of the terminal or nonterminal sym-bols of the grammar.

• The marker separates the contents of the stack from the expectedform of some prefix of the rest of the input.

• Given a rule X → α, where X is a nonterminal symbol and α isa 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 – onefor each position in α.

Page 185: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 185 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

What does an LR(0) itemsignify?

The LR(0) itemX → βNγ

signifies that at some stage of parsing

• β is the string (of terminals and nonterminals) on the top of the stackand

• some prefix of the rest of the input can be generated by γ

so that whenever βγ appears on the stack, βγ may be reduced immedi-ately to X .

Page 186: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 186 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

LR0 Parsing StrategyThe 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 andthe 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.

Page 187: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 187 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Favourite ExampleConsider our favourite augmented grammar

1. S → E$2. E → E−T3. E → T4. T → a5. T → (E)

Page 188: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 188 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Rule 1: ItemsRule 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.

Page 189: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 189 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Rule 2: ItemsRule 2

R2. E → E−Thas the following items

I2.1 E → NE−TI2.2 E → EN−TI2.3 E → E−NTI2.4 E → E−TN

Page 190: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 190 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Rule 3: ItemsRule 3

R3. E → T

has just the itemsI3.1 E → NTI3.2 E → TN

Page 191: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 191 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Rule 4: ItemsRule 4

R4. T → a

has the itemsI4.1 T → NaI4.2 T → aN

Page 192: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 192 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Rule 5: ItemsRule 5

R5. T → (E)

has the itemsI5.1 T → N(E)I5.2 T → (NE)I5.3 T → (EN)I5.4 T → (E)N

Page 193: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 193 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Significance of I1.*I1.1 S → NE$. Hence

1. The parsing stack is empty and2. 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 and2. 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 and2. the stack contents may be reduced to the start symbol

Page 194: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 194 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

DFA States: Initial andFinal

• 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 symbolE fromthe initial state (corresponding to item I1.1) to the state correspond-ing 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 corre-sponding to item I1.2 to the accepting state corresponding to itemI1.3.

• There should be a reduce action using rule 1 when the DFA reachesthe state corresponding to item I1.3.

Page 195: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 195 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Input PossibilitiesConsider item I1.1.

1. How will a grammatically valid sentence input reduce to E$? Fromthe grammar it is obvious that this can happen only if the input is ofa 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 Tor

(b) If the entire input could be reduced to the form (E) then it couldbe 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 thati. α is a prefix that can be reduced to E, and

ii. β = −, andiii. γ is a suffix that can be reduced to Tthen it could be reduced to E−T

Page 196: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 196 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Closures of ItemsTheoretically each item is a state of a NFA. The above reasoning leadsto forming closures of items to obtain DFA states, in a manner similarto the the subset construction. Essentially all NFA states with similarinitial behaviours are grouped together to form a single DFA state.

NFA to DFA construction

Algorithm 4: Closures of ItemsInput: Set I of LR(0) items of a CFG with rule set POutput: Closure of I for a subset I ⊆ I of itemsCLOSURE(I)(1) repeat(2) foreach A→ αNXβ ∈ I(3) foreach X → γ ∈ P(4) I := I ∪ {X → Nγ}(5) endforeach(6) endforeach(7) until no more changes occur in I

Page 197: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 197 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

State Changes onNonterminals

As in the case of the NFA to DFA construction with each state transitionwe also need to compute closures on the target states.

Algorithm 5: Goto for a set of I of itemsInput: I ⊆ I and X ∈ NOutput: States of the DFAGOTO(I, X)(1) J := ∅(2) foreach A→ αNXβ ∈ I(3) J := J ∪ {A→ αXNβ}(4) endforeach(5) return CLOSURE(J )

Page 198: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 198 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

State S1S1 = CLOSURE({S → NE$})

= {S → NE$, E → NE−T,E → NT,T → Na, T → N(E)}

S1(−→ CLOSURE({T → (NE)}) = S2

S1E−→ CLOSURE({S → EN$, E → EN−T}) = S3

S1T−→ CLOSURE({E → TN}) = S7

S1a−→ CLOSURE({T → aN}) = S8

Page 199: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 199 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

State S2S2 = CLOSURE({T → (NE)})

= {T → N(E), E → NE−T,E → NT,T → Na, T → N(E)}

S2(−→ CLOSURE({T → (NE)}) = S2

S2E−→ CLOSURE({T → (EN), E → EN−T}) = S9

S2T−→ CLOSURE({E → TN}) = S7

S2a−→ CLOSURE({T → aN}) = S8

Page 200: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 200 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Other StatesS3 = CLOSURE({S → EN$, E → EN−T})

= {S → EN$, E → EN−T}However,

S3−−→ CLOSURE({E → E−NT})

andCLOSURE({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}

Page 201: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 201 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Example: DFAParsing 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

S3S6

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

Ta

−−

Page 202: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 202 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Example: Parsing TableDFA

States Input Nonterminalsa ( ) $ − S E T

S1 S8 S2 G3 G7S2 S8 S2 G9 G7S3 ACC S4S4 S8 S2 G5S5 R2 R2 R2 R2 R2S6 R1 R1 R1 R1 R1S7 R3 R3 R3 R3 R3S8 R4 R4 R4 R4 R4S9 S10 S4S10 R5 R5 R5 R5 R5

Note: All empty entries denote errors

Page 203: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 203 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Lecture 13LR(0) Parsing Contd.

Page 204: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 204 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Example Parse 1DFA Parsing Table

Page 205: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 204 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Example Parse 1DFA Parsing Table

S1 a$ Shift S8

Page 206: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 204 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Example Parse 1DFA Parsing Table

S1 a$ Shift S8S1 a S8 $ Reduce Rule 4

Page 207: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 204 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Example Parse 1DFA Parsing Table

S1 a$ Shift S8S1 a S8 $ Reduce Rule 4

S1 T $ Goto S7

Page 208: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 204 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Example Parse 1DFA Parsing Table

S1 a$ Shift S8S1 a S8 $ Reduce Rule 4

S1 T $ Goto S7

S1 T S7 $ Reduce Rule 3

Page 209: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 204 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Example Parse 1DFA Parsing Table

S1 a$ Shift S8S1 a S8 $ Reduce Rule 4

S1 T $ Goto S7

S1 T S7 $ Reduce Rule 3

S1 E $ Goto S3

Page 210: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 204 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Example Parse 1DFA Parsing Table

S1 a$ Shift S8S1 a S8 $ Reduce Rule 4

S1 T $ Goto S7

S1 T S7 $ Reduce Rule 3

S1 E $ Goto S3

S1 E S3 $ Accept

Page 211: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 205 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Example Parse 2: 1DFA Parsing Table

Page 212: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 205 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Example Parse 2: 1DFA Parsing Table

S1 a− (a− a)$ Shift S8

Page 213: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 205 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Example Parse 2: 1DFA Parsing Table

S1 a− (a− a)$ Shift S8

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

Page 214: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 205 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Example Parse 2: 1DFA Parsing Table

S1 a− (a− a)$ Shift S8

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

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

Page 215: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 205 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Example Parse 2: 1DFA 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

Page 216: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 205 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Example Parse 2: 1DFA 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

Page 217: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 206 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Example Parse 2: 2DFA Parsing Table

Page 218: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 206 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Example Parse 2: 2DFA Parsing Table

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

Page 219: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 206 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Example Parse 2: 2DFA Parsing Table

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

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

Page 220: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 206 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Example Parse 2: 2DFA Parsing Table

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

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

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

Page 221: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 206 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Example Parse 2: 2DFA Parsing Table

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

Page 222: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 206 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Example Parse 2: 2DFA Parsing Table

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

Page 223: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 207 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Example Parse 2: 3DFA Parsing Table

Page 224: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 207 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Example Parse 2: 3DFA Parsing Table

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

Page 225: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 207 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Example Parse 2: 3DFA Parsing Table

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

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

Page 226: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 207 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Example Parse 2: 3DFA Parsing Table

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

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

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

Page 227: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 207 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Example Parse 2: 3DFA Parsing Table

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

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

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

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

Page 228: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 207 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Example Parse 2: 3DFA Parsing Table

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

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

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

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

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

Page 229: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 208 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Example Parse 2: 4DFA Parsing Table

Page 230: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 208 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Example Parse 2: 4DFA Parsing Table

S1 E S3 − S4 ( S2 E S9 − S4 T )$ Go to S5

Page 231: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 208 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Example Parse 2: 4DFA Parsing Table

S1 E S3 − S4 ( S2 E S9 − S4 T )$ Go to S5

S1 E S3 − S4 ( S2 E S9 − S4 T S5 )$ ReduceRule 2

Page 232: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 208 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Example Parse 2: 4DFA Parsing Table

S1 E S3 − S4 ( S2 E S9 − S4 T )$ Go to S5

S1 E S3 − S4 ( S2 E S9 − S4 T S5 )$ ReduceRule 2

S1 E S3 − S4 ( S2 E )$ Go to S9

Page 233: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 208 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Example Parse 2: 4DFA Parsing Table

S1 E S3 − S4 ( S2 E S9 − S4 T )$ Go to S5

S1 E S3 − S4 ( S2 E S9 − S4 T S5 )$ ReduceRule 2

S1 E S3 − S4 ( S2 E )$ Go to S9

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

Page 234: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 208 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Example Parse 2: 4DFA Parsing Table

S1 E S3 − S4 ( S2 E S9 − S4 T )$ Go to S5

S1 E S3 − S4 ( S2 E S9 − S4 T S5 )$ ReduceRule 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

Page 235: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 209 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Example Parse 2: 5DFA Parsing Table

Page 236: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 209 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Example Parse 2: 5DFA Parsing Table

S1 E S3 − S4 T $ Go to S5

Page 237: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 209 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Example Parse 2: 5DFA Parsing Table

S1 E S3 − S4 T $ Go to S5

S1 E S3 − S4 T S5 $ Reduce Rule 2

Page 238: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 209 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Example Parse 2: 5DFA Parsing Table

S1 E S3 − S4 T $ Go to S5

S1 E S3 − S4 T S5 $ Reduce Rule 2

S1 E $ Go to S3

Page 239: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 209 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Example Parse 2: 5DFA Parsing Table

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

Page 240: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 210 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

LR0 Parsing: Problems1. 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 hasmore 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) parserfor comments in C.

Page 241: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 211 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Lecture 14LR(0) to SLR

Page 242: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 212 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

LR(0) withRight-Association

Consider the following grammar

1. S → E$2. E → P ˆE3. E → P4. P → a5. P → (E)

The following items make up the initial state S1 of the DFA

I1.1 S → NE$I2.1 E → NP ˆEI3.1 E → NPI4.1 P → NaI5.1 P → N(E)

Page 243: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 213 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Shift-Reduce Conflicts inLR(0)

There is a transition on the nonterminal P to the state S2 which is madeup of the following items.

I2.2 E → PNˆEI3.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 ac-tions took place regardless of the input symbol. Clearly now that prin-ciple will have to be modified.The parsing table in this case would have a shift action if the input instate S2 is a ˆ and a reduce action for all other input symbols.

Page 244: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 214 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

FOLLOW SetsWe construct for each non-terminal symbol a set of terminal symbolsthat can follow this non-terminal in any rightmost derivation. In theprevious grammar we have

FOLLOW(E) = {$, )}FOLLOW(P ) = {ˆ}

Depending upon the input symbol and whether it appears in the FOL-LOW set of the non-terminal under question we resolve the shift-reduceconflict.This modification to LR(0) is called Simple LR (SLR) parsing method.However SLR is not powerful enough for many useful grammar con-structions that are encountered in many programming languages.

Page 245: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 215 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Computing FIRST SetsIn order to compute FOLLOW sets we require FIRST sets of sententialforms 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 thatY1Y2 · · ·Yi ⇒ ε, FIRST(Yi+1) ⊆ FIRST(X).

Page 246: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 216 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Computing FOLLOW SetsOnce 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 augmentedgrammar.

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

Page 247: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 217 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

if-then-else vs. if-thenMost programming languages have two separate constructs if-thenand if-then-else. We abbreviate the keywords and use the follow-ing symbols

Tokens Symbolsif ithen telse e

booleans bother expressions a

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 E31. I → M 32. I → a41. U → i b t I 42. E → e I51. U → i b tM e U 52. E → ε61. M → i b tM eM71. M → a

Page 248: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 218 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Problems in LR parsing1. Prove that grammar G2 is ambiguous.

2. Construct the LR(0) parsing tables for both G1 and G2 and find allshift-reduce conflicts in the parsing table.

3. Construct the FOLLOW sets in each case and try to resolve the con-flicts.

4. Show that the following augmented grammar cannot be parsed (i.e.there are conflicts that cannot be resolved by FOLLOW sets) eitherby LR(0) or SLR parsers. (Hint First construct the LR(0) DFA).

1. S → E$2. E → L = R3. E → R4. L → ∗ R5. L → a6. R → L

Page 249: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 219 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

NullableA nonterminal symbol X is nullable if it can derive the empty string,i.e. X ⇒∗ ε.

Algorithm 6: Nullable.Input: CFG G = 〈N, T, S, P 〉Output: NULLABLE(N ∪ T )NULLABLE(N ∪ T )(1) foreach α ∈ N ∪ T(2) if α→ ε ∈ P then NULLABLE(α) := true(3) else NULLABLE(α) := false(4) endforeach(5) repeat(6) foreach X → α1 . . . αk ∈ P(7) if ∀i : 1 ≤ i ≤ k : NULLABLE(αi)(8) NULLABLE(X) := true(9) endif(10) endforeach(11) until NULLABLE(N ∪ T ) is unchanged

Page 250: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 220 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

FirstFIRST (α) is the set of terminal symbols that can be the first symbolof any string that α can derive, i.e. a ∈ FIRST (α) if and only if thereexists a derivation α⇒∗ ax for any string of terminals x.

Algorithm 7: First.Input: CFG G = 〈N, T, S, P 〉Output: FIRST (N ∪ T )FIRST(N )(1) For each a ∈ T , FIRST (a) := {a}(2) For each X ∈ N , FIRST (X) := ∅(3) repeat(4) foreach X → α1 . . . αk ∈ P(5) for i := 1 to k(6) if ∀i′ : 1 ≤ i′ < i : NULLABLE(αi′)(7) FIRST (X) := FIRST (X) ∪ FIRST (αi)(8) endif(9) endfor(10) endforeach(11) until FIRST (N ∪ T ) sets are all unchanged

Page 251: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 221 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

First And FollowNotice that if X → αZβ is a production then one cannot ignore theFIRST (Z) in computing FIRST (X) especially if α⇒∗ ε. Furtherif Z is also nullable then FIRST (β) ⊆ FIRST (X).FOLLOW (X) for any nonterminal symbol X is the set of terminalsymbols 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 tothe right of X in a rightmost derivation.Notice that if there exists a a rightmost derivation of the form

S ⇒∗ · · ·Xα1 . . . αka · · · ⇒∗

such that α1, . . . , αk are all nullable then again we have

S ⇒∗ · · ·Xα1 . . . αka · · · ⇒∗ · · ·Xa · · · ⇒∗

Page 252: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 222 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Computing FollowAlgorithm 8: Follow.

Input: CFG G = 〈N, T, S, P 〉Output: FOLLOW (N)FOLLOW(N )(1) For each α ∈ N ∪ T , FOLLOW (α) := ∅(2) repeat(3) foreach X → α1 . . . αk ∈ P(4) for i := 1 to k(5) if ∀i′ : i + 1 ≤ i′ ≤ k : NULLABLE(αi′)(6) FOLLOW (αi) := FOLLOW (αi)∪FOLLOW (X)(7) endif(8) for j := i + 1 to k(9) if ∀i′ : i + 1 ≤ i′ ≤ j − 1 : NULLABLE(αi′)(10) FOLLOW (αi) := FOLLOW (αi) ∪ FIRST (αj)(11) endif(12) endfor(13) endfor(14) endforeach(15) until FOLLOW (N ∪ T ) sets are all unchanged

Page 253: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 223 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Lecture 20Concrete to Abstract

Page 254: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 224 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Page 255: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 225 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

5. Semantic Analysis

Syntax-Directed Definitions(SDD)

Syntax-Directed definitions are high-level specifications which specifythe evaluation of

1. various attributes

2. various procedures such as

• transformations• generating code• saving information• issuing error messages

They hide various implementation details and free the compiler writerfrom explicitly defining the order in which translation, transformations,and code generation take place.

Page 256: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 226 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

AttributesAn attribute can represent anything we choose e.g.

• a string

• a number

• 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 semanticrule associated with the production used at that node.

Page 257: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 227 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Kinds of AttributesThere 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 parsetree.

A syntax-directed definition that uses only synthesized attributes iscalled an S-attributed definition. See example

Inherited attributes An inherited attribute is one whose value dependsupon the values of the attributes of its parents or siblings in the parsetree.

Inherited attributes are convenient for expressing the dependence ofa language construct on the context in which it appears.

Page 258: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 228 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

What is Syntax-directed?A syntax-directed definition is a generalisation of a context-free gram-mar in which each grammar symbol has an associated set of attributes,partitioned into two subsets called synthesized and inherited attributes.

The various attributes are computed by so called semantic rules associ-ated with each production of the grammar which allows the computationof the various attributes.

These semantic rules are in general executed during parsing at the stagewhen a reduction needs to be performed by the given rule. Hence alongwith the reduction the corresponding semantic rules are also executed.

A parse tree showing the various attributes at each node is called anannotated parse tree.

Page 259: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 229 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Forms of SDDsIn 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 the gram-mar symbols of the production and

synthesized: a is a synthesized attribute of X (denoted X.a) or

inherited a is an inherited attribute of one of the grammar symbols ofα (denoted B.a if a is an attribute of B).

In each case the attribute a is said to depend upon the attributesa1, · · · , ak .

Page 260: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 230 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Attribute GrammarsAn attribute grammar is a syntax-directed definition in which the func-tions in semantic rules can have no side-effects.

In general for different occurrences of non-terminal symbols in the sameproduction will be distinguished by appropriate subscripts when defin-ing the semantic rules associated with the rule.

The following example illustrates the concept of a syntax-directed defi-nition using synthesized attributes.

Page 261: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 231 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Attribute Grammars:Example

Determining the values of arithmetic expressions. Consider a simpleattribute val associated with an expression

E0 → E1−T � E0.val := E1.val − T.val

E → T � E.val := T.val

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

T → F � T.val := F.val

F → (E) � F.val := E.val

F → n � F.val := n.val

Page 262: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 232 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Attributes: BasicAssumptions

• Terminal symbols are assumed to have only synthesized attributes.Their attributes are all supplied by the lexical analyser during scan-ning.

• The start symbol of the grammar can have only synthesized at-tributes.

• In the case of LR parsing with its special start symbol, the startsymbol cannot have any inherited attributes because

1. it does not have any parent nodes in the parse tree and2. it does not occur on the right-hand side of any production.

Page 263: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 233 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Abstract Syntax TreesThe construction of ASTs from concrete parse trees is another exam-ple of a transformation that can be performed using a syntax-directeddefinition that has no side-effects.Hence we define it using an attribute grammar.

Page 264: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 234 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Abstract Syntax: 0E → E−T | TT → T/F | FF → n | (E)

Suppose we want to evaluate an expression (4− 1)/2. What we actu-ally want is a tree that looks like this:

4 1

2−−

/

Page 265: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 235 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Evaluation: 0

4 1

2−−

/

Page 266: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 236 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Evaluation: 1

4 1

2−−

/

Page 267: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 237 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Evaluation: 2

2

/

3

Page 268: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 238 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Evaluation: 3

2

/

3

Page 269: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 239 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Evaluation: 4

1

But what we actually get during parsing is a tree that looks like . . .

Page 270: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 240 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Abstract Syntax: 1. . . THIS! E

T

T F

/

nF

( )E

E T−

TF

Fn

n

n n

/

n−−

Page 271: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 241 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Abstract Syntax: 1Parsing produces a concrete syntax tree from the rightmost derivation.The syntax tree is concrete in the sense that

• It contains a lot of redundant symbols that are important or usefulonly during the parsing stage.

– punctuation marks– brackets of various kinds

• It makes no distinction between operators, operands, and punctua-tion symbols

On the other hand the abstract syntax tree (AST) contains no punctua-tions and makes a clear distinction between an operand and an operator.

Page 272: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 242 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Abstract Syntax: 2We use attribute grammar rules to construct the abstract syntax tree(AST)! from the parse tree.But in order to do that we first require two procedures for tree construc-tion.

makeLeaf(literal) : Creates a node with label literal and returns apointer to it.

makeBinaryNode(opr, opd1, opd2) : Creates a node with label opr(with fields which point to opd1 and opd2) and returns a pointer tothe newly created node.

Now we may associate a synthesized attribute called ptr with each ter-minal and nonterminal symbol which points to the root of the subtreecreated for it.

Page 273: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 243 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Abstract Syntax: 3E0 → E1−T � E0.ptr := makeBinaryNode(−, E1.ptr, T.ptr)

E → T � E.ptr := T.ptr

T0 → T1/F � T0.ptr := makeBinaryNode(/, T1.ptr, F.ptr)

T → F � T.ptr := F.ptr

F → (E) � F.ptr := E.ptr

F → n � F.ptr := makeLeaf (n.val)

The

Big Picture

Page 274: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 244 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Semantic Analysis: 1• Every Programming langauge can be used to program any com-

putable function, assuming of course, it has

– unbounded memory, and– unbounded time

• The parser of a programming language provides the frameworkwithin which the target code is to be generated.

• The parser also provides a structuring mechanism that divides thetask of code generation into bits and pieces determined by the indi-vidual nonterminals and production rules.

• However, contex-free grammars are not powerful enough to repre-sent all computable functions. Example, the language {anbncn|n >0}.

Page 275: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 245 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Semantic Analysis: 2• There are context-sensitive aspects of a program that cannot be rep-

resented/enforced by a context-free grammar definition. Examplesinclude

– correspondence between formal and actual parameters– type consistency between declaration and use.– scope and visibility issues with respect to identifiers in a pro-

gram.

Page 276: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 246 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Page 277: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 247 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Synthesized Attributes: 0

ET F

T

T F

/

/

(

nF

( )E

E

) n

E T

TF

Fn

n

Page 278: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 248 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Synthesized Attributes: 1

ET F

T

T F

/

/

(

nF

( )E

E

) n

E T

TF

Fn

n

4

4

3 2 1

Synthesized Attributes

Page 279: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 249 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Synthesized Attributes: 2

ET F

T

T F

/

/

(

nF

( )E

E

) n

E T

TF

Fn

n

4

4

3 2 1

4

Synthesized Attributes

Page 280: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 250 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Synthesized Attributes: 3

ET F

T

T F

/

/

(

nF

( )E

E

) n

E T

TF

Fn

n

4

4

4

4

3 2 1

Synthesized Attributes

Page 281: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 251 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Synthesized Attributes: 4

ET F

T

T F

/

/

(

nF

( )E

E

) n

E T

TF

Fn

n

4

4

4

4

4

3 2 1

Synthesized Attributes

Page 282: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 252 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Synthesized Attributes: 5

ET F

T

T F

/

/

(

nF

( )E

E

) n

E T

TF

Fn

n

4

4

4

4

4

1

3 2 1

Synthesized Attributes

Page 283: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 253 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Synthesized Attributes: 6

ET F

T

T F

/

/

(

nF

( )E

E

) n

E T

TF

Fn

n

4

4

4

4

4

1

1

3 2 1

Synthesized Attributes

Page 284: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 254 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Synthesized Attributes: 7

ET F

T

T F

/

/

(

nF

( )E

E

) n

E T

TF

Fn

n

4

4

4

4

4

1

1

1

3 2 1

Synthesized Attributes

Page 285: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 255 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Synthesized Attributes: 8

ET F

T

T F

/

/

(

nF

( )E

E

) n

E T

TF

Fn

n

4

4

4

4

4

1

1

1

3 2 1

3

Synthesized Attributes

Page 286: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 256 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Synthesized Attributes: 9

ET F

T

T F

/

/

(

nF

( )E

E

) n

E T

TF

Fn

n

4

4

4

4

4

1

1

1

3

3 2 1

3

Synthesized Attributes

Page 287: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 257 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Synthesized Attributes: 10

ET F

T

T F

/

/

(

nF

( )E

E

) n

E T

TF

Fn

n

4

4

4

4

4

1

1

1

3

3

3 2 1

3

Synthesized Attributes

Page 288: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 258 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Synthesized Attributes: 11

ET F

T

T F

/

/

(

nF

( )E

E

) n

E T

TF

Fn

n

4

4

4

4

4

1

1

1

3 2

3

3 2 1

3

Synthesized Attributes

Page 289: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 259 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Synthesized Attributes: 12

ET F

T

T F

/

/

(

nF

( )E

E

) n

E T

TF

Fn

n

4

4

4

4

4

1

1

1

3 2

23

3 2 1

3

Synthesized Attributes

Page 290: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 260 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Synthesized Attributes: 13

ET F

T

T F

/

/

(

nF

( )E

E

) n

E T

TF

Fn

n

4

4

4

4

4

1

1

1

3 2

23

3 2 1

1

3

Synthesized Attributes

Page 291: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 261 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Synthesized Attributes: 14

ET F

T

T F

/

/

(

nF

( )E

E

) n

E T

TF

Fn

n

4

4

4

4

4

1

1

1

3 2

23

3 2 1

1

1

3

Synthesized Attributes

Page 292: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 262 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

An Attribute Grammar

E

T

T F

/

nF

( )E

E T−

TF

Fn

n4

4

4

4

1

1

1

3 2

23

1

1

3

E0 → E1−T � E0.val := sub(E1.val, T.val)

E → T � E.val := T.val

T0 → T1/F � T0.val := div(T1.val, F.val)

T → F � T.val := F.val

F → (E) � F.val := E.val

F → n � F.val := n.val

Page 293: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 263 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Evaluation of SynthesizedAttributes

During parsing synthesized attributes are are evaluated as follows:

1. Keep an attribute value stack along with the parsing stack.

2. Just before applying a reduction of the form Z → Y1 . . . Yk com-pute the attribute values ofZ from the attribute values of Y1, · · · , Yk

and place them in the same position on the attribute value stack cor-responding to the one where the symbolZ will appear on the parsingstack as a result of the reduction.

Page 294: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 264 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Lecture 21Inherited Attributes

Page 295: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 265 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Synthesized and InheritedAttributes

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 the gram-mar symbols of the production and

Synthesized: a is a synthesized attribute of X (denoted X.a) or

Inherited: a is an inherited attribute of one of the grammar symbols ofα (denoted B.b if b is an attribute of B).

In each case the attribute b is said to depend upon the the attributesa1, · · · , ak .

Page 296: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 266 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Inherited Attributes: 0C-style declarations generating int x,y, z.

D → T L T → int | floatL → 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

,

Page 297: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 267 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Inherited Attributes: 1C-style declarations generating int x,y, z.

D → T L T → int | floatL → 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,

Page 298: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 268 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Inherited Attributes: 2C-style declarations generating int x,y, z.

D → T L T → int | floatL → 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,

Page 299: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 269 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Inherited Attributes: 3C-style declarations generating int x,y, z.

D → T L T → int | floatL → 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

Page 300: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 270 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Inherited Attributes: 4C-style declarations generating int x,y, z.

D → T L T → int | floatL → 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

Page 301: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 271 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Inherited Attributes: 5T

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

Page 302: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 272 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Inherited Attributes: 6C-style declarations generating int x,y, z.

D → T L T → int | floatL → 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

Page 303: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 273 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Inherited Attributes: 7C-style declarations generating int x,y, z.

D → T L T → int | floatL → 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

Page 304: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 274 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

An Attribute Grammar

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 � L.in := T.type

T → int � T.type := int.int

T → float � T.type := float.f loat

L0 → L1,I � L1 := L0.in

L → I � I.in := L.in

I → id � id.type := I.in

Page 305: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 275 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

L-attributed DefinitionsA grammar is L-attributed if for each production of the form Y →X1 . . . Xk, each inherited attribute of the symbol Xj, 1 ≤ j ≤ k de-pends only on

1. the inherited attributes of the symbol Y and

2. the synthesized or inherited attributes of X1, · · · , Xj−1.

Page 306: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 276 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Why L-attributedness?Intuitively, if Xj.inh is an inherited attribute then

• it cannot depend on any synthesized attribute Y.syn of Y becauseit is possible that the computation of Y.syn requires the value ofXj.inh leading to circularity in the definition.

• if the value ofXj.inh depends upon the attributes of one or more ofthe symbols Xj+1, · · · , Xk then the computation of Xj.inh cannotbe performed just before the reduction by the rule Y → X1 . . . Xk

during parsing. Instead it may have to be postponed till the end ofparsing.

• it could depend on the synthesized or inherited attributes of any ofthe symbols X1 . . . Xj−1 since they would already be available onthe attribute value stack.

• it could depend upon the inherited attributes of Y because these in-herited attributes can be computed from the attributes of the symbolslying below X1 on the stack, provided these inherited attributes ofY are also L-attributed.

Page 307: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 277 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

A Non L-attributedDefinition

Our attribute grammar for C-style declarations is definitely L-attributed.However consider the following grammar for declarations in Pascal andML.

D → L:T � L.in := T.typeT → int � T.type := int.int

T → real � T.type := real.real

L0 → L1,I � L1 := L0.in

L → I � I.in := L.in

I → id � id.type := I.in

In the first semantic rule the symbol L.in is inherited from a symbol toits right viz. T.type and hence is not L-attributed.

Page 308: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 278 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Evaluating Non-L-attributedDefinitions

In many languages like ML which allow higher order functions as val-ues, a definition not being L-attributed may not be of serious concern.But in most other languages it is serious enough to warrant changing thegrammar of the language so as to replace inherited attributes by corre-sponding synthesized ones. The language of the grammar of Pascal andML declarations can be generated as follows:

D → idL � addtype(id, L.type)L → :T � L.in := T.type

L → ,id L � L0.type := L1.type;

addtype(id.L1.type)T → int � T.type := int.int

T → real � T.type := real.real

Page 309: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 279 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Dependency GraphsIn general, the attributes required to be computed during parsing couldbe synthesized or inherited and further it is possible that some synthe-sized attributes of some symbols may depend on the inherited attributesof some other symbols. In such a scenario it is necessary to construct adependency graph of the attributes of each node of the parse tree.

Page 310: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 280 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Dependency GraphConstruction

Algorithm 9: Dependency Graphs.Input: A parse tree of a CFG and the list of attributesOutput: A dependency graphDEPENDENCY GRAPH OF ATTRIBUTES()(1) foreach node n of the parse tree(2) foreach attribute a of node n(3) Create an attribute node n.a(4) endforeach(5) endforeach(6) foreach node n of the parse tree(7) foreach semantic rule a := f (b1, . . . , bk)(8) foreach i : 1 ≤ i ≤ k(9) Create a directed edge bi → a(10) endforeach(11) endforeach(12) endforeach

Page 311: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 281 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Page 312: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 282 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Page 313: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 283 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

6. Symbol Table

Symbol Table:1• The store house of context-sensitive and run-time information about

every identifier in the source program.

• All accesses relating to an identifier require to first find the attributesof the identifier from the symbol table

• Usually organized as a hash table – provides fast access.

• Compiler-generated temporaries may also be stored in the symboltable

Page 314: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 284 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Symbol Table:2Attributes stored in a symbol table for each identifier:

• type

• size

• scope/visibility information

• base address

• addresses to location of auxiliary symbol tables (in case of records,procedures, classes)

• address of the location containing the string which actually namesthe identifier and its length in the string pool

Page 315: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 285 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Symbol Table:3• A symbol table exists through out the compilation and run-time.

• Major operations required of a symbol table:

– insertion– search– deletions are purely logical (depending on scope and visibility)

and not physical

• Keywords are often stored in the symbol table before the compila-tion process begins.

Page 316: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 286 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Symbol Table:4Accesses to the symbol table at every stage of the compilation process,

Scanning: Insertion of new identifiers.

Parsing: Access to the symbol table to ensure that an operand exists(declaration before use).

Semantic analysis:• Determination of types of identifiers from declarations• type checking to ensure that operands are used in type-valid con-

texts.• Checking scope, visibility violations.

Page 317: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 287 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Symbol Table:5IR generation: . Memory allocation and relativea address calculation.

Optimization: All memory accesses through symbol table

Target code: Translation of relative addresses to absolute addresses interms of word length, word boundary etc.

The Big pictureai.e.relative to a base address that is known only at run-time

Page 318: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 288 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Symbol Tables: 6Symbol tables (or environments) are the repository of all the context-sensitive information needed for successful compilation and running ofprograms. They map identifiers to their types, locations, provide type,scope,information.

Many languages have a declaration/definition before use for identifiers.However, in the case of features like mutual recursion, having forwardreferences to names that are declared or defined later than their use isinevitable.

Page 319: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 289 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Implementation StylesThere are two styles of implementation of symbol-tables.

The functional style:

• As new scopes are entered during compilation, new modifiedenvironments are created• As scopes are exited the modified environments are discarded

(only logically, not physically) to return to the previous environ-ment.

All updates to the environment are therefore non-destructive.

The imperative style: As new scopes are entered, existing environ-ments are modified through destructive updates. An “undo” stackneeds to be maintained to ensure that on scope exit, the symbolspopped out have their latest bindings removed.

Either style may be used and this decision is independent of whether thesource language S or the language C (in which the compiler is written)is functional or imperative.

Page 320: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 290 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Keywords and ReservedWords

The symbol table may be initialised with the keywords of the language.

Keywords: Some languages with keywords allow identifiers with thesame name to be declared as variables. By the usual scope rule suchnames then refer to the variable rather than the keyword. Keywordsthat are not reserved can be identified only during the parsing phaseand not during the lexical phase because the context in which theword is used can be identified only during the parsing phase.

Reserved words: Some languages reserve their keywords so that thesenames cannot be used as names of variables in the program. Thsidecision considerably simplifies the compilation process, becausethese words can be identified during the lexical phase of the compi-lation without creating any other confusion.

Page 321: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 291 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Scope and Visibility• Most languages use the rule that inner declarations of any identifier

hide all outer declarations of the same identifier.

• Record structures and modules have some of the properties of scope,but record fields and variables declared in modules do not share thevisibility properties of scope. Each record or module defines a newscope but the visibility of variables in a module (similarly the fieldnames of the record) extends beyond the scope of the module insidewhich they are declared or defined.

Page 322: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 292 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Page 323: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 293 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Page 324: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 294 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

7. Code Generation

Intermediate RepresentationIntermediate representations are important for reasons of portability.

• (more or less) independent of specific features of the high-level lan-guage.Example. Java byte-code for any high-level language.

• (more or less) independent of specific features of any particular tar-get architecture (e.g. number of registers, memory size)

– number of registers– memory size– word length

Page 325: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 295 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

IR Properties: 11. 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 tomost high-level programming languages.How high can you rise?

3. To ensure portability

• an unbounded number of variables and memory locations• no commitment to Representational Issues

4. To ensure type-safety

• memory locations are also typed according to the data they maycontain,• no commitment is made regarding word boundaries, and the

structure of individual data items.

Next

Page 326: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 296 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •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:1

Page 327: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 297 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •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 aspecific meaning).

Back to IR Properties:1

Page 328: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 298 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •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:1

Page 329: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 299 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

A typical instruction set: 1Three address code: A suite of instructions. Each instruction has at most3 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) oper-ation.

If an operation requires less than 3 operands then one or more of theoperands is made null.

Page 330: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 300 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

A typical instruction set: 2• Assignments (LOAD-STORE)

• Jumps (conditional and unconditional)

• Procedures and parameters

• Arrays and array-indexing

• Pointer Referencing and Dereferencing

Page 331: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 301 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

A typical instruction set: 2• 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

Page 332: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 302 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

A typical instruction set: 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

Page 333: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 303 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

A typical instruction set: 2• 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

Page 334: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 304 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

A typical instruction set: 2• 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-valueor r-value is desired. x and y are always simple variables

• Pointer Referencing and Dereferencing

Page 335: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 305 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

A typical instruction set: 2• 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

Page 336: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 306 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Pointersx

x

*y

y

@z

*x

*x @z

*zz

*zz

x := ^y

x := *y

*x := y

*y@y

@z

*z

*z

x y

yx

x y

z

x

yy

*y

*y*y

@zz

Page 337: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 307 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

IR: Generation• Can be generated by recursive traversal of the abstract syntax tree.

• Can be generated by syntax-directed translation as follows:For every non-terminal symbolN in the grammar of the source lan-guage there exist two attributes

N.place , which denotes the address of a temporary variable wherethe result of the execution of the generated code is stored

N.code , which is the actual code segment generated.

• In addition a global counter for the instructions generated is main-tained as part of the generation process.

• It is independent of the source language but can express target ma-chine operations without committing to too much detail.

Page 338: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 308 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

IR: InfrastructureGiven an abstract syntax tree T, with T also denoting its root node.

T.place address of temporary variable where result of execution of theT is stored.

newtemp returns a fresh variable name and also installs it in the symboltable 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 codewhich may be the target of a jump.

emit emits an instructions (regarded as a string).

Page 339: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 309 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

IR: InfrastructureColour and font coding of IR code generation

• Green: Nodes of the Abstract Syntax Tree

• Brown: Characters and strings of the IntermediateRepresentation

• Red: Variables and data structures of the language in which the IRcode generator is written

• blue: Names of relevant procedures used in IR code generation.

• Black: All other stuff.

Page 340: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 310 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

IR: Example

E → id �

E.place := id.place;E.code := emit()

E0 → E1 − E2 �

E0.place := newtemp;E0.code := E1.code

|| E2.code|| emit(E0.place := E1.place− E2.place)

Page 341: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 311 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

IR: Example

S → id := E �

S.code := E.code|| emit(id.place:=E.place)

S0 → while E do S1 �

S0.begin := newlabel;S0.after := newlabel;S0.code := emit(S0.begin:)

|| E.code|| emit(if E.place= 0 goto S0.after)|| S1.code|| emit(gotoS0.begin)|| emit(S0.after:)

Page 342: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 312 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

IR: Example

S → id := E �

S.code := E.code|| emit(id.place:=E.place)

S0 → while E do S1 �

S0.begin := newlabel;S0.after := newlabel;S0.code := emit(S0.begin:)

|| E.code|| emit(if E.place= 0 goto S0.after)|| S1.code|| emit(gotoS0.begin)|| emit(S0.after:)

Page 343: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 313 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

IR: GenerationWhile generating the intermediate representation, it is sometimes nec-essary to generate jumps into code that has not been generated as yet(hence the address of the label is unknown). This usually happens whileprocessing

• forward jumps

• short-circuit evaluation of boolean expressions

It is usual in such circumstances to either fill up the empty label entriesin a second pass over the the code or through a process of backpatching(which is the maintenance of lists of jumps to the same instruction num-ber), wherein the blank entries are filled in once the sequence numberof the target instruction becomes known.

Page 344: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 314 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Page 345: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 315 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Page 346: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 316 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

8. Runtime Structure

A Calling ChainMain program

Globals

Procedure P2Locals 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

Page 347: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 317 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Run-time Structure: 1Main 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

Page 348: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 318 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Run-time Structure: 2Main 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 MainDynamic link to Main

Static link to Main

Body of P21

Page 349: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 319 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Run-time Structure: 3Main 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

Page 350: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 320 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Run-time Structure: 4Main 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

Page 351: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 321 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Run-time Structure: 5Main 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

Page 352: Code Generation Runtime Structure ECE 726: Home Page …sak/courses/cdp/eth09-10/eth09-10... · 2016-01-04 · Lecture 02 Lexical Analysis. Index to Lectures Introduction Lexical

Index to Lectures

Introduction

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol Table

Code Generation

Runtime Structure

Home Page

Title Page

JJ II

J I

Page 322 of 322

Go Back

Full Screen

Close

Quit

•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit