Pascal Programs |
2 |
![]() |
Building a program with SPARCompiler Pascal requires three steps:
Compiling the Program
Now compile the program with pc, the Pascal compiler, by typing at the system prompt:hostname% pc temp.p
Pascal names the compiled version of the program a.out by default. Running the Program
To run the program, enter a.out at the prompt. The output of temp.p is then displayed:
Renaming the Executable File
It is inconvenient to have the result of every compilation in a file called a.out. If such a file already exists, it is overwritten. You can avoid this in either of the two following ways:
hostname% temp Fahrenheit Celsius ---------- ------- 32 0.00 33 0.56 34 1.11 . . . . . . |
An Interactive Pascal Program
In Pascal, the predefined file variable, input, is equivalent to the operating system standard input, stdin. Similarly, the file variable, output, is equivalent to the standard output, stdout.
program copy(input, output); { This program copies input to output. } var c: char; begin while not eof do begin while not eoln do begin read(c); write(c) end; readln; writeln end end. { copy } |
Compiling the Program
Use the pc command to compile the program and store it in the executable file copy. Here is the command format:hostname% pc -o copy copy.p
Running the Program
Because the standard input and standard output default to the keyboard and the display screen, respectively, the program simply echoes on the screen each line you type on the keyboard. The program terminates when you type the end-of-file (Control-d) character at the beginning of a line. Try it:
hostname% copy<Return> Hello, are you listening?<Return> Hello, are you listening? Goodbye, I must go now.<Return> Goodbye, I must go now. (Control-d) hostname% |
Redirecting I/O
To write the output to a file instead of to the display screen, use the redirection operator, >, followed by a file name. For instance, to write to a file called data, enter the following:
hostname% copy > data<Return> Hello, are you listening?<Return> Goodbye, I must go now.<Return> (Control-d) hostname% |
Using the same program, but with the < operator to redirect input, you can print the file on the display screen:
hostname% copy < data<Return> Hello, are you listening? Goodbye, I must go now. |
Using a File Name as a File Variable
You can redirect the program input or output to files by listing the files as file variables in the program statement. The Pascal library associates each (input, output) file variable with a file named in the program statement. For example, copy2.p lists data as the input file variable and output as the output file variable:
Assuming that the file data is still in the current directory, you can compile and run the program as follows:
hostname% pc -o copy2 copy2.p hostname% copy2 Hello, are you listening? Goodbye, I must go now. |
Where Did My Program Fail?
SPARCompiler Pascal can trace why a program failed; its traceback utility finds the routine that triggers the error. Using Pascal Traceback
Pascal traceback installs signal handlers on selected signals and dumps a backtrace when those signals are caught. The backtrace shows the chain of calls leading from the routine in which the error occurred, all the way back to the main program.
SIGQUIT |
SIGIOT |
SIGFPE |
SIGSYS |
SIGTERM |
SIGILL |
SIGABRT |
SIGBUS |
SIGPIPE |
SIGLOST |
SIGTRAP |
SIGEMT |
SIGSEGV |
|
|
See the signal(3) man page for further information on these signals.
Using a Sample Program with Segmentation Violation
A segmentation violation occurs when your program tries to reference memory outside your address space. The operating system detects this action and generates an error message. Following is an example program, SegViol.p, which contains a segmentation violation:
Compiling and Running the Program
When you compile and run the program, you receive output similar to the following. The first line indicates the name of the offending signal--in this case, a segmentation violation.
In this example, ErrorInHere reported the error. The ErrorInHere procedure was called by Call1.Call2, which was in turn called by the main program. Routine names, such as Call1.Call2, indicate a nested routine. If Pascal cannot find the name of a routine, for example, because the executable file has been stripped, it prints the hex address.
Using the -g Option
If you compile the program with the -g option, the traceback also reports the arguments, the line number, and the file name of each routine.
The program prints the ASCII values of character variables.