One of the common gripes about HiSoft™ Pascal Version 4 for theTimex/Sinclair 2068 is its lack of built-in operators for handling a dimensioned array of type CHAR, henceforth referred to as a string. The crux of the problem is that if a string is not completely filled by data or spaces, the remainder of the string is left filled with whatever garbage is in memory at the time. When a WRITE or WRITELN is called, the procedure attempts to print the entire contents of the string, which includes the garbage left in the unused portion of the string, plus all the data. This results in garbage characters being printed on the screen or printer if the system does not simply crash. ¹
Parser is a program which demonstrates how a record type — called “word” in this example — containing a string and an internal counter, which is used to keep track of the number of printable characters the string contains, can be constructed and used to solve this problem. Reading and writing information to and from this “word” type; however; also requires special modified versions of the procedures READ() and WRITE(), which in this program are called StringRead() and StringWrite(). ²
Program Listing
PROGRAM PARSER;
{
Purpose:
The object of this program is to accept a sentence from the
user then to break the sentence into its component words
and to display each word on a separate line.
History:
Program by David Solly,
Ottawa, Canada,
March 25, 1990.
}
CONST
maxword = 15;
maxsentence = 15;
space = CHR(32);
first = 1;
TYPE
string = PACKED ARRAY[1..maxword] OF CHAR;
word = RECORD
body : string;
length : INTEGER
END;
VAR
sentence : ARRAY[1..maxsentence] OF word;
row, col, nextcol, count : INTEGER;
demarker : BOOLEAN;
PROCEDURE SpaceTrap;
{
Insures that there is ony 1 space between words
}
BEGIN
REPEAT
READ(sentence[row].body[first])
UNTIL sentence[row].body[first] <> space
END;
PROCEDURE StringWrite(VAR phrase : word);
{
Writes only the required length of each character string.
This is required when using 32 col. mode.
}
VAR letter : INTEGER;
BEGIN
FOR letter := first TO phrase.length DO
WRITE(phrase.body[letter])
END; {Procedure StringWrite}
PROCEDURE StringRead;
VAR I : INTEGER;
BEGIN
{
Intitialize the variables
}
count := 1;
row := first;
col := first;
nextcol := col + 1;
demarker := FALSE;
FOR I := first TO maxsentence DO
sentence[I].length := 1;
WRITE('Type a sentence > ');
READLN; {Clears the buffer of EOLN}
WHILE (NOT EOLN) AND (row < maxsentence) DO
BEGIN
READ(sentence[row].body[col]);
IF sentence[row].body[first] = space THEN
SpaceTrap;
IF sentence[row].body[col] = space THEN
demarker := TRUE;
IF (NOT demarker) AND (nextcol < maxword)
THEN
BEGIN
col := col + 1;
nextcol := nextcol + 1
END
ELSE
BEGIN
sentence[row].length := col;
count := count + 1;
row := row + 1;
col := first;
nextcol := col + 1;
demarker := FALSE
END; {If...then...else}
IF EOLN THEN sentence[row].length := col - 1
{Accounts for the last word entered less the EOLN
marker.}
END {While loop}
END; {Procedure StringRead}
PROCEDURE PrintItOut;
VAR
subsequent : INTEGER;
BEGIN
subsequent := first + 1;
WRITE('Parsing > ');
StringWrite(sentence[first]);
WRITELN;
IF count >= subsequent THEN
BEGIN
FOR row := subsequent TO count DO
BEGIN
WRITE(' ');
StringWrite(sentence[row]);
WRITELN
END
END
END; {Procedure PrintItOut}
PROCEDURE SongAndDance;
BEGIN
PAGE;
WRITELN(' Parser');
WRITELN;
WRITELN(' Program By David Solly');
WRITELN;
WRITELN('The object of this program');
WRITELN('is to accept a sentence from');
WRITELN('the user then to break the');
WRITELN('sentence down into its')
WRITELN('component words and to display');
WRITELN('each word on a separate line.');
WRITELN;
WRITELN;
END; {Procedure SongAndDance}
BEGIN {Main Program}
SongAndDance;
StringRead;
WRITELN;
PrintItOut;
WRITELN;
WRITELN('End of Demonstration.')
END. {Main Program}
David Solly [e-mail address omitted]
Ottawa, Ontario, CANADA
Voice: [phone number omitted]
¹ HiSoft has released several updates of its ZX Spectrum Pascal program since version 4 was converted and released for the Timex/Sinclair 2068. The latest ZX Spectrum version, (version 1.7M), features a 51 column mode plus the string handling capabilities we are trying to emulate here.
² For those of you who have additional questions, please feel free to write me at [e-mail address omitted]
Note: Type-in program listings on this website use ZMAKEBAS notation for graphics characters.