(*======================================================================*)
(*            Modula-2 Lexical Analyzer  --  Finite State Machine       *)
(*======================================================================*)
(*  Version:  1.00              Author:   Dennis Brueni                 *)
(*  Date:     07-07-91          Changes:  original                      *)
(*======================================================================*)
(*  The lexical analyzer is implemented as a Finite State Automaton     *)
(*  which recoginizes the next valid Modula-2 token.  See accompanying  *)
(*  technical documentation for specific details.                       *)
(*======================================================================*) 
(*  This module contains the definitions and initialization code for    *)
(*  the major tables used by the Finite State Machine of the Lexical    *)
(*  Analyzer.                                                           *)
(*======================================================================*)

DEFINITION MODULE FSM;

IMPORT FIO;

(*----------------------------------------------------------------------*)
(*  TYPE declarations                                                   *)
(*                                                                      *)
(*  Recall that a DFA is a five-tuple (K,Sigma,Delta,s,F) where         *)
(*                                                                      *)
(*      K = States:        is a finite set of states.                   *)
(*                                                                      *)
(*      Sigma = Alphabet   is the input alphabet.                       *)
(*                                                                      *)
(*      Delta = StateTable the transition function, is a function       *)
(*                         from (K X Sigma) --> K [ X Actions ]         *)
(*                         Our DFA has embedded actions in each trans.  *)
(*                         which are simply procedures                  *)
(*                                                                      *)
(*      s = STStart       is a start state.                             *)
(*                                                                      *)
(*      F = {STTerm}      is the set of final states.                   *)
(*----------------------------------------------------------------------*)

TYPE Alphabet =(CHNoClass,     CHSQuote,
                CHEOLN,        CHEOF,
                CHLetter,      CHDQuote,
                CHSpace,       CHAtSign,
                CHBSlash,      CHComma,
                CHLParen,      CHRParen,
                CHDollar,      CHDigit,
                CHAnd,         CHOr,
                CHNot,         CHStar                
               );

TYPE States = ( STStart,       STTerm,
                STStr,         STEQuote,
                STID,          STSkipSpaces,
                STLParen,      STCommBeg,
                STCommSkip,    STCommNest,
                STCommEnd,     STCommLine,    
                STString,      STSQuote
              );

TYPE StateTabEntry = RECORD
                        NextState:  States;
                        Action:     PROC;
                     END;

     StateTable = ARRAY [MIN(States)..MAX(States)] 
                        OF ARRAY [MIN(Alphabet)..MAX(Alphabet)] 
                                 OF StateTabEntry;

(*----------------------------------------------------------------------*)
(*  Table declarations                                                  *)
(*                                                                      *)
(*      CharClass:   Defines a mapping from the ASCII charactors to     *)
(*                   to the input alphabet of the state machine.        *)
(*                                                                      *)
(*      Transitions: Defines a mapping from (Alphabet X States) to      *)
(*                   (States X Actions), that is, all transitions of    *)
(*                   the DFA, as well as the imbedded actions.          *)
(*----------------------------------------------------------------------*)

VAR CharClass:   ARRAY [MIN(CHAR)..MAX(CHAR)] OF Alphabet;

    Transitions: StateTable;

(*----------------------------------------------------------------------*)
(*  Token types recognized by the Lexical analyzer.                     *)
(*----------------------------------------------------------------------*)

TYPE Lexicals = (M2IF,         M2ELSE,        (****************)
                 M2END,        M2DEFINE,      (* Keywords     *)
                 M2UNDEF,      M2INCLUDE,     (****************)
                 M2ELSIF,      M2THEN,
                 M2MACRO,      M2ENDM,
                 M2LINE,       M2SPACE,
                 M2STRIP,      M2NOSTRIP,
                 M2AND,        M2OR,
                 M2NOT,        M2ID,
                 M2RParen,     M2LParen,      (****************)
                 M2AtSign,     M2Comma,       (* Punctuation  *)
                 M2ch,         M2String,      (* and Others   *)
                 M2Str,        M2KillArg,     (****************)
                 M2Block,      M2EOF
                );

     LexSet = SET OF Lexicals;

(*----------------------------------------------------------------------*)
(*  An array of Strings for each Lexical                                *)
(*----------------------------------------------------------------------*)

TYPE StringsType = ARRAY [0..7] OF CHAR;

VAR  Strings:  ARRAY [MIN(Lexicals)..MAX(Lexicals)] OF StringsType;

(*----------------------------------------------------------------------*)
(*  Token record type definition                                        *)
(*----------------------------------------------------------------------*)

CONST TokenSize=255;

TYPE TokenRec = RECORD
                   String:  ARRAY [0..TokenSize] OF CHAR;
                   Length:  CARDINAL;
                   Class :  Lexicals;
                END;

VAR Token       : TokenRec;
    SourceFile  : FIO.FILE;
    DestFile    : FIO.FILE;
    IncludeLevel: CARDINAL;
    StripFlag   : BOOLEAN;

(*----------------------------------------------------------------------*)
(*  GETBSU      Gets the next Basic Syntactic Unit from the source      *)
(*              file.  This is the driver of the Finite State Machine.  *)
(*----------------------------------------------------------------------*)

PROCEDURE GetBSU;

END FSM.
