Category : Modula II Source Code
Archive   : PATTERN.ZIP
Filename : DFAPATTE.MOD

 
Output of file : DFAPATTE.MOD contained in archive : PATTERN.ZIP
IMPLEMENTATION MODULE DFAPattern;
FROM FAInput IMPORT Position;
FROM RTSMain IMPORT Status,Terminate;
FROM Terminal IMPORT WriteLn,WriteString;

IMPORT ASCII,FAInput,FileSystem,SYSTEM;

MODULE DFAHandler;
IMPORT EndMarker,lastLexeme,Position;

FROM FAInput IMPORT BackTrackPosition,DeterminePosition,NextChar,
ReturnLexeme,SetPosition;
FROM FileSystem IMPORT File,ReadByte,ReadWord,Response;

EXPORT DefineIndex,DefineState,InitializeDFA,ReadDFAFromDisk,ReadPattern;

CONST
maxStates = 1024;

TYPE
DFAStateRecord = RECORD
accept,next,state : CARDINAL;
first,last : CHAR;
END; (* DFAStateRecord *)

VAR
stateIndex : ARRAY [1..maxStates] OF CARDINAL;
stateNum : CARDINAL;
states : ARRAY [1..4096] OF DFAStateRecord;

PROCEDURE DefineIndex (state,cursor : CARDINAL);
BEGIN (* DefineIndex *)
stateIndex [state] := cursor;
END DefineIndex;

PROCEDURE DefineState (state,next,accepting : CARDINAL; first,last : CHAR);
VAR
info : DFAStateRecord;

BEGIN (* DefineState *)
info.accept := accepting;
info.state := state;
info.next := next;

info.first := first;
info.last := last;

states [stateNum] := info;

INC (stateNum);
END DefineState;

PROCEDURE InitializeDFA;
PROCEDURE ClearStateIndex;
VAR
index : CARDINAL;

BEGIN (* ClearStateIndex *)
FOR index := 1 TO maxStates DO
stateIndex [index] := 0;
END; (* FOR *)
END ClearStateIndex;

BEGIN (* InitializeDFA *)
ClearStateIndex;

stateNum := 1;
END InitializeDFA;

PROCEDURE ReadDFAFromDisk (VAR f : File);
VAR
newAccept,newNext,newState : CARDINAL;
oldAccept,oldNext,oldState : CARDINAL;
oldFirst,oldLast : CHAR;
newChar : CHAR;

BEGIN (* ReadDFAFromDisk *)
InitializeDFA;

oldState := MAX (CARDINAL);

LOOP
ReadWord (f,newState);

IF ((f.res <> done) OR (newState = EndMarker)) THEN
EXIT;
ELSE
ReadWord (f,newNext);
ReadWord (f,newAccept);
ReadByte (f,newChar);

IF ((newState <> oldState) OR (newNext <> oldNext) OR
(newAccept <> oldAccept) OR (ORD (newChar) <> ORD (oldLast) + 1)) THEN
IF oldState <> MAX (CARDINAL) THEN
DefineState (oldState,oldNext,oldAccept,oldFirst,oldLast);
END; (* IF *)

IF newState <> oldState THEN
DefineIndex (newState,stateNum);
END; (* IF *)

oldAccept := newAccept;
oldState := newState;
oldNext := newNext;

oldFirst := newChar;
oldLast := newChar;
ELSE
oldLast := newChar;
END; (* IF *)
END; (* IF *)
END; (* LOOP *)

DefineState (oldState,oldNext,oldAccept,oldFirst,oldLast);
END ReadDFAFromDisk;

PROCEDURE ReadPattern (VAR num : CARDINAL);
VAR
index,lastAccept,next,state : CARDINAL;
endPos,startPos : Position;

PROCEDURE ComputeMove (state : CARDINAL; VAR next : CARDINAL; ch : CHAR);
VAR
index : CARDINAL;

BEGIN (* ComputeMove *)
index := stateIndex [state];
next := 0;

WHILE ((states [index].last < ch) AND (states [index].state = state)) DO
INC (index);
END; (* WHILE *)

IF ((states [index].state = state) AND (ch >= states [index].first) AND (ch <= states [index].last)) THEN
next := states [index].next;
END; (* IF *)
END ComputeMove;

BEGIN (* ReadPattern *)
DeterminePosition (startPos);

lastAccept := MAX (CARDINAL);
next := 1;

REPEAT
index := stateIndex [next];
state := next;

IF states [index].accept <> MAX (CARDINAL) THEN
lastAccept := states [index].accept;
DeterminePosition (endPos);
END; (* IF *)

ComputeMove (state,next,NextChar ());
UNTIL next = 0;

IF ((states [index].accept = MAX (CARDINAL)) AND (lastAccept = MAX (CARDINAL))) THEN
DeterminePosition (endPos);
END; (* IF *)

SetPosition (endPos);

ReturnLexeme (startPos,endPos,lastLexeme);

index := stateIndex [state];

IF states [index].accept <> MAX (CARDINAL) THEN
num := states [index].accept;
ELSE
num := lastAccept;
END; (* IF *)
END ReadPattern;

BEGIN (* DFAHandler *)
InitializeDFA;
END DFAHandler;
END DFAPattern.


  3 Responses to “Category : Modula II Source Code
Archive   : PATTERN.ZIP
Filename : DFAPATTE.MOD

  1. Very nice! Thank you for this wonderful archive. I wonder why I found it only now. Long live the BBS file archives!

  2. This is so awesome! 😀 I’d be cool if you could download an entire archive of this at once, though.

  3. But one thing that puzzles me is the “mtswslnkmcjklsdlsbdmMICROSOFT” string. There is an article about it here. It is definitely worth a read: http://www.os2museum.com/wp/mtswslnk/