home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 549b.lha / M2P_v1.0_sources / source.lzh / FSM.def < prev    next >
Text File  |  1991-08-10  |  7KB  |  139 lines

  1. (*======================================================================*)
  2. (*            Modula-2 Lexical Analyzer  --  Finite State Machine       *)
  3. (*======================================================================*)
  4. (*  Version:  1.00              Author:   Dennis Brueni                 *)
  5. (*  Date:     07-07-91          Changes:  original                      *)
  6. (*======================================================================*)
  7. (*  The lexical analyzer is implemented as a Finite State Automaton     *)
  8. (*  which recoginizes the next valid Modula-2 token.  See accompanying  *)
  9. (*  technical documentation for specific details.                       *)
  10. (*======================================================================*) 
  11. (*  This module contains the definitions and initialization code for    *)
  12. (*  the major tables used by the Finite State Machine of the Lexical    *)
  13. (*  Analyzer.                                                           *)
  14. (*======================================================================*)
  15.  
  16. DEFINITION MODULE FSM;
  17.  
  18. IMPORT FIO;
  19.  
  20. (*----------------------------------------------------------------------*)
  21. (*  TYPE declarations                                                   *)
  22. (*                                                                      *)
  23. (*  Recall that a DFA is a five-tuple (K,Sigma,Delta,s,F) where         *)
  24. (*                                                                      *)
  25. (*      K = States:        is a finite set of states.                   *)
  26. (*                                                                      *)
  27. (*      Sigma = Alphabet   is the input alphabet.                       *)
  28. (*                                                                      *)
  29. (*      Delta = StateTable the transition function, is a function       *)
  30. (*                         from (K X Sigma) --> K [ X Actions ]         *)
  31. (*                         Our DFA has embedded actions in each trans.  *)
  32. (*                         which are simply procedures                  *)
  33. (*                                                                      *)
  34. (*      s = STStart       is a start state.                             *)
  35. (*                                                                      *)
  36. (*      F = {STTerm}      is the set of final states.                   *)
  37. (*----------------------------------------------------------------------*)
  38.  
  39. TYPE Alphabet =(CHNoClass,     CHSQuote,
  40.                 CHEOLN,        CHEOF,
  41.                 CHLetter,      CHDQuote,
  42.                 CHSpace,       CHAtSign,
  43.                 CHBSlash,      CHComma,
  44.                 CHLParen,      CHRParen,
  45.                 CHDollar,      CHDigit,
  46.                 CHAnd,         CHOr,
  47.                 CHNot,         CHStar                
  48.                );
  49.  
  50. TYPE States = ( STStart,       STTerm,
  51.                 STStr,         STEQuote,
  52.                 STID,          STSkipSpaces,
  53.                 STLParen,      STCommBeg,
  54.                 STCommSkip,    STCommNest,
  55.                 STCommEnd,     STCommLine,    
  56.                 STString,      STSQuote
  57.               );
  58.  
  59. TYPE StateTabEntry = RECORD
  60.                         NextState:  States;
  61.                         Action:     PROC;
  62.                      END;
  63.  
  64.      StateTable = ARRAY [MIN(States)..MAX(States)] 
  65.                         OF ARRAY [MIN(Alphabet)..MAX(Alphabet)] 
  66.                                  OF StateTabEntry;
  67.  
  68. (*----------------------------------------------------------------------*)
  69. (*  Table declarations                                                  *)
  70. (*                                                                      *)
  71. (*      CharClass:   Defines a mapping from the ASCII charactors to     *)
  72. (*                   to the input alphabet of the state machine.        *)
  73. (*                                                                      *)
  74. (*      Transitions: Defines a mapping from (Alphabet X States) to      *)
  75. (*                   (States X Actions), that is, all transitions of    *)
  76. (*                   the DFA, as well as the imbedded actions.          *)
  77. (*----------------------------------------------------------------------*)
  78.  
  79. VAR CharClass:   ARRAY [MIN(CHAR)..MAX(CHAR)] OF Alphabet;
  80.  
  81.     Transitions: StateTable;
  82.  
  83. (*----------------------------------------------------------------------*)
  84. (*  Token types recognized by the Lexical analyzer.                     *)
  85. (*----------------------------------------------------------------------*)
  86.  
  87. TYPE Lexicals = (M2IF,         M2ELSE,        (****************)
  88.                  M2END,        M2DEFINE,      (* Keywords     *)
  89.                  M2UNDEF,      M2INCLUDE,     (****************)
  90.                  M2ELSIF,      M2THEN,
  91.                  M2MACRO,      M2ENDM,
  92.                  M2LINE,       M2SPACE,
  93.                  M2STRIP,      M2NOSTRIP,
  94.                  M2AND,        M2OR,
  95.                  M2NOT,        M2ID,
  96.                  M2RParen,     M2LParen,      (****************)
  97.                  M2AtSign,     M2Comma,       (* Punctuation  *)
  98.                  M2ch,         M2String,      (* and Others   *)
  99.                  M2Str,        M2KillArg,     (****************)
  100.                  M2Block,      M2EOF
  101.                 );
  102.  
  103.      LexSet = SET OF Lexicals;
  104.  
  105. (*----------------------------------------------------------------------*)
  106. (*  An array of Strings for each Lexical                                *)
  107. (*----------------------------------------------------------------------*)
  108.  
  109. TYPE StringsType = ARRAY [0..7] OF CHAR;
  110.  
  111. VAR  Strings:  ARRAY [MIN(Lexicals)..MAX(Lexicals)] OF StringsType;
  112.  
  113. (*----------------------------------------------------------------------*)
  114. (*  Token record type definition                                        *)
  115. (*----------------------------------------------------------------------*)
  116.  
  117. CONST TokenSize=255;
  118.  
  119. TYPE TokenRec = RECORD
  120.                    String:  ARRAY [0..TokenSize] OF CHAR;
  121.                    Length:  CARDINAL;
  122.                    Class :  Lexicals;
  123.                 END;
  124.  
  125. VAR Token       : TokenRec;
  126.     SourceFile  : FIO.FILE;
  127.     DestFile    : FIO.FILE;
  128.     IncludeLevel: CARDINAL;
  129.     StripFlag   : BOOLEAN;
  130.  
  131. (*----------------------------------------------------------------------*)
  132. (*  GETBSU      Gets the next Basic Syntactic Unit from the source      *)
  133. (*              file.  This is the driver of the Finite State Machine.  *)
  134. (*----------------------------------------------------------------------*)
  135.  
  136. PROCEDURE GetBSU;
  137.  
  138. END FSM.
  139.