lexdfa.pas 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. if not start_excl^[i div 2] then
  39. setunion(first_pos_table^[i]^, first_pos_table^[i mod 2]^);
  40. for i := 0 to 2*n_start_states+1 do
  41. act_state := newState(first_pos_table^[i]);
  42. act_state := -1;
  43. while succ(act_state)<n_states do
  44. begin
  45. inc(act_state);
  46. (* add transitions for active state: *)
  47. startStateTrans;
  48. for i := 1 to size(state_table^[act_state].state_pos^) do
  49. with pos_table^[state_table^[act_state].state_pos^[i]] do
  50. if pos_type=char_pos then
  51. addTrans([c], follow_pos)
  52. else if pos_type=cclass_pos then
  53. addTrans(cc^, follow_pos)
  54. else if pos=0 then
  55. state_table^[act_state].final := true;
  56. (* assign next states: *)
  57. for i := state_table^[act_state].trans_lo to n_trans do
  58. with trans_table^[i] do
  59. next_state := addState(follow_pos);
  60. (* merge transitions for the same next state: *)
  61. mergeTrans;
  62. (* sort transitions: *)
  63. sortTrans;
  64. endStateTrans;
  65. end;
  66. end(*makeDFATable*);
  67. end(*LexDFA*).