lexlib.pas 11 KB

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