CS360: Mini Language. Michael Conway Original slides by Mark Boady. March 11, 2015

Similar documents
Project 2 Interpreter for Snail. 2 The Snail Programming Language

Today. Assignments. Lecture Notes CPSC 326 (Spring 2019) Quiz 4. Syntax Analysis: Abstract Syntax Trees. HW3 due. HW4 out (due next Thurs)

ffl assign : The assign has the form: identifier = expression ; For example, this is a valid assign : var1 = 20-3*2 ; In the assign the identifier get

ffl assign The assign has the form: identifier = expression; For example, this is a valid assign : var1 = 20-3*2; In the assign the identifier gets th

(2). In your own words, explain how dynamic scoping differs from static scoping.

Parser Generators. Mark Boady. August 14, 2013

Today. Assignments. Lecture Notes CPSC 326 (Spring 2019) Quiz 2. Lexer design. Syntax Analysis: Context-Free Grammars. HW2 (out, due Tues)

ASTs, Objective CAML, and Ocamlyacc

Syntax. In Text: Chapter 3

Programming Languages (CS 550) Lecture 4 Summary Scanner and Parser Generators. Jeremy R. Johnson

Topic 3: Syntax Analysis I

Last time. What are compilers? Phases of a compiler. Scanner. Parser. Semantic Routines. Optimizer. Code Generation. Sunday, August 29, 2010

SCHEME 7. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. October 29, 2015

SCHEME 8. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. March 23, 2017

Class Information ANNOUCEMENTS

Spring 2018 Discussion 7: March 21, Introduction. 2 Primitives

Last Time. What do we want? When do we want it? An AST. Now!

Defining syntax using CFGs

CS 11 Ocaml track: lecture 6

Name EID. (calc (parse '{+ {with {x {+ 5 5}} {with {y {- x 3}} {+ y y} } } z } ) )

Fall 2017 Discussion 7: October 25, 2017 Solutions. 1 Introduction. 2 Primitives

Project 1: Scheme Pretty-Printer

CSE 340 Fall 2014 Project 4

PLT 4115 LRM: JaTesté

Summer 2017 Discussion 10: July 25, Introduction. 2 Primitives and Define

Code Generation: An Example

Exercise 1: Balanced Parentheses

CS 61A Interpreters, Tail Calls, Macros, Streams, Iterators. Spring 2019 Guerrilla Section 5: April 20, Interpreters.

SCHEME 10 COMPUTER SCIENCE 61A. July 26, Warm Up: Conditional Expressions. 1. What does Scheme print? scm> (if (or #t (/ 1 0)) 1 (/ 1 0))

Fall 2018 Discussion 8: October 24, 2018 Solutions. 1 Introduction. 2 Primitives

Integers and variables

CS 230 Programming Languages

CS 315 Programming Languages Syntax. Parser. (Alternatively hand-built) (Alternatively hand-built)

ΕΠΛ323 - Θεωρία και Πρακτική Μεταγλωττιστών

CPS 506 Comparative Programming Languages. Syntax Specification

Introduction. Compiler Design CSE Overview. 2 Syntax-Directed Translation. 3 Phases of Translation

The Compilation Process

CSE P 501 Compilers. Parsing & Context-Free Grammars Hal Perkins Winter /15/ Hal Perkins & UW CSE C-1

Chapter 3. Syntax - the form or structure of the expressions, statements, and program units

A clarification on terminology: Recognizer: accepts or rejects strings in a language. Parser: recognizes and generates parse trees (imminent topic)

For example, we might have productions such as:

Integers and variables

COP 3402 Systems Software Top Down Parsing (Recursive Descent)

Context-free grammars (CFG s)

Syntax Errors; Static Semantics

Today. Assignments. Lecture Notes CPSC 326 (Spring 2019) Quiz 5. Exam 1 overview. Type checking basics. HW4 due. HW5 out, due in 2 Tuesdays

Syntax/semantics. Program <> program execution Compiler/interpreter Syntax Grammars Syntax diagrams Automata/State Machines Scanning/Parsing

Chapter 3. Describing Syntax and Semantics ISBN

Parsing II Top-down parsing. Comp 412

Programming Language Definition. Regular Expressions

Week 3: Compilers and Interpreters

Syntax-Directed Translation. Lecture 14

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

B The SLLGEN Parsing System

CSE 582 Autumn 2002 Exam Sample Solution

Assignment 1. Due 08/28/17

Syntax Analysis, III Comp 412

CS 360 Programming Languages Interpreters

Describing Syntax and Semantics

Parsing III. CS434 Lecture 8 Spring 2005 Department of Computer Science University of Alabama Joel Jones

Computing Inside The Parser Syntax-Directed Translation, II. Comp 412 COMP 412 FALL Chapter 4 in EaC2e. source code. IR IR target.

Formal Languages. Formal Languages

TML Language Reference Manual

Why do we need an interpreter? SICP Interpretation part 1. Role of each part of the interpreter. 1. Arithmetic calculator.

Syntax Analysis/Parsing. Context-free grammars (CFG s) Context-free grammars vs. Regular Expressions. BNF description of PL/0 syntax

SCHEME AND CALCULATOR 5b

Context Free Grammars and Recursive Descent Parsing

UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division. P. N. Hilfinger

Compiler Techniques MN1 The nano-c Language

More Scheme CS 331. Quiz. 4. What is the length of the list (()()()())? Which element does (car (cdr (x y z))) extract from the list?

B E C Y. Reference Manual

CS 4240: Compilers and Interpreters Project Phase 1: Scanner and Parser Due Date: October 4 th 2015 (11:59 pm) (via T-square)

CS 415 Midterm Exam Spring 2002

CS 536 Midterm Exam Spring 2013

Programming Languages

Syntactic Analysis. The Big Picture Again. Grammar. ICS312 Machine-Level and Systems Programming

Programming Language Syntax and Analysis

THE PROGRAMMING LANGUAGE NINC PARSER PROJECT. In this project, you will write a parser for the NINC programming language. 1.

Interpreters and Tail Calls Fall 2017 Discussion 8: November 1, 2017 Solutions. 1 Calculator. calc> (+ 2 2) 4

C and Programming Basics

Defining syntax using CFGs

Chapter 3: Syntax and Semantics. Syntax and Semantics. Syntax Definitions. Matt Evett Dept. Computer Science Eastern Michigan University 1999

Decaf Language Reference

Scheme in Scheme: The Metacircular Evaluator Eval and Apply

SCHEME The Scheme Interpreter. 2 Primitives COMPUTER SCIENCE 61A. October 29th, 2012

Writing Evaluators MIF08. Laure Gonnord

Project 2: Scheme Interpreter

CS558 Programming Languages. Winter 2013 Lecture 1

Chapter 4. Syntax - the form or structure of the expressions, statements, and program units

Compiler Construction

Parsing. Note by Baris Aktemur: Our slides are adapted from Cooper and Torczon s slides that they prepared for COMP 412 at Rice.

CSE 413 Programming Languages & Implementation. Hal Perkins Winter 2019 Grammars, Scanners & Regular Expressions

Program Analysis ( 软件源代码分析技术 ) ZHENG LI ( 李征 )

Chapter 3. Describing Syntax and Semantics ISBN

Structurally Recursive Descent Parsing. Nils Anders Danielsson (Nottingham) Joint work with Ulf Norell (Chalmers)

CSE P 501 Compilers. Implementing ASTs (in Java) Hal Perkins Autumn /20/ Hal Perkins & UW CSE H-1

Lab 2 Tutorial (An Informative Guide)

Compilers Crash Course

CS Concepts of Compiler Design

Chapter 3. Describing Syntax and Semantics

Transcription:

CS360: Mini Language Michael Conway Original slides by Mark Boady March 11, 2015

Mini language A simple programming language Implemented in PLY You will modify it for PA4

Grammar At the top level, we dene a program program -> stmt_list stmt_list -> stmt SEMICOLON stmt_list stmt stmt -> assign_stmt while_stmt if_stmt define_stmt

Grammar What kind of statements can be used to write a program? assign_stmt -> INDENT ASSIGNOP expr while_stmt -> WHILE expr DO stmt_list OD if_stmt -> IF expr THEN stmt_list ELSE stmt_list FI define_stmt -> DEFINE IDENT PROC LPAREN param_list RPAREN stmt_list END

Grammar Expressions are commands that can actually be evaluated expr -> expr PLUS term term expr -> expr MINUS term term term -> term TIMES fact fact fact -> NUMBER ( expr ) IDENT func_call Basic Elements are numbers or Variable Names Number -> [0-9]+ Ident -> [a-z]+ except reserved words

Grammar Function calls and denitions need inputs func_call -> IDENT LPAREN expr_list RPAREN expr_list -> expr COMMA expr_list expr param_list -> IDENT COMMA param_list IDENT

Parsing Instead of evaluating as we parse Step 1: Build a Parse Tree Step 2: Evaluate the Parse Tree def p_add( p ) : 'expr : expr PLUS term' p[0] = Plus( p[1], p[3] ) def p_assn( p ) : 'assign_stmt : IDENT ASSIGNOP expr' p[0] = AssignStmt( p[1], p[3] )

Parse Tree Parse Tree Objects Constructor Creates a Tree eval walks the tree and evaluates the expression Display is for debugging class Plus( Expr ) : def init ( self, lhs, rhs ) : self.lhs = lhs self.rhs = rhs def eval( self, nt, ft ) : return (self.lhs.eval( nt, ft ) + self.rhs.eval( nt, ft )) def display( self, nt, ft, depth=0 ) :...

Evaluating Evaluating the Parse Tree def eval( self, nt, ft ) : return (self.lhs.eval( nt, ft ) + self.rhs.eval( nt, ft )) nt - The name table, stores the names of variables and values ft - function table, stores the dened functions

Parse Tree Parse Tree for a := 4 * 7; b := a+5

Parse Tree Parse Tree for a := 4 * 7; b := a+5

Parse Tree Parse Tree for a := 4 * 7; b := a+5

Parse Tree Parse Tree for a := 4 * 7; b := a+5

Decorating the Tree Parse Tree for a := 4 * 7; b := a+5

Decorating the Tree Parse Tree for a := 4 * 7; b := a+5

Decorating the Tree Parse Tree for a := 4 * 7; b := a+5

Decorating the Tree Parse Tree for a := 4 * 7; b := a+5

Decorating the Tree Decorating the parse tree evaluates an expression In the Mini Language, we use the eval(nt,ft) function for this The Mini Language is interpreted, it parses straight to a result We can also use the parse tree to compile Now we see how the code could be decorated with assembly code

Decorating the Tree (ver. 2) Parse Tree for a := 4 * 7; b := a+5

Decorating the Tree (ver. 2) Parse Tree for a := 4 * 7; b := a+5

Decorating the Tree (ver. 2) Parse Tree for a := 4 * 7; b := a+5

Assembly Code Note: If you used g++ it would make 4*7 a constant 28. Ltmp4:.cfi_def_cfa_register %rbp movl $0, %eax movl $4, -4(%rbp) movl $7, -8(%rbp) movl -4(%rbp), %ecx imull -8(%rbp), %ecx movl %ecx, -12(%rbp) movl $5, -16(%rbp) movl -12(%rbp), %ecx addl -16(%rbp), %ecx movl %ecx, -20(%rbp) popq %rbp ret.cfi_endproc

An Example Program A program to nd the factorial of n n := 0-5; if n then i := n else i := 0 - n fi; fact := 1; while i do fact := fact * i; i := i - 1 od For while and if, false means <=0 and true means >0

Running a Program $ python interpreterext.py < TestInput/fact.p [42] Running Program Dump of Symbol Table Name Table i -> 0 fact -> 120 n -> -5 Function Table The program has no print command We can see the nal symbol table and look for answers

PA4 Mini Language Question of PA4 Add the comparison operators: < > <= >= ==!= Currently: true means > 0 false means <= 0 You can make the operators return 0 or 1 (4 < 5) returns 1 for true (5 == 7) return 0 for false

PA4 Add Lists expr : list_stuff list_stuff : LBRACKET expr_list RBRACKET list_stuff : LBRACKET RBRACKET list_stuff : CAR expr list_stuff : CDR expr list_stuff : CONS expr COMMA expr list_stuff : NULL expr

PA4 Problem 2 Base it on your code from PA3 Problem 3 Redo Problem 3 of PA2 in SICP Query Language Read the SICP Book You can base on the address database we saw in lab 4

Lab 6 Part 1: Add Exponent to Recursive Descent Parser Part 2: Complete Recursive Descent Parse for Reg Expr Part 3: Test the Mini Language

Lab 6 def expr(self): result=self.term() while self.token == '+': self.match('+',"+ expected") result +=self.term() return result def term(self): result = self.factor() while self.token == '*': self.match('*',"* Expected") result *= self.factor() return result

Lab 6 def factor(self): if self.token=="(": self.match("(","( Expected") result = self.expr() self.match(")",") Expected") else: result = self.number() return result def number(self): mystr="" while self.token.isdigit(): mystr+=self.token self.gettoken() return int(mystr)

Concat No Character to match on! def concat(self): #concat -> closure { closure} result = self.closure() while What do I match on: Do I need to match anything? result = RegConcat(result,self.closure()) return result What can be after the concat? letters a-z or a left parenthesis What can't be after concat? *,,), newline