Little Languages

See all articles from CATS v8 n9
Author(s): Tim Swenson
Publication: CATS Newsletter
Pub Details: CATS v8 n9 (Volume 8, Issue 9)
Date: February 1991
Pages: 6-8

Lexical Analyser:

The first part of any translator or compiler, is the lexical analyser. A lexical analyser takes the “words” of a program and breaks them up into parts that it understands. It seperates the operators ( +, =, *, etc), the reserved words (FORWARD, RIGHT, LEFT, etc), and variables. It does not check to make sure the program makes any sense. All operators and reserved words are broken down to “tokens”. The word FORWARD is translated to just F. It is easier for the program to handle F than FORWARD. All words that are not operators or reserved words are considered variables and are stored in thier full length.

Below is the procedure LEX. It takes in a file name of a Little LOGO program, opens the file, runs it through the lexical analyser, and sends output to a file. If you look at this file, you will see a token or variable on each line. This file will be used in the next step of the program, the syntactical analyser.

By definition, Little LOGO uses spaces between it’s words. This makes it easy for the lexical analyser to see each word. The procedure finds each word and runs it through the evaluator. The evaluator decides which operator or reserved word the word is. Note that most of the procedure evalutor is nothing but nested IF statements. The SELECT statement for SuperBasic is not useable for strings, only numeric variables, so I was forced to use an un-elegant nested IF construct.

DEFine PROCedure lex
LOCal onn, off, length, in$, temp$, drive$, in_file$, out_file$, beg, ennd
off = 0
onn = 1

CLS
INPUT "Enter Drive to Use (flp1_, ram1_, etc) :";drive$
INPUT "Enter Name of Little LOGO File: ";in_file$

in_file$ = drive$&in_file$
out_file$ = drive$&"lex_out"

OPEN_IN #4,in_file$
DELETE out_file$
OPEN_NEW #5,out_file$

REPEAT loop

beg = 1 : ennd = 1
token = off

INPUT #4, in$
IF EOF(#4) THEN EXIT loop
length = LEN(in$)
IF in$="" THEN REPeat loop

REPEAT loop2
IF in$(ennd)<>" " THEN token = onn
IF in$(ennd)=" " AND token = off THEN beg = ennd+1
IF (in$(ennd)=" " OR ennd=length) AND token = onn THEN
IF ennd=length THEN
temp$ = in$(beg to ennd)
ELSE
temp$ = in$(beg TO ennd-1)
ENDIF
upper(temp$)
eval(temp$)
beg = ennd + 1
token = off
ENDIF

ennd = ennd + 1
IF ennd > length THEN EXIT loop2
END REPEAT loop2
END REPEAT loop

CLOSE #4
CLOSE #5

END DEFine lex

DEFine PROCedure upper (a$)
LOCal x, temp

FOR x = 1 TO LEN(a$)
temp = CODE(a$(x))
IF (temp > 96) and (temp < 123) THEN a$(x) = CHR$(temp-32)
END FOR x

END DEFine upper

DEFine PROCedure eval (a$)

IF a$="FORWARD" OR a$="FD" THEN
PRINT #5,"F"
ELSE
IF a$="RIGHT" OR a$="RT" THEN
PRINT #5,"R"
ELSE
IF a$="LEFT" OR a$="LF" THEN
PRINT #5,"L"
ELSE
IF a$="BACK" OR a$="BK" THEN
PRINT #5,"B"
ELSE
IF a$="CLEARSCREEN" OR a$="CS" THEN
PRINT #5,"C"
ELSE
IF a$="TO" THEN
PRINT #5,"T"
ELSE
IF a$="END" THEN
PRINT #5,"E"
ELSE
IF a$="PRINT" THEN
PRINT #5,"P"
ELSE
IF a$="REPEAT" THEN
PRINT #5,"R"
ELSE
IF a$="SETCRUNCH" THEN
PRINT #5,"SC"
ELSE
IF a$="PENUP" OR a$="PU" THEN
PRINT #5,"U"
ELSE
IF a$="PENDOWN" OR a$="PD" THEN
PRINT #5,"D"
ELSE
IF a$="SETBC" THEN
PRINT #5,"SB"
ELSE
IF a$="SETPC" THEN
PRINT #5,"SP"
ELSE
IF a$="WAIT" THEN
PRINT #5,"W"
ELSE
PRINT #5,a$
ENDIF
ENDIF
ENDIF
ENDIF
ENDIF
ENDIF
ENDIF
ENDIF
ENDIF
ENDIF
ENDIF
ENDIF
ENDIF
ENDIF
ENDIF

END DEFine eval

Note: Type-in program listings on this website use ZMAKEBAS notation for graphics characters.