Top Banner
Compiler CompilerTutorial CSA2010 Compiler Techniques Gordon Mangion
86

Compiler CompilerTutorialsspi3/CSA2010_Assignment_Tutorial_2011.pdf · Introduction With so many Compilers around, do we need to write parsers/compilers in industry? FTP interface

Mar 19, 2020

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Compiler CompilerTutorialsspi3/CSA2010_Assignment_Tutorial_2011.pdf · Introduction With so many Compilers around, do we need to write parsers/compilers in industry? FTP interface

Compiler Compiler TutorialCSA2010 – Compiler Techniques

Gordon Mangion

Page 2: Compiler CompilerTutorialsspi3/CSA2010_Assignment_Tutorial_2011.pdf · Introduction With so many Compilers around, do we need to write parsers/compilers in industry? FTP interface

Introduction

With so many Compilers around, do we

need to write parsers/compilers in

industry?

FTP interface

Translator from VB to Java

EPS Reader

NLP

Page 3: Compiler CompilerTutorialsspi3/CSA2010_Assignment_Tutorial_2011.pdf · Introduction With so many Compilers around, do we need to write parsers/compilers in industry? FTP interface

Topics

Prerequisites

Compiler Architecture/Modules

JavaCC

Semantic Analysis

Code Generation and Execution

Examples

The assignment (SFL)

Page 4: Compiler CompilerTutorialsspi3/CSA2010_Assignment_Tutorial_2011.pdf · Introduction With so many Compilers around, do we need to write parsers/compilers in industry? FTP interface

Prerequisites

Java

Regular Expressions

◦ ? + * …

Production rules and EBNF

Semantics

Page 5: Compiler CompilerTutorialsspi3/CSA2010_Assignment_Tutorial_2011.pdf · Introduction With so many Compilers around, do we need to write parsers/compilers in industry? FTP interface

Regular Expressions

◦ Repetitions

+ = 1 or more

* = 0 or more

? = 0 or 1

◦ Alternative

a | b = a or b

◦ Ranges

[a-z] = a to z

[fls] = f, l and s

[^cde] = not c,d,e

◦ Examples

a+ - a, aa, aaa

b* - , b, bb

c? - , c

a | b - a, b

- a,b,c,d,e,f,g,…,y,z

- f,l,s

- a,b,f,g,…,y,z

Page 6: Compiler CompilerTutorialsspi3/CSA2010_Assignment_Tutorial_2011.pdf · Introduction With so many Compilers around, do we need to write parsers/compilers in industry? FTP interface

Compiler

What is a compiler?

◦ Where to start from?

◦ Design / Architecture of a Compiler

Page 7: Compiler CompilerTutorialsspi3/CSA2010_Assignment_Tutorial_2011.pdf · Introduction With so many Compilers around, do we need to write parsers/compilers in industry? FTP interface

Compiler

Is essentially a complex function which

maps a program in a source language

onto a program in the target language.

Source

Code

C o m p i l e r

Lexical

Analysis

Syntax

Analysis

Semantic

Analysis

I-Code

Optimiser

Code

Generator

Code

Execution

Code

OptimiserOptimiser

Error

Handler

Symbol

Table…

Target

Code

Page 8: Compiler CompilerTutorialsspi3/CSA2010_Assignment_Tutorial_2011.pdf · Introduction With so many Compilers around, do we need to write parsers/compilers in industry? FTP interface

Translation

Source Code

function Sqr( x : int ) : int{

let n : int = x;n <- n * n;return n;

}

Target Code

push ebpmov ebp,espsub esp,0CChpush ebxpush esipush edilea edi,[ebp-0CCh]mov ecx,33hmov eax,0CCCCCCCChrep stos dword ptr es:[edi]mov eax,dword ptr [x] mov dword ptr [n],eaxmov eax,dword ptr [n]imul eax,dword ptr [n]mov dword ptr [n],eaxmov eax,dword ptr [n]pop edipop esipop ebxmov esp,ebppop ebpret

Page 9: Compiler CompilerTutorialsspi3/CSA2010_Assignment_Tutorial_2011.pdf · Introduction With so many Compilers around, do we need to write parsers/compilers in industry? FTP interface

Translation

Source Code

function Sqr( x:int ): int{

let n : int = x;n <- n * n;return n;

}

Compiler

All possible translations

of the source program

Page 10: Compiler CompilerTutorialsspi3/CSA2010_Assignment_Tutorial_2011.pdf · Introduction With so many Compilers around, do we need to write parsers/compilers in industry? FTP interface

Translation

Source Code

function Sqr( x : int ) : int{

let n : int = x;n <- n * n;return n;

}

Target Codepush ebpmov ebp,espsub esp,0CChpush ebxpush esipush edilea edi,[ebp-0CCh]mov ecx,33hmov eax,0CCCCCCCChrep stos dword ptr es:[edi]

mov eax,dword ptr [x] mov dword ptr [n],eax

mov eax,dword ptr [n]imul eax,dword ptr [n]mov dword ptr [n],eax

mov eax,dword ptr [n]

pop edipop esipop ebxmov esp,ebppop ebpret

Page 11: Compiler CompilerTutorialsspi3/CSA2010_Assignment_Tutorial_2011.pdf · Introduction With so many Compilers around, do we need to write parsers/compilers in industry? FTP interface

Production Rules

Context Free Grammars

◦ A B “c” D

BNF

◦ A ::= B “c” D

EBNF

◦ A ::= B* “c”? D+

Page 12: Compiler CompilerTutorialsspi3/CSA2010_Assignment_Tutorial_2011.pdf · Introduction With so many Compilers around, do we need to write parsers/compilers in industry? FTP interface

The Language game

Source Language

Target Language

Implementation Language

Page 13: Compiler CompilerTutorialsspi3/CSA2010_Assignment_Tutorial_2011.pdf · Introduction With so many Compilers around, do we need to write parsers/compilers in industry? FTP interface

Tombstone Diagram

Implementation

Language

Source Target

Language Language

Page 14: Compiler CompilerTutorialsspi3/CSA2010_Assignment_Tutorial_2011.pdf · Introduction With so many Compilers around, do we need to write parsers/compilers in industry? FTP interface

Compiler Architecture

Lexical

Analysis

Syntax

Analysis

Semantic

Analysis

I-Code

Optimiser

Code

Generator

Code

Execution

Code

OptimiserOptimiser

Error

Handler

Source

Code

Symbol

Table…

Sourc

e L

angu

age

Toke

ns

Intermediate

Code

Inte

rmedia

te

Code

Intermediate

Code

Page 15: Compiler CompilerTutorialsspi3/CSA2010_Assignment_Tutorial_2011.pdf · Introduction With so many Compilers around, do we need to write parsers/compilers in industry? FTP interface

Simplified Compiler Architecture

Lexical

Analysis

Syntax

Analysis

Semantic

Analysis

Code

Generator

Source

Code

Symbol

Table

Source Language

Tokens

Intermediate Code

Intermediate Code

Target

Code

Page 16: Compiler CompilerTutorialsspi3/CSA2010_Assignment_Tutorial_2011.pdf · Introduction With so many Compilers around, do we need to write parsers/compilers in industry? FTP interface

Lexical Analysis

Tokenises the source file

◦ Removes whitespace

◦ Tokens

◦ Keywords

Source Code

function Sqr( x : int ) : int{

let n : int = x;n <- n * n;return n;

}

Function (Keyword)Sqr (Identifier)( (Lparen)X (Identifier): (Colon)Int (Keyword)) (Rparen): (Colon)Int (Keyword){ (Lbrace)Let (Keyword)N (Identifier): (Colon)Int (Keyword)= (Eq)X (Identifier); (Semicolon)…

Page 17: Compiler CompilerTutorialsspi3/CSA2010_Assignment_Tutorial_2011.pdf · Introduction With so many Compilers around, do we need to write parsers/compilers in industry? FTP interface

Syntax Analysis (Parser)

Checks that the source file conforms to

the language grammar

◦ E.g. FunctionCall ::= “function” identifier “(“ …

Creates intermediate code

◦ ParseTree

Source Code

function Sqr( x : int ) …

Source Code

function 1.5( x : int )…

2

+

5

Page 18: Compiler CompilerTutorialsspi3/CSA2010_Assignment_Tutorial_2011.pdf · Introduction With so many Compilers around, do we need to write parsers/compilers in industry? FTP interface

Semantic Analysis

Checks that the meaning behind the

syntax makes sense, according to the

type-system

◦ E.g. let x : int = 3;

x:int, 3:int

◦ E.g. let x : int = 3.2;

x:int, 3.2:Real

Page 19: Compiler CompilerTutorialsspi3/CSA2010_Assignment_Tutorial_2011.pdf · Introduction With so many Compilers around, do we need to write parsers/compilers in industry? FTP interface

Code Generation / Execution

Traverses the Intermediate Code

representation and generates or executes

the code

2

+

5

PlusNode.Execute()

{

return leftChild.Execute() +

rightChild.Execute() ;

}

IntLiteralNode.Execute()

{

return Integer.parseInt(this.getValue());

}

Page 20: Compiler CompilerTutorialsspi3/CSA2010_Assignment_Tutorial_2011.pdf · Introduction With so many Compilers around, do we need to write parsers/compilers in industry? FTP interface

Symbol Table

Important Data Structure◦ Keeps information about every identifier in the

source Variables

functions

Labels …

◦ Keeps the scope information

◦ Entry Properties Name

Type

Value

Scope

Page 21: Compiler CompilerTutorialsspi3/CSA2010_Assignment_Tutorial_2011.pdf · Introduction With so many Compilers around, do we need to write parsers/compilers in industry? FTP interface

Symbol Table

A data structure that maps an Identifier‟s

name onto a structure that contains the

identifier‟s information

Name ( Name, Type, Value, Scope )

Page 22: Compiler CompilerTutorialsspi3/CSA2010_Assignment_Tutorial_2011.pdf · Introduction With so many Compilers around, do we need to write parsers/compilers in industry? FTP interface

Symbol Table

Easiest way is to make use of a hash-table

/ hash-map

Create a new Hash-map for each new

scope

Page 23: Compiler CompilerTutorialsspi3/CSA2010_Assignment_Tutorial_2011.pdf · Introduction With so many Compilers around, do we need to write parsers/compilers in industry? FTP interface

Simplified Compiler Architecture

Lexical

Analysis

Syntax

Analysis

Semantic

Analysis

Code

Generator

Source

Code

Symbol

Table

Source Language

Tokens

Intermediate Code

Intermediate Code

Target

Code

Front-End

Back-End

Page 24: Compiler CompilerTutorialsspi3/CSA2010_Assignment_Tutorial_2011.pdf · Introduction With so many Compilers around, do we need to write parsers/compilers in industry? FTP interface

Compiler Implementation

Compiler Architecture

Lexical

AnalysisParser

(Syntax Analysis)

Type-Checker(Semantic Analysis)

Code

Execution

Source

Code

Symbol

Table

Com

pile

r In

terf

aceParse

Tree

Vis

itor

Inte

rfac

eV

isitor

Inte

rfac

e

Call

Visitor

Call

Visitor

Page 25: Compiler CompilerTutorialsspi3/CSA2010_Assignment_Tutorial_2011.pdf · Introduction With so many Compilers around, do we need to write parsers/compilers in industry? FTP interface

Java Interfaces

Interface is a contract

Class implements interface

◦ Honours the contract

◦ Implements the methods of the interface

Interface variable can hold instance of a

class that implements that interface

Page 26: Compiler CompilerTutorialsspi3/CSA2010_Assignment_Tutorial_2011.pdf · Introduction With so many Compilers around, do we need to write parsers/compilers in industry? FTP interface

Java Interfaces

public interface IAdder

{

int add(int n, int m);

}

public interface IMinus

{

int subtract(int n, int m);

}

public class SimpleMath

implements IAdder, IMinus

{

public int add(int n, int m)

{ … }

public int subtract(int n, int m)

{ … }

}

public static void main(…)

{

IAdder a = new SimpleMath();

IMinus s = new SimpleMath();

a.add(1,2);

s.subtract(4,2);

}

Page 27: Compiler CompilerTutorialsspi3/CSA2010_Assignment_Tutorial_2011.pdf · Introduction With so many Compilers around, do we need to write parsers/compilers in industry? FTP interface

Program Generators?

Source-to-Executable

◦ Compiler

Reverse Polish Notation

◦ 2 + 3 - 4 2 3 + 4 -

Source-to-Source

◦ Preprocessors

Page 28: Compiler CompilerTutorialsspi3/CSA2010_Assignment_Tutorial_2011.pdf · Introduction With so many Compilers around, do we need to write parsers/compilers in industry? FTP interface

Javacc

A program that creates parsers

The source code(input) is a definition file

◦ Grammar definition

◦ Inline Java code

The target(output) is java-based parser

Recognizer?

Page 29: Compiler CompilerTutorialsspi3/CSA2010_Assignment_Tutorial_2011.pdf · Introduction With so many Compilers around, do we need to write parsers/compilers in industry? FTP interface

Javacc

Javacclangdef.jj

*.java*.java

*.java*.java

Page 30: Compiler CompilerTutorialsspi3/CSA2010_Assignment_Tutorial_2011.pdf · Introduction With so many Compilers around, do we need to write parsers/compilers in industry? FTP interface

JJTree

From a recognizer to a parser

Preprocessor to Javacc

Produces Parse Tree building actions

Creates “.jj” files from “.jjt” files

Page 31: Compiler CompilerTutorialsspi3/CSA2010_Assignment_Tutorial_2011.pdf · Introduction With so many Compilers around, do we need to write parsers/compilers in industry? FTP interface

JJTree

Javacc

langdef.jjt

*.java*.java

*.java*.java

JJTree langdef.jj

Page 32: Compiler CompilerTutorialsspi3/CSA2010_Assignment_Tutorial_2011.pdf · Introduction With so many Compilers around, do we need to write parsers/compilers in industry? FTP interface

JJTree

Javacc

langdef.jjt

*.java*.java

*.java*.java

JJTree langdef.jj

“J J T” T O O L

Page 33: Compiler CompilerTutorialsspi3/CSA2010_Assignment_Tutorial_2011.pdf · Introduction With so many Compilers around, do we need to write parsers/compilers in industry? FTP interface

Installing Javacc

https://javacc.dev.java.net/

◦ Javacc.zip / Javacc.tar.gz

Eclipse plugin

◦ Preferences->Install/Update->Add

http://eclipse-javacc.sourceforge.net/

◦ Help->New Software

Page 34: Compiler CompilerTutorialsspi3/CSA2010_Assignment_Tutorial_2011.pdf · Introduction With so many Compilers around, do we need to write parsers/compilers in industry? FTP interface

.jjt Grammar Files

Four (4) sections

◦ Options

◦ Parser

◦ Tokens

◦ Production Rules

Page 35: Compiler CompilerTutorialsspi3/CSA2010_Assignment_Tutorial_2011.pdf · Introduction With so many Compilers around, do we need to write parsers/compilers in industry? FTP interface

.jjt Options

options

{

BUILD_NODE_FILES=false;

STATIC=false;

MULTI=true;

}

Page 36: Compiler CompilerTutorialsspi3/CSA2010_Assignment_Tutorial_2011.pdf · Introduction With so many Compilers around, do we need to write parsers/compilers in industry? FTP interface

.jjt Parser block

PARSER_BEGIN(parser_name)

. . .

public class parser_name

{

}

PARSER_END(parser_name)

Page 37: Compiler CompilerTutorialsspi3/CSA2010_Assignment_Tutorial_2011.pdf · Introduction With so many Compilers around, do we need to write parsers/compilers in industry? FTP interface

.jjt Tokens

Lexeme to ignore

SKIP :

{

“ ”

| “\t”

| “\n”

| “\r”

| <"//" (~["\n","\r"])* ("\n"|"\r"|"\r\n")>

}

Page 38: Compiler CompilerTutorialsspi3/CSA2010_Assignment_Tutorial_2011.pdf · Introduction With so many Compilers around, do we need to write parsers/compilers in industry? FTP interface

.jjt Tokens

Tokens that are to be returned

TOKEN :

{

<PLUS: “+”>

|<TRUE: "true" >

| <FALSE: "false" >

| <LETTER: (["A"-"Z"] | ["a"-"z"]) >

| <STRING_LITERAL: "\"" (~["\"","\r","\n"])* "\"" >

| <#DIGIT: [“0”-”9”]>

}

Page 39: Compiler CompilerTutorialsspi3/CSA2010_Assignment_Tutorial_2011.pdf · Introduction With so many Compilers around, do we need to write parsers/compilers in industry? FTP interface

.jjt Production Rules

Assume the following:

Header “Script” <STRING_LITERAL>

or

Header ::= “Script” <STRING_LITERAL>

Page 40: Compiler CompilerTutorialsspi3/CSA2010_Assignment_Tutorial_2011.pdf · Introduction With so many Compilers around, do we need to write parsers/compilers in industry? FTP interface

.jjt Production Rules

Assume the following:

Header ::= “Script” <STRING_LITERAL>

void Header():

{

<SCRIPT> <STRING_LITERAL>

}

Page 41: Compiler CompilerTutorialsspi3/CSA2010_Assignment_Tutorial_2011.pdf · Introduction With so many Compilers around, do we need to write parsers/compilers in industry? FTP interface

.jjt Production Rules

Header ::= “Script” <STRING_LITERAL>

void Header(): { int n = 0; n++;}

{

<SCRIPT> <STRING_LITERAL>

{ n++; }

}

Page 42: Compiler CompilerTutorialsspi3/CSA2010_Assignment_Tutorial_2011.pdf · Introduction With so many Compilers around, do we need to write parsers/compilers in industry? FTP interface

.jjt Production Rules

Calling Non-Terminals

void Integer(): {}

{ … }

void Header(): { }

{

<SCRIPT>

Integer()

}

Page 43: Compiler CompilerTutorialsspi3/CSA2010_Assignment_Tutorial_2011.pdf · Introduction With so many Compilers around, do we need to write parsers/compilers in industry? FTP interface

.jjt Production Rules

Token matching - Actions

void Header(): { int n = 0; n++; }

{

<SCRIPT>{ n = 1; }

<STRING_LITERAL>

{ n++; }

}

Page 44: Compiler CompilerTutorialsspi3/CSA2010_Assignment_Tutorial_2011.pdf · Introduction With so many Compilers around, do we need to write parsers/compilers in industry? FTP interface

.jjt Production Rules

Getting Token value

void Number()

{ Token t;

int n = 0; }

{

t=<DIGIT>{ n = integer.parseInt(t.image); }

}

Page 45: Compiler CompilerTutorialsspi3/CSA2010_Assignment_Tutorial_2011.pdf · Introduction With so many Compilers around, do we need to write parsers/compilers in industry? FTP interface

Lookahead

Consider the following java statements

◦ public int name;

◦ public int name[];

◦ public int name() { … }

Page 46: Compiler CompilerTutorialsspi3/CSA2010_Assignment_Tutorial_2011.pdf · Introduction With so many Compilers around, do we need to write parsers/compilers in industry? FTP interface

Building the AST

options

{

STATIC=false;

MULTI=true;

BUILD_NODE_FILES=true;

NODE_USES_PARSER=false;

NODE_PREFIX=“”;

}

Page 47: Compiler CompilerTutorialsspi3/CSA2010_Assignment_Tutorial_2011.pdf · Introduction With so many Compilers around, do we need to write parsers/compilers in industry? FTP interface

Building the AST

SimpleNode Start() : {}

{

Expression()

“;”

<EOF>

{

return jjtThis;

}

}

Page 48: Compiler CompilerTutorialsspi3/CSA2010_Assignment_Tutorial_2011.pdf · Introduction With so many Compilers around, do we need to write parsers/compilers in industry? FTP interface

Parsing

PARSER_BEGIN(MyParser)

...

MyParser parser = new MyParser(System.in);

try {

SimpleNode n = parser.Start();

System.out.println(“Parsed!");

} catch (Exception e) {

System.out.println("Oops!");

System.out.println(e.getMessage());

}

...

PARSER_END(MyParser)

Page 49: Compiler CompilerTutorialsspi3/CSA2010_Assignment_Tutorial_2011.pdf · Introduction With so many Compilers around, do we need to write parsers/compilers in industry? FTP interface

ExampleDigit ::= [“0” - “9”]

Number ::= Digit+

Plus ::= “+”

Minus ::= “-”

Op ::= Plus | Minus

Expression ::= Number { Op Number }

Page 50: Compiler CompilerTutorialsspi3/CSA2010_Assignment_Tutorial_2011.pdf · Introduction With so many Compilers around, do we need to write parsers/compilers in industry? FTP interface

ExampleTOKEN:

{

< NUMBER: <DIGIT>+ >

| < Digit: [“0” - “9”] >

| < PLUS: “+” >

| < MINUS: “-” >

}

Digit ::= [“0” - “9”]

Number ::= Digit+

Plus ::= “+”

Minus ::= “-”

Page 51: Compiler CompilerTutorialsspi3/CSA2010_Assignment_Tutorial_2011.pdf · Introduction With so many Compilers around, do we need to write parsers/compilers in industry? FTP interface

Examplevoid Op() :{}

{

< PLUS > | < MINUS >

}

void Expression(): {}

{

< Number >

(Op() < Number >)*

}

Op ::= Plus | Minus

Expression ::=

Number { Op Number }

Page 52: Compiler CompilerTutorialsspi3/CSA2010_Assignment_Tutorial_2011.pdf · Introduction With so many Compilers around, do we need to write parsers/compilers in industry? FTP interface

Example

PARSER_BEGIN(MyParser)

...

MyParser parser = new MyParser(System.in);

try {

parser.Expression();

System.out.println(“Parsed!");

} catch (Exception e) {

System.out.println("Oops!");

System.out.println(e.getMessage());

}

...

PARSER_END(MyParser)

Page 53: Compiler CompilerTutorialsspi3/CSA2010_Assignment_Tutorial_2011.pdf · Introduction With so many Compilers around, do we need to write parsers/compilers in industry? FTP interface

Generated Sources

Page 54: Compiler CompilerTutorialsspi3/CSA2010_Assignment_Tutorial_2011.pdf · Introduction With so many Compilers around, do we need to write parsers/compilers in industry? FTP interface

Example – Evaluating

Token Op() :{ Token t; }

{

(t = < PLUS > | t = < MINUS >)

{ return t; }

}

Page 55: Compiler CompilerTutorialsspi3/CSA2010_Assignment_Tutorial_2011.pdf · Introduction With so many Compilers around, do we need to write parsers/compilers in industry? FTP interface

Exampleint Expression(): { Token t, op; int n;}

{

t = < NUMBER >

{

n = Integer.parseInt( t.image );

}

( op=Op()

t = < NUMBER > {

if(op.image.equals("+"))

n += Integer.parseInt( t.image );

else

n -= Integer.parseInt( t.image ); }

)*

{ return n; }

}

Page 56: Compiler CompilerTutorialsspi3/CSA2010_Assignment_Tutorial_2011.pdf · Introduction With so many Compilers around, do we need to write parsers/compilers in industry? FTP interface

ExamplePARSER_BEGIN(MyParser)

public class MyParser

{

...

MyParser parser = new MyParser(System.in);

try

{

int n = parser.Expression();

...

PARSER_END(MyParser)

Page 57: Compiler CompilerTutorialsspi3/CSA2010_Assignment_Tutorial_2011.pdf · Introduction With so many Compilers around, do we need to write parsers/compilers in industry? FTP interface

Example - Building the AST

options

{

STATIC=false;

MULTI=true;

BUILD_NODE_FILES=true;

NODE_USES_PARSER=false;

NODE_PREFIX=“AST”;

}

Page 58: Compiler CompilerTutorialsspi3/CSA2010_Assignment_Tutorial_2011.pdf · Introduction With so many Compilers around, do we need to write parsers/compilers in industry? FTP interface

Generated Code

Page 59: Compiler CompilerTutorialsspi3/CSA2010_Assignment_Tutorial_2011.pdf · Introduction With so many Compilers around, do we need to write parsers/compilers in industry? FTP interface

Examplevoid Op() :{}

{

< PLUS > | < MINUS >

}

SimpleNode Expression(): {}

{

< Number >

(Op() < Number >)*

{ return jjtThis; }

}

Op ::= Plus | Minus

Expression ::=

Number { Op Number }

Page 60: Compiler CompilerTutorialsspi3/CSA2010_Assignment_Tutorial_2011.pdf · Introduction With so many Compilers around, do we need to write parsers/compilers in industry? FTP interface

ExamplePARSER_BEGIN(MyParser)

public class MyParser

{

...

MyParser parser = new MyParser(System.in);

try

{

SimpleNode rootNode = parser.Expression();

...

PARSER_END(MyParser)

Page 61: Compiler CompilerTutorialsspi3/CSA2010_Assignment_Tutorial_2011.pdf · Introduction With so many Compilers around, do we need to write parsers/compilers in industry? FTP interface

Example Grammar 2

Digit ::= [“0” - “9”]

Number ::= Digit+

Factor ::= Expression | Number

Term ::= Factor [ “*” | “/” Factor ]

Expression ::= Term { “+” | “-” Term }

Start ::= Expression

Page 62: Compiler CompilerTutorialsspi3/CSA2010_Assignment_Tutorial_2011.pdf · Introduction With so many Compilers around, do we need to write parsers/compilers in industry? FTP interface

The Visitor Design Pattern

The Problem

◦ Number of operations

to be performed on

each node

◦ Options

Implement each operation

inside each node

Make use of visitor

pattern

+

2 *

A 7

Page 63: Compiler CompilerTutorialsspi3/CSA2010_Assignment_Tutorial_2011.pdf · Introduction With so many Compilers around, do we need to write parsers/compilers in industry? FTP interface

The Visitor Design Pattern

Consider one Node

◦ Printing Operation

We can implement the

operation in a separate

class e.g. PrintVisitor

This can be done for all

type of nodes

Plus

PrintVisitor

void PrettyPrint( Plus )

{

}

Page 64: Compiler CompilerTutorialsspi3/CSA2010_Assignment_Tutorial_2011.pdf · Introduction With so many Compilers around, do we need to write parsers/compilers in industry? FTP interface

The Visitor Design Pattern

Modification on node

Client

PrintVisitor

void PrettyPrint( Plus )

{

}

Plus

void Print( Visitor p_visitor)

{

p_visitor.PrettyPrint( this );

}

Caller

{

PrintVisitor pr = new PrintVisitor();

Plus plus …

plus.Print(pr);

}

Page 65: Compiler CompilerTutorialsspi3/CSA2010_Assignment_Tutorial_2011.pdf · Introduction With so many Compilers around, do we need to write parsers/compilers in industry? FTP interface

The Visitor Design Pattern

Finally

Interface IVisitor

void Visit( Plus );

Plus

void Accept( IVisitor p_visitor)

{

p_visitor.Visit( this );

}

PrintVisitor

implements IVisitor

void Visit( Plus )

{

}

Caller

{

PrintVisitor pr = new PrintVisitor();

Plus plus …

plus.Accept(pr);

}

Page 66: Compiler CompilerTutorialsspi3/CSA2010_Assignment_Tutorial_2011.pdf · Introduction With so many Compilers around, do we need to write parsers/compilers in industry? FTP interface

The Visitor Design Pattern

Benefits

Interface IVisitor

void Visit( Plus );

void Visit( Times );

Plus

void Accept( IVisitor p_visitor)

{

p_visitor.Visit( this );

}

PrintVisitor

implements IVisitor

void Visit( Plus ) {…}

void Visit( Times ) {…}

TypeCheckVisitor

implements IVisitor

void Visit( Plus ) {…}

void Visit( Times ) {…}

EvaluationVisitor

implements IVisitor

void Visit( Plus ) {…}

void Visit( Times ) {…}

Page 67: Compiler CompilerTutorialsspi3/CSA2010_Assignment_Tutorial_2011.pdf · Introduction With so many Compilers around, do we need to write parsers/compilers in industry? FTP interface

Compiler Implementation

Compiler Architecture

Lexical

AnalysisParser

(Syntax Analysis)

Type-Checker(Semantic Analysis)

Code

Execution

Source

Code

Symbol

Table

Com

pile

r In

terf

aceParse

Tree

Vis

itor

Inte

rfac

eV

isitor

Inte

rfac

e

Call

Visitor

Call

Visitor

Page 68: Compiler CompilerTutorialsspi3/CSA2010_Assignment_Tutorial_2011.pdf · Introduction With so many Compilers around, do we need to write parsers/compilers in industry? FTP interface

JJTree and the Visitor Pattern

Options

{

VISITOR=true;

}

Page 69: Compiler CompilerTutorialsspi3/CSA2010_Assignment_Tutorial_2011.pdf · Introduction With so many Compilers around, do we need to write parsers/compilers in industry? FTP interface

Visitor interfacepublic interface MyParserVisitor

{

public Object visit(SimpleNode node, Object data);

public Object visit(ASTOp node, Object data);

public Object visit(ASTExpression node, Object data);

}

Page 70: Compiler CompilerTutorialsspi3/CSA2010_Assignment_Tutorial_2011.pdf · Introduction With so many Compilers around, do we need to write parsers/compilers in industry? FTP interface

Visitor - Node modification

Public class ASTExpression extends SimpleNode {

public ASTExpression(int id) {

super(id);

}

public ASTExpression(MyParser p, int id) {

super(p, id);

}

/** Accept the visitor. **/

public Object jjtAccept(MyParserVisitor visitor, Object data) {

return visitor.visit(this, data);

}

}

Page 71: Compiler CompilerTutorialsspi3/CSA2010_Assignment_Tutorial_2011.pdf · Introduction With so many Compilers around, do we need to write parsers/compilers in industry? FTP interface

JJTree and the Visitor Pattern

Options

{

VISITOR=true;

VISITOR_DATA_TYPE=“SymbolTable”;

VISITOR_RETURN_TYPE=“Object”;

}

Page 72: Compiler CompilerTutorialsspi3/CSA2010_Assignment_Tutorial_2011.pdf · Introduction With so many Compilers around, do we need to write parsers/compilers in industry? FTP interface

Visitor - return type

We need a return-type generic enough to accommodate the results returned by all the visitor implementations

By default jjt uses the Object class◦ Can easily be used however class-casts

instanceof

A generic container can be designed to hold the results

Page 73: Compiler CompilerTutorialsspi3/CSA2010_Assignment_Tutorial_2011.pdf · Introduction With so many Compilers around, do we need to write parsers/compilers in industry? FTP interface

Visitor - return type

Types –

◦ Create an

enumeration

containing the types

of the language‟s

type-system.

enum DataType

{

Unknown,

Error,

Boolean,

Integer,

Function,

Void

}

Page 74: Compiler CompilerTutorialsspi3/CSA2010_Assignment_Tutorial_2011.pdf · Introduction With so many Compilers around, do we need to write parsers/compilers in industry? FTP interface

Visitor - return type

Result classClass Result

{

DataType type;

Object value;

Getter & Setter

Conversion

}

Page 75: Compiler CompilerTutorialsspi3/CSA2010_Assignment_Tutorial_2011.pdf · Introduction With so many Compilers around, do we need to write parsers/compilers in industry? FTP interface

Type-Checking – (Semantic Analysis)

Consider the following decision rule

◦ IfStatement ::= “if” “(“ Expression “)” …

if( 3 + 2 )

{

}

Page 76: Compiler CompilerTutorialsspi3/CSA2010_Assignment_Tutorial_2011.pdf · Introduction With so many Compilers around, do we need to write parsers/compilers in industry? FTP interface

Type-Checking – (Semantic Analysis)

Consider the following assignment rule

◦ Assignment ::= Identifier = Expression

◦ n = 3 + 2;

Page 77: Compiler CompilerTutorialsspi3/CSA2010_Assignment_Tutorial_2011.pdf · Introduction With so many Compilers around, do we need to write parsers/compilers in industry? FTP interface

Type-Checking – (Semantic Analysis)

◦ n = 3 + 2;

Ident

„n‟

=

+

3 2

Expr

visit( AssignmentNode )

if(LHS.visit.type == RHS.visit.type)

return new Result(RHS.visit.type);

else

return new Result(DataType.Integer);

visit( PlusNode )

if(LHS.visit.type == RHS.visit.type)

return new Result(RHS.visit.type);

else

return new Result(DataType.Integer);

visit( IntLiteral )

return new Result(DataType.Integer)

Identifier.visit()

result = SymbolTable.get( node.value )

return result;

Page 78: Compiler CompilerTutorialsspi3/CSA2010_Assignment_Tutorial_2011.pdf · Introduction With so many Compilers around, do we need to write parsers/compilers in industry? FTP interface

Type-Checking – (Semantic Analysis)

Implement the call to the visitor:Public class MyParser {

... main ...

Try

{

SimpleNode root = parser.Expression();

MyParserVisitor visitor = new TypeChecker();

Result result = root.jjtAccept(visitor, null );

System.out.println("DataType is " +

result .getType().toString());

...

Page 79: Compiler CompilerTutorialsspi3/CSA2010_Assignment_Tutorial_2011.pdf · Introduction With so many Compilers around, do we need to write parsers/compilers in industry? FTP interface

Interpreter – Code Execution

In a similar way to Semantic Analysis, we

can make use of the visitor pattern

This time, we return values as well rather

than type-information only

We have to take special care about the

difference in the semantics of the

languages involved

Page 80: Compiler CompilerTutorialsspi3/CSA2010_Assignment_Tutorial_2011.pdf · Introduction With so many Compilers around, do we need to write parsers/compilers in industry? FTP interface

Interpreter – Code Execution

◦ n = 3 + 2;

Ident

„n‟

=

+

3 2

Expr

visit( AssignmentNode )

result =

((IdentifierNode)getChild(0)).jjtAccept(…)

result.value = getChild(1). jjtAccept(…).value

return result

visit( PlusNode )

Result result = new Result();

result.value = getChild(0). jjtAccept(…).value +

getChild(1). jjtAccept(…).value;

visit(IntLiteral )

Result result = new Result();

result.type = DataType.Integer;

result.value = Integer.parseInt(node.value);

return result;

Page 81: Compiler CompilerTutorialsspi3/CSA2010_Assignment_Tutorial_2011.pdf · Introduction With so many Compilers around, do we need to write parsers/compilers in industry? FTP interface

Interpreter – Code Execution

IfStatement ::= "if" "(" Expression ")"

Statement ("else" Statement)?

visit(IfStatement node, SymbolTable symTbl){

value = node.jjtGetChild(0).jjtAccept(this, symTbl);

boolean bCond = value.toBool();

if( bCond )

return node.jjtGetChild(1).jjtAccept(this, symTbl);

else

{

if(node.jjtGetNumChildren() > 2 )

return node.jjtGetChild(2).jjtAccept(this, symTbl);

}

return VoidValue;

}

Page 82: Compiler CompilerTutorialsspi3/CSA2010_Assignment_Tutorial_2011.pdf · Introduction With so many Compilers around, do we need to write parsers/compilers in industry? FTP interface

Interpreter – Code Execution

VariableDecl ::= "let" Identifier ":"

Type "=" Expression ";"

visit(VariableDecl node, SymbolTable SymTbl)

{

String strVar = ((ASTIdentifier) node.jjtGetChild(0)).jjtGetValue().toString();

valType = node.jjtGetChild(1).jjtAccept(this, SymTbl);

value = node.jjtGetChild(2).jjtAccept(this, SymTbl);

SymTbl.put(strVar, value);

return value;

}

Page 83: Compiler CompilerTutorialsspi3/CSA2010_Assignment_Tutorial_2011.pdf · Introduction With so many Compilers around, do we need to write parsers/compilers in industry? FTP interface

Code Generation

Once again we make use of the visitor pattern

We are translating portions of the source language into snippets of the target language

We have to take special care about the difference in the semantics of the languages involved

Page 84: Compiler CompilerTutorialsspi3/CSA2010_Assignment_Tutorial_2011.pdf · Introduction With so many Compilers around, do we need to write parsers/compilers in industry? FTP interface

Example

Translate

◦ Assignment ::= Identifier „=‟ Expression

◦ To

◦ Assignment ::= Identifier „<-‟ Expression

Page 85: Compiler CompilerTutorialsspi3/CSA2010_Assignment_Tutorial_2011.pdf · Introduction With so many Compilers around, do we need to write parsers/compilers in industry? FTP interface

Code GenerationVisit( AssignmentNode )

{

//Source: Identifier = Expression;

//Target: Identifier <- Expression;

((IdentNode)getChild(0)).jjtAccept(…);

Emit( “<-” );

((ExprNode)getChild(1)).jjtAccept(…);

}

Page 86: Compiler CompilerTutorialsspi3/CSA2010_Assignment_Tutorial_2011.pdf · Introduction With so many Compilers around, do we need to write parsers/compilers in industry? FTP interface

Questions?

The End