SlideShare a Scribd company logo
Implementing a Simple Interpreter
Ray Song

May 27, 2012

Ray Song

Implementing a Simple Interpreter
Flow sheet

Lexical analysis
Grammatical analysis

Figure : Flow

Ray Song

Implementing a Simple Interpreter
Flow sheet

Lexical analysis
Grammatical analysis

Figure : Flow

Ray Song

Implementing a Simple Interpreter
Lexical Analysis

manual
flex

Ray Song

Implementing a Simple Interpreter
Lexical Analysis

manual
flex

Ray Song

Implementing a Simple Interpreter
Tokens

identifier
integer
string
while
if
else
print
NEWLINE
NO WHITESPACE

Ray Song

Implementing a Simple Interpreter
Tokens

identifier
integer
string
while
if
else
print
NEWLINE
NO WHITESPACE

Ray Song

Implementing a Simple Interpreter
Tokens

identifier
integer
string
while
if
else
print
NEWLINE
NO WHITESPACE

Ray Song

Implementing a Simple Interpreter
Tokens

identifier
integer
string
while
if
else
print
NEWLINE
NO WHITESPACE

Ray Song

Implementing a Simple Interpreter
Tokens

identifier
integer
string
while
if
else
print
NEWLINE
NO WHITESPACE

Ray Song

Implementing a Simple Interpreter
Tokens

identifier
integer
string
while
if
else
print
NEWLINE
NO WHITESPACE

Ray Song

Implementing a Simple Interpreter
Tokens

identifier
integer
string
while
if
else
print
NEWLINE
NO WHITESPACE

Ray Song

Implementing a Simple Interpreter
Tokens

identifier
integer
string
while
if
else
print
NEWLINE
NO WHITESPACE

Ray Song

Implementing a Simple Interpreter
Tokens

identifier
integer
string
while
if
else
print
NEWLINE
NO WHITESPACE

Ray Song

Implementing a Simple Interpreter
Off-side rule

def hello():
print ‘world’
indentation sensitive
Ex: ISWIM(1966), occam(1983), Miranda(1985),
Haskell(1990), Python(1991)

Ray Song

Implementing a Simple Interpreter
Off-side rule

def hello():
print ‘world’
indentation sensitive
Ex: ISWIM(1966), occam(1983), Miranda(1985),
Haskell(1990), Python(1991)

Ray Song

Implementing a Simple Interpreter
Off-side rule - Cont.

represented as virtual tokens
INDENT
DEDENT

Ray Song

Implementing a Simple Interpreter
Off-side rule - Cont.

represented as virtual tokens
INDENT
DEDENT

Ray Song

Implementing a Simple Interpreter
Off-side rule - Cont.

represented as virtual tokens
INDENT
DEDENT

Ray Song

Implementing a Simple Interpreter
Example

if a > 2:
print 5
print a
IF a > 2 : NEWLINE INDENT PRINT 5 NEWLINE
DEDENT PRINT a NEWLINE

Ray Song

Implementing a Simple Interpreter
Grammatical analysis

Cocke–Younger–Kasami algorithm
Earley’s algorithm
LR parser
Recursive-descent parser

Ray Song

Implementing a Simple Interpreter
Grammatical analysis

Cocke–Younger–Kasami algorithm
Earley’s algorithm
LR parser
Recursive-descent parser

Ray Song

Implementing a Simple Interpreter
Grammatical analysis

Cocke–Younger–Kasami algorithm
Earley’s algorithm
LR parser
Recursive-descent parser

Ray Song

Implementing a Simple Interpreter
Grammatical analysis

Cocke–Younger–Kasami algorithm
Earley’s algorithm
LR parser
Recursive-descent parser

Ray Song

Implementing a Simple Interpreter
Tools

Bison
Can generate C, C++ and Java codes
ANTLR

Ray Song

Implementing a Simple Interpreter
Tools

Bison
Can generate C, C++ and Java codes
ANTLR

Ray Song

Implementing a Simple Interpreter
Tools

Bison
Can generate C, C++ and Java codes
ANTLR

Ray Song

Implementing a Simple Interpreter
Expression

Precedence climbing method
Shunting-yard algorithm
Parsing expressions in infix notation
Output in Reverse Polish notation (RPN)
Output in Abstract syntax tree (AST)
Operator precedence parser

Ray Song

Implementing a Simple Interpreter
Expression

Precedence climbing method
Shunting-yard algorithm
Parsing expressions in infix notation
Output in Reverse Polish notation (RPN)
Output in Abstract syntax tree (AST)
Operator precedence parser

Ray Song

Implementing a Simple Interpreter
Expression

Precedence climbing method
Shunting-yard algorithm
Parsing expressions in infix notation
Output in Reverse Polish notation (RPN)
Output in Abstract syntax tree (AST)
Operator precedence parser

Ray Song

Implementing a Simple Interpreter
Expression

Precedence climbing method
Shunting-yard algorithm
Parsing expressions in infix notation
Output in Reverse Polish notation (RPN)
Output in Abstract syntax tree (AST)
Operator precedence parser

Ray Song

Implementing a Simple Interpreter
Expression

Precedence climbing method
Shunting-yard algorithm
Parsing expressions in infix notation
Output in Reverse Polish notation (RPN)
Output in Abstract syntax tree (AST)
Operator precedence parser

Ray Song

Implementing a Simple Interpreter
Expression

Precedence climbing method
Shunting-yard algorithm
Parsing expressions in infix notation
Output in Reverse Polish notation (RPN)
Output in Abstract syntax tree (AST)
Operator precedence parser

Ray Song

Implementing a Simple Interpreter
Parser combinator

Straightforward to construct
Readability
Parsec

Ray Song

Implementing a Simple Interpreter
Parser combinator

Straightforward to construct
Readability
Parsec

Ray Song

Implementing a Simple Interpreter
Parser combinator

Straightforward to construct
Readability
Parsec

Ray Song

Implementing a Simple Interpreter
Impl
Two top-level nonterminals: STMT and EXPR
STMT: SIMPLE STMT ‘n’ | COMPOUND
SIMPLE STMT: EXPR
SIMPLE STMT: IDENT ‘=’ EXPR
SIMPLE STMT: BREAK
SIMPLE STMT: print EXPR
COMPOUND: if EXPR ’:’ SUITE OPT ELSE
COMPOUND: while EXPR ’:’ SUITE
SUITE: many1(‘n’) INDENT many1(STMT) DEDENT
SUITE: SIMPLE STMT ‘n’
OPT ELSE: ELSE ’:’ SUITE
OPT ELSE: /* empty */
Ray Song

Implementing a Simple Interpreter
Impl
Two top-level nonterminals: STMT and EXPR
STMT: SIMPLE STMT ‘n’ | COMPOUND
SIMPLE STMT: EXPR
SIMPLE STMT: IDENT ‘=’ EXPR
SIMPLE STMT: BREAK
SIMPLE STMT: print EXPR
COMPOUND: if EXPR ’:’ SUITE OPT ELSE
COMPOUND: while EXPR ’:’ SUITE
SUITE: many1(‘n’) INDENT many1(STMT) DEDENT
SUITE: SIMPLE STMT ‘n’
OPT ELSE: ELSE ’:’ SUITE
OPT ELSE: /* empty */
Ray Song

Implementing a Simple Interpreter
Impl
Two top-level nonterminals: STMT and EXPR
STMT: SIMPLE STMT ‘n’ | COMPOUND
SIMPLE STMT: EXPR
SIMPLE STMT: IDENT ‘=’ EXPR
SIMPLE STMT: BREAK
SIMPLE STMT: print EXPR
COMPOUND: if EXPR ’:’ SUITE OPT ELSE
COMPOUND: while EXPR ’:’ SUITE
SUITE: many1(‘n’) INDENT many1(STMT) DEDENT
SUITE: SIMPLE STMT ‘n’
OPT ELSE: ELSE ’:’ SUITE
OPT ELSE: /* empty */
Ray Song

Implementing a Simple Interpreter
Impl
Two top-level nonterminals: STMT and EXPR
STMT: SIMPLE STMT ‘n’ | COMPOUND
SIMPLE STMT: EXPR
SIMPLE STMT: IDENT ‘=’ EXPR
SIMPLE STMT: BREAK
SIMPLE STMT: print EXPR
COMPOUND: if EXPR ’:’ SUITE OPT ELSE
COMPOUND: while EXPR ’:’ SUITE
SUITE: many1(‘n’) INDENT many1(STMT) DEDENT
SUITE: SIMPLE STMT ‘n’
OPT ELSE: ELSE ’:’ SUITE
OPT ELSE: /* empty */
Ray Song

Implementing a Simple Interpreter
Impl
Two top-level nonterminals: STMT and EXPR
STMT: SIMPLE STMT ‘n’ | COMPOUND
SIMPLE STMT: EXPR
SIMPLE STMT: IDENT ‘=’ EXPR
SIMPLE STMT: BREAK
SIMPLE STMT: print EXPR
COMPOUND: if EXPR ’:’ SUITE OPT ELSE
COMPOUND: while EXPR ’:’ SUITE
SUITE: many1(‘n’) INDENT many1(STMT) DEDENT
SUITE: SIMPLE STMT ‘n’
OPT ELSE: ELSE ’:’ SUITE
OPT ELSE: /* empty */
Ray Song

Implementing a Simple Interpreter
Impl
Two top-level nonterminals: STMT and EXPR
STMT: SIMPLE STMT ‘n’ | COMPOUND
SIMPLE STMT: EXPR
SIMPLE STMT: IDENT ‘=’ EXPR
SIMPLE STMT: BREAK
SIMPLE STMT: print EXPR
COMPOUND: if EXPR ’:’ SUITE OPT ELSE
COMPOUND: while EXPR ’:’ SUITE
SUITE: many1(‘n’) INDENT many1(STMT) DEDENT
SUITE: SIMPLE STMT ‘n’
OPT ELSE: ELSE ’:’ SUITE
OPT ELSE: /* empty */
Ray Song

Implementing a Simple Interpreter
Impl
Two top-level nonterminals: STMT and EXPR
STMT: SIMPLE STMT ‘n’ | COMPOUND
SIMPLE STMT: EXPR
SIMPLE STMT: IDENT ‘=’ EXPR
SIMPLE STMT: BREAK
SIMPLE STMT: print EXPR
COMPOUND: if EXPR ’:’ SUITE OPT ELSE
COMPOUND: while EXPR ’:’ SUITE
SUITE: many1(‘n’) INDENT many1(STMT) DEDENT
SUITE: SIMPLE STMT ‘n’
OPT ELSE: ELSE ’:’ SUITE
OPT ELSE: /* empty */
Ray Song

Implementing a Simple Interpreter
Impl
Two top-level nonterminals: STMT and EXPR
STMT: SIMPLE STMT ‘n’ | COMPOUND
SIMPLE STMT: EXPR
SIMPLE STMT: IDENT ‘=’ EXPR
SIMPLE STMT: BREAK
SIMPLE STMT: print EXPR
COMPOUND: if EXPR ’:’ SUITE OPT ELSE
COMPOUND: while EXPR ’:’ SUITE
SUITE: many1(‘n’) INDENT many1(STMT) DEDENT
SUITE: SIMPLE STMT ‘n’
OPT ELSE: ELSE ’:’ SUITE
OPT ELSE: /* empty */
Ray Song

Implementing a Simple Interpreter
Impl
Two top-level nonterminals: STMT and EXPR
STMT: SIMPLE STMT ‘n’ | COMPOUND
SIMPLE STMT: EXPR
SIMPLE STMT: IDENT ‘=’ EXPR
SIMPLE STMT: BREAK
SIMPLE STMT: print EXPR
COMPOUND: if EXPR ’:’ SUITE OPT ELSE
COMPOUND: while EXPR ’:’ SUITE
SUITE: many1(‘n’) INDENT many1(STMT) DEDENT
SUITE: SIMPLE STMT ‘n’
OPT ELSE: ELSE ’:’ SUITE
OPT ELSE: /* empty */
Ray Song

Implementing a Simple Interpreter
Impl
Two top-level nonterminals: STMT and EXPR
STMT: SIMPLE STMT ‘n’ | COMPOUND
SIMPLE STMT: EXPR
SIMPLE STMT: IDENT ‘=’ EXPR
SIMPLE STMT: BREAK
SIMPLE STMT: print EXPR
COMPOUND: if EXPR ’:’ SUITE OPT ELSE
COMPOUND: while EXPR ’:’ SUITE
SUITE: many1(‘n’) INDENT many1(STMT) DEDENT
SUITE: SIMPLE STMT ‘n’
OPT ELSE: ELSE ’:’ SUITE
OPT ELSE: /* empty */
Ray Song

Implementing a Simple Interpreter
Impl
Two top-level nonterminals: STMT and EXPR
STMT: SIMPLE STMT ‘n’ | COMPOUND
SIMPLE STMT: EXPR
SIMPLE STMT: IDENT ‘=’ EXPR
SIMPLE STMT: BREAK
SIMPLE STMT: print EXPR
COMPOUND: if EXPR ’:’ SUITE OPT ELSE
COMPOUND: while EXPR ’:’ SUITE
SUITE: many1(‘n’) INDENT many1(STMT) DEDENT
SUITE: SIMPLE STMT ‘n’
OPT ELSE: ELSE ’:’ SUITE
OPT ELSE: /* empty */
Ray Song

Implementing a Simple Interpreter
Impl
Two top-level nonterminals: STMT and EXPR
STMT: SIMPLE STMT ‘n’ | COMPOUND
SIMPLE STMT: EXPR
SIMPLE STMT: IDENT ‘=’ EXPR
SIMPLE STMT: BREAK
SIMPLE STMT: print EXPR
COMPOUND: if EXPR ’:’ SUITE OPT ELSE
COMPOUND: while EXPR ’:’ SUITE
SUITE: many1(‘n’) INDENT many1(STMT) DEDENT
SUITE: SIMPLE STMT ‘n’
OPT ELSE: ELSE ’:’ SUITE
OPT ELSE: /* empty */
Ray Song

Implementing a Simple Interpreter
Impl - Cont.
EXPR: EXPR ‘==’ TERM
EXPR: EXPR ’ !=’ TERM
EXPR: TERM
TERM: TERM ‘+’ FACTOR
TERM: FACTOR
FACTOR: FACTOR ‘*’ ATOM
FACTOR: ATOM
ATOM: identifier
ATOM: literal integer
ATOM: literal string
ATOM: ‘(’ EXPR ’)’

Ray Song

Implementing a Simple Interpreter
Impl - Cont.
EXPR: EXPR ‘==’ TERM
EXPR: EXPR ’ !=’ TERM
EXPR: TERM
TERM: TERM ‘+’ FACTOR
TERM: FACTOR
FACTOR: FACTOR ‘*’ ATOM
FACTOR: ATOM
ATOM: identifier
ATOM: literal integer
ATOM: literal string
ATOM: ‘(’ EXPR ’)’

Ray Song

Implementing a Simple Interpreter
Impl - Cont.
EXPR: EXPR ‘==’ TERM
EXPR: EXPR ’ !=’ TERM
EXPR: TERM
TERM: TERM ‘+’ FACTOR
TERM: FACTOR
FACTOR: FACTOR ‘*’ ATOM
FACTOR: ATOM
ATOM: identifier
ATOM: literal integer
ATOM: literal string
ATOM: ‘(’ EXPR ’)’

Ray Song

Implementing a Simple Interpreter
Impl - Cont.
EXPR: EXPR ‘==’ TERM
EXPR: EXPR ’ !=’ TERM
EXPR: TERM
TERM: TERM ‘+’ FACTOR
TERM: FACTOR
FACTOR: FACTOR ‘*’ ATOM
FACTOR: ATOM
ATOM: identifier
ATOM: literal integer
ATOM: literal string
ATOM: ‘(’ EXPR ’)’

Ray Song

Implementing a Simple Interpreter
Impl - Cont.
EXPR: EXPR ‘==’ TERM
EXPR: EXPR ’ !=’ TERM
EXPR: TERM
TERM: TERM ‘+’ FACTOR
TERM: FACTOR
FACTOR: FACTOR ‘*’ ATOM
FACTOR: ATOM
ATOM: identifier
ATOM: literal integer
ATOM: literal string
ATOM: ‘(’ EXPR ’)’

Ray Song

Implementing a Simple Interpreter
Impl - Cont.
EXPR: EXPR ‘==’ TERM
EXPR: EXPR ’ !=’ TERM
EXPR: TERM
TERM: TERM ‘+’ FACTOR
TERM: FACTOR
FACTOR: FACTOR ‘*’ ATOM
FACTOR: ATOM
ATOM: identifier
ATOM: literal integer
ATOM: literal string
ATOM: ‘(’ EXPR ’)’

Ray Song

Implementing a Simple Interpreter
Impl - Cont.
EXPR: EXPR ‘==’ TERM
EXPR: EXPR ’ !=’ TERM
EXPR: TERM
TERM: TERM ‘+’ FACTOR
TERM: FACTOR
FACTOR: FACTOR ‘*’ ATOM
FACTOR: ATOM
ATOM: identifier
ATOM: literal integer
ATOM: literal string
ATOM: ‘(’ EXPR ’)’

Ray Song

Implementing a Simple Interpreter
Impl - Cont.
EXPR: EXPR ‘==’ TERM
EXPR: EXPR ’ !=’ TERM
EXPR: TERM
TERM: TERM ‘+’ FACTOR
TERM: FACTOR
FACTOR: FACTOR ‘*’ ATOM
FACTOR: ATOM
ATOM: identifier
ATOM: literal integer
ATOM: literal string
ATOM: ‘(’ EXPR ’)’

Ray Song

Implementing a Simple Interpreter
Impl - Cont.
EXPR: EXPR ‘==’ TERM
EXPR: EXPR ’ !=’ TERM
EXPR: TERM
TERM: TERM ‘+’ FACTOR
TERM: FACTOR
FACTOR: FACTOR ‘*’ ATOM
FACTOR: ATOM
ATOM: identifier
ATOM: literal integer
ATOM: literal string
ATOM: ‘(’ EXPR ’)’

Ray Song

Implementing a Simple Interpreter
Impl - Cont.
EXPR: EXPR ‘==’ TERM
EXPR: EXPR ’ !=’ TERM
EXPR: TERM
TERM: TERM ‘+’ FACTOR
TERM: FACTOR
FACTOR: FACTOR ‘*’ ATOM
FACTOR: ATOM
ATOM: identifier
ATOM: literal integer
ATOM: literal string
ATOM: ‘(’ EXPR ’)’

Ray Song

Implementing a Simple Interpreter
Impl - Cont.
EXPR: EXPR ‘==’ TERM
EXPR: EXPR ’ !=’ TERM
EXPR: TERM
TERM: TERM ‘+’ FACTOR
TERM: FACTOR
FACTOR: FACTOR ‘*’ ATOM
FACTOR: ATOM
ATOM: identifier
ATOM: literal integer
ATOM: literal string
ATOM: ‘(’ EXPR ’)’

Ray Song

Implementing a Simple Interpreter
Example

if a > 2:
print 5
print a

Ray Song

Implementing a Simple Interpreter
Example - Cont.

Figure : Parse
Ray Song

Implementing a Simple Interpreter
Interpreting

Abstract syntax tree (AST).
Define semantics for each class of nodes
object(atom): trivial
binary operator BinOP(operator, lhs, rhs, RESULT) :- obj1 =
eval(lhs), obj2 = eval(rhs), calc(op, obj1, obj2, RESULT).
Object & BinOP inherit from Expr

Ray Song

Implementing a Simple Interpreter
Interpreting

Abstract syntax tree (AST).
Define semantics for each class of nodes
object(atom): trivial
binary operator BinOP(operator, lhs, rhs, RESULT) :- obj1 =
eval(lhs), obj2 = eval(rhs), calc(op, obj1, obj2, RESULT).
Object & BinOP inherit from Expr

Ray Song

Implementing a Simple Interpreter
Interpreting

Abstract syntax tree (AST).
Define semantics for each class of nodes
object(atom): trivial
binary operator BinOP(operator, lhs, rhs, RESULT) :- obj1 =
eval(lhs), obj2 = eval(rhs), calc(op, obj1, obj2, RESULT).
Object & BinOP inherit from Expr

Ray Song

Implementing a Simple Interpreter
Interpreting

Abstract syntax tree (AST).
Define semantics for each class of nodes
object(atom): trivial
binary operator BinOP(operator, lhs, rhs, RESULT) :- obj1 =
eval(lhs), obj2 = eval(rhs), calc(op, obj1, obj2, RESULT).
Object & BinOP inherit from Expr

Ray Song

Implementing a Simple Interpreter
Interpreting

Abstract syntax tree (AST).
Define semantics for each class of nodes
object(atom): trivial
binary operator BinOP(operator, lhs, rhs, RESULT) :- obj1 =
eval(lhs), obj2 = eval(rhs), calc(op, obj1, obj2, RESULT).
Object & BinOP inherit from Expr

Ray Song

Implementing a Simple Interpreter
Subclasses of Stmt - Cont.

Assign
eval() need a parameter: Binding (which variable holds which
object)
ExprStmt
Print
Continue (throwing an exception)

Ray Song

Implementing a Simple Interpreter
Subclasses of Stmt - Cont.

Assign
eval() need a parameter: Binding (which variable holds which
object)
ExprStmt
Print
Continue (throwing an exception)

Ray Song

Implementing a Simple Interpreter
Subclasses of Stmt - Cont.

Assign
eval() need a parameter: Binding (which variable holds which
object)
ExprStmt
Print
Continue (throwing an exception)

Ray Song

Implementing a Simple Interpreter
Subclasses of Stmt - Cont.

Assign
eval() need a parameter: Binding (which variable holds which
object)
ExprStmt
Print
Continue (throwing an exception)

Ray Song

Implementing a Simple Interpreter
Subclasses of Stmt - Cont.

Assign
eval() need a parameter: Binding (which variable holds which
object)
ExprStmt
Print
Continue (throwing an exception)

Ray Song

Implementing a Simple Interpreter

More Related Content

Viewers also liked

Skills Required To Become a Professional Translator
Skills Required To Become a Professional TranslatorSkills Required To Become a Professional Translator
Skills Required To Become a Professional Translator
HSS Translation
 
NYC Parking Tickets debunking 3 myths
NYC Parking Tickets debunking 3 mythsNYC Parking Tickets debunking 3 myths
NYC Parking Tickets debunking 3 myths
Lawrence Berezin
 
Thanks for Making That Mistake! Using Errors as an Interpreter Teaching Tool
Thanks for Making That Mistake! Using Errors as an Interpreter Teaching ToolThanks for Making That Mistake! Using Errors as an Interpreter Teaching Tool
Thanks for Making That Mistake! Using Errors as an Interpreter Teaching Tool
National Council on Interpreting in Health Care (NCIHC)
 
Translation and Interpretation
Translation and InterpretationTranslation and Interpretation
Translation and Interpretationwendo1513
 
Types and modes of interpretation
Types and modes of interpretationTypes and modes of interpretation
Types and modes of interpretation
Jorge Carro
 
Translation vs. Interpretation
Translation vs. Interpretation Translation vs. Interpretation
Translation vs. Interpretation
Rolando Tellez
 
Translation vs. Interpretation
Translation vs. Interpretation Translation vs. Interpretation
Translation vs. Interpretation
Rolando Tellez
 
Translation Types
Translation TypesTranslation Types
Translation Types
Elena Shapa
 

Viewers also liked (9)

Skills Required To Become a Professional Translator
Skills Required To Become a Professional TranslatorSkills Required To Become a Professional Translator
Skills Required To Become a Professional Translator
 
NYC Parking Tickets debunking 3 myths
NYC Parking Tickets debunking 3 mythsNYC Parking Tickets debunking 3 myths
NYC Parking Tickets debunking 3 myths
 
Thanks for Making That Mistake! Using Errors as an Interpreter Teaching Tool
Thanks for Making That Mistake! Using Errors as an Interpreter Teaching ToolThanks for Making That Mistake! Using Errors as an Interpreter Teaching Tool
Thanks for Making That Mistake! Using Errors as an Interpreter Teaching Tool
 
Translation and Interpretation
Translation and InterpretationTranslation and Interpretation
Translation and Interpretation
 
Types and modes of interpretation
Types and modes of interpretationTypes and modes of interpretation
Types and modes of interpretation
 
Translation vs. Interpretation
Translation vs. Interpretation Translation vs. Interpretation
Translation vs. Interpretation
 
Translation vs. Interpretation
Translation vs. Interpretation Translation vs. Interpretation
Translation vs. Interpretation
 
Methods Of Translation
Methods Of TranslationMethods Of Translation
Methods Of Translation
 
Translation Types
Translation TypesTranslation Types
Translation Types
 

Similar to Implementing a Simple Interpreter

lect26-em.ppt
lect26-em.pptlect26-em.ppt
lect26-em.ppt
SYAMDAVULURI
 
WINSEM2022-23_CSI2005_TH_VL2022230504110_Reference_Material_II_22-12-2022_1.2...
WINSEM2022-23_CSI2005_TH_VL2022230504110_Reference_Material_II_22-12-2022_1.2...WINSEM2022-23_CSI2005_TH_VL2022230504110_Reference_Material_II_22-12-2022_1.2...
WINSEM2022-23_CSI2005_TH_VL2022230504110_Reference_Material_II_22-12-2022_1.2...
Reddyjanardhan221
 
5-Introduction to Parsing and Context Free Grammar-09-05-2023.pptx
5-Introduction to Parsing and Context Free Grammar-09-05-2023.pptx5-Introduction to Parsing and Context Free Grammar-09-05-2023.pptx
5-Introduction to Parsing and Context Free Grammar-09-05-2023.pptx
venkatapranaykumarGa
 
Finite automata-for-lexical-analysis
Finite automata-for-lexical-analysisFinite automata-for-lexical-analysis
Finite automata-for-lexical-analysis
Dattatray Gandhmal
 
Compiler Construction | Lecture 5 | Transformation by Term Rewriting
Compiler Construction | Lecture 5 | Transformation by Term RewritingCompiler Construction | Lecture 5 | Transformation by Term Rewriting
Compiler Construction | Lecture 5 | Transformation by Term Rewriting
Eelco Visser
 
Speech To Sign Language Interpreter System
Speech To Sign Language Interpreter SystemSpeech To Sign Language Interpreter System
Speech To Sign Language Interpreter Systemkkkseld
 
Ll(1) Parser in Compilers
Ll(1) Parser in CompilersLl(1) Parser in Compilers
Ll(1) Parser in Compilers
Mahbubur Rahman
 

Similar to Implementing a Simple Interpreter (7)

lect26-em.ppt
lect26-em.pptlect26-em.ppt
lect26-em.ppt
 
WINSEM2022-23_CSI2005_TH_VL2022230504110_Reference_Material_II_22-12-2022_1.2...
WINSEM2022-23_CSI2005_TH_VL2022230504110_Reference_Material_II_22-12-2022_1.2...WINSEM2022-23_CSI2005_TH_VL2022230504110_Reference_Material_II_22-12-2022_1.2...
WINSEM2022-23_CSI2005_TH_VL2022230504110_Reference_Material_II_22-12-2022_1.2...
 
5-Introduction to Parsing and Context Free Grammar-09-05-2023.pptx
5-Introduction to Parsing and Context Free Grammar-09-05-2023.pptx5-Introduction to Parsing and Context Free Grammar-09-05-2023.pptx
5-Introduction to Parsing and Context Free Grammar-09-05-2023.pptx
 
Finite automata-for-lexical-analysis
Finite automata-for-lexical-analysisFinite automata-for-lexical-analysis
Finite automata-for-lexical-analysis
 
Compiler Construction | Lecture 5 | Transformation by Term Rewriting
Compiler Construction | Lecture 5 | Transformation by Term RewritingCompiler Construction | Lecture 5 | Transformation by Term Rewriting
Compiler Construction | Lecture 5 | Transformation by Term Rewriting
 
Speech To Sign Language Interpreter System
Speech To Sign Language Interpreter SystemSpeech To Sign Language Interpreter System
Speech To Sign Language Interpreter System
 
Ll(1) Parser in Compilers
Ll(1) Parser in CompilersLl(1) Parser in Compilers
Ll(1) Parser in Compilers
 

More from Ray Song

C++ exception handling
C++ exception handlingC++ exception handling
C++ exception handling
Ray Song
 
RISC-V Linker Relaxation and LLD
RISC-V Linker Relaxation and LLDRISC-V Linker Relaxation and LLD
RISC-V Linker Relaxation and LLD
Ray Song
 
gcov和clang中的实现
gcov和clang中的实现gcov和clang中的实现
gcov和clang中的实现
Ray Song
 
r2con 2017 r2cLEMENCy
r2con 2017 r2cLEMENCyr2con 2017 r2cLEMENCy
r2con 2017 r2cLEMENCy
Ray Song
 
Cyber Grand Challenge及DEFCON 24 CTF决赛介绍
Cyber Grand Challenge及DEFCON 24 CTF决赛介绍Cyber Grand Challenge及DEFCON 24 CTF决赛介绍
Cyber Grand Challenge及DEFCON 24 CTF决赛介绍
Ray Song
 
OI算法竞赛中树形数据结构
OI算法竞赛中树形数据结构OI算法竞赛中树形数据结构
OI算法竞赛中树形数据结构
Ray Song
 
2011年信息学竞赛冬令营《星际探险》
2011年信息学竞赛冬令营《星际探险》2011年信息学竞赛冬令营《星际探险》
2011年信息学竞赛冬令营《星际探险》
Ray Song
 
8门编程语言的设计思考
8门编程语言的设计思考8门编程语言的设计思考
8门编程语言的设计思考
Ray Song
 
Introduction to makefile
Introduction to makefileIntroduction to makefile
Introduction to makefile
Ray Song
 

More from Ray Song (9)

C++ exception handling
C++ exception handlingC++ exception handling
C++ exception handling
 
RISC-V Linker Relaxation and LLD
RISC-V Linker Relaxation and LLDRISC-V Linker Relaxation and LLD
RISC-V Linker Relaxation and LLD
 
gcov和clang中的实现
gcov和clang中的实现gcov和clang中的实现
gcov和clang中的实现
 
r2con 2017 r2cLEMENCy
r2con 2017 r2cLEMENCyr2con 2017 r2cLEMENCy
r2con 2017 r2cLEMENCy
 
Cyber Grand Challenge及DEFCON 24 CTF决赛介绍
Cyber Grand Challenge及DEFCON 24 CTF决赛介绍Cyber Grand Challenge及DEFCON 24 CTF决赛介绍
Cyber Grand Challenge及DEFCON 24 CTF决赛介绍
 
OI算法竞赛中树形数据结构
OI算法竞赛中树形数据结构OI算法竞赛中树形数据结构
OI算法竞赛中树形数据结构
 
2011年信息学竞赛冬令营《星际探险》
2011年信息学竞赛冬令营《星际探险》2011年信息学竞赛冬令营《星际探险》
2011年信息学竞赛冬令营《星际探险》
 
8门编程语言的设计思考
8门编程语言的设计思考8门编程语言的设计思考
8门编程语言的设计思考
 
Introduction to makefile
Introduction to makefileIntroduction to makefile
Introduction to makefile
 

Recently uploaded

Assure Contact Center Experiences for Your Customers With ThousandEyes
Assure Contact Center Experiences for Your Customers With ThousandEyesAssure Contact Center Experiences for Your Customers With ThousandEyes
Assure Contact Center Experiences for Your Customers With ThousandEyes
ThousandEyes
 
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdfFIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance
 
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptx
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptxSecstrike : Reverse Engineering & Pwnable tools for CTF.pptx
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptx
nkrafacyberclub
 
When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...
Elena Simperl
 
Key Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdfKey Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdf
Cheryl Hung
 
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 previewState of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
Prayukth K V
 
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance
 
Introduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - CybersecurityIntroduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - Cybersecurity
mikeeftimakis1
 
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Thierry Lestable
 
How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...
Product School
 
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdfFIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance
 
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Ramesh Iyer
 
Assuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyesAssuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyes
ThousandEyes
 
SAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdf
SAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdfSAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdf
SAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdf
Peter Spielvogel
 
Generative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to ProductionGenerative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to Production
Aggregage
 
Epistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI supportEpistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI support
Alan Dix
 
Le nuove frontiere dell'AI nell'RPA con UiPath Autopilot™
Le nuove frontiere dell'AI nell'RPA con UiPath Autopilot™Le nuove frontiere dell'AI nell'RPA con UiPath Autopilot™
Le nuove frontiere dell'AI nell'RPA con UiPath Autopilot™
UiPathCommunity
 
Leading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdfLeading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdf
OnBoard
 
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdfSmart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
91mobiles
 
UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3
DianaGray10
 

Recently uploaded (20)

Assure Contact Center Experiences for Your Customers With ThousandEyes
Assure Contact Center Experiences for Your Customers With ThousandEyesAssure Contact Center Experiences for Your Customers With ThousandEyes
Assure Contact Center Experiences for Your Customers With ThousandEyes
 
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdfFIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
 
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptx
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptxSecstrike : Reverse Engineering & Pwnable tools for CTF.pptx
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptx
 
When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...
 
Key Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdfKey Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdf
 
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 previewState of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
 
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
 
Introduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - CybersecurityIntroduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - Cybersecurity
 
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
 
How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...
 
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdfFIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
 
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
 
Assuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyesAssuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyes
 
SAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdf
SAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdfSAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdf
SAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdf
 
Generative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to ProductionGenerative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to Production
 
Epistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI supportEpistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI support
 
Le nuove frontiere dell'AI nell'RPA con UiPath Autopilot™
Le nuove frontiere dell'AI nell'RPA con UiPath Autopilot™Le nuove frontiere dell'AI nell'RPA con UiPath Autopilot™
Le nuove frontiere dell'AI nell'RPA con UiPath Autopilot™
 
Leading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdfLeading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdf
 
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdfSmart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
 
UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3
 

Implementing a Simple Interpreter

  • 1. Implementing a Simple Interpreter Ray Song May 27, 2012 Ray Song Implementing a Simple Interpreter
  • 2. Flow sheet Lexical analysis Grammatical analysis Figure : Flow Ray Song Implementing a Simple Interpreter
  • 3. Flow sheet Lexical analysis Grammatical analysis Figure : Flow Ray Song Implementing a Simple Interpreter
  • 15. Off-side rule def hello(): print ‘world’ indentation sensitive Ex: ISWIM(1966), occam(1983), Miranda(1985), Haskell(1990), Python(1991) Ray Song Implementing a Simple Interpreter
  • 16. Off-side rule def hello(): print ‘world’ indentation sensitive Ex: ISWIM(1966), occam(1983), Miranda(1985), Haskell(1990), Python(1991) Ray Song Implementing a Simple Interpreter
  • 17. Off-side rule - Cont. represented as virtual tokens INDENT DEDENT Ray Song Implementing a Simple Interpreter
  • 18. Off-side rule - Cont. represented as virtual tokens INDENT DEDENT Ray Song Implementing a Simple Interpreter
  • 19. Off-side rule - Cont. represented as virtual tokens INDENT DEDENT Ray Song Implementing a Simple Interpreter
  • 20. Example if a > 2: print 5 print a IF a > 2 : NEWLINE INDENT PRINT 5 NEWLINE DEDENT PRINT a NEWLINE Ray Song Implementing a Simple Interpreter
  • 21. Grammatical analysis Cocke–Younger–Kasami algorithm Earley’s algorithm LR parser Recursive-descent parser Ray Song Implementing a Simple Interpreter
  • 22. Grammatical analysis Cocke–Younger–Kasami algorithm Earley’s algorithm LR parser Recursive-descent parser Ray Song Implementing a Simple Interpreter
  • 23. Grammatical analysis Cocke–Younger–Kasami algorithm Earley’s algorithm LR parser Recursive-descent parser Ray Song Implementing a Simple Interpreter
  • 24. Grammatical analysis Cocke–Younger–Kasami algorithm Earley’s algorithm LR parser Recursive-descent parser Ray Song Implementing a Simple Interpreter
  • 25. Tools Bison Can generate C, C++ and Java codes ANTLR Ray Song Implementing a Simple Interpreter
  • 26. Tools Bison Can generate C, C++ and Java codes ANTLR Ray Song Implementing a Simple Interpreter
  • 27. Tools Bison Can generate C, C++ and Java codes ANTLR Ray Song Implementing a Simple Interpreter
  • 28. Expression Precedence climbing method Shunting-yard algorithm Parsing expressions in infix notation Output in Reverse Polish notation (RPN) Output in Abstract syntax tree (AST) Operator precedence parser Ray Song Implementing a Simple Interpreter
  • 29. Expression Precedence climbing method Shunting-yard algorithm Parsing expressions in infix notation Output in Reverse Polish notation (RPN) Output in Abstract syntax tree (AST) Operator precedence parser Ray Song Implementing a Simple Interpreter
  • 30. Expression Precedence climbing method Shunting-yard algorithm Parsing expressions in infix notation Output in Reverse Polish notation (RPN) Output in Abstract syntax tree (AST) Operator precedence parser Ray Song Implementing a Simple Interpreter
  • 31. Expression Precedence climbing method Shunting-yard algorithm Parsing expressions in infix notation Output in Reverse Polish notation (RPN) Output in Abstract syntax tree (AST) Operator precedence parser Ray Song Implementing a Simple Interpreter
  • 32. Expression Precedence climbing method Shunting-yard algorithm Parsing expressions in infix notation Output in Reverse Polish notation (RPN) Output in Abstract syntax tree (AST) Operator precedence parser Ray Song Implementing a Simple Interpreter
  • 33. Expression Precedence climbing method Shunting-yard algorithm Parsing expressions in infix notation Output in Reverse Polish notation (RPN) Output in Abstract syntax tree (AST) Operator precedence parser Ray Song Implementing a Simple Interpreter
  • 34. Parser combinator Straightforward to construct Readability Parsec Ray Song Implementing a Simple Interpreter
  • 35. Parser combinator Straightforward to construct Readability Parsec Ray Song Implementing a Simple Interpreter
  • 36. Parser combinator Straightforward to construct Readability Parsec Ray Song Implementing a Simple Interpreter
  • 37. Impl Two top-level nonterminals: STMT and EXPR STMT: SIMPLE STMT ‘n’ | COMPOUND SIMPLE STMT: EXPR SIMPLE STMT: IDENT ‘=’ EXPR SIMPLE STMT: BREAK SIMPLE STMT: print EXPR COMPOUND: if EXPR ’:’ SUITE OPT ELSE COMPOUND: while EXPR ’:’ SUITE SUITE: many1(‘n’) INDENT many1(STMT) DEDENT SUITE: SIMPLE STMT ‘n’ OPT ELSE: ELSE ’:’ SUITE OPT ELSE: /* empty */ Ray Song Implementing a Simple Interpreter
  • 38. Impl Two top-level nonterminals: STMT and EXPR STMT: SIMPLE STMT ‘n’ | COMPOUND SIMPLE STMT: EXPR SIMPLE STMT: IDENT ‘=’ EXPR SIMPLE STMT: BREAK SIMPLE STMT: print EXPR COMPOUND: if EXPR ’:’ SUITE OPT ELSE COMPOUND: while EXPR ’:’ SUITE SUITE: many1(‘n’) INDENT many1(STMT) DEDENT SUITE: SIMPLE STMT ‘n’ OPT ELSE: ELSE ’:’ SUITE OPT ELSE: /* empty */ Ray Song Implementing a Simple Interpreter
  • 39. Impl Two top-level nonterminals: STMT and EXPR STMT: SIMPLE STMT ‘n’ | COMPOUND SIMPLE STMT: EXPR SIMPLE STMT: IDENT ‘=’ EXPR SIMPLE STMT: BREAK SIMPLE STMT: print EXPR COMPOUND: if EXPR ’:’ SUITE OPT ELSE COMPOUND: while EXPR ’:’ SUITE SUITE: many1(‘n’) INDENT many1(STMT) DEDENT SUITE: SIMPLE STMT ‘n’ OPT ELSE: ELSE ’:’ SUITE OPT ELSE: /* empty */ Ray Song Implementing a Simple Interpreter
  • 40. Impl Two top-level nonterminals: STMT and EXPR STMT: SIMPLE STMT ‘n’ | COMPOUND SIMPLE STMT: EXPR SIMPLE STMT: IDENT ‘=’ EXPR SIMPLE STMT: BREAK SIMPLE STMT: print EXPR COMPOUND: if EXPR ’:’ SUITE OPT ELSE COMPOUND: while EXPR ’:’ SUITE SUITE: many1(‘n’) INDENT many1(STMT) DEDENT SUITE: SIMPLE STMT ‘n’ OPT ELSE: ELSE ’:’ SUITE OPT ELSE: /* empty */ Ray Song Implementing a Simple Interpreter
  • 41. Impl Two top-level nonterminals: STMT and EXPR STMT: SIMPLE STMT ‘n’ | COMPOUND SIMPLE STMT: EXPR SIMPLE STMT: IDENT ‘=’ EXPR SIMPLE STMT: BREAK SIMPLE STMT: print EXPR COMPOUND: if EXPR ’:’ SUITE OPT ELSE COMPOUND: while EXPR ’:’ SUITE SUITE: many1(‘n’) INDENT many1(STMT) DEDENT SUITE: SIMPLE STMT ‘n’ OPT ELSE: ELSE ’:’ SUITE OPT ELSE: /* empty */ Ray Song Implementing a Simple Interpreter
  • 42. Impl Two top-level nonterminals: STMT and EXPR STMT: SIMPLE STMT ‘n’ | COMPOUND SIMPLE STMT: EXPR SIMPLE STMT: IDENT ‘=’ EXPR SIMPLE STMT: BREAK SIMPLE STMT: print EXPR COMPOUND: if EXPR ’:’ SUITE OPT ELSE COMPOUND: while EXPR ’:’ SUITE SUITE: many1(‘n’) INDENT many1(STMT) DEDENT SUITE: SIMPLE STMT ‘n’ OPT ELSE: ELSE ’:’ SUITE OPT ELSE: /* empty */ Ray Song Implementing a Simple Interpreter
  • 43. Impl Two top-level nonterminals: STMT and EXPR STMT: SIMPLE STMT ‘n’ | COMPOUND SIMPLE STMT: EXPR SIMPLE STMT: IDENT ‘=’ EXPR SIMPLE STMT: BREAK SIMPLE STMT: print EXPR COMPOUND: if EXPR ’:’ SUITE OPT ELSE COMPOUND: while EXPR ’:’ SUITE SUITE: many1(‘n’) INDENT many1(STMT) DEDENT SUITE: SIMPLE STMT ‘n’ OPT ELSE: ELSE ’:’ SUITE OPT ELSE: /* empty */ Ray Song Implementing a Simple Interpreter
  • 44. Impl Two top-level nonterminals: STMT and EXPR STMT: SIMPLE STMT ‘n’ | COMPOUND SIMPLE STMT: EXPR SIMPLE STMT: IDENT ‘=’ EXPR SIMPLE STMT: BREAK SIMPLE STMT: print EXPR COMPOUND: if EXPR ’:’ SUITE OPT ELSE COMPOUND: while EXPR ’:’ SUITE SUITE: many1(‘n’) INDENT many1(STMT) DEDENT SUITE: SIMPLE STMT ‘n’ OPT ELSE: ELSE ’:’ SUITE OPT ELSE: /* empty */ Ray Song Implementing a Simple Interpreter
  • 45. Impl Two top-level nonterminals: STMT and EXPR STMT: SIMPLE STMT ‘n’ | COMPOUND SIMPLE STMT: EXPR SIMPLE STMT: IDENT ‘=’ EXPR SIMPLE STMT: BREAK SIMPLE STMT: print EXPR COMPOUND: if EXPR ’:’ SUITE OPT ELSE COMPOUND: while EXPR ’:’ SUITE SUITE: many1(‘n’) INDENT many1(STMT) DEDENT SUITE: SIMPLE STMT ‘n’ OPT ELSE: ELSE ’:’ SUITE OPT ELSE: /* empty */ Ray Song Implementing a Simple Interpreter
  • 46. Impl Two top-level nonterminals: STMT and EXPR STMT: SIMPLE STMT ‘n’ | COMPOUND SIMPLE STMT: EXPR SIMPLE STMT: IDENT ‘=’ EXPR SIMPLE STMT: BREAK SIMPLE STMT: print EXPR COMPOUND: if EXPR ’:’ SUITE OPT ELSE COMPOUND: while EXPR ’:’ SUITE SUITE: many1(‘n’) INDENT many1(STMT) DEDENT SUITE: SIMPLE STMT ‘n’ OPT ELSE: ELSE ’:’ SUITE OPT ELSE: /* empty */ Ray Song Implementing a Simple Interpreter
  • 47. Impl Two top-level nonterminals: STMT and EXPR STMT: SIMPLE STMT ‘n’ | COMPOUND SIMPLE STMT: EXPR SIMPLE STMT: IDENT ‘=’ EXPR SIMPLE STMT: BREAK SIMPLE STMT: print EXPR COMPOUND: if EXPR ’:’ SUITE OPT ELSE COMPOUND: while EXPR ’:’ SUITE SUITE: many1(‘n’) INDENT many1(STMT) DEDENT SUITE: SIMPLE STMT ‘n’ OPT ELSE: ELSE ’:’ SUITE OPT ELSE: /* empty */ Ray Song Implementing a Simple Interpreter
  • 48. Impl Two top-level nonterminals: STMT and EXPR STMT: SIMPLE STMT ‘n’ | COMPOUND SIMPLE STMT: EXPR SIMPLE STMT: IDENT ‘=’ EXPR SIMPLE STMT: BREAK SIMPLE STMT: print EXPR COMPOUND: if EXPR ’:’ SUITE OPT ELSE COMPOUND: while EXPR ’:’ SUITE SUITE: many1(‘n’) INDENT many1(STMT) DEDENT SUITE: SIMPLE STMT ‘n’ OPT ELSE: ELSE ’:’ SUITE OPT ELSE: /* empty */ Ray Song Implementing a Simple Interpreter
  • 49. Impl - Cont. EXPR: EXPR ‘==’ TERM EXPR: EXPR ’ !=’ TERM EXPR: TERM TERM: TERM ‘+’ FACTOR TERM: FACTOR FACTOR: FACTOR ‘*’ ATOM FACTOR: ATOM ATOM: identifier ATOM: literal integer ATOM: literal string ATOM: ‘(’ EXPR ’)’ Ray Song Implementing a Simple Interpreter
  • 50. Impl - Cont. EXPR: EXPR ‘==’ TERM EXPR: EXPR ’ !=’ TERM EXPR: TERM TERM: TERM ‘+’ FACTOR TERM: FACTOR FACTOR: FACTOR ‘*’ ATOM FACTOR: ATOM ATOM: identifier ATOM: literal integer ATOM: literal string ATOM: ‘(’ EXPR ’)’ Ray Song Implementing a Simple Interpreter
  • 51. Impl - Cont. EXPR: EXPR ‘==’ TERM EXPR: EXPR ’ !=’ TERM EXPR: TERM TERM: TERM ‘+’ FACTOR TERM: FACTOR FACTOR: FACTOR ‘*’ ATOM FACTOR: ATOM ATOM: identifier ATOM: literal integer ATOM: literal string ATOM: ‘(’ EXPR ’)’ Ray Song Implementing a Simple Interpreter
  • 52. Impl - Cont. EXPR: EXPR ‘==’ TERM EXPR: EXPR ’ !=’ TERM EXPR: TERM TERM: TERM ‘+’ FACTOR TERM: FACTOR FACTOR: FACTOR ‘*’ ATOM FACTOR: ATOM ATOM: identifier ATOM: literal integer ATOM: literal string ATOM: ‘(’ EXPR ’)’ Ray Song Implementing a Simple Interpreter
  • 53. Impl - Cont. EXPR: EXPR ‘==’ TERM EXPR: EXPR ’ !=’ TERM EXPR: TERM TERM: TERM ‘+’ FACTOR TERM: FACTOR FACTOR: FACTOR ‘*’ ATOM FACTOR: ATOM ATOM: identifier ATOM: literal integer ATOM: literal string ATOM: ‘(’ EXPR ’)’ Ray Song Implementing a Simple Interpreter
  • 54. Impl - Cont. EXPR: EXPR ‘==’ TERM EXPR: EXPR ’ !=’ TERM EXPR: TERM TERM: TERM ‘+’ FACTOR TERM: FACTOR FACTOR: FACTOR ‘*’ ATOM FACTOR: ATOM ATOM: identifier ATOM: literal integer ATOM: literal string ATOM: ‘(’ EXPR ’)’ Ray Song Implementing a Simple Interpreter
  • 55. Impl - Cont. EXPR: EXPR ‘==’ TERM EXPR: EXPR ’ !=’ TERM EXPR: TERM TERM: TERM ‘+’ FACTOR TERM: FACTOR FACTOR: FACTOR ‘*’ ATOM FACTOR: ATOM ATOM: identifier ATOM: literal integer ATOM: literal string ATOM: ‘(’ EXPR ’)’ Ray Song Implementing a Simple Interpreter
  • 56. Impl - Cont. EXPR: EXPR ‘==’ TERM EXPR: EXPR ’ !=’ TERM EXPR: TERM TERM: TERM ‘+’ FACTOR TERM: FACTOR FACTOR: FACTOR ‘*’ ATOM FACTOR: ATOM ATOM: identifier ATOM: literal integer ATOM: literal string ATOM: ‘(’ EXPR ’)’ Ray Song Implementing a Simple Interpreter
  • 57. Impl - Cont. EXPR: EXPR ‘==’ TERM EXPR: EXPR ’ !=’ TERM EXPR: TERM TERM: TERM ‘+’ FACTOR TERM: FACTOR FACTOR: FACTOR ‘*’ ATOM FACTOR: ATOM ATOM: identifier ATOM: literal integer ATOM: literal string ATOM: ‘(’ EXPR ’)’ Ray Song Implementing a Simple Interpreter
  • 58. Impl - Cont. EXPR: EXPR ‘==’ TERM EXPR: EXPR ’ !=’ TERM EXPR: TERM TERM: TERM ‘+’ FACTOR TERM: FACTOR FACTOR: FACTOR ‘*’ ATOM FACTOR: ATOM ATOM: identifier ATOM: literal integer ATOM: literal string ATOM: ‘(’ EXPR ’)’ Ray Song Implementing a Simple Interpreter
  • 59. Impl - Cont. EXPR: EXPR ‘==’ TERM EXPR: EXPR ’ !=’ TERM EXPR: TERM TERM: TERM ‘+’ FACTOR TERM: FACTOR FACTOR: FACTOR ‘*’ ATOM FACTOR: ATOM ATOM: identifier ATOM: literal integer ATOM: literal string ATOM: ‘(’ EXPR ’)’ Ray Song Implementing a Simple Interpreter
  • 60. Example if a > 2: print 5 print a Ray Song Implementing a Simple Interpreter
  • 61. Example - Cont. Figure : Parse Ray Song Implementing a Simple Interpreter
  • 62. Interpreting Abstract syntax tree (AST). Define semantics for each class of nodes object(atom): trivial binary operator BinOP(operator, lhs, rhs, RESULT) :- obj1 = eval(lhs), obj2 = eval(rhs), calc(op, obj1, obj2, RESULT). Object & BinOP inherit from Expr Ray Song Implementing a Simple Interpreter
  • 63. Interpreting Abstract syntax tree (AST). Define semantics for each class of nodes object(atom): trivial binary operator BinOP(operator, lhs, rhs, RESULT) :- obj1 = eval(lhs), obj2 = eval(rhs), calc(op, obj1, obj2, RESULT). Object & BinOP inherit from Expr Ray Song Implementing a Simple Interpreter
  • 64. Interpreting Abstract syntax tree (AST). Define semantics for each class of nodes object(atom): trivial binary operator BinOP(operator, lhs, rhs, RESULT) :- obj1 = eval(lhs), obj2 = eval(rhs), calc(op, obj1, obj2, RESULT). Object & BinOP inherit from Expr Ray Song Implementing a Simple Interpreter
  • 65. Interpreting Abstract syntax tree (AST). Define semantics for each class of nodes object(atom): trivial binary operator BinOP(operator, lhs, rhs, RESULT) :- obj1 = eval(lhs), obj2 = eval(rhs), calc(op, obj1, obj2, RESULT). Object & BinOP inherit from Expr Ray Song Implementing a Simple Interpreter
  • 66. Interpreting Abstract syntax tree (AST). Define semantics for each class of nodes object(atom): trivial binary operator BinOP(operator, lhs, rhs, RESULT) :- obj1 = eval(lhs), obj2 = eval(rhs), calc(op, obj1, obj2, RESULT). Object & BinOP inherit from Expr Ray Song Implementing a Simple Interpreter
  • 67. Subclasses of Stmt - Cont. Assign eval() need a parameter: Binding (which variable holds which object) ExprStmt Print Continue (throwing an exception) Ray Song Implementing a Simple Interpreter
  • 68. Subclasses of Stmt - Cont. Assign eval() need a parameter: Binding (which variable holds which object) ExprStmt Print Continue (throwing an exception) Ray Song Implementing a Simple Interpreter
  • 69. Subclasses of Stmt - Cont. Assign eval() need a parameter: Binding (which variable holds which object) ExprStmt Print Continue (throwing an exception) Ray Song Implementing a Simple Interpreter
  • 70. Subclasses of Stmt - Cont. Assign eval() need a parameter: Binding (which variable holds which object) ExprStmt Print Continue (throwing an exception) Ray Song Implementing a Simple Interpreter
  • 71. Subclasses of Stmt - Cont. Assign eval() need a parameter: Binding (which variable holds which object) ExprStmt Print Continue (throwing an exception) Ray Song Implementing a Simple Interpreter