Docsity

Using Algorithms and Data Structures to Implement an Interpreter: An Overview of CSE 12, Essays (high school) of English Literature

Partial preview of the text

Download Using Algorithms and Data Structures to Implement an Interpreter: An Overview of CSE 12 and more Essays (high school) English Literature in PDF only on Docsity!

  • Compilers and Interpreters
  • Parse Trees and Abstract Syntax Trees (AST's)
  • Creating and Evaluating AST's
  • The Table ADT and Symbol Tables

CSE 12

Abstract Syntax Trees

Using Algorithms and Data

Structures

 Digital computers can do anything that can conceivably be done with information: they are general purpose information processing machines

 You get a computer to do something by programming it to run algorithms that manipulate data structures

 Picking what algorithms and data structures are suitable for a particular application is something every good programmer needs to know

 One important application area is: writing programs that understand programs…

Towards an interpreter

 An interpreter reads a line or statement at a time of a source language text, and then does what that line or statement says to do

 The source language should have clearly defined syntax and semantics rules  the syntax rules say what strings legally belong to the source language, and what their structure is  the semantics rules say what each legal string in the language means, based on its structure

 Without clearly defined syntax and semantics, writing an interpreter isn’t really possible!

Tasks for an interpreter

 A language interpreter does two things with each line or statement in the input: syntactic analysis and semantic evaluation

 Syntactic analysis is done in accordance with the syntax rules for the language

  • the output from syntactic analysis is a tree data structure
  • corresponds to "compile time" in a compiled language

 Semantic evaluation is done in accordance with the semantic rules

  • Semantic evaluation takes as input the tree created in the syntactic analysis phase
  • corresponds to "run time" for compiled code

 The result of all this is: doing what the statement says! (For example, computing a value and returning it or printing it out)

Syntax rules and BNF

 The syntax rules for the language can be written down in Backus-Naur Form (BNF) or a similar notation

 A BNF grammar is a list of syntax rules

 Each rule defines one "nonterminal symbol", which appears at the left of a " := " sign in the rule

 Alternative definitions of the nonterminal symbol appear to the right of the " := " sign, separated by " | " signs

 Often the definition of a nonterminal in a BNF grammar is recursive: it defines the nonterminal in terms of itself

Syntax rules and BNF

 The nonterminal symbol defined in rule listed first in the grammar is called the "start" symbol of the grammar

 A symbol not defined by a rule in the grammar is a "terminal symbol", and is usually taken literally

 If a string satisfies the definition of the "start" symbol, it is in the language defined by the BNF grammar; otherwise not

 The process of using the grammar to check to see if a string is in the language is called parsing the string

A BNF grammar for a language

 Here is a BNF grammar for a simple formal language

 The "start” symbol for this grammar is

 If a string satisfies the definition of , it is in the language

defined by this grammar; otherwise not

:= |

:= = := + | - | := * | / | := | | () := w | x | y | z := 0 | 1 | 2 | 3 | 4

Example derivations

  • A derivation starts with the start symbol, and at each step, replaces one nonterminal symbol by one of the definitions of that nonterminal.
  • Here are derivations of the strings 2 and 2 + w + 3 based on that grammar:

=> => => => => 2

=> => + => + =>

  • => + 3 =>
    • 3 => + + 3 =>
    • 3 => + + 3 =>
    • 3 => + + 3 => 2 + + 3 => 2 + w + 3

Parsing and parse trees

 For every derivation, there is a corresponding tree: a parse tree

 Each node in the parse tree corresponds to one symbol in the BNF grammar

 Leaves in the parse tree correspond to terminal symbols; internal nodes correspond to nonterminal symbols

 The root of the parse tree is the "start" symbol

 The children of an internal node in the parse tree correspond to the symbols in a definition of the nonterminal symbol corresponding to their parent node

 Reading the leaves of the parse tree left to right gives you the string that has been parsed

Parse trees and derivations

 How to build a parse tree?

 Given a derivation of a string, you could build a parse tree “top down”:  The start symbol is the root of the tree.  Children of the root are symbols in the definition of the start symbol used to create the next step in the derivation.  In general, children of a node in the parse tree are symbols in the definition of a nonterminal that was used to create a next step in the derivation.

Parse tree from a derivation

  • A derivation: => => => => => 2
  • Parse tree corresponding to that derivation:

2

Another parse tree from a derivation

  • Another derivation: => => - => - => - =>
    • 3 => + - 3 => + - 3 =>
      • 3 => + - 3 => + - 3 =>
      • 3 => 2 + - 3 => 2 + w - 3
  • Parse tree corresponding to that derivation:

3

(^) +

2

w

Abstract syntax trees

 A parse tree includes all the information in a derivation of a string from the start symbol  In a parse tree, the children of a nonterminal symbol node include nodes for all of the symbols in the definition of that nonterminal

 But this may be more information than is required for some applications of the tree

 For example: do not need to include nodes for parentheses just used for grouping; information in nodes corresponding to expression operators can be moved into their parent nodes; etc.

 A parse tree with nonessential nodes left out is called an Abstract Syntax Tree (AST)

Parse tree and AST

  • Parse tree and an AST for the string 1 – ( 2 + 3 )

1

( (^) )

2

3

1

2

3