You are on page 1of 5

A Sequence of Lab E x e r c i s e s for an I n t r o d u c t o r y C o m p i l e r C o n s t r u c t i o n

L a w r e n c e A. C o o n D e p a r t m e n t of C o m p u t e r S c i e n c e R o c h e s t e r I n s t i t u t e of T e c h n o l o g y Rochester, New York 14623 (716) 4 7 5 - 2 2 3 7 e-mail: lac@cs.rit.edu

Course

i.

Abstract

A sequence of l a b o r a t o r y e x e r c i s e s for an introd u c t o r y c o m p i l e r c o n s t r u c t i o n c o u r s e is d e s c r i b e d . T h e labs a r e b a s e d on four i n c r e a s i n g l y complex versions of an imperative language designed so that e a c h v e r s i o n builds on the p r e v i o u s . The t h i r d v e r s i o n s u p p o r t s i n t e g e r and c h a r a c t e r data t y p e s a n d a r r a y s of i n t e g e r s a n d c h a r a c t e r s . The fourth version adds procedures, but has only integer data. The procedures do not nest, but d i r e c t r e c u r s i o n is supported.

Usinq a mini-language that c o n c e n t r a t e s on a b a s i c f e a t u r e a n d r e d u c i n g o t h e r l a n g u a g e f e a t u r e s to a b a r e c o r e a p p e a r s to m a x i m i z e l e a r n i n g a n d m i n i m ize b u s y w o r k . However, the c o r e l a n g u a g e m u s t b e designed carefully, a n d the s a t i s f a c t i o n of e n d i n g up w i t h a "real" l a n g u a g e at the end of the c o u r s e is m i s s i n g . S t u d e n t s c a n p r o d u c e u g l y code and f e w i n s t r u c t o r s have the t i m e or the g r a d i n g support needed to give the feedback they'd like, so r e a d i n g (and modifying) w e l l d e s i g n e d real c o d e is a l s o important. In the following, a set of g o a l s is d e v e l o p e d based on a combination of the a b o v e a p p r o a c h e s . T h e n a s e r i e s of p r o j e c t s that m e e t t h o s e g o a l s is d i s c u s s e d in detail.

2.

Introduction
to d e s i g n i n g construction a subset or Ada.

There are several popular approaches p r o j e c t s f o r an i n t r o d u c t o r y c o m p i l e r course. T h e s e include: the "tiny" language approach: of a popular language like [Aho, Fis]

using Pascal

3.

Goals

the "mini" language approach: language that concentrates on feature with minimal support features. [Mar]

defining a a specific for other

T h e f u n d a m e n t a l o b j e c t i v e is to c r a m the b a s i c s of compiler design into a p r o j e c t that a d i l i g e n t s t u d e n t c a n c o m p l e t e in o n e term. (At m y s c h o o l this m e a n s a I0 w e e k quarter.) To that e n d the f o l l o w i n g g o a l s w e r e adopted: (I) Use tools where possible The mastery of some b a s i c c o m p i l e r c o n s t r u c tion tools is an important end in itself. Tool u s e a l s o a v o i d s s t u d e n t s g e t t i n g c a u g h t up in the d e t a i l s of w r i t i n g l e x i c a l a n a l y z e r s and parsers (which s h o u l d b e the s u b j e c t of o t h e r courses) a n d a l l o w s t h e m to q u i c k l y get on to m o r e important issues. The tools I selected were the ubiquitous lex and yacc because of t h e i r w i d e a v a i l a b i l i t y a n d generality. [Lev] (2) Use b o t h top-down and bottom-up parsing Introduce recursive-descent techniques and use t h e m for the s i m p l e s t project, then s h i f t to t o p - d o w n t e c h n i q u e s u s i n g y a c c for the rest of the p r o j e c t s . (3) U s e ad h o c semantics Time considerations force ad h o c semantics (unless students e n t e r w i t h an u n d e r s t a n d i n g of one of the standard methodologies and a p p r o p r i a t e s u p p o r t tools a r e a v a i l a b l e . )

the " f i l l i n g in the b l a n k s " a p p r o a c h : p r o v i d ing the s k e l e t o n of a c o m p i l e r w i t h i m p l e m e n tation details for critical features to b e p r o v i d e d b y the student. for is

T h e s e can g e n e r a t e c o d e for real m a c h i n e s , or hypothetical machines for w h i c h a simulator available.

In addition, t h e r e is the "reading" a p p r o a c h w h e r e students analyze and discuss the code for an existing compiler. This is p r o b a b l y most often u s e d in c o n d u c t i o n w i t h one of the above. Each has its advantages.

Basing the project languaqe on a s u b s e t of a l a n g u a g e k n o w n b y the s t u d e n t s m i n i m i z e s the overh e a d n e e d e d to u n d e r s t a n d the l a n g u a g e a n d e n a b l e s the s t u d e n t to c o n c e n t r a t e on the c o m p i l e r d e s i g n itself. Furthermore, simple variations of language features can generate interesting discussions about language design without adding undo confusion. These can also disabuse students of the n o t i o n that all that is g o o d is in t h e i r favorite language, a n d i n t r o d u c e t h e m to the idea that language and compiler design not only go hand-in-hand b u t a r e b a s e d on the art of c o m p r o m ise.

(4) Concentrate

on the front end

Again, time c o n s i d e r a t i o n s f o r c e an e m p h a s i s on the w e l l u n d e r s t o o d and a d e - e m p h a s i s on the m o r e " i n t e r e s t i n g " issues such as r e g i s t e r allocation, optimization, etc. A l s o this is

S GL C BIU LS EE TIN

VoL 28 No. 3 Sept:. 1996

60

an u n d e r g r a d u a t e c o u r s e and f r o n t e n d i s s u e s a r e w h a t w i l l b e the m o s t u s e f u l for t h o s e that get programming j o b s a n d m i g h t h a v e to create interfaces and other simple language translators. For e x a m p l e , p a r s i n g a n d interpreting an a r g u m e n t s t r e a m to a cgi p r o g r a m u s e d at a W o r l d W i d e W e b site. (5) U s e q u a d s as an i n t e r m e d i a t e c o d e Q u a d s a r e e a s y to u n d e r s t a n d , a n d t h e y can be r e a d i l y i n t e r p r e t e d or u s e d to g e n e r a t e code. Other approaches might be m o r e interesting, b u t time is a h a r d m a s t e r . (6) Use a quad interpreter f o r the m o r e c o m p l e x versions A s i m p l e t e m p l a t e b a s e d c o d e g e n e r a t o r for an assembly language can be assigned for the simpler versions. However when implementing more advanced features code generation gets tedious and time consuming, so a v o i d it b y j u s t i n t e r p r e t i n g the quads. s t i c k to the m o s t b a s i c c o n t r o l s t r u c t u r e s U s e " w h i l e " as the o n l y l o o p i n g c o n s t r u c t and " i f / t h e n / e l s e " as the o n l y d e c i s i o n c o n s t r u c t . This gives full functionality without the b u s y w o r k of i m p l e m e n t i n g o t h e r " n o n e s s e n t i a l " constructs. u s e two d a t a t y p e s s h o u l d f a c e the i s s u e s of t y p e c h e c k i n g two t y p e s is the m i n i m u m . I use integer character, and include arrays of both. w a y y o u h a v e s i m p l e s t r i n g s for free.

4.1.

Eenie

E e n i e is a s i m p l e e x p r e s s i o n l a n g u a g e w i t h no c o n trol s t r u c t u r e s . (See A p p e n d i x A for a c o m p l e t e specification}. The language features supported are: Here input and outpu~ of integers.

variable

declaration

assignment basic integer arithmetic eenie program:

is an e x a m p l e -- E x a m p l e program decls body

example int sam,

has y

(7)

end

r e a d (sam) y <- s a m + -3 w r i t e (y) example

(8) O n l y They and and This (9)

This leads to two projects. The first is to i m p l e m e n t it u s i n g r e c u r s i v e d e s c e n t (lex for lexical analysis but no yacc). The second is to implement an interpreter using yacc. Students have already s e e n h o w to h a n d l e a r i t h m e t i c w h e n s t u d y i n g l e x a n d yacc, so t h e y o n l y n e e d to a d d the io, d e c l a r a t i o n and syntactic s u g a r for the program structure.

S t i c k to b a s i c s u b r o u t i n e i s s u e s Use simple C-like procedures with no nesting to a v o i d the c o m p l e x s y m b o l t a b l e m a n a g e m e n t and the complex runtime stack management n e e d e d to r e s o l v e scope. Investigate some simple language design issues H e r e is w h e r e a few d i f f e r e n c e s f r o m the u s u a l w o r l d of C, P a s c a l a n d M o d u l a 2 can b e introduced. S o m e of the i s s u e s I l i k e are: The use minator of e n d - o f - l i n e as a s t a t e m e n t rather than a semicolon. ter-

4.2.

Meeney

(i0)

Meeney adds simple control structures to eenie. (See A p p e n d i x B for a complete specification}. T h e l a n g u a g e f e a t u r e s s u p p o r t e d are: relational operators

boolean expressions in the C s e n s e of z e r o m e a n i n g TRUE. (Other v e r s i o n s u s e d the traditional B o o l e a n o p e r a t o r s . ) if s t a t e m e n t while with optional else

T h e u s e of e n d - o f - l i n e as p a r t of the syntax of l a n g u a g e e l e m e n t s to e n f o r c e s t y l e standards; e.g. the if s y n t a x h a s an eol after the "then" to f o r c e the b o d y to s t a r t on a s e p a r a t e line. Simple run-time and compile-time d e t e c t i o n ; e.g. d i v i s i o n by z e r o and bounds checking. Global and local scoping error array

Here

statement meeney has program:

is an e x a m p l e program locals body

example int a

for v a r i a b l e s .

4.

Overview

of

the Languages

The p r o j e c t language is an imperative language w i t h f e a t u r e s f r o m P a s c a l a n d C. It is d e v e l o p e d in four stages: eenie, meenie, miney a n d moe. E a c h of the f i r s t t h r e e is a s u p e r s e t of the previous, a n d is t o t a l l y w r i t t e n b y the s t u d e n t f r o m a B N F s y n t a x s p e c i f i c a t i o n a n d an i n f o r m a l statem e n t of the s e m a n t i c s . T h e last v e r s i o n o n l y supp o r t s i n t e g e r d a t a a n d no a r r a y s -- the e m p h a s i s is on p r o c e d u r e s and scoping variables. For this version, the s t u d e n t a d d s c o d e to s u p p o r t the n e w f e a t u r e s to ~ w o r k i n g v e r s i o n f r o m w h i c h t h a t c o d e has b e e n s t r i p p e d .

a <- 9 w h i l e a > 0 do a <- a-1 if ( a - 2* (a/2)) w r i t e (a) endif endwhi Ie writeln end example

then

4.3.

Miney

Miney adds a character data type and arrays to meeney. (See A p p e n d i x C f o r a c o m p l e t e s p e c i f i c a tion). T h e l a n g u a g e f e a t u r e s s u p p o r t e d are: character checking and integer data types with and type
and

character and integer arrays compile-time bounds checking

run-time

SIGCSE BULLETIN

V o i 28 No. 3 Sept. 1996

61

Here

block simple is an

assignment string example support miney using program: character arrays

[Fis] Fisher and Benjamin Cummings, [Mar]


Landscape,

LeBlanc, 1988.

Crafting

Compiler, Language and yacc,

program example has decls int array x with 5 elts char array y with 5 elts body x[1] <- 999 write ( x[l] ) y(l] <- 'a' y[2] <- 'b' y[3] <- 'c' y[4] <- '^' write ( y ) writeln end example

Marcotty SRA,

and Ledgard, 1986.

Programming lex

[Levi Levine, Mason O'Reilly & Associates, Appendix pgm head decpart bodypart tail varlst stmlst stmt io asgn exp term factor A: Eenie

and Brown, 1992.

::= ::= ::= ::= : := ::= : := ::= ::= ::= : := : := ::=

4.4.

Moe

M o e is a p r o c e d u r e language with only integers and no arrays. (See A p p e n d i x D f o r a c o m p l e t e s p e c i f ication). T h e l a n g u a g e f e a t u r e s s u p p o r t e d are: Here procedures parameter global is a n and with recursion by reference

BNF for eenie head decpart bodypart tail program NAME has EOS decls EOS int varlst EOS I body EOS stmtlst end NAME v a r l s t , ID I ID stmtlst stmt I stmt io E O S I a s g n EOS r e a d ( ID ) I w r i t e ( e x p ) I writeln ID <- e x p exp + term ~ exp - term I term term * factor ~ term / factor I term % factor I factor ID ~ N U M I ( e x p ) I " f a c t o r Semantics for eenie

passing local

scoping program:

example

moe

program factorial has ......................... p r o c f a c t ( i n t x, y ) h a s locals i n t a, b body a <- x if a = 1 t h e n y <- 1 else a <- a - 1 f a c t ( a , b) y <- x * b endif endproc fact ......................... locals int body a <- 5 f a c t ( a , ans) w r i t e (ans) writeln factorial a, ans

The reserved words are: program, has, d e c l s , int, body, end, read, w r i t e , and writeln. T h e ID a n d NUM stand for identifier and integer constant respectively. Identifiers w i l l b e a n y s e q u e n c e of lower case letters. T h e r e is a s e p a r a t e d e c l a r a tion section for variables, as in P a s c a l , which a r e a l w a y s of t y p e i n t e g e r . When an identifier is d e f i n e d , y o u s h o u l d c r e a t e a symbol table entry after checking for double definition. The symbol table should also be used for storage. When an identifier is referenced s i m p l y l o o k it u p in t h e t a b l e a n d u s e (or store) the value there. The semantics are as expected. Comments start with two dashes and continue to the e n d of t h e line (as in Ada) . For this language, t h e e n d of statement (EOS) is t h e e n d of t h e l i n e . This has the advantage of f o r c i n g some formatting conventions on a program. For example, the declaration section is i n t r o d u c e d by a line containing only the reserved word decls. This also has the disadvantage of restricting the size of arithmetic expressions. To get around t h i s w e w i l l u s e an @ character as a l i n e c o n t i n u a t i o n character. The sequence "@\n" i n d i c a t e s t h a t the n e x t l i n e is a continuation of t h e c u r r e n t line. When printing an integer, l e a v e o n e s p a c e a f t e r it so t h a t w h e n m o r e t h a n o n e is p r i n t e d o n a l i n e the v a l u e s a r e readable. Appendix pgm head decparC bodypart tail varlst s~mlst s t~mt B: meeny

end

A global declaration s e c t i o n is a d d e d w h o s e v a r i ables are available in all p r o c e d u r e s a n d in the main. A l s o a d d e d is t h e a b i l i t y to d e c l a r e l o c a l variables for procedures and for the main. 5. Conclusions

For the last 7 years I have successfully utilized this approach in o n e q u a r t e r introductory underg r a d u a t e a n d g r a d u a t e c o m p i l e r c o u r s e s w h e r e it is imperative that a meaningful a m o u n t of m a t e r i a l b e covered in a s h o r t p e r i o d . Student feedback is quite positive and student success is v e r y h i g h . References [Aho] ples, Aho, Sethi Techniques, and Ullman, Compilers: Princiand Tools, Addison Wesley, 1986.

BNF for meeney ::= h e a d d e c p a r t b o d y p a r t tail ::= p r o g r a m N A M E h a s E O S ::= decls EOS int varlst EOS [ 8 ::=body EOS stmtlst : := end NAME : := varlst , ID / ID ::= stmCls~ s~m~ / s~m~ ::= io EOS i asgn EOS J loop EOS condl EOS

SIGCSE BULLETIN

Vol. 28 No. 3 Sept, 199(;

62

condl

loop bexp relop io asgn exp term factor

: : = if b e x p then E O S s t m t l s t endif ] if b e x p then E O S s t m t l s t else E O S stmtlst endif ::= while bexp do EOS stmtlst endwhile ::= exp / exp relop exp
::=<

/ >

/ <=

/ >=

/ =

/ #

::= ::= ::= ::=

read ( ID ) / write ( exp ) ~ writeln I D <- e x p exp + term / exp - term ~ term term * factor ] term / factor / term % factor / factor ::= ID / N5~ / ( exp ) [ - factor

loop bexp relop io asgn exp term factor var

/ if b e x p then E O S s t m t l s t else E O S scmtlst endif ::=while bexp do EOS stmtlst endwhile ::= exp / exp relop exp
::=< I >

/ <=

/ >=

/ =

t #

::=read ( var ) / write ( exp ) ] writeln ::= var <- exp ::= exp + term / exp - term / term ::= term * factor / term / factor / term % factor / factor ::= var ] NUM / CHAR f ( exp ) / - factor ::= ID / ID [ exp ]

interpreting

meeney
Initialize ter arrays

Semantics

for m i n e y

Y o u a r e to i m p l e m e n t a t r a n s l a t o r f o r t h e l a n g u a g e t h a t p r o d u c e s q u a d s a n d i n t e r p r e t s them. You are to submit all necessary files including a Makefile, yacc and lax source files. Running make should produce an executable module called meeney that compiles the source file into quads as d e s c r i b e d b e l o w a n d i n t e r p r e t s them. If m e e n e y is g i v e n a f l a g -q, it s h o u l d w r i t e the q u a d s to a t e x t f i l e c a l l e d n a m e . q ( w h e r e n a m e is t h e n a m e of t h e s o u r c e file) b e f o r e d o i n g t h e i n t e r p r e t i n g . The quad + * / % < > 1 g e # r w n = t j ? h operators you will use in t h i s l a b are:

i n t e g e r a r r a y s to a l l z e r o s a n d c h a r a c to a l l ?'s A r r a y b o u n d s s t a r t a t 0.

add subtract multiply divide modulo l e s s than, if o p d l is l e s s t h a n opd2, set r e s u l t to 1 e l s e to 0 g r e a t e r than, if o p d l is l e s s t h a n o p d 2 , set r e s u l t to 1 e l s e to 0 l e s s t h a n o r e q u a l , if o p d l is l e s s t h a n o r e q u a l to opd2, s e t r e s u l t to ! e l s e to 0 g r e a t e r t h a n o r e q u a l , if o p d l is g r e a t e r t h a n o r e q u a l to o p d 2 , s e t r e s u l t to 1 e l s e to 0 e q u a l , if o p d l is e q u a l to opd2, s e t r e s u l t to 1 e l s e to 0 n o t e q u a l , if o p d l is n o t e q u a l to o p d 2 , s e t r e s u l t to 1 e l s e to 0 read an integer into result write an integer from result write a newline assign the v a l u e of t h e f i r s t o p e r a n d to r e s u l t t e s t f i r s t o p e r a n d , if it is z e r o j u m p to the q u a d in r e s u l t j u m p to t h e q u a d in r e s u l t handle a runtime error, result will hold an error message number halt C: M i n e y

S i n c e t h e r e a r e n o w t w o b a s i c d a t a t y p e s a n d two c o m p o u n d d a t a types, t y p e c h e c k i n g is n e c e s s a r y . Compile time type checking should include checking f o r a s s i g n m e n t of v a r i a b l e s of o n e t y p e to v a r i ables of another (e.g. character constants to integer variables) and checking for arithmetic operations involving characters constants or variables. These are errors. Type checking should i n c l u d e a r r a y b o u n d s c h e c k i n g as w i t h z e r o d i v i sion, t h i s c a n o c c u r a t c o m p i l e t i m e o r r u n time. Reading in a c h a r a c t e r variable should be done using getchar() . R e a d i n g in a n i n t e g e r v a r i a b l e should be done using scanf("%d"). Two other semantic niceties should be implemented: block assignments a n d s t r i n g I/O. These involve using the b a r e a r r a y n a m e , o t h e r u s e s of the b a r e a r r a y name are errors. If a n a s s i g n m e n t s t a t e m e n t t a k e s the f o r m ' f r e d <sam' w h e r e f r e d a n d s a m a r e a r r a y s of t h e s a m e s i z e a n d e l e m e n t type, d o a b l o c k t r a n s f e r (i.e. c o p y a l l e l e m e n t s in s a m i n t o fred). This avoids o n e of t h e p r i n c i p l e u s e s o f t h e f o r - s t a t e m e n t a n d m a k e s its o m i s s i o n l e s s o n e r o u s . If t h e n a m e of a c h a r a r r a y o c c u r s in a n I / O s t a t e m e n t ; r e a d i n t o t h e a r r a y u n t i l a n e w l i n e is encountered a n d a d d a '^' a t t h e end, o r w r i t e f r o m t h e a r r a y u n t i l a '^' c h a r a c t e r is e n c o u n tered. T h i s w i l l a l l o w us to s i m u l a t e s t r i n g s . I want the terminating c h a r a c t e r &xpHcitly p l a c e d to r e d u c e t h e c h a n c e of e r r o r a n d I w a n t a ' v i s i b l e ' fill character. This removes the up-arrow character from the usable printable character set for this l a n g u a g e . For example, executing the statement read(x) where x is d e c l a r e d to b e a ? e l e m e n t c h a r a c t e r a r r a y a n d t h e i n p u t f r o m s t a n d a r d i n p u t is " h e l l o " w i l l f i l l t h e f i r s t 5 e l e m e n t s a r r a y , p u t a '^' in the 6 t h a n d l e a v e a '?' in t h e 7th. Then executing the statement write(x) w i l l c a u s e " h e l l o " to b e s e n t to s t a n d a r d o u t p u t . Executing write(x) when x c o n t a i n s 'h', 'e', 'i', 'i', 'o', ' ', '?' is a n error and should be detected when the upper bound is e n c o u n t e r e d s i n c e b l a n k s a n d q u e s t i o n m a r k s a r e perfectly good printable characters. Also, exec u t i n g t h e s t a t e m e n t r e a d ( x ) w h e r e x is d e c l a r e d to b e a 7 e l e m e n t c h a r a c t e r a r r a y a n d t h e i n p u t f r o m s t a n d a r d i n p u t is " h e l l o f r e d " is a n e r r o r s i n c e t h e r e is n o room. In fact, x c a n o n l y h o l d strings with at most 6 characters since there must b e r o o m f o r t h e '^' t e r m i n a t i n g character. But e x e c u t i n g r e a d ( y ) w h e r e y is a c h a r v a r i a b l e o r r e a d ( x [ 5 ] ) w i t h h in t h e i n p u t is fine. Note that t h e q u o t e s a r e n o t r e a l l y in the i n p u t o r o u t p u t b u t a r e j u s t t h e r e f o r e m p h a s i s in t h i s d o c u m e n t .

Appendix pgm head decpart declst decl varlst vardecl bodypart tail stmlst stmt condl

BNF
::= ::= ::= ::= ::=

for m i n e y

head decpart bodypart tail program NAME has EOS decls EOS declst ~ s decl declst / decl int varlst EOS ] char v a r l s t EOS ::= varlst , vardecl / vardecl

I D / a r r a y I D w i t h N U M elts body EOS stmClst end N A M E stmtlst stmt ] stmC io EOS [ asgn EOS / loop EOS [ condl EOS : : = if b e x p then E O S s t m t l s t endif ::= ::= ::= ::= ::=

BULLETIN

SIGCSE

Vol. 28 No. 3 Sept. 1996

63

Appendix pgm head gbldecl rtndecl rtnlst rtndef rtnhead


rCntail pardeclsc pardec2 locdecl declst decln varlst body~art stmt io asgn condl

D: M o e B N F for m o e ::= h e a d g b i d e c l r t n d e c l l o c d e c l b o d y p a r t tail ::= p r o g r a m N A M E h a s E O S ::= g l o b a l s E O S d e c l s t / c ::= r ~ n l s t / 8 ::= r t n l s t r t n d e f / r t n d e f ::= r t n h e a d l o c d e c l b o d y p a r t r t n t a i ! ::=proc NAME ( pardeclst ) has E O S / p r o c N A M E has E O S ::= e n d p r o c N A M E E O S ::=pardeclst ; pardecl / pardecl ::= i n t v a r l s t ::= l o c a l s E O S d e c l s t ] 8 ::= d e c l s t d e c l n / d e c l n ::= i n t v a r l s t E O S ::= v a r l s ~ , I D / I D ::=body EOS s~mtls~ ::= i o E O S ] a s g n E O S / c o n d l ~ O S loop EOS [ procall EOS ::= r e a d ( I D ) / w r i t e ( e x p ) ] writeln ::= I D <- e x p ::= if b e x p t h e n E O S s t m t l s t e n d i f / if b e x p then E O S s t m t l s t e l s e E O S s t m t l s t endif ::=while b e x p do E O S s t m t l s t e n d w h i l e ::= I D ( p a r l s t ) / I D ::=parlst , ID /ID ::= e x p / exp top exp : : = < / > I <= f >= l = I # ::= e x p + t e r m ] e x p - t e r m [ t e r m ::= t e r m * f a c C o r / t e r m / f a c t o r / term % factor / factor ::= I D / ~ M [ ( exD ) / " factor

Some

Lexical

Details

Parameters a r e to b e p a s s e d b y r e f e r e n c e and the procedures can be called recursively. Since there is no 'forward' statement and I wouldn't require y o u to m a k e t w o p a s s e s o v e r the p r o c e d u r e d e c l a r a tion section, indirect recursion need not be implemented -- o n l y d i r e c t r e c u r s i o n . Therefore, procedures can be called only after they are declared. The g l o b a l declaration section defines variables that a r e a c c e s s i b l e to all r o u t i n e s i n c l u d i n g t h e main program. The local declaration sections define variables accessible to the b o d y of the a s s o c i a t e d p r o c e d u r e o r to the m a i n p r o g r a m . Note that p r o c e d u r e s a r e d e c l a r e d in the g l o b a l s e c t i o n so local v a r i a b l e s c a n b e d e c l a r e d w i t h the s a m e n a m e as a p r o c e d u r e .

References
{A/ao] Aho, Sethi pleS, Techniques, [Fis] Fisher and Benjamin Cummings, [Mar] M a r c o t t y Landscape, SRA, a n d Ullman, Compilers: Princia n d Tools, A d d i s o n Wesley, 1986. LeBlanc, 1988.
Crafting a Compiler, Language and yacc,

a n d Ledgard, 1986.

Programming lex

loop procall parlst bexp rop exp term factor

[Lev] Levine, Mason O'Reiily & Associates,

and Brown, 1992.

********************************************* From Page ***********************************

Werth and J. Werth (Eds.), 13th Intern. Conference on Software Engineering, Austin (TX), May 12-16, 1991 [40] Zalewski J., Cohesive Use of Commercial 'fools in a Classroom. Proc. 7th SEI Conf. on Software Engineering Education, pp. 65-75, San Antonio (TX), January 5-7, 1994, J.L. Diaz-Herrera (Ed.), Springer-Verlag, Berlin, 1994

[41] Zalewski J., Boiler Water Content Controller Based on EWICS Safety Model. Proc. Intern. Invitational Workshop on the Design and Review of Software Co'ntrolled Safety-Related Systems, Ottawa, Canada, June 28-29, 1993. University of Waterloo, Institute of Risk Research, 1994 [42] Zalewski J. (Ed.), Advanced Multimicroprocessor Bus Architectures. IEEE Computer Society Press, Los Alamitos (CA), 1995

SIGCSE BULLETIN

Vol. 28 No. 3 Sept. 1996

64

You might also like