Lecture 9: Procedures: Part I
Background Material
Reading
- Chapter 8 (sec. 1-4) of the text (Procedures and Environments)
Theme
In this lecture we extend the mini language
from lecture 6 to include user defined
functions and function calls. The implementation of the mini
language from lecture 8 is extended to
include functions.
In this implementation we will insist that all functions are closed.
I.E. they only communicate with the calling environment through parameter
passing and their meaning is determined soley from the statements in their
definition and the parameter values. In the next lecture, we will deal
with non-local reference and scope rules.
Topics
- Review implementation of the mini language from
Lecture 8.
- Procedures and activation records.
- Parameter passing: pass by value, pass by reference, pass by
value-result (also called copy-in, copy-out or copy-restore), pass
by name.
- Extended syntax (define statement and function call).
See programext.y for the
extended grammar and parser, and
programext.l for the extended
tokens and scanner.
- Implementation of procedures (Proc class).
Interface and implementation in
programext.h and
programext.cpp.
See tproc.cpp for main programs that
tests the Proc class.
- Implementation of define statements and programs with procedures.
See tprogext.cpp for main programs that
tests procedures.
- Recursive procedures (already supported)
See tprogrec.cpp for main programs that
tests procedures.
- Interfacing with bison
(interpreterext.ypp,
programext.y,
programext.l)
- Sample programs with procedures
- add.p
- addr.p - recursion
- Instructions for building interpreter
- bison interpreterext.ypp # to create C++ file interpreter.tab.cpp
- bison -d progamext.y # to create header file program.tab.h for lexer
- flex programext.l
- gcc -c lex.yy.c # create the object file lex.yy.o
- g++ interpreterext.tab.cpp programext.cpp lex.yy.o -o interpreterext # create the object file lex.yy.o
References and programs
- John R. Levine, Tony Mason, and Doug Brown,
lex & yacc, 2nd Edition,
O'Reilly & Associates, Inc. (Use Safari at
Drexel Library
Exercises
- Trace through execution of interpreter.
- Study code from the lecture.
- Assignment 2
Created: Feb. 5, 2007 by jjohnson AT cs DOT drexel DOT DOT edu