home *** CD-ROM | disk | FTP | other *** search
-
- { TABLE.L: Lex demonstration program for the use of character tables;
- accepts identifiers, signed integers, whitespace and newline as input
- and prints out the names of the matched tokens; unrecognized characters
- are converted to and echoed as '?' (ASCII 63). }
-
- uses LexLib;
-
- { redefined input functions that remap characters: }
- function input : char; forward;
- procedure unput(c : char); forward;
-
- %T
- 1 Aa
- 2 Bb
- 3 Cc
- { ... }
- 26 Zz
- 27 \ \t
- 28 +
- 29 -
- 30 0
- 31 1
- { ... }
- 39 9
- 40 \n
- %T
-
- letter [A-Z]
- digit [0-9]
-
- %%
-
- {letter}({letter}|{digit})* write('<identifier> ');
- [+-]?{digit}+ write('<number> ');
- " "+ write('<space> ');
- \n writeln('<newline>');
- . write(yytext[1]);
-
- %%
-
- (* implementation of input functions and main program: *)
-
- var buf : string; (* lookahead buffer *)
- function input : char;
- var c : char;
- begin
- if length(buf)>0 then
- (* get char from lookahead buffer: *)
- begin
- input := buf[1];
- delete(buf, 1, 1)
- end
- else
- begin
- (* get character from input file (converting line end <CR><LF>
- to newline, and handling end of file): *)
- if eof(yyin) then
- c := #0
- else if eoln(yyin) then
- begin
- c := #10; (* newline *)
- readln(yyin)
- end
- else
- read(yyin, c);
- (* remap characters: *)
- case c of
- #0: input := #0;
- 'a'..'z': input := chr(succ(ord(c)-ord('a')));
- 'A'..'Z': input := chr(succ(ord(c)-ord('A')));
- ' ', #9: input := #27;
- '+': input := #28;
- '-': input := #29;
- '0'..'9': input := chr(ord(c)-ord('0')+30);
- #10: input := #40;
- else input := '?'; (* denotes illegal character *)
- end;
- end
- end(*input*);
- procedure unput(c : char);
- begin
- buf := c+buf
- end(*unput*);
-
- begin
- buf := '';
- if yylex=0 then (* done *)
- end.