lexlib.pas 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. {$I-}
  2. unit LexLib;
  3. (* Standard Lex library unit for TP Lex Version 3.0.
  4. 2-11-91 AG *)
  5. interface
  6. (* The Lex library unit supplies a collection of variables and routines
  7. needed by the lexical analyzer routine yylex and application programs
  8. using Lex-generated lexical analyzers. It also provides access to the
  9. input/output streams used by the lexical analyzer and the text of the
  10. matched string, and provides some utility functions which may be used
  11. in actions.
  12. This `standard' version of the LexLib unit is used to implement lexical
  13. analyzers which read from and write to MS-DOS files (using standard input
  14. and output, by default). It is suitable for many standard applications
  15. for lexical analyzers, such as text conversion tools or compilers.
  16. However, you may create your own version of the LexLib unit, tailored to
  17. your target applications. In particular, you may wish to provide another
  18. set of I/O functions, e.g., if you want to read from or write to memory
  19. instead to files, or want to use different file types. *)
  20. (* Variables:
  21. The variable yytext contains the current match, yyleng its length.
  22. The variable yyline contains the current input line, and yylineno and
  23. yycolno denote the current input position (line, column). These values
  24. are often used in giving error diagnostics (however, they will only be
  25. meaningful if there is no rescanning across line ends).
  26. The variables yyinput and yyoutput are the text files which are used
  27. by the lexical analyzer. By default, they are assigned to standard
  28. input and output, but you may change these assignments to fit your
  29. target application (use the Turbo Pascal standard routines assign,
  30. reset, and rewrite for this purpose). *)
  31. var
  32. yyinput, yyoutput : Text; (* input and output file *)
  33. yyline : String; (* current input line *)
  34. yylineno, yycolno : Integer; (* current input position *)
  35. yytext : String; (* matched text (should be considered r/o) *)
  36. yyleng : Byte (* length of matched text *)
  37. absolute yytext;
  38. (* I/O routines:
  39. The following routines get_char, unget_char and put_char are used to
  40. implement access to the input and output files. Since \n (newline) for
  41. Lex means line end, the I/O routines have to translate MS-DOS line ends
  42. (carriage-return/line-feed) into newline characters and vice versa. Input
  43. is buffered to allow rescanning text (via unput_char).
  44. The input buffer holds the text of the line to be scanned. When the input
  45. buffer empties, a new line is obtained from the input stream. Characters
  46. can be returned to the input buffer by calls to unget_char. At end-of-
  47. file a null character is returned.
  48. The input routines also keep track of the input position and set the
  49. yyline, yylineno, yycolno variables accordingly.
  50. Since the rest of the Lex library only depends on these three routines
  51. (there are no direct references to the yyinput and yyoutput files or
  52. to the input buffer), you can easily replace get_char, unget_char and
  53. put_char by another suitable set of routines, e.g. if you want to read
  54. from/write to memory, etc. *)
  55. function get_char : Char;
  56. (* obtain one character from the input file (null character at end-of-
  57. file) *)
  58. procedure unget_char ( c : Char );
  59. (* return one character to the input file to be reread in subsequent calls
  60. to get_char *)
  61. procedure put_char ( c : Char );
  62. (* write one character to the output file *)
  63. (* Utility routines: *)
  64. procedure echo;
  65. (* echoes the current match to the output stream *)
  66. procedure yymore;
  67. (* append the next match to the current one *)
  68. procedure yyless ( n : Integer );
  69. (* truncate yytext to size n and return the remaining characters to the
  70. input stream *)
  71. procedure reject;
  72. (* reject the current match and execute the next one *)
  73. (* reject does not actually cause the input to be rescanned; instead,
  74. internal state information is used to find the next match. Hence
  75. you should not try to modify the input stream or the yytext variable
  76. when rejecting a match. *)
  77. procedure return ( n : Integer );
  78. procedure returnc ( c : Char );
  79. (* sets the return value of yylex *)
  80. procedure start ( state : Integer );
  81. (* puts the lexical analyzer in the given start state; state=0 denotes
  82. the default start state, other values are user-defined *)
  83. (* yywrap:
  84. The yywrap function is called by yylex at end-of-file (unless you have
  85. specified a rule matching end-of-file). You may redefine this routine
  86. in your Lex program to do application-dependent processing at end of
  87. file. In particular, yywrap may arrange for more input and return false
  88. in which case the yylex routine resumes lexical analysis. *)
  89. function yywrap : Boolean;
  90. (* The default yywrap routine supplied here closes input and output files
  91. and returns true (causing yylex to terminate). *)
  92. (* The following are the internal data structures and routines used by the
  93. lexical analyzer routine yylex; they should not be used directly. *)
  94. var
  95. yystate : Integer; (* current state of lexical analyzer *)
  96. yyactchar : Char; (* current character *)
  97. yylastchar : Char; (* last matched character (#0 if none) *)
  98. yyrule : Integer; (* matched rule *)
  99. yyreject : Boolean; (* current match rejected? *)
  100. yydone : Boolean; (* yylex return value set? *)
  101. yyretval : Integer; (* yylex return value *)
  102. procedure yynew;
  103. (* starts next match; initializes state information of the lexical
  104. analyzer *)
  105. procedure yyscan;
  106. (* gets next character from the input stream and updates yytext and
  107. yyactchar accordingly *)
  108. procedure yymark ( n : Integer );
  109. (* marks position for rule no. n *)
  110. procedure yymatch ( n : Integer );
  111. (* declares a match for rule number n *)
  112. function yyfind ( var n : Integer ) : Boolean;
  113. (* finds the last match and the corresponding marked position and adjusts
  114. the matched string accordingly; returns:
  115. - true if a rule has been matched, false otherwise
  116. - n: the number of the matched rule *)
  117. function yydefault : Boolean;
  118. (* executes the default action (copy character); returns true unless
  119. at end-of-file *)
  120. procedure yyclear;
  121. (* reinitializes state information after lexical analysis has been
  122. finished *)
  123. implementation
  124. procedure fatal ( msg : String );
  125. (* writes a fatal error message and halts program *)
  126. begin
  127. writeln('LexLib: ', msg);
  128. halt(1);
  129. end(*fatal*);
  130. (* I/O routines: *)
  131. const nl = #10; (* newline character *)
  132. const max_chars = 2048;
  133. var
  134. bufptr : Integer;
  135. buf : array [1..max_chars] of Char;
  136. function get_char : Char;
  137. var i : Integer;
  138. begin
  139. if (bufptr=0) and not eof(yyinput) then
  140. begin
  141. readln(yyinput, yyline);
  142. inc(yylineno); yycolno := 1;
  143. buf[1] := nl;
  144. for i := 1 to length(yyline) do
  145. buf[i+1] := yyline[length(yyline)-i+1];
  146. inc(bufptr, length(yyline)+1);
  147. end;
  148. if bufptr>0 then
  149. begin
  150. get_char := buf[bufptr];
  151. dec(bufptr);
  152. inc(yycolno);
  153. end
  154. else
  155. get_char := #0;
  156. end(*get_char*);
  157. procedure unget_char ( c : Char );
  158. begin
  159. if bufptr=max_chars then fatal('input buffer overflow');
  160. inc(bufptr);
  161. dec(yycolno);
  162. buf[bufptr] := c;
  163. end(*unget_char*);
  164. procedure put_char ( c : Char );
  165. begin
  166. if c=#0 then
  167. { ignore }
  168. else if c=nl then
  169. writeln(yyoutput)
  170. else
  171. write(yyoutput, c)
  172. end(*put_char*);
  173. (* Variables:
  174. Some state information is maintained to keep track with calls to yymore,
  175. yyless, reject, start and yymatch/yymark, and to initialize state
  176. information used by the lexical analyzer.
  177. - yystext: contains the initial contents of the yytext variable; this
  178. will be the empty string, unless yymore is called which sets yystext
  179. to the current yytext
  180. - yysstate: start state of lexical analyzer (set to 0 during
  181. initialization, and modified in calls to the start routine)
  182. - yylstate: line state information (1 if at beginning of line, 0
  183. otherwise)
  184. - yystack: stack containing matched rules; yymatches contains the number of
  185. matches
  186. - yypos: for each rule the last marked position (yymark); zeroed when rule
  187. has already been considered
  188. - yysleng: copy of the original yyleng used to restore state information
  189. when reject is used *)
  190. const
  191. max_matches = 1024;
  192. max_rules = 256;
  193. var
  194. yystext : String;
  195. yysstate, yylstate : Integer;
  196. yymatches : Integer;
  197. yystack : array [1..max_matches] of Integer;
  198. yypos : array [1..max_rules] of Integer;
  199. yysleng : Byte;
  200. (* Utilities: *)
  201. procedure echo;
  202. var i : Integer;
  203. begin
  204. for i := 1 to yyleng do
  205. put_char(yytext[i])
  206. end(*echo*);
  207. procedure yymore;
  208. begin
  209. yystext := yytext;
  210. end(*yymore*);
  211. procedure yyless ( n : Integer );
  212. var i : Integer;
  213. begin
  214. for i := yyleng downto n+1 do
  215. unget_char(yytext[i]);
  216. yyleng := n;
  217. end(*yyless*);
  218. procedure reject;
  219. var i : Integer;
  220. begin
  221. yyreject := true;
  222. for i := yyleng+1 to yysleng do
  223. yytext := yytext+get_char;
  224. dec(yymatches);
  225. end(*reject*);
  226. procedure return ( n : Integer );
  227. begin
  228. yyretval := n;
  229. yydone := true;
  230. end(*return*);
  231. procedure returnc ( c : Char );
  232. begin
  233. yyretval := ord(c);
  234. yydone := true;
  235. end(*returnc*);
  236. procedure start ( state : Integer );
  237. begin
  238. yysstate := state;
  239. end(*start*);
  240. (* yywrap: *)
  241. function yywrap : Boolean;
  242. begin
  243. close(yyinput); close(yyoutput);
  244. yywrap := true;
  245. end(*yywrap*);
  246. (* Internal routines: *)
  247. procedure yynew;
  248. begin
  249. if yylastchar<>#0 then
  250. if yylastchar=nl then
  251. yylstate := 1
  252. else
  253. yylstate := 0;
  254. yystate := yysstate+yylstate;
  255. yytext := yystext;
  256. yystext := '';
  257. yymatches := 0;
  258. yydone := false;
  259. end(*yynew*);
  260. procedure yyscan;
  261. begin
  262. if yyleng=255 then fatal('yytext overflow');
  263. yyactchar := get_char;
  264. inc(yyleng);
  265. yytext[yyleng] := yyactchar;
  266. end(*yyscan*);
  267. procedure yymark ( n : Integer );
  268. begin
  269. if n>max_rules then fatal('too many rules');
  270. yypos[n] := yyleng;
  271. end(*yymark*);
  272. procedure yymatch ( n : Integer );
  273. begin
  274. inc(yymatches);
  275. if yymatches>max_matches then fatal('match stack overflow');
  276. yystack[yymatches] := n;
  277. end(*yymatch*);
  278. function yyfind ( var n : Integer ) : Boolean;
  279. begin
  280. yyreject := false;
  281. while (yymatches>0) and (yypos[yystack[yymatches]]=0) do
  282. dec(yymatches);
  283. if yymatches>0 then
  284. begin
  285. yysleng := yyleng;
  286. n := yystack[yymatches];
  287. yyless(yypos[n]);
  288. yypos[n] := 0;
  289. if yyleng>0 then
  290. yylastchar := yytext[yyleng]
  291. else
  292. yylastchar := #0;
  293. yyfind := true;
  294. end
  295. else
  296. begin
  297. yyless(0);
  298. yylastchar := #0;
  299. yyfind := false;
  300. end
  301. end(*yyfind*);
  302. function yydefault : Boolean;
  303. begin
  304. yyreject := false;
  305. yyactchar := get_char;
  306. if yyactchar<>#0 then
  307. begin
  308. put_char(yyactchar);
  309. yydefault := true;
  310. end
  311. else
  312. begin
  313. yylstate := 1;
  314. yydefault := false;
  315. end;
  316. yylastchar := yyactchar;
  317. end(*yydefault*);
  318. procedure yyclear;
  319. begin
  320. bufptr := 0;
  321. yysstate := 0;
  322. yylstate := 1;
  323. yylastchar := #0;
  324. yytext := '';
  325. yystext := '';
  326. end(*yyclear*);
  327. begin
  328. assign(yyinput, '');
  329. assign(yyoutput, '');
  330. reset(yyinput); rewrite(yyoutput);
  331. yylineno := 0;
  332. yyclear;
  333. end(*LexLib*).