Simple One-Pass Compiler

24
Simple One-Pass Compiler Natawut Nupairoj, Ph.D. Department of Computer Engineering Chulalongkorn University

description

Simple One-Pass Compiler. Natawut Nupairoj, Ph.D. Department of Computer Engineering Chulalongkorn University. Outline. Translation Scheme. Annotated Parse Tree. Parsing Fundamental. Top-Down Parsers. Abstract Stack Machine. Simple Code Generation. Simple One-Pass Compiler. Scanner. - PowerPoint PPT Presentation

Transcript of Simple One-Pass Compiler

Page 1: Simple One-Pass Compiler

Simple One-Pass Compiler

Natawut Nupairoj, Ph.D.

Department of Computer EngineeringChulalongkorn University

Page 2: Simple One-Pass Compiler

Outline

Translation Scheme. Annotated Parse Tree. Parsing Fundamental. Top-Down Parsers. Abstract Stack Machine. Simple Code Generation.

Page 3: Simple One-Pass Compiler

Simple One-Pass Compiler

ScannerSource program(text stream)

Parser Object Code(text stream)

m a i n ( ) {

Page 4: Simple One-Pass Compiler

Sample Grammarexpr expr + termexpr expr - termexpr termterm 0 | 1 | 2 | ... | 9

Page 5: Simple One-Pass Compiler

Derivation String: 9 – 5 + 2

expr expr + term expr – term + term term – term + term 9 – term + term 9 – 5 + term 9 – 5 + 2

leftmost/rightmost derivation

Page 6: Simple One-Pass Compiler

Parse Treeexpr

expr term

expr term

term

9 - 5 + 2

Page 7: Simple One-Pass Compiler

Translation Scheme Context-free Grammar with Embedded Semantic Actions.

expr ::= expr + term

expr ::= expr – term

expr ::= term

term ::= 0

term ::= 1

...

term ::= 9

emitting (พ่�น) a translation

{ print(‘+’); }

{ print(‘-’); }

{ print(‘0’); }

{ print(‘1’); }

{ print(‘9’); }

Page 8: Simple One-Pass Compiler

Parse Tree with Semantic Actions

expr

+ { print(‘+’) }

expr term

- { print(‘-’) } 2 { print(‘2’) }

expr term

term 5 { print(‘5’) }

9 { print(‘9’) }

Depth-first traversal

Input: 9 – 5 + 2Output:

9 5 - 2 +

Page 9: Simple One-Pass Compiler

Location of Semantic Actions Semantic Actions can be placed anywhere on the RHS.

expr ::= {print(‘+’);} expr + term

expr ::= {print(‘-’);} expr – term

expr ::= term

term ::= 0 {print(‘0’);}

term ::= 1 {print(‘1’);}

...

term ::= 9 {print(‘9’);}

Page 10: Simple One-Pass Compiler

Parsing Approaches Top-down parsing

build parse tree from start symbolmatch result terminal string with input streamsimple but limit in power

Bottom-up parsingstart from input token streambuild parse tree from terminal until get start

symbolcomplex but powerful

Page 11: Simple One-Pass Compiler

Top Down vs. Bottom Up

start here

resultmatch

input token stream input token stream

start here

result

Top-down Parsing Bottom-up Parsing

Page 12: Simple One-Pass Compiler

Exampletype ::= simple

| ^id

| array [ simple ] of type

simple ::= integer

| char

| num dotdot num

Input Token String

array [ num dotdot num ] of integer

Page 13: Simple One-Pass Compiler

Top-Down Parsing with Left-to-right Scanning of Input Stream

type

array [ simple ] of type

Input array [ num dotdot num ] of integer

lookahead token

Page 14: Simple One-Pass Compiler

Backtracking(Recursive-Descent Parsing)

simple

integer char num

Input array [ num dotdot num ] of integer

lookahead token

Page 15: Simple One-Pass Compiler

Predictive Parsingtype ::= simple

| ^id | array [ simple ] of

typesimple ::= integer

| char | num dotdot num

type

array [ simple ] of type

Input array [ num dotdot num ] of integer

lookahead token

Page 16: Simple One-Pass Compiler

The Program for Predictive Parser

match(scanner)

Input(text stream)

PredictiveParser

Output

match(‘array’)

OKa r r a y [

Page 17: Simple One-Pass Compiler

The Program for Predictive Parsingprocedure match ( t : token ); procedure simple;

begin begin

if lookahead = t then if lookahead = integer then

lookahead := nexttoken match ( integer )

else error else if lookahead = char then

end; match ( char )

else if lookahead = num then begin

procedure type; match ( num )

match ( dotdot )

match ( num )

begin end

if lookahead is in { integer, char, num } then else error

simple end;

else if lookahead = ‘ ^ ‘ then begin

match ( ‘ ^ ’ ); match ( id )

end

else if lookahead = array then begin

match ( array ); match ( ‘ [ ‘ ); simple; match ( ‘ ] ‘ ); match ( of ); type

end

else error

end;

Page 18: Simple One-Pass Compiler

Mapping Between Production and Parser Codes

type -> arrary [ simple ] of type

match(array); match(‘[‘); simple; match(‘]’); match(of); type

parsing (recognition)of simple

scanner

parser

Page 19: Simple One-Pass Compiler

Lookahead SymbolsA ->

FIRST( ) = set of fist token in strings

generated from

FIRST(simple) = { integer, char, num }

FIRST( ^id ) = { ^ }

FIRST(array [ simple ] of type) = { array }

Page 20: Simple One-Pass Compiler

Rules for Predictive Parser If A -> and A -> then

FIRST() and FIRST() are disjoint

-production stmt -> begin opt_stmts end

opt_stmts -> stmt_list opt_stmts |

Page 21: Simple One-Pass Compiler

Left Recursion Left Recursion => Parser loops forever

A -> A | expr -> expr + term | term

Rewriting...A -> R

R -> R |

Page 22: Simple One-Pass Compiler

Exampleexpr expr + termexpr expr - termexpr term

term 0 | 1 | 2 | ... | 9

expr term restrest + term rest

| - term rest

| term 0 | 1 | 2 | ... | 9

Page 23: Simple One-Pass Compiler

Semantic Actions

expr term restrest + term {print(‘+’);} rest

| - term {print(‘-’);} rest

| term 0 {print(‘0’);}

| 1 {print(‘1’);}

...

Page 24: Simple One-Pass Compiler

expr term restrest + term {print(‘+’);} rest

| - term {print(‘-’);} rest

| term 0 {print(‘0’);}...

procedure rest;begin

if lookahead = ‘+’ then begin

match(‘+’);term();print(‘+’);rest();

else if lookahead = ‘-’ then begin

match(‘-’);term();print(‘-’);rest();

end;end;

procedure expr;begin

term();rest();

end;