CS 153: Concepts of Compiler Design

 

Fall Semester 2009

Department of Computer Science
San Jose State University

Instructor: Ron Mak

Assignment #1

Assigned:

Tuesday, August 25

Due:

Tuesday, September 1 at 11:59 pm

 

Individual assignment, 100 points max

Write a Pascal program

The purpose of this assignment is to give you a crash course in Pascal programming. It is important for you to be familiar with Pascal because the source programs for our interpreter and compiler will be written in that language. (For your team compiler project, you can choose a different source language.)

 

Pascal has a relatively simple syntax, but it is a powerful procedure-oriented language. It was popular during the mid-1970s through the 1980s as a teaching language. If you had taken Introduction to Programming during that period, the language you learned would most likely have been Pascal. The original version of Pascal, which the booklets Essentials of Pascal I and II describe, was not object-oriented, unlike later versions of the language and its descendants.

 

For this assignment, write a Pascal program named EmployeeListing that prints a sorted employee listing from data read in from a text file. Here’s an input file that your program must process:

 

1234:P:Jones:Mary

3052:X:Smith:Tom

5886:Y:Lowe:Marie-Antoinette

2912:B:Adams:Wendy

4040:H:Schlesinger:Frank

7023:E:von Braun:William

6273:C:Thomas:Leslie

 

Each line of input contains four fields separated by a : character:

 

  • A four-digit employee id
  • A one-character job code which can be any upper-case letter
  • A last name of indefinite length which can contain any characters except :
  • A first name of indefinite length which can contain any characters except :

 

Note that the first name is not followed by a : character.

 

From this input file, your program should produce an output listing similar to:

 

        Id: 1234

  Job code: P

 Last name: Jones

First name: Mary

Profession: scientist

 

        Id: 2912

  Job code: B

 Last name: Adams

First name: Wendy

Profession: scientist

 

        Id: 3052

  Job code: X

 Last name: Smith

First name: Tom

Profession: unknown

 

        Id: 4040

  Job code: H

 Last name: Schlesinge

First name: Frank

Profession: teacher

 

        Id: 5886

  Job code: Y

 Last name: Lowe

First name: Marie-Anto

Profession: unknown

 

        Id: 6273

  Job code: C

 Last name: Thomas

First name: Leslie

Profession: scientist

 

        Id: 7023

  Job code: E

 Last name: von Braun

First name: William

Profession: teacher

 

7 employees read.

 

Note that your program must sort the employees by id for printing, and that your program must limit each first and last name to 10 characters (although it must be able to read longer names). The program figures out each employee’s profession based on the job code:

 

Profession 

Job codes

scientist

B C P

teacher

E H U

unknown

any other letter

 

To make sure that you exercise some of the key features of Pascal, you must use the following:

 

  • A record data type to contain the information for each employee.
  • You can store the records in a fixed-size array, or you can dynamically allocate the records and store them in a data structure such as a linked list.
  • Use a set type and set operations to determine each employee’s profession.
  • Procedures and functions.
  • At least one parameter passed by value and one parameter passed by reference (some of your procedures and functions might have no parameters at all).
  • Restrict your use of Pascal to the “original” language as described in the two booklets. We will not be using the later advanced Pascal features in this course.
  • You may assume that the input file does not contain any errors and that there are no duplicate employee ids. However, you may not assume that the input file is already sorted.

 

Other than these requirements, keep your program simple! Remember the purpose is to learn Pascal, not to show off your data structures and algorithms skills.

 

Text input hints:

 

  • If ch is a character variable, then read(ch) will read the next character from the input file.
  • A call to readln will skip the rest of the current input line and the next character to be read is the first character of the next input line.

What to turn in                               

  • Your Pascal source file, reasonably commented, as a text file.
  • A text file of your program’s output listing.

 

E-mail your two files as attachments to mak@cs.sjsu.edu by the due date and time. Do not e-mail any executable files because some mailers will reject the entire message. In the subject line, please include the label ASSIGNMENT #1.

The Free Pascal compiler                        

To do your assignment, you can use the open-source Free Pascal compiler, which you download from http://www.freepascal.org/. Besides the compiler that generates executable code for various platforms (Windows, Linux, Mac OS X), it comes with a clunky character-based screen editor. If you’re more adventurous, you can try Lazarus (http://www.lazarus.freepascal.org/) which has a friendlier GUI-based development environment, but it assumes that you know a more advanced version of Pascal.