An Interactive Desk Calculator. Project P2 of. Common Lisp: An Interactive Approach. Stuart C. Shapiro. Department of Computer Science

Similar documents
This book is licensed under a Creative Commons Attribution 3.0 License

University of Utrecht. 1992; Fokker, 1995), the use of monads to structure functional programs (Wadler,

Compiler Construction

Compiler Construction

Unit 3. Operators. School of Science and Technology INTRODUCTION

The PCAT Programming Language Reference Manual

Introduction. Following are the types of operators: Unary requires a single operand Binary requires two operands Ternary requires three operands

The simplest notion of a list matching a pattern is when the pattern is exactly the same as the list. The list certainly matches the pattern but doesn

Chapter 3: Describing Syntax and Semantics. Introduction Formal methods of describing syntax (BNF)

Comp 411 Principles of Programming Languages Lecture 3 Parsing. Corky Cartwright January 11, 2019

LECTURE 17. Expressions and Assignment

Lecture 19: Functions, Types and Data Structures in Haskell

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

A Simple Syntax-Directed Translator

3.5 Practical Issues PRACTICAL ISSUES Error Recovery

Formal Languages and Automata Theory, SS Project (due Week 14)

Expression Evaluation and Control Flow. Outline

1 Lexical Considerations

Prefix/Infix/Postfix Notation

Examples of attributes: values of evaluated subtrees, type information, source file coordinates,

Context-Free Grammar (CFG)

Lambda Calculus. Gunnar Gotshalks LC-1

(defmacro while (condition &body body) `(iterate loop () (if,condition (loop)))))

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

4. Inputting data or messages to a function is called passing data to the function.

CS 440: Programming Languages and Translators, Spring 2019 Mon 2/4

The SPL Programming Language Reference Manual

Functional Programming. Pure Functional Programming

Compiler Techniques MN1 The nano-c Language

9/21/17. Outline. Expression Evaluation and Control Flow. Arithmetic Expressions. Operators. Operators. Notation & Placement

Lexical Considerations

COMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou

Assignment 4 Publication date: 27/12/2015 Submission date: 17/01/ :59 People in-charge: R. Mairon and Y. Twitto

STACKS. A stack is defined in terms of its behavior. The common operations associated with a stack are as follows:

EDIABAS BEST/2 LANGUAGE DESCRIPTION. VERSION 6b. Electronic Diagnostic Basic System EDIABAS - BEST/2 LANGUAGE DESCRIPTION

Modern Programming Languages. Lecture LISP Programming Language An Introduction

Stack Applications. Lecture 27 Sections Robb T. Koether. Hampden-Sydney College. Wed, Mar 29, 2017

TDDD55 - Compilers and Interpreters Lesson 3

Functional programming with Common Lisp

Assignment 5. Introduction

Chapter 7 Control I Expressions and Statements

CMSC 330 Practice Problem 4 Solutions

Chapter 15. Functional Programming Languages

Lexical Considerations

CIS4/681 { Articial Intelligence 2 > (insert-sort '( )) ( ) 2 More Complicated Recursion So far everything we have dened requires

More on Syntax. Agenda for the Day. Administrative Stuff. More on Syntax In-Class Exercise Using parse trees

Principles of Programming Languages COMP251: Syntax and Grammars

CPS 506 Comparative Programming Languages. Syntax Specification

Will introduce various operators supported by C language Identify supported operations Present some of terms characterizing operators

IPCoreL. Phillip Duane Douglas, Jr. 11/3/2010

CMSC 330, Fall 2009, Practice Problem 3 Solutions

Lambda Calculus LC-1

CPS 506 Comparative Programming Languages. Programming Language Paradigm

CMPT 379 Compilers. Parse trees

A simple syntax-directed

PL Revision overview

Chapter 2 Working with Data Types and Operators

CS 164 Handout 11. Midterm Examination. There are seven questions on the exam, each worth between 10 and 20 points.

Postfix Notation is a notation in which the operator follows its operands in the expression (e.g ).

Algorithmic "imperative" language

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

Monday, April 9, 2018

COMPILERS AND INTERPRETERS Lesson 4 TDDD16

15 Unification and Embedded Languages in Lisp

CSE au Final Exam Sample Solution

An Introduction to Trees

Semantic analysis and intermediate representations. Which methods / formalisms are used in the various phases during the analysis?

Compilers. Compiler Construction Tutorial The Front-end

2.2 Syntax Definition

15 212: Principles of Programming. Some Notes on Grammars and Parsing

Advanced Algorithms and Computational Models (module A)

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

Data Structures and Algorithms

Decaf Language Reference

Linear Data Structure

STUDENT OUTLINE. Lesson 8: Structured Programming, Control Structures, if-else Statements, Pseudocode

Expressions and Assignment

The S-Expression Design Language (SEDL) James C. Corbett. September 1, Introduction. 2 Origins of SEDL 2. 3 The Language SEDL 2.

Stating the obvious, people and computers do not speak the same language.

QUIZ: What value is stored in a after this

Fifth Generation CS 4100 LISP. What do we need? Example LISP Program 11/13/13. Chapter 9: List Processing: LISP. Central Idea: Function Application

Chapter 3: Operators, Expressions and Type Conversion

CSci 450, Org. of Programming Languages. Chapter 12 Expression Language Compilation

MIDTERM EXAM (Solutions)

Lexical and Syntax Analysis. Top-Down Parsing

Lecture Chapter 6 Recursion as a Problem Solving Technique

X Language Definition

Principles of Programming Languages 2017W, Functional Programming

Wednesday, November 8, 2017

9 Using Equation Networks

EDAN65: Compilers, Lecture 04 Grammar transformations: Eliminating ambiguities, adapting to LL parsing. Görel Hedin Revised:

CSc 10200! Introduction to Computing. Lecture 4-5 Edgardo Molina Fall 2013 City College of New York

Stacks. stacks of dishes or trays in a cafeteria. Last In First Out discipline (LIFO)

Symbolic Reasoning. Dr. Neil T. Dantam. Spring CSCI-561, Colorado School of Mines. Dantam (Mines CSCI-561) Symbolic Reasoning Spring / 86

Control Flow February 9, Lecture 7

Lambda Calculus and Lambda notation in Lisp II. Based on Prof. Gotshalks notes on Lambda Calculus and Chapter 9 in Wilensky.

Some Applications of Stack. Spring Semester 2007 Programming and Data Structure 1

Stacks. Chapter 5. Copyright 2012 by Pearson Education, Inc. All rights reserved

The purpose of this lesson is to familiarize you with the basics of Racket (a dialect of Scheme). You will learn about

SMURF Language Reference Manual Serial MUsic Represented as Functions

Transcription:

An Interactive Desk Calculator Project P2 of Common Lisp: An Interactive Approach Stuart C. Shapiro Department of Computer Science State University of New York at Bualo January 25, 1996 The goal of this project is to implement an interactive desk calculator. The moral of the project is that if you don't like the syntax of Common Lisp, you can change it. This lesson can be appreciated by seeing how we could evaluate the arithmetic expression 10 52 3 4 5 6 25 at the end of each of several chapters: 4: > (- (- (/ (* (expt 10 (expt 5 2)) 3) 4) (* 5 6)) 25) 19: > (compute '(10 ^ 5 ^ 2 * 3 / 4-5 * 6-25)) 20: > (compute 10 '^ 5 '^ 2 '* 3 '/ 4 '- 5 '* 6 '- 25) 23: > (compute 10 ^ 5 ^ 2 * 3 / 4-5 * 6-25) 29: > 10 ^ 5 ^ 2 * 3 / 4-5 * 6-2 = 7499999999999999999999968 Each version translates an expression using some initial syntax into an expression in Cambridge Prex notation, and then evaluates the latter expression. The versions dier in having the initial syntax closer and closer to the standard syntax of arithmetic expressions. This syntax is: expression ::= term expression {+ -} term term ::= factor term {* /} factor factor ::= operand operand ^ factor This grammar ignores several complications such as unary + and -, embedded expressions enclosed in parentheses, and embedded calls to Common Lisp functions. These complications are gradually introduced as the exercises progress. 1

The key function that does the translation is (defun prefix (expression) "Returns the arithmetic EXPRESSION translated into Cambridge Prefix notation." The design of prefix is based on the idea of a recursive descent parser (Notice how this directly follows the above grammar.): To get an expression: 1. Get the rst term. 2. If there's no more to the expression, return the rst term. 3. Else, the rst element of the remaining expression will be + or -; call that the operator, remove it, and get the rst term of the remainder, calling it the second term. 4. Combine the rst term, the operator, and the second term into Cambridge Prex notation, and put it back on the front of the expression. 5. Get an expression, and return it. To get a term: 1. Get the rst factor. 2. If there's no more to the expression, return the rst factor. 3. Else, if the rst element of the remaining expression is + or -, return the rst factor. 4. Else, the rst element of the remaining expression will be * or /; call that the operator, remove it, and get the rst factor of the remainder, calling it the second factor. 5. Combine the rst factor, the operator, and the second factor into Cambridge Prex notation, and put it back on the front of the expression. 6. Get a term, and return it. To get a factor: 1. Get the rst operand. 2. If there's no more to the expression, return the rst operand. 3. Else, if the rst element of the remaining expression is not ^, return the rst operand. 4. Else, the rst element of the remaining expression will be ^; call that the operator, remove it, and get the rst factor of the remainder, calling it the second argument. 2

5. Combine the rst operand, the operator, and the second argument into Cambridge Prex notation, and return it. The procedures to get an expression, a term, and a factor should return two things: 1. an expression, term, or factor; 2. the rest of the expression. Common Lisp has a way for functions to return multiple values, but that is an advanced topic, so until that topic is reached, the technique we will use is to cons the expression, term, or factor onto the front of the rest of the expression, where it will look like an initial argument. Thus the major functions we will write will have the following specications: (defun prefix (expr) "Returns the expression in Cambridge Prefix." (defun enclose-expression (expr) "EXPR is a cons representing an expression in infix. Its first term is prefixed and enclosed as its first member. Returns EXPR entirely prefixed and enclosed in a list." (defun enclose-term (expr) "EXPR is a cons representing an expression in infix. Its first factor is prefixed and enclosed as its first member. Returns EXPR with its first term prefixed and enclosed in a list." (defun enclose-factor (expr) "EXPR is a cons representing an expression in infix. Returns EXPR with the first factor prefixed and enclosed in a list." (defun combine-expr (op x e) "OP is an operator. X is its first operand. E is a list that starts with OP's second operand, Y. Returns E with (OP X Y) replacing Y." 3

Here is a schematic trace of the entire procedure: Translate to prex (10 ^ 5 ^ 2 * 3 / 4-5 * 6-25) 1. Enclose a factor of (10 ^ 5 ^ 2 * 3 / 4-5 * 6-25) 1.1. The rst operand is 10 1.2. The operator is ^ 1.3. Enclose a factor of (5 ^ 2 * 3 / 4-5 * 6-25) 1.3.1. The rst operand is 5 1.3.2. The operator is ^ 1.3.3. Enclose a factor of (2 * 3 / 4-5 * 6-25) 1.3.3.1. The rst operand is 2 1.3.3.2. Return (2 * 3 / 4-5 * 6-25) 1.3.4. The second argument is 2 1.3.5. Return ((^ 5 2) * 3 / 4-5 * 6-25) 1.4. The second argument is (^ 5 2) 1.5. Return ((^ 10 ( ^ 5 2)) * 3 / 4-5 * 6-25) 2. Enclose a term of ((^ 10 ( ^ 5 2)) * 3 / 4-5 * 6-25) 2.1. The rst factor is (^ 10 ( ^ 5 2)) 2.2. The operator is * 2.3. Enclose a factor of (3 / 4-5 * 6-25) 2.3.1. The rst operand is 3. 2.3.2. Return (3 / 4-5 * 6-25) 2.4. The second factor is 3 2.5. Enclose a term of ((* (^ 10 ( ^ 5 2)) 3) / 4-5 * 6-25) 2.5.1. The rst factor is (* (^ 10 ( ^ 5 2)) 3) 2.5.2. The operator is / 2.5.3. Enclose a factor of (4-5 * 6-25) 2.5.3.1. The rst operand is 4 2.5.3.2. Return (4-5 * 6-25) 2.5.4. The second factor is 4 2.5.5. Enclose a term of ((/ (* (^ 10 ( ^ 5 2)) 3) 4) - 5 * 6-25) 2.5.5.1. The rst factor is (/ (* (^ 10 ( ^ 5 2)) 3) 4) 2.5.5.2. Return ((/ (* (^ 10 ( ^ 5 2)) 3) 4) - 5 * 6-25) 2.5.6. Return ((/ (* (^ 10 ( ^ 5 2)) 3) 4) - 5 * 6-25) 2.6. Return ((/ (* (^ 10 ( ^ 5 2)) 3) 4) - 5 * 6-25) 4

3. Enclose an expression of ((/ (* (^ 10 ( ^ 5 2)) 3) 4) - 5 * 6-25) 3.1. The rst term is (/ (* (^ 10 ( ^ 5 2)) 3) 4) 3.2. The operator is - 3.3. Enclose a factor of (5 * 6-25) 3.3.1. The rst operand is 5 3.3.2. Return (5 * 6-25) 3.4. Enclose a term of (5 * 6-25) 3.4.1. The rst factor is 5 3.4.2. The operator is * 3.4.3. Enclose a factor of (6-25) 3.4.3.1. The rst operand is 6 3.4.3.2. Return (6-25) 3.4.4. The second factor is 6 3.4.5. Enclose a term of ((* 5 6) - 25) 3.4.5.1. The rst factor is (* 5 6) 3.4.5.2. Return ((* 5 6) - 25) 3.4.6. Return ((* 5 6) - 25) 3.5. The second term is (* 5 6) 3.6. Enclose an expression of ((- (/ (* (^ 10 ( ^ 5 2)) 3) 4) (* 5 6)) - 25) 3.6.1. The rst term is (- (/ (* (^ 10 ( ^ 5 2)) 3) 4) (* 5 6)) 3.6.2. The operator is - 3.6.3. Enclose a factor of (25) 3.6.3.1. The rst operand is (25) 3.6.3.2. Return (25) 3.6.4. Enclose a term of(25) 3.6.4.1. The rst factor is (25) 3.6.4.1. Return (25) 3.6.5. Enclose an expression of ((- (- (/ (* (^ 10 ( ^ 5 2)) 3) 4) (* 5 6)) 25)) 3.6.5.1. The rst term is (- (- (/ (* (^ 10 ( ^ 5 2)) 3) 4) (* 5 6)) 25) 3.6.5.2. Return ((- (- (/ (* (^ 10 ( ^ 5 2)) 3) 4) (* 5 6)) 25)) 3.6.6. Return ((- (- (/ (* (^ 10 ( ^ 5 2)) 3) 4) (* 5 6)) 25)) 3.7. Return ((- (- (/ (* (^ 10 ( ^ 5 2)) 3) 4) (* 5 6)) 25)) 4. Return (- (- (/ (* (^ 10 ( ^ 5 2)) 3) 4) (* 5 6)) 25) To simplify and to save space this has ignored the fact that the Cambridge Prex version of ^ is EXPT. 5

Revised Sequence of P2 Exercises You should do the p2 exercises in the order shown below, even if the numbering is now out of order. 12.5{6 Do as specied on p. 81. 12.3 Compile your calculator le by evaluating (compile-file "calculator"). 12.4 Load your calculator le using the load function. Can you tell that you've loaded the compiled le instead of the source le? Test combine-expr again. 17.33 Do as specied on p. 121. 17.36 Do as specied on p. 122. 17.35 Do as specied on pp. 121{122, but allow the exponentiation operator, ^, to be in the expression also. 17.34 Write enclose-expression to meet the specications shown above, assuming that the only operators are binary + and -, along with *, /, and ^, and assuming that the input expression is a list whose only sublist may be the initial term. 18.28{29 Dene prefix to take an expression that may have binary + and -, *, /, and ^, and return the expression in Cambridge Prex Notation. 19.10{13 Do as specied on p. 147. 19.14 Do as specied on p. 147, but note that, although + and - are names of Common Lisp functions, unary + or - may be the rst element of a list, and that list should be modied. 19.15{16 Do as specied on p. 147. 20.3{5 Do as specied on p. 152. 21.9{10 Do as specied on p. 158. 23.6{8 Do as specied on p. 171. 24.8 Do as specied on p. 180. 29.33 Do as specied on p. 228. 6