lexdfa.pas 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. {
  2. DFA table construction. This code, admittedly, is not the most aesthetic,
  3. but it's quite efficient (and that's the main goal). For further
  4. explanation, refer to Aho/Sethi/Ullman 1986, Section 3.9.
  5. Copyright (c) 1990-92 Albert Graef <[email protected]>
  6. Copyright (C) 1996 Berend de Boer <[email protected]>
  7. This program is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2 of the License, or
  10. (at your option) any later version.
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU General Public License for more details.
  15. You should have received a copy of the GNU General Public License
  16. along with this program; if not, write to the Free Software
  17. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18. $Revision: 1.2 $
  19. $Modtime: 96-08-01 6:13 $
  20. $History: LEXDFA.PAS $
  21. *
  22. * ***************** Version 2 *****************
  23. * User: Berend Date: 96-10-10 Time: 21:16
  24. * Updated in $/Lex and Yacc/tply
  25. * Updated for protected mode, windows and Delphi 1.X and 2.X.
  26. }
  27. unit LexDFA;
  28. interface
  29. procedure makeDFATable;
  30. (* construct DFA from position table *)
  31. implementation
  32. uses LexBase, LexTable;
  33. procedure makeDFATable;
  34. var i : Integer;
  35. begin
  36. (* initialize start states: *)
  37. for i := 2 to 2*n_start_states+1 do
  38. setunion(first_pos_table^[i]^, first_pos_table^[i mod 2]^);
  39. for i := 0 to 2*n_start_states+1 do
  40. act_state := newState(first_pos_table^[i]);
  41. act_state := -1;
  42. while succ(act_state)<n_states do
  43. begin
  44. inc(act_state);
  45. (* add transitions for active state: *)
  46. startStateTrans;
  47. for i := 1 to size(state_table^[act_state].state_pos^) do
  48. with pos_table^[state_table^[act_state].state_pos^[i]] do
  49. if pos_type=char_pos then
  50. addTrans([c], follow_pos)
  51. else if pos_type=cclass_pos then
  52. addTrans(cc^, follow_pos)
  53. else if pos=0 then
  54. state_table^[act_state].final := true;
  55. (* assign next states: *)
  56. for i := state_table^[act_state].trans_lo to n_trans do
  57. with trans_table^[i] do
  58. next_state := addState(follow_pos);
  59. (* merge transitions for the same next state: *)
  60. mergeTrans;
  61. (* sort transitions: *)
  62. sortTrans;
  63. endStateTrans;
  64. end;
  65. end(*makeDFATable*);
  66. end(*LexDFA*).