Expressions Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section 8.1-8.2.

Slides:



Advertisements
Similar presentations
Expressions and Statements. 2 Contents Side effects: expressions and statements Expression notations Expression evaluation orders Conditional statements.
Advertisements

Stacks - 3 Nour El-Kadri CSI Evaluating arithmetic expressions Stack-based algorithms are used for syntactical analysis (parsing). For example.
Prefix, Postfix, Infix Notation
C o n f i d e n t i a l Developed By Nitendra NextHome Subject Name: Data Structure Using C Title : Overview of Stack.
PZ14A Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ14A - Text processing Programming Language Design and.
1 Languages and Compilers (SProg og Oversættere) Sequence control and Subprogram Control.
Louden, Chapter 7 - Control I: Expressions and Statements Programming Languages: Principles and Practice, 2nd Ed. Kenneth C. Louden.
1 Chapter 7: Expressions Expressions are the fundamental means of specifying computations in a programming language To understand expression evaluation,
Chapter 8 . Sequence Control
CS 206 Introduction to Computer Science II 03 / 16 / 2009 Instructor: Michael Eckmann.
Chapter 7 Expressions and Assignment Statements. Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Arithmetic Expressions Arithmetic evaluation.
CS 206 Introduction to Computer Science II 10 / 28 / 2009 Instructor: Michael Eckmann.
Lecture 18 Last Lecture Today’s Topic Instruction formats
Sequence Control Chapter 6. 2 l Control structures: the basic framework within which operations and data are combined into programs. Sequence control.
Intro to Programming Problem 1: Computers have to be told exactly what do to. Problem 2: Computers only understand “Machine language”. Problem 3: Machine.
PZ07A Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ07A - Expressions Programming Language Design and Implementation.
Stack Applications.
Appendixes 4. An Introduction to PostScript ® CVG Lab.
DATA STRUCTURE & ALGORITHMS CHAPTER 3: STACKS. 2 Objectives In this chapter, you will: Learn about stacks Examine various stack operations Discover stack.
Chapter 3: Data Types and Operators JavaScript - Introductory.
Overview What is PostScript? Types Language Concepts Control Operators Examples.
CSI 3120, Expressions and assignment, page 1 Expressions and assignment Points for discussion Arithmetic expressions Overloading Logical expressions Assignment.
Linear Data Structures LIFO – Polish notation Context Saving.
Expressions and Statements. Expressions Literals and identifiers are expressions More complex expressions are built from simple expressions by the application.
Expressions and Assignment Statements
ISBN Chapter 7 Expressions and Assignment Statements.
PZ07A Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ07A - Expressions Programming Language Design and Implementation.
PZ07A Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ07A - Expressions Programming Language Design and Implementation.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 19: Stacks and Queues (part 2)
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 18: Stacks and Queues (part 2)
Text processing Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section 12.1.
1 Expressions Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
IS 1014 Introduction to Computer Graphics -- Paul Munro A Postscript Tutorial Book available at: cdf.fnal.gov/offline/PostScript/BLUEBOOK.PDF.
Semantics (1).
Copyright © Curt Hill Stacks An Useful Abstract Data Type.
Expressions Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
1 Introduction to PostScript Sep. 21 Dae-Eun Hyun 3D MAP Lab.
CPS Review of Data Structures l We’ve studied concrete data structures/type (CDT)  Vectors Homogeneous aggregates supporting random access  Linked.
Prefix, Postfix, Infix Notation. Infix Notation  To add A, B, we write A+B  To multiply A, B, we write A*B  The operators ('+' and '*') go in between.
ECE 103 Engineering Programming Chapter 4 Operators Herbert G. Mayer, PSU CS Status 6/19/2015 Initial content copied verbatim from ECE 103 material developed.
OPERATORS IN C CHAPTER 3. Expressions can be built up from literals, variables and operators. The operators define how the variables and literals in the.
CSE 3302 Programming Languages Chengkai Li, Weimin He Spring 2008 Control I Expressions and Statements Lecture 9 – Control I, Spring CSE3302 Programming.
Expressions and Assignment Statements
Review Use of Stack Introduction Stack in our life Stack Operations
Expressions and Assignment
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Computing Fundamentals
CO4301 – Advanced Games Development Week 2 Introduction to Parsing
Stack application: postponing data usage
PART II STACK APPLICATIONS
Stack Data Structure, Reverse Polish Notation, Homework 7
Chap. 6 :: Control Flow Michael L. Scott.
Expressions and Assignment Statements
Page description language from Adobe
C Operators, Operands, Expressions & Statements
Chap. 6 :: Control Flow Michael L. Scott.
Associativity and Prescedence
Languages and Compilers (SProg og Oversættere)
Expressions Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
CSC 143 Java Applications of Trees.
Chapter 7 Expressions and Assignment Statements.
Text processing Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section 12.1.
C++ Programming Language Lecture 4 C++ Basics – Part II
Chapter 6 Programming the basic computer
ENERGY 211 / CME 211 Lecture 5 October 1, 2008.
PZ07A - Expressions Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section PZ07A.
Expressions and Assignment Statements
Expressions Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Stacks A stack is an ordered set of elements, for which only the last element placed into the stack is accessible. The stack data type is also known as.
Presentation transcript:

Expressions Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section 8.1-8.2

Execution-time representation Machine code sequence Tree representation Pre-fix or post-fix

Tree structure rules Slow  software interpreter Problem parse tree Uniform evaluation rules Eager evaluation ? Lazy evaluation (LISP, PROLOG) Z+(Y==0 ? X : X/Y) Side effect a * fun(x) + a Error conditions Optimization, … Short-circuit boolean expression if((A==0) || (B/A)>C) {…} while((I<=UB) && (V[I]>0)) {…}

Postfix Infix notation: Operator appears between operands: 2 + 3  5 3 + 6  9 Implied precedence: 2 + 3 * 4  2 + (3 * 4 ), not (2 + 3 ) * 4 Prefix notation: Operator precedes operands: + 2 3  5 + 2 * 3 5  (+ 2 ( * 3 5 ) )  + 2 15  17 Postfix notation: Operator follows operands: 2 3 +  5 2 3 * 5 + (( 2 3 * 5 +)  6 5 +  11 Called Polish postfix since few could pronounce Polish mathematician Lukasiewicz, who invented it. An interesting, but unimportant mathematical curiosity when presented in 1920s. Only became important in 1950s when Burroughs rediscovered it for their ALGOL compiler.

Evaluation of postfix 1. If argument is an operand, stack it. 2. If argument is an n-ary operator, then the n arguments are already onthe stack. Pop the n arguments from the stack and replace by the value of the operator applied to the arguments. Example: 2 3 4 + 5 * + 1. 2 - stack 2. 3 - stack 3. 4 - stack 4. + - replace 3 and 4 on stack by 7 5. 5 - stack 6. * - replace 5 and 7 on stack by 35 7. + - replace 35 and 2 on stack by 37

Importance of Postfix to Compilers Code generation same as expression evaluation. To generate code for 2 3 4 + 5 * +, do: 1. 2 - stack L-value of 2 2. 3 - stack L-value of 3 3. 4 - stack L-value of 4 4. + - generate code to take R-value of top stack element (L-value of 4) and add to R-value of next stack element (L-value of 3) and place L-value of result on stack 5. 5 - stack L-value of 5 6. * - generate code to take R-value of top stack element (L-value of 5) and multiply to R-value of next stack element (L-value of 7) and place L-value of result on stack 7. + - generate code to take R-value of top stack element (L-value of 35) and add to R-value of next stack element (L-value of 2) and place L-value of result (37) on stack

Forth - A language based on postfix Postfix source language leads to an efficient execution model, even though generally interpreted. System runs on two stacks - a subroutine return stack and an expression evaluation stack. Run-time model very small making it useful on small embedded computers. Forth was developed by Charles Moore around 1970. The name was a contraction of “Fourth Generation Programming Language” with the program name limited to five characters. The language was a replacement for FORTRAN on small minicomputers in the 1970s where space was at a premium and the only input-output device was often a very slow and cumbersome paper tape. Having a resident translator/interpreter made for easy program development on the target system.

Example Forth program Program to compute: 12+22+ ... +92+102$ [Notation: a,b,c is expression stack. c is stack(top)] : SQR DUP * ; (Defines square by: n  n,n (n*n)) : DOSUM SWAP 1 + SWAP OVER SQR + ; Execution: ( N,S  N+1,S+(N+1)2) ( N,S  S,N  S,(N+1) (N+1),S  (N+1),S, N+1) (N+1),S, N+1)2  (N+1),S+(N+1)2) 3 6 DOSUM . . 22 4 ok (Period (.) prints stack(top). Output is 22 = 42+6) 0 0 10 0 DO DOSUM LOOP . 385 ok (Apply DOSUM from 0 to 9 (Stop at 10)) }}

Postscript Postscript is Forth with painting commands added. 1: %Same as Forth program 2: /Helvetica findfont 3: 20 scalefont 4: setfont 5: 200 400 moveto 6: /formatit {10 10 string cvrs show} def 7: /sqr {dup mul} def 8: /dosum {exch 1 add exch 1 index sqr add} def 9: 3 6 dosum 2 copy formatit ( ) show formatit 10: clear 11: 200 375 moveto 12: 0 0 0 1 9 {pop dosum} for formatit 13: showpage

Postscript painting commands 14: % Lets draw a truck 15: /box {newpath 0 0 moveto 0 1 lineto 3 1 lineto 3 0 lineto 16: closepath} def 17: .1 setlinewidth 0 setgray 18: gsave 19: 72 72 scale 20: 2 5 translate box stroke 21: 3.2 0 translate .5 .5 scale box fill 22: 0 1 translate .6 .6 scale box fill 23: grestore 24: /tire {newpath 1 0 moveto 0 0 1 0 360 arc closepath} def 25: .5 setlinewidth 10 10 scale 26: 16 34 translate tire stroke 27: 3 0 translate tire stroke 28: 17 0 translate tire stroke 29: 3 0 translate tire stroke 30: 8 0 translate tire stroke 13: showpage

Precedence of operators Assumed order of evaluation for arithmetic expressions: 2*3+4*5 assumed to be 26 since assumed ordering is (2*3)+(4*5). This is specified by precedence of operators. In any expression, the highest precedence operations are evaluated first, and so on. Most languages have an implied precedence. APL and Smalltalk do not. Neither language emphasizes arithmetic data, so it is not clear what precedence means in this case. C has 17 levels of precedence (given next)

C precedence levels Precedence Operators Operator names 17 tokens, a[k], f()Literals, subscripting, function call .,-> Selection 16 ++, -- Postfix increment/decrement 15* ++, -- Prefix inc/dec , -, sizeof Unary operators, storage !,&,* Logical negation, indirection 14 typename Casts 13 *, /, % Multiplicative operators 12 +,- Additive operators 11 <<, >> Shift 10 <,>,<=, >= Relational 9 ==, != Equality 8 & Bitwise and 7  Bitwise xor 6 | Bitwise or 5 && Logical and 4 || Logical or 3 ?: Conditional 2 =, +=, -=, *=, Assignment /=, %=, <<=, >>=, &=, =, |= 1 , Sequential evaluation