course overview

23
1 Syntax Analysis (Chapter 4) Course Overview PART I: overview material 1 Introduction 2 Language processors (tombstone diagrams, bootstrapping) 3 Architecture of a compiler PART II: inside a compiler 4 Syntax analysis 5 Contextual analysis 6 Runtime organization 7 Code generation PART III: conclusion 8 Interpretation 9 Review

Upload: conley

Post on 06-Jan-2016

20 views

Category:

Documents


0 download

DESCRIPTION

Course Overview. PART I: overview material 1Introduction 2Language processors (tombstone diagrams, bootstrapping) 3Architecture of a compiler PART II: inside a compiler 4Syntax analysis 5Contextual analysis 6Runtime organization 7Code generation PART III: conclusion - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Course Overview

1Syntax Analysis (Chapter 4)

Course Overview

PART I: overview material1 Introduction

2 Language processors (tombstone diagrams, bootstrapping)

3 Architecture of a compiler

PART II: inside a compiler4 Syntax analysis

5 Contextual analysis

6 Runtime organization

7 Code generation

PART III: conclusion8 Interpretation

9 Review

Page 2: Course Overview

2Syntax Analysis (Chapter 4)

Nullable, First sets (starter sets), and Follow sets

• A non-terminal is nullable if it derives the empty string

• First(N) or starters(N) is the set of all terminals that can begin a sentence derived from N

• Follow(N) is the set of terminals that can follow N in some sentential form

Next we will see algorithms to compute each of these.

Page 3: Course Overview

3Syntax Analysis (Chapter 4)

Algorithm for computing Nullable

For each terminal tNullable(t) = false

For each non-terminal NNullable(N) = is there a production N ::= ?

RepeatFor each production N ::= x1 x2 x3 … xn

If Nullable(xi) for all of xi then set Nullable(N) to true

Until nothing new becomes Nullable

Page 4: Course Overview

4Syntax Analysis (Chapter 4)

Generalizing the definition of Nullable

Define Nullable(x1 x2 x3 … xn) as:

if n==0 then

true

else if !Nullable(x1) then

false

else

Nullable(x2 x3 … xn)

Page 5: Course Overview

5Syntax Analysis (Chapter 4)

Algorithm for computing First sets

For each terminal tFirst(t) = { t }

For each non-terminal NFirst(N) = { }

RepeatFor each production N ::= x1 x2 x3 … xn

First(N) = First(N) First(x1)

For each i from 2 through n

If Nullable(x1 … xi-1), then

First(N) = First(N) First(xi)

Until no First set changes

Page 6: Course Overview

6Syntax Analysis (Chapter 4)

Generalizing the definition of First sets

Define First(x1 x2 x3 … xn) as:

if !Nullable(x1) then

First(x1) else

First(x1) First(x2 x3 … xn)

Note: some textbooks add (empty string) to First(N) whenever N is nullable, so that First(N) is never { } (empty set)

Page 7: Course Overview

7Syntax Analysis (Chapter 4)

Algorithm for computing Follow sets

Follow(S) = {$} // the end-of-file symbolFor each non-terminal N other than S

Follow(N) = { }

RepeatFor each production N ::= x1 x2 x3 … xn

For each i from 1 through n-1

if xi is a non-terminal then

Follow(xi) = Follow(xi) First(xi+1 … xn)For each i from n downto 1

if xi is a non-terminal and Nullable(xi+1 … xn) then

Follow(xi) = Follow(xi) Follow(N)

Until no Follow set changes

Page 8: Course Overview

8Syntax Analysis (Chapter 4)

Example of computing Nullable, First, Follow

S ::= TUVW | WVUT

T ::= aT | e

U ::= Ub | f

V ::= cV | W ::= Wd |

Nullable? First Follow

S false {a, e, d, c, f} {$}

T false {a, e} {f, $}

U false {f} {c, d, $, a, e, b}

V true {c} or {c, } {d, $, f}

W true {d} or {d, } {$, c, f, d}

Page 9: Course Overview

9Syntax Analysis (Chapter 4)

Parsing

We will now look at parsing.

Topics:– Some terminology

– Different types of parsing strategies

• bottom up

• top down

– Recursive descent parsing

• What is it

• How to implement a parser given an EBNF specification

Page 10: Course Overview

10Syntax Analysis (Chapter 4)

Parsing: Some Terminology

• RecognitionTo answer the question “does the input conform to the syntax of

the language”

• ParsingRecognition + also determine structure of program (for example

by creating an AST data structure)

• Unambiguous grammar:A grammar is unambiguous if there is only at most one way to

parse any input. (i.e. for syntactically correct program there is precisely one parse tree)

Page 11: Course Overview

11Syntax Analysis (Chapter 4)

Different kinds of Parsing Algorithms

• Two big groups of algorithms can be distinguished:– bottom up strategies– top down strategies

• Example: parsing of “Micro-English”

Sentence ::= Subject Verb Object .Subject ::= I | A Noun | The Noun Object ::= me | a Noun | the NounNoun ::= cat | bat | ratVerb ::= like | is | see | sees

Sentence ::= Subject Verb Object .Subject ::= I | A Noun | The Noun Object ::= me | a Noun | the NounNoun ::= cat | bat | ratVerb ::= like | is | see | sees

The cat sees the rat.The rat sees me.I like a cat.

The rat like me.I see the rat.I sees a rat.

Page 12: Course Overview

12Syntax Analysis (Chapter 4)

Bottom up parsing

The cat sees a rat .The cat

Noun

Subject

sees

Verb

a rat

Noun

Object

.

Sentence

The parse tree “grows” from the bottom (leafs) up to the top (root).

Page 13: Course Overview

13Syntax Analysis (Chapter 4)

Top-down parsing

The cat sees a rat .The cat sees rat .

The parse tree is constructed starting at the top (root).

Sentence

Subject Verb Object .

Sentence

Noun

Subject

The

Noun

cat

Verb

sees a

Noun

Object

Noun

rat .

Page 14: Course Overview

14Syntax Analysis (Chapter 4)

Quick review

• Syntactic analysis– Lexical analysis

• Group letters into words (or group characters into tokens)

• Use regular expressions and deterministic FSM’s

– Grammar transformations

• Left-factoring

• Left-recursion removal

• Substitution

– Parsing = structural analysis of program

• Group words into sentences, paragraphs, and documents (or tokens into expressions, commands, and programs)

• Top-Down and Bottom-Up

Page 15: Course Overview

15Syntax Analysis (Chapter 4)

Recursive Descent Parsing

• Recursive descent parsing is a straightforward top-down parsing algorithm.

• We will now look at how to develop a recursive descent parser from an EBNF specification.

• Idea: the parse tree structure corresponds to the recursive calling structure of parsing functions that call each other.

Page 16: Course Overview

16Syntax Analysis (Chapter 4)

Recursive Descent Parsing

Sentence ::= Subject Verb Object .Subject ::= I | A Noun | The Noun Object ::= me | a Noun | the NounNoun ::= cat | bat | ratVerb ::= like | is | see | sees

Sentence ::= Subject Verb Object .Subject ::= I | A Noun | The Noun Object ::= me | a Noun | the NounNoun ::= cat | bat | ratVerb ::= like | is | see | sees

Define a procedure parseN for each non-terminal N

private void parseSentence( ) ;private void parseSubject( );private void parseObject( ); private void parseNoun( );private void parseVerb( );

private void parseSentence( ) ;private void parseSubject( );private void parseObject( ); private void parseNoun( );private void parseVerb( );

Page 17: Course Overview

17Syntax Analysis (Chapter 4)

Recursive Descent Parsing

public class MicroEnglishParser {

private TerminalSymbol currentTerminal;

//Auxiliary methods will go here ...

//Parsing methods will go here ...}

public class MicroEnglishParser {

private TerminalSymbol currentTerminal;

//Auxiliary methods will go here ...

//Parsing methods will go here ...}

Page 18: Course Overview

18Syntax Analysis (Chapter 4)

Recursive Descent Parsing: Auxiliary Methods

public class MicroEnglishParser {

private TerminalSymbol currentTerminal;

private void accept (TerminalSymbol expected) {if (currentTerminal matches expected) currentTerminal = next input terminal ;else report a syntax error

}

...}

public class MicroEnglishParser {

private TerminalSymbol currentTerminal;

private void accept (TerminalSymbol expected) {if (currentTerminal matches expected) currentTerminal = next input terminal ;else report a syntax error

}

...}

Page 19: Course Overview

19Syntax Analysis (Chapter 4)

Recursive Descent Parsing: Parsing Methods

private void parseSentence( ) { parseSubject( ); parseVerb( ); parseObject( ); accept(‘.’);}

private void parseSentence( ) { parseSubject( ); parseVerb( ); parseObject( ); accept(‘.’);}

Sentence ::= Subject Verb Object .Sentence ::= Subject Verb Object .

Page 20: Course Overview

20Syntax Analysis (Chapter 4)

Recursive Descent Parsing: Parsing Methods

private void parseSubject( ) { if (currentTerminal matches ‘I’) accept(‘I’); else if (currentTerminal matches ‘A’) { accept(‘A’); parseNoun( ); } else if (currentTerminal matches ‘The’) { accept(‘The’); parseNoun( ); } else report a syntax error}

private void parseSubject( ) { if (currentTerminal matches ‘I’) accept(‘I’); else if (currentTerminal matches ‘A’) { accept(‘A’); parseNoun( ); } else if (currentTerminal matches ‘The’) { accept(‘The’); parseNoun( ); } else report a syntax error}

Subject ::= I | A Noun | The Noun Subject ::= I | A Noun | The Noun

Page 21: Course Overview

21Syntax Analysis (Chapter 4)

Recursive Descent Parsing: Parsing Methods

private void parseNoun( ) { if (currentTerminal matches ‘cat’) accept(‘cat’); else if (currentTerminal matches ‘bat’) accept(‘bat’); else if (currentTerminal matches ‘rat’) accept(‘rat’); else report a syntax error}

private void parseNoun( ) { if (currentTerminal matches ‘cat’) accept(‘cat’); else if (currentTerminal matches ‘bat’) accept(‘bat’); else if (currentTerminal matches ‘rat’) accept(‘rat’); else report a syntax error}

Noun ::= cat | bat | ratNoun ::= cat | bat | rat

Page 22: Course Overview

22Syntax Analysis (Chapter 4)

Recursive Descent Parsing: Parsing Methods

private void parseObject( ) {

?}

private void parseVerb( ) {

?}

private void parseObject( ) {

?}

private void parseVerb( ) {

?}

Object ::= me | a Noun | the NounVerb ::= like | is | see | sees

Object ::= me | a Noun | the NounVerb ::= like | is | see | sees

Test yourself: Can you complete parseObject( ) and parseVerb( ) ?

Page 23: Course Overview

23Syntax Analysis (Chapter 4)

Systematic Development of Rec. Descent Parser

(1) Express grammar in EBNF(2) Grammar Transformations:

Left factorization and Left recursion elimination

(3) Create a parser class with– private variable currentToken– methods to call the scanner: accept and acceptIt

(4) Implement a public method for main function to call:– public parse method that

• fetches the first token from the scanner• calls parseS (where S is start symbol of the grammar)• verifies that scanner next produces the end–of–file token

(5) Implement private parsing methods:– add private parseN method for each non terminal N