ChAmElEoN Parse Tree

Similar documents
CSE 12 Abstract Syntax Trees

2.2 Syntax Definition

Today. Assignments. Lecture Notes CPSC 326 (Spring 2019) Operator Associativity & Precedence. AST Navigation. HW4 out (due next Thurs)

COP4020 Programming Languages. Semantics Robert van Engelen & Chris Lacher

COP4020 Programming Languages. Semantics Prof. Robert van Engelen

Chapter 20: Binary Trees

A Simple Syntax-Directed Translator

Compilers Project 3: Semantic Analyzer

Compiler Design (40-414)

Functions CHAPTER 5. FIGURE 1. Concrete syntax for the P 2 subset of Python. (In addition to that of P 1.)

Project Compiler. CS031 TA Help Session November 28, 2011

Lecture 14 Sections Mon, Mar 2, 2009

SEMANTIC ANALYSIS TYPES AND DECLARATIONS

Announcements. The current topic: Scheme. Review: BST functions. Review: Representing trees in Scheme. Reminder: Lab 2 is due on Monday at 10:30 am.

Semantic Analysis. Outline. The role of semantic analysis in a compiler. Scope. Types. Where we are. The Compiler so far

Objects CHAPTER 6. FIGURE 1. Concrete syntax for the P 3 subset of Python. (In addition to that of P 2.)

Functions CHAPTER 5. FIGURE 1. Concrete syntax for the P 2 subset of Python. (In addition to that of P 1.)

Parsing CSCI-400. Principles of Programming Languages.

Semantic actions for declarations and expressions

Project 2: Scheme Interpreter

The role of semantic analysis in a compiler

3.5 Practical Issues PRACTICAL ISSUES Error Recovery

Semantic actions for declarations and expressions. Monday, September 28, 15

Semantic Analysis Wilhelm/Seidl/Hack: Compiler Design Syntactic and Semantic Analysis, Chapter 4

Assignment 7: functions and closure conversion (part 1)

Recap: Functions as first-class values

What is a compiler? var a var b mov 3 a mov 4 r1 cmpi a r1 jge l_e mov 2 b jmp l_d l_e: mov 3 b l_d: ;done

Parsing II Top-down parsing. Comp 412

Syntax and Grammars 1 / 21

An Implementation of Hybrid A Concurrent, Object-Oriented Language

Lecture 20 Sections Wed, Apr 1, 2009

Semantic Analysis. Outline. The role of semantic analysis in a compiler. Scope. Types. Where we are. The Compiler Front-End

CSI33 Data Structures

Principles of Programming Languages COMP251: Syntax and Grammars

CSE450. Translation of Programming Languages. Lecture 11: Semantic Analysis: Types & Type Checking

Semantic actions for declarations and expressions

EECS 6083 Intro to Parsing Context Free Grammars

ASTs, Objective CAML, and Ocamlyacc

Cool Abstract Syntax Trees

CSI33 Data Structures

Static Semantics. Lecture 15. (Notes by P. N. Hilfinger and R. Bodik) 2/29/08 Prof. Hilfinger, CS164 Lecture 15 1

Assignment 7: functions and closure conversion

Syntax Analysis Check syntax and construct abstract syntax tree

Structure of a compiler. More detailed overview of compiler front end. Today we ll take a quick look at typical parts of a compiler.

CS152 Programming Language Paradigms Prof. Tom Austin, Fall Syntax & Semantics, and Language Design Criteria

LECTURE 3. Compiler Phases

CMPT 379 Compilers. Parse trees

CS558 Programming Languages

PRINCIPLES OF COMPILER DESIGN UNIT I INTRODUCTION TO COMPILERS

Compiler construction in4020 lecture 5

Compiling and Interpreting Programming. Overview of Compilers and Interpreters

Semantic Analysis. Lecture 9. February 7, 2018

The Compiler So Far. CSC 4181 Compiler Construction. Semantic Analysis. Beyond Syntax. Goals of a Semantic Analyzer.

CSCC24 Functional Programming Scheme Part 2

Binary Trees. Examples:

Syntax Errors; Static Semantics

Binary Trees, Binary Search Trees

Introduction to Parsing

([1-9] 1[0-2]):[0-5][0-9](AM PM)? What does the above match? Matches clock time, may or may not be told if it is AM or PM.

Prof. Mohamed Hamada Software Engineering Lab. The University of Aizu Japan

CS558 Programming Languages

CS 360 Programming Languages Interpreters

Compiler Theory. (Semantic Analysis and Run-Time Environments)

a. Determine the EPS, FIRST and FOLLOWs set for this grammar.

9/5/17. The Design and Implementation of Programming Languages. Compilation. Interpretation. Compilation vs. Interpretation. Hybrid Implementation

More Assigned Reading and Exercises on Syntax (for Exam 2)

Lecture 16: Static Semantics Overview 1

M/s. Managing distributed workloads. Language Reference Manual. Miranda Li (mjl2206) Benjamin Hanser (bwh2124) Mengdi Lin (ml3567)

COMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou

Time : 1 Hour Max Marks : 30

CS301 - Data Structures Glossary By

Static and Dynamic Semantics

Programming Languages Third Edition

Compiler Principle and Technology. Prof. Dongming LU April 15th, 2019

Context-Free Grammar. Concepts Introduced in Chapter 2. Parse Trees. Example Grammar and Derivation

Let us construct the LR(1) items for the grammar given below to construct the LALR parsing table.

Introduction to Programming Using Java (98-388)

CS558 Programming Languages

Jim Lambers ENERGY 211 / CME 211 Autumn Quarter Programming Project 4

Formal Languages and Compilers Lecture I: Introduction to Compilers

Building the Abstract Syntax Trees

Principles of Programming Languages 2017W, Functional Programming

Compiler construction

Decaf Language Reference

CS 406: Syntax Directed Translation

The Environment Model. Nate Foster Spring 2018

Faculty of Electrical Engineering, Mathematics, and Computer Science Delft University of Technology

A programming language requires two major definitions A simple one pass compiler

CS /534 Compiler Construction University of Massachusetts Lowell

JASMINT: Language to User-Friendly AST with Emphasis on Transpilation. By: John Bradbury. Advisor: John Clements. Computer Science Department

An Introduction to Functions

Syntactic Directed Translation

Abstract Syntax Trees

Chapter 3: CONTEXT-FREE GRAMMARS AND PARSING Part 1

CS1622. Semantic Analysis. The Compiler So Far. Lecture 15 Semantic Analysis. How to build symbol tables How to use them to find

What is a compiler? Xiaokang Qiu Purdue University. August 21, 2017 ECE 573

Implementing Actions

Faculty of Electrical Engineering, Mathematics, and Computer Science Delft University of Technology

Abstract Syntax Trees Synthetic and Inherited Attributes

Principles of Programming Languages

Transcription:

ChAmElEoN Parse Tree Jack L. Watkin May 9, 2017 The objective of this appendix is to describe the abstract syntax tree (ast) generated by the ChAmElEoN parser. 1 Tree Node The ChAmElEoNparser builds an ast in which each node contains the node type, a leaf, a list of children, and a line number. The tree node structure is shown in Fig. 1. Figure 1: Visual representation of TreeNode class. 2 Numbers, Identifiers, and Operators For all numbers (ntnumber), identifiers (ntidentifier), and primitive operators (ntprimitive) the value of the token is stored in the leaf of the node (e.g., Fig 2). 3 Interpreter Overview The evaluate expr function is the main loop of the interpreter. Each node type corresponds to a case of the conditional statement in evaluate expr. The following shows a simplified example evaluate expr of the function. def e v a l u a t e e x p r ( expr, environ ) : i f expr. type == ntprimative op : e l i f expr. type == ntnumber : 1

Figure 2: Example of a TreeNode for an identifier. e l i f expr. type == n t I d e n t i f i e r : 4 Arguments and Expression Lists 4.1 Abstract Syntax Tree The following rules are used to represent an argument list. Argument lists are used to describe the arguments to a function or primitive. (r 1 ) <argument list> ::= <argument> (r 2 ) <argument list> ::= <argument>, <argument list> Similarly, the following rules are used to represent a parameter list. Parameter lists are used to describe the parameters in a function declaration. (r 1 ) <parameter list> ::= <identifier> (r 2 ) <parameter list> ::= <identifier>, <parameter list> (r 3 ) <parameter list> ::= ɛ Argument and parameter lists are built by the parser as trees and then transmuted into a list a by the interpreter. Fig. 3 shows the ast that describes the expression *(7,x). A blue box represents the type field of the TreeNode, a red box represents represents one member of the children list, and a green box represents the leaf field. Note that expression is an overloaded operator in that it is used with both the arguments list to a primitive and as the arguments to a function. 4.2 Interpreter Both argument and parameter list asts are traversed such that the right recursive case is performed last. The leaf of the parameter/argument list node is placed at the front of a empty list. In argument lists, value of the leaf is evaluated before placing it in the list. If a child exists, it becomes the next parameter/argument list node to be searched. This continues until an parameter/argument list node without a child is reached. A list is returned by the recursive case and appended to list the created with the leaf of the node. The following is an example from the interpreter that illustrates this idea. e l i f expr. type == ntargumentlist : ArgList = [ ] 2

Figure 3: Example of tree containing an argument list. ArgList. append ( e v a l u a t e e x p r ( expr. c h i l d r e n [ 0 ], environ ) ) i f len ( expr. c h i l d r e n ) > 1 : ArgList. extend ( e v a l u a t e e x p r ( expr. c h i l d r e n [ 1 ], environ ) ) return ArgList 5 let, let*, and letrec 5.1 Abstract Syntax Tree The ast that describes a let, let*, or letrec expression is similar to the ast which describes an argument list. These statements rely on a right recursive grammatical structure to produce the desired ast. One nuance in this grammar is that this does not allow for let statements with no declarations. (r 1 ) <expression> ::= let <let statement> in <expression> (r 2 ) <let statement> ::= <let assignment> (r 3 ) <let statement> ::= <let assignment> <let statement> (r 4 ) <let assignment> ::= <identifier> = <expression> The let* and letrec expressions are defined with similar rules with an exception in letrec. With respect to <letrec assignment> rule, only a function can be assigned to the identifier. <recursive f unc> can be used to build an ast for a recursive or non-recursive function. This is done because the interpreter needs to know whether to build the function with itself in the environment or not. (r 1 ) <letrec assignment> ::= <identifier> = <recursive func> Fig. 4 shows a simplified version of the ast that represents a let statement. This is also applicable for letrec and let*. 3

l e t x = 1 y = 2 in ( x, y ) As before, a blue box represents the type field of the TreeNode, a red box represents represents one member of the children list and a green box represents the leaf field. Figure 4: Example of a tree containing a let statement. 5.2 Interpreter While the ast for let, let*, or letrec are largely the same, the differences in their functionality arises from how the interpreter processes the tree. Similar to parameter/argument lists, the tree that describes a let is searched in a preorder fashion from within the ntlet case. The ntletassignment case creates a single element dictionary containing a name value pair defined within the let. e l i f ( expr. type == ntletstatement ) : temp = e v a l u a t e e x p r ( expr. c h i l d r e n [ 0 ], environ ) i f len ( expr. c h i l d r e n ) > 1 : temp. update ( e v a l u a t e e x p r ( expr. c h i l d r e n [ 1 ], environ ) ) return temp 4

e l i f expr. type == ntletassignment : return { expr. l e a f : e v a l u a t e e x p r ( expr. c h i l d r e n [ 0 ], environ )} When all ntletstatements have been processed, a dictionary containing all name value pairs is returned to the ntlet case. The dictionary is then split into two list one containing only names, the other containing only values. These values are placed into the environment. The body of the let statement is called with this new environment. The ntletrecstatement and ntletstarstatement traversed in the same way, however these statements differ in how values are added to the environment. For let* statements, each declaration inside the statement is added one by one. This allows for values declared at the beginning of the let* to be used within subsequent statements in the let*. This stands in contrast to how the let statement adds them all at once. For letrec statements, the difference with let lies only in that the ntletrecassignment returns a list containing three lists: one that is a list of identifier to which each function is bound, the argument lists of each function, and the body of each function. 6 Functions In ChAmElEoN, functions are represented as closures in one of three forms: abstract syntax representation (asr), closure representation (cls), or Python closure representation. For all representations, the arguments For asr and cls representations, the environment the function is declared in is stored in the closure (static scoping). Fig. 5 shows the asr closure. When invoked, these closures are passed the values to be bound to the arguments of a function. Recursive functions are created by storing a pointer to the function within the environment, and then creating a closure. Figure 5: Abstract syntax representation of a closure. For Python closures, the environment is passed to the closure when the function is called (dynamic scoping). 7 Debugging The way that exceptions are handled within this interpreter causes issues for debugging. There are several catch-all statements in the interpreter that serve to trap errors, but do not present helpful debugging messages. For example, an unbound identifier exception does not necessarily mean that there is an unbound identifier. Rather, it means there was an unhandled exception is attempting to resolve the identifiers value. When debugging the environment, list of vector representation is the easiest print. This is because the environment is a list, which can be printed with the Python print function. For abstract syntax representation, use environ.symbols and environ.values. Closure representations of the environment cannot be easily printed outside the closure function itself. 5