![]() |
|||
| HSG |
|
Eine (Bonsai-) Datei der folgenden Form soll in Tokens zerlegt werden.
jmp 4 inc 1 dec 2 tst 2 jmp 2 hlt # 3 # 6 ; einfaches Additionsprogramm ; 1.Summand in Register 1 ; 2.Summand in Register 2
Als Tokens identifiziert man
Schlüsselwörter, Groß-/Kleinschreibung soll keine Rolle spielen
inc 257
dec 258
jmp 259
tst 260
hlt 261
natürliche Zahlen n (Folge von Ziffern) 262
Raute r (#) ord('#')
Kommentarzeile z (; gefolgt von beliebigen Zeichen bis Zeilenende \n) 263
Whitespaces ws (' ',\n,\r,\t) 264
Die Tokens werden mit integer-Zahlen codiert. Hier soll zunächst ein reiner Scanner erzeugt werden. Daher werden die angegebenen Code-Nummern verwendet, die später im Verbund mit dem Parser automatisch erzeugt werden. 0 steht für end-of-file und 256 für Fehler.
(* vor dem ersten Trenner %% koennen 'definitions' stehen *)
%%
var result : integer;
[0-9]+ begin
val(yytext, yylval.yyInteger, result);
if result=0 then
return(262)
else
return(256)
end;
[iI][nN][cC] return(257);
[dD][eE][cC] return(258);
[jJ][mM][pP] return(259);
[tT][eE][tT] return(260);
[hH][lL][tT] return(261);
"#" ;
;.* return(263);
[ \n\r\t] return(264);
%%
(* nach dem zweiten Trenner %% koennen 'auxiliary procedures' stehen *)
Der lex-Compiler erzeugt mit
lex bonlex.l
aus der Datei 'bonlex.l' die Datei 'bonlex.pas'. Er benötigt dazu die Datei 'yylex.cod'.
Die erzeugte Datei benötigt die Bibliothek 'lexlib.pas'. Das Interface der Bibliothek wird folgendermaßen beschrieben:
(* The Lex library unit supplies a collection of variables and routines needed by the lexical analyzer routine yylex and application programs using Lex-generated lexical analyzers. It also provides access to the input/output streams used by the lexical analyzer and the text of the matched string, and provides some utility functions which may be used in actions. This `standard' version of the LexLib unit is used to implement lexical analyzers which read from and write to MS-DOS files (using standard input and output, by default). It is suitable for many standard applications for lexical analyzers, such as text conversion tools or compilers. However, you may create your own version of the LexLib unit, tailored to your target applications. In particular, you may wish to provide another set of I/O functions, e.g., if you want to read from or write to memory instead to files, or want to use different file types. *) (* Variables: The variable yytext contains the current match, yyleng its length. The variable yyline contains the current input line, and yylineno and yycolno denote the current input position (line, column). These values are often used in giving error diagnostics (however, they will only be meaningful if there is no rescanning across line ends). The variables yyinput and yyoutput are the text files which are used by the lexical analyzer. By default, they are assigned to standard input and output, but you may change these assignments to fit your target application (use the Turbo Pascal standard routines assign, reset, and rewrite for this purpose). *)