+ All Categories
Home > Technology > Implementing a Simple Interpreter

Implementing a Simple Interpreter

Date post: 02-Jul-2015
Category:
Upload: ray-song
View: 178 times
Download: 1 times
Share this document with a friend
Description:
Implementing a Simple Interpreter
71
Implementing a Simple Interpreter Ray Song May 27, 2012 Ray Song Implementing a Simple Interpreter
Transcript
Page 1: Implementing a Simple Interpreter

Implementing a Simple Interpreter

Ray Song

May 27, 2012

Ray Song Implementing a Simple Interpreter

Page 2: Implementing a Simple Interpreter

Flow sheet

I Lexical analysis

I Grammatical analysis

Figure : Flow

Ray Song Implementing a Simple Interpreter

Page 3: Implementing a Simple Interpreter

Flow sheet

I Lexical analysis

I Grammatical analysis

Figure : Flow

Ray Song Implementing a Simple Interpreter

Page 4: Implementing a Simple Interpreter

Lexical Analysis

I manual

I flex

Ray Song Implementing a Simple Interpreter

Page 5: Implementing a Simple Interpreter

Lexical Analysis

I manual

I flex

Ray Song Implementing a Simple Interpreter

Page 6: Implementing a Simple Interpreter

Tokens

I identifier

I integer

I string

I while

I if

I else

I print

I NEWLINE

I NO WHITESPACE

Ray Song Implementing a Simple Interpreter

Page 7: Implementing a Simple Interpreter

Tokens

I identifier

I integer

I string

I while

I if

I else

I print

I NEWLINE

I NO WHITESPACE

Ray Song Implementing a Simple Interpreter

Page 8: Implementing a Simple Interpreter

Tokens

I identifier

I integer

I string

I while

I if

I else

I print

I NEWLINE

I NO WHITESPACE

Ray Song Implementing a Simple Interpreter

Page 9: Implementing a Simple Interpreter

Tokens

I identifier

I integer

I string

I while

I if

I else

I print

I NEWLINE

I NO WHITESPACE

Ray Song Implementing a Simple Interpreter

Page 10: Implementing a Simple Interpreter

Tokens

I identifier

I integer

I string

I while

I if

I else

I print

I NEWLINE

I NO WHITESPACE

Ray Song Implementing a Simple Interpreter

Page 11: Implementing a Simple Interpreter

Tokens

I identifier

I integer

I string

I while

I if

I else

I print

I NEWLINE

I NO WHITESPACE

Ray Song Implementing a Simple Interpreter

Page 12: Implementing a Simple Interpreter

Tokens

I identifier

I integer

I string

I while

I if

I else

I print

I NEWLINE

I NO WHITESPACE

Ray Song Implementing a Simple Interpreter

Page 13: Implementing a Simple Interpreter

Tokens

I identifier

I integer

I string

I while

I if

I else

I print

I NEWLINE

I NO WHITESPACE

Ray Song Implementing a Simple Interpreter

Page 14: Implementing a Simple Interpreter

Tokens

I identifier

I integer

I string

I while

I if

I else

I print

I NEWLINE

I NO WHITESPACE

Ray Song Implementing a Simple Interpreter

Page 15: Implementing a Simple Interpreter

Off-side rule

def hello():

print ‘world’

I indentation sensitive

I Ex: ISWIM(1966), occam(1983), Miranda(1985),Haskell(1990), Python(1991)

Ray Song Implementing a Simple Interpreter

Page 16: Implementing a Simple Interpreter

Off-side rule

def hello():

print ‘world’

I indentation sensitive

I Ex: ISWIM(1966), occam(1983), Miranda(1985),Haskell(1990), Python(1991)

Ray Song Implementing a Simple Interpreter

Page 17: Implementing a Simple Interpreter

Off-side rule - Cont.

I represented as virtual tokens

I INDENT

I DEDENT

Ray Song Implementing a Simple Interpreter

Page 18: Implementing a Simple Interpreter

Off-side rule - Cont.

I represented as virtual tokens

I INDENT

I DEDENT

Ray Song Implementing a Simple Interpreter

Page 19: Implementing a Simple Interpreter

Off-side rule - Cont.

I represented as virtual tokens

I INDENT

I DEDENT

Ray Song Implementing a Simple Interpreter

Page 20: Implementing a Simple Interpreter

Example

if a > 2:

print 5

print a

I IF a > 2 : NEWLINE INDENT PRINT 5 NEWLINEDEDENT PRINT a NEWLINE

Ray Song Implementing a Simple Interpreter

Page 21: Implementing a Simple Interpreter

Grammatical analysis

I Cocke–Younger–Kasami algorithm

I Earley’s algorithm

I LR parser

I Recursive-descent parser

Ray Song Implementing a Simple Interpreter

Page 22: Implementing a Simple Interpreter

Grammatical analysis

I Cocke–Younger–Kasami algorithm

I Earley’s algorithm

I LR parser

I Recursive-descent parser

Ray Song Implementing a Simple Interpreter

Page 23: Implementing a Simple Interpreter

Grammatical analysis

I Cocke–Younger–Kasami algorithm

I Earley’s algorithm

I LR parser

I Recursive-descent parser

Ray Song Implementing a Simple Interpreter

Page 24: Implementing a Simple Interpreter

Grammatical analysis

I Cocke–Younger–Kasami algorithm

I Earley’s algorithm

I LR parser

I Recursive-descent parser

Ray Song Implementing a Simple Interpreter

Page 25: Implementing a Simple Interpreter

Tools

I Bison

I Can generate C, C++ and Java codes

I ANTLR

Ray Song Implementing a Simple Interpreter

Page 26: Implementing a Simple Interpreter

Tools

I Bison

I Can generate C, C++ and Java codes

I ANTLR

Ray Song Implementing a Simple Interpreter

Page 27: Implementing a Simple Interpreter

Tools

I Bison

I Can generate C, C++ and Java codes

I ANTLR

Ray Song Implementing a Simple Interpreter

Page 28: Implementing a Simple Interpreter

Expression

I Precedence climbing method

I Shunting-yard algorithm

I Parsing expressions in infix notation

I Output in Reverse Polish notation (RPN)

I Output in Abstract syntax tree (AST)

I Operator precedence parser

Ray Song Implementing a Simple Interpreter

Page 29: Implementing a Simple Interpreter

Expression

I Precedence climbing method

I Shunting-yard algorithm

I Parsing expressions in infix notation

I Output in Reverse Polish notation (RPN)

I Output in Abstract syntax tree (AST)

I Operator precedence parser

Ray Song Implementing a Simple Interpreter

Page 30: Implementing a Simple Interpreter

Expression

I Precedence climbing method

I Shunting-yard algorithm

I Parsing expressions in infix notation

I Output in Reverse Polish notation (RPN)

I Output in Abstract syntax tree (AST)

I Operator precedence parser

Ray Song Implementing a Simple Interpreter

Page 31: Implementing a Simple Interpreter

Expression

I Precedence climbing method

I Shunting-yard algorithm

I Parsing expressions in infix notation

I Output in Reverse Polish notation (RPN)

I Output in Abstract syntax tree (AST)

I Operator precedence parser

Ray Song Implementing a Simple Interpreter

Page 32: Implementing a Simple Interpreter

Expression

I Precedence climbing method

I Shunting-yard algorithm

I Parsing expressions in infix notation

I Output in Reverse Polish notation (RPN)

I Output in Abstract syntax tree (AST)

I Operator precedence parser

Ray Song Implementing a Simple Interpreter

Page 33: Implementing a Simple Interpreter

Expression

I Precedence climbing method

I Shunting-yard algorithm

I Parsing expressions in infix notation

I Output in Reverse Polish notation (RPN)

I Output in Abstract syntax tree (AST)

I Operator precedence parser

Ray Song Implementing a Simple Interpreter

Page 34: Implementing a Simple Interpreter

Parser combinator

I Straightforward to construct

I Readability

I Parsec

Ray Song Implementing a Simple Interpreter

Page 35: Implementing a Simple Interpreter

Parser combinator

I Straightforward to construct

I Readability

I Parsec

Ray Song Implementing a Simple Interpreter

Page 36: Implementing a Simple Interpreter

Parser combinator

I Straightforward to construct

I Readability

I Parsec

Ray Song Implementing a Simple Interpreter

Page 37: Implementing a Simple Interpreter

Impl

I Two top-level nonterminals: STMT and EXPR

I STMT: SIMPLE STMT ‘\n’ | COMPOUND

I SIMPLE STMT: EXPR

I SIMPLE STMT: IDENT ‘=’ EXPR

I SIMPLE STMT: BREAK

I SIMPLE STMT: print EXPR

I COMPOUND: if EXPR ’:’ SUITE OPT ELSE

I COMPOUND: while EXPR ’:’ SUITE

I SUITE: many1(‘\n’) INDENT many1(STMT) DEDENT

I SUITE: SIMPLE STMT ‘\n’

I OPT ELSE: ELSE ’:’ SUITE

I OPT ELSE: /* empty */

Ray Song Implementing a Simple Interpreter

Page 38: Implementing a Simple Interpreter

Impl

I Two top-level nonterminals: STMT and EXPR

I STMT: SIMPLE STMT ‘\n’ | COMPOUND

I SIMPLE STMT: EXPR

I SIMPLE STMT: IDENT ‘=’ EXPR

I SIMPLE STMT: BREAK

I SIMPLE STMT: print EXPR

I COMPOUND: if EXPR ’:’ SUITE OPT ELSE

I COMPOUND: while EXPR ’:’ SUITE

I SUITE: many1(‘\n’) INDENT many1(STMT) DEDENT

I SUITE: SIMPLE STMT ‘\n’

I OPT ELSE: ELSE ’:’ SUITE

I OPT ELSE: /* empty */

Ray Song Implementing a Simple Interpreter

Page 39: Implementing a Simple Interpreter

Impl

I Two top-level nonterminals: STMT and EXPR

I STMT: SIMPLE STMT ‘\n’ | COMPOUND

I SIMPLE STMT: EXPR

I SIMPLE STMT: IDENT ‘=’ EXPR

I SIMPLE STMT: BREAK

I SIMPLE STMT: print EXPR

I COMPOUND: if EXPR ’:’ SUITE OPT ELSE

I COMPOUND: while EXPR ’:’ SUITE

I SUITE: many1(‘\n’) INDENT many1(STMT) DEDENT

I SUITE: SIMPLE STMT ‘\n’

I OPT ELSE: ELSE ’:’ SUITE

I OPT ELSE: /* empty */

Ray Song Implementing a Simple Interpreter

Page 40: Implementing a Simple Interpreter

Impl

I Two top-level nonterminals: STMT and EXPR

I STMT: SIMPLE STMT ‘\n’ | COMPOUND

I SIMPLE STMT: EXPR

I SIMPLE STMT: IDENT ‘=’ EXPR

I SIMPLE STMT: BREAK

I SIMPLE STMT: print EXPR

I COMPOUND: if EXPR ’:’ SUITE OPT ELSE

I COMPOUND: while EXPR ’:’ SUITE

I SUITE: many1(‘\n’) INDENT many1(STMT) DEDENT

I SUITE: SIMPLE STMT ‘\n’

I OPT ELSE: ELSE ’:’ SUITE

I OPT ELSE: /* empty */

Ray Song Implementing a Simple Interpreter

Page 41: Implementing a Simple Interpreter

Impl

I Two top-level nonterminals: STMT and EXPR

I STMT: SIMPLE STMT ‘\n’ | COMPOUND

I SIMPLE STMT: EXPR

I SIMPLE STMT: IDENT ‘=’ EXPR

I SIMPLE STMT: BREAK

I SIMPLE STMT: print EXPR

I COMPOUND: if EXPR ’:’ SUITE OPT ELSE

I COMPOUND: while EXPR ’:’ SUITE

I SUITE: many1(‘\n’) INDENT many1(STMT) DEDENT

I SUITE: SIMPLE STMT ‘\n’

I OPT ELSE: ELSE ’:’ SUITE

I OPT ELSE: /* empty */

Ray Song Implementing a Simple Interpreter

Page 42: Implementing a Simple Interpreter

Impl

I Two top-level nonterminals: STMT and EXPR

I STMT: SIMPLE STMT ‘\n’ | COMPOUND

I SIMPLE STMT: EXPR

I SIMPLE STMT: IDENT ‘=’ EXPR

I SIMPLE STMT: BREAK

I SIMPLE STMT: print EXPR

I COMPOUND: if EXPR ’:’ SUITE OPT ELSE

I COMPOUND: while EXPR ’:’ SUITE

I SUITE: many1(‘\n’) INDENT many1(STMT) DEDENT

I SUITE: SIMPLE STMT ‘\n’

I OPT ELSE: ELSE ’:’ SUITE

I OPT ELSE: /* empty */

Ray Song Implementing a Simple Interpreter

Page 43: Implementing a Simple Interpreter

Impl

I Two top-level nonterminals: STMT and EXPR

I STMT: SIMPLE STMT ‘\n’ | COMPOUND

I SIMPLE STMT: EXPR

I SIMPLE STMT: IDENT ‘=’ EXPR

I SIMPLE STMT: BREAK

I SIMPLE STMT: print EXPR

I COMPOUND: if EXPR ’:’ SUITE OPT ELSE

I COMPOUND: while EXPR ’:’ SUITE

I SUITE: many1(‘\n’) INDENT many1(STMT) DEDENT

I SUITE: SIMPLE STMT ‘\n’

I OPT ELSE: ELSE ’:’ SUITE

I OPT ELSE: /* empty */

Ray Song Implementing a Simple Interpreter

Page 44: Implementing a Simple Interpreter

Impl

I Two top-level nonterminals: STMT and EXPR

I STMT: SIMPLE STMT ‘\n’ | COMPOUND

I SIMPLE STMT: EXPR

I SIMPLE STMT: IDENT ‘=’ EXPR

I SIMPLE STMT: BREAK

I SIMPLE STMT: print EXPR

I COMPOUND: if EXPR ’:’ SUITE OPT ELSE

I COMPOUND: while EXPR ’:’ SUITE

I SUITE: many1(‘\n’) INDENT many1(STMT) DEDENT

I SUITE: SIMPLE STMT ‘\n’

I OPT ELSE: ELSE ’:’ SUITE

I OPT ELSE: /* empty */

Ray Song Implementing a Simple Interpreter

Page 45: Implementing a Simple Interpreter

Impl

I Two top-level nonterminals: STMT and EXPR

I STMT: SIMPLE STMT ‘\n’ | COMPOUND

I SIMPLE STMT: EXPR

I SIMPLE STMT: IDENT ‘=’ EXPR

I SIMPLE STMT: BREAK

I SIMPLE STMT: print EXPR

I COMPOUND: if EXPR ’:’ SUITE OPT ELSE

I COMPOUND: while EXPR ’:’ SUITE

I SUITE: many1(‘\n’) INDENT many1(STMT) DEDENT

I SUITE: SIMPLE STMT ‘\n’

I OPT ELSE: ELSE ’:’ SUITE

I OPT ELSE: /* empty */

Ray Song Implementing a Simple Interpreter

Page 46: Implementing a Simple Interpreter

Impl

I Two top-level nonterminals: STMT and EXPR

I STMT: SIMPLE STMT ‘\n’ | COMPOUND

I SIMPLE STMT: EXPR

I SIMPLE STMT: IDENT ‘=’ EXPR

I SIMPLE STMT: BREAK

I SIMPLE STMT: print EXPR

I COMPOUND: if EXPR ’:’ SUITE OPT ELSE

I COMPOUND: while EXPR ’:’ SUITE

I SUITE: many1(‘\n’) INDENT many1(STMT) DEDENT

I SUITE: SIMPLE STMT ‘\n’

I OPT ELSE: ELSE ’:’ SUITE

I OPT ELSE: /* empty */

Ray Song Implementing a Simple Interpreter

Page 47: Implementing a Simple Interpreter

Impl

I Two top-level nonterminals: STMT and EXPR

I STMT: SIMPLE STMT ‘\n’ | COMPOUND

I SIMPLE STMT: EXPR

I SIMPLE STMT: IDENT ‘=’ EXPR

I SIMPLE STMT: BREAK

I SIMPLE STMT: print EXPR

I COMPOUND: if EXPR ’:’ SUITE OPT ELSE

I COMPOUND: while EXPR ’:’ SUITE

I SUITE: many1(‘\n’) INDENT many1(STMT) DEDENT

I SUITE: SIMPLE STMT ‘\n’

I OPT ELSE: ELSE ’:’ SUITE

I OPT ELSE: /* empty */

Ray Song Implementing a Simple Interpreter

Page 48: Implementing a Simple Interpreter

Impl

I Two top-level nonterminals: STMT and EXPR

I STMT: SIMPLE STMT ‘\n’ | COMPOUND

I SIMPLE STMT: EXPR

I SIMPLE STMT: IDENT ‘=’ EXPR

I SIMPLE STMT: BREAK

I SIMPLE STMT: print EXPR

I COMPOUND: if EXPR ’:’ SUITE OPT ELSE

I COMPOUND: while EXPR ’:’ SUITE

I SUITE: many1(‘\n’) INDENT many1(STMT) DEDENT

I SUITE: SIMPLE STMT ‘\n’

I OPT ELSE: ELSE ’:’ SUITE

I OPT ELSE: /* empty */

Ray Song Implementing a Simple Interpreter

Page 49: Implementing a Simple Interpreter

Impl - Cont.

I EXPR: EXPR ‘==’ TERM

I EXPR: EXPR ’!=’ TERM

I EXPR: TERM

I TERM: TERM ‘+’ FACTOR

I TERM: FACTOR

I FACTOR: FACTOR ‘*’ ATOM

I FACTOR: ATOM

I ATOM: identifier

I ATOM: literal integer

I ATOM: literal string

I ATOM: ‘(’ EXPR ’)’

Ray Song Implementing a Simple Interpreter

Page 50: Implementing a Simple Interpreter

Impl - Cont.

I EXPR: EXPR ‘==’ TERM

I EXPR: EXPR ’!=’ TERM

I EXPR: TERM

I TERM: TERM ‘+’ FACTOR

I TERM: FACTOR

I FACTOR: FACTOR ‘*’ ATOM

I FACTOR: ATOM

I ATOM: identifier

I ATOM: literal integer

I ATOM: literal string

I ATOM: ‘(’ EXPR ’)’

Ray Song Implementing a Simple Interpreter

Page 51: Implementing a Simple Interpreter

Impl - Cont.

I EXPR: EXPR ‘==’ TERM

I EXPR: EXPR ’!=’ TERM

I EXPR: TERM

I TERM: TERM ‘+’ FACTOR

I TERM: FACTOR

I FACTOR: FACTOR ‘*’ ATOM

I FACTOR: ATOM

I ATOM: identifier

I ATOM: literal integer

I ATOM: literal string

I ATOM: ‘(’ EXPR ’)’

Ray Song Implementing a Simple Interpreter

Page 52: Implementing a Simple Interpreter

Impl - Cont.

I EXPR: EXPR ‘==’ TERM

I EXPR: EXPR ’!=’ TERM

I EXPR: TERM

I TERM: TERM ‘+’ FACTOR

I TERM: FACTOR

I FACTOR: FACTOR ‘*’ ATOM

I FACTOR: ATOM

I ATOM: identifier

I ATOM: literal integer

I ATOM: literal string

I ATOM: ‘(’ EXPR ’)’

Ray Song Implementing a Simple Interpreter

Page 53: Implementing a Simple Interpreter

Impl - Cont.

I EXPR: EXPR ‘==’ TERM

I EXPR: EXPR ’!=’ TERM

I EXPR: TERM

I TERM: TERM ‘+’ FACTOR

I TERM: FACTOR

I FACTOR: FACTOR ‘*’ ATOM

I FACTOR: ATOM

I ATOM: identifier

I ATOM: literal integer

I ATOM: literal string

I ATOM: ‘(’ EXPR ’)’

Ray Song Implementing a Simple Interpreter

Page 54: Implementing a Simple Interpreter

Impl - Cont.

I EXPR: EXPR ‘==’ TERM

I EXPR: EXPR ’!=’ TERM

I EXPR: TERM

I TERM: TERM ‘+’ FACTOR

I TERM: FACTOR

I FACTOR: FACTOR ‘*’ ATOM

I FACTOR: ATOM

I ATOM: identifier

I ATOM: literal integer

I ATOM: literal string

I ATOM: ‘(’ EXPR ’)’

Ray Song Implementing a Simple Interpreter

Page 55: Implementing a Simple Interpreter

Impl - Cont.

I EXPR: EXPR ‘==’ TERM

I EXPR: EXPR ’!=’ TERM

I EXPR: TERM

I TERM: TERM ‘+’ FACTOR

I TERM: FACTOR

I FACTOR: FACTOR ‘*’ ATOM

I FACTOR: ATOM

I ATOM: identifier

I ATOM: literal integer

I ATOM: literal string

I ATOM: ‘(’ EXPR ’)’

Ray Song Implementing a Simple Interpreter

Page 56: Implementing a Simple Interpreter

Impl - Cont.

I EXPR: EXPR ‘==’ TERM

I EXPR: EXPR ’!=’ TERM

I EXPR: TERM

I TERM: TERM ‘+’ FACTOR

I TERM: FACTOR

I FACTOR: FACTOR ‘*’ ATOM

I FACTOR: ATOM

I ATOM: identifier

I ATOM: literal integer

I ATOM: literal string

I ATOM: ‘(’ EXPR ’)’

Ray Song Implementing a Simple Interpreter

Page 57: Implementing a Simple Interpreter

Impl - Cont.

I EXPR: EXPR ‘==’ TERM

I EXPR: EXPR ’!=’ TERM

I EXPR: TERM

I TERM: TERM ‘+’ FACTOR

I TERM: FACTOR

I FACTOR: FACTOR ‘*’ ATOM

I FACTOR: ATOM

I ATOM: identifier

I ATOM: literal integer

I ATOM: literal string

I ATOM: ‘(’ EXPR ’)’

Ray Song Implementing a Simple Interpreter

Page 58: Implementing a Simple Interpreter

Impl - Cont.

I EXPR: EXPR ‘==’ TERM

I EXPR: EXPR ’!=’ TERM

I EXPR: TERM

I TERM: TERM ‘+’ FACTOR

I TERM: FACTOR

I FACTOR: FACTOR ‘*’ ATOM

I FACTOR: ATOM

I ATOM: identifier

I ATOM: literal integer

I ATOM: literal string

I ATOM: ‘(’ EXPR ’)’

Ray Song Implementing a Simple Interpreter

Page 59: Implementing a Simple Interpreter

Impl - Cont.

I EXPR: EXPR ‘==’ TERM

I EXPR: EXPR ’!=’ TERM

I EXPR: TERM

I TERM: TERM ‘+’ FACTOR

I TERM: FACTOR

I FACTOR: FACTOR ‘*’ ATOM

I FACTOR: ATOM

I ATOM: identifier

I ATOM: literal integer

I ATOM: literal string

I ATOM: ‘(’ EXPR ’)’

Ray Song Implementing a Simple Interpreter

Page 60: Implementing a Simple Interpreter

Example

if a > 2:

print 5

print a

Ray Song Implementing a Simple Interpreter

Page 61: Implementing a Simple Interpreter

Example - Cont.

Figure : Parse

Ray Song Implementing a Simple Interpreter

Page 62: Implementing a Simple Interpreter

Interpreting

I Abstract syntax tree (AST).

I Define semantics for each class of nodes

I object(atom): trivial

I binary operator BinOP(operator, lhs, rhs, RESULT) :- obj1 =eval(lhs), obj2 = eval(rhs), calc(op, obj1, obj2, RESULT).

I Object & BinOP inherit from Expr

Ray Song Implementing a Simple Interpreter

Page 63: Implementing a Simple Interpreter

Interpreting

I Abstract syntax tree (AST).

I Define semantics for each class of nodes

I object(atom): trivial

I binary operator BinOP(operator, lhs, rhs, RESULT) :- obj1 =eval(lhs), obj2 = eval(rhs), calc(op, obj1, obj2, RESULT).

I Object & BinOP inherit from Expr

Ray Song Implementing a Simple Interpreter

Page 64: Implementing a Simple Interpreter

Interpreting

I Abstract syntax tree (AST).

I Define semantics for each class of nodes

I object(atom): trivial

I binary operator BinOP(operator, lhs, rhs, RESULT) :- obj1 =eval(lhs), obj2 = eval(rhs), calc(op, obj1, obj2, RESULT).

I Object & BinOP inherit from Expr

Ray Song Implementing a Simple Interpreter

Page 65: Implementing a Simple Interpreter

Interpreting

I Abstract syntax tree (AST).

I Define semantics for each class of nodes

I object(atom): trivial

I binary operator BinOP(operator, lhs, rhs, RESULT) :- obj1 =eval(lhs), obj2 = eval(rhs), calc(op, obj1, obj2, RESULT).

I Object & BinOP inherit from Expr

Ray Song Implementing a Simple Interpreter

Page 66: Implementing a Simple Interpreter

Interpreting

I Abstract syntax tree (AST).

I Define semantics for each class of nodes

I object(atom): trivial

I binary operator BinOP(operator, lhs, rhs, RESULT) :- obj1 =eval(lhs), obj2 = eval(rhs), calc(op, obj1, obj2, RESULT).

I Object & BinOP inherit from Expr

Ray Song Implementing a Simple Interpreter

Page 67: Implementing a Simple Interpreter

Subclasses of Stmt - Cont.

I Assign

I eval() need a parameter: Binding (which variable holds whichobject)

I ExprStmt

I Print

I Continue (throwing an exception)

Ray Song Implementing a Simple Interpreter

Page 68: Implementing a Simple Interpreter

Subclasses of Stmt - Cont.

I Assign

I eval() need a parameter: Binding (which variable holds whichobject)

I ExprStmt

I Print

I Continue (throwing an exception)

Ray Song Implementing a Simple Interpreter

Page 69: Implementing a Simple Interpreter

Subclasses of Stmt - Cont.

I Assign

I eval() need a parameter: Binding (which variable holds whichobject)

I ExprStmt

I Print

I Continue (throwing an exception)

Ray Song Implementing a Simple Interpreter

Page 70: Implementing a Simple Interpreter

Subclasses of Stmt - Cont.

I Assign

I eval() need a parameter: Binding (which variable holds whichobject)

I ExprStmt

I Print

I Continue (throwing an exception)

Ray Song Implementing a Simple Interpreter

Page 71: Implementing a Simple Interpreter

Subclasses of Stmt - Cont.

I Assign

I eval() need a parameter: Binding (which variable holds whichobject)

I ExprStmt

I Print

I Continue (throwing an exception)

Ray Song Implementing a Simple Interpreter


Recommended