CRT.def 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. DEFINITION MODULE CRT;
  2. (* Symbol Table and Top-Down Graph *)
  3. IMPORT FileIO, Sets;
  4. TYPE
  5. INT32 = FileIO.INT32;
  6. CONST
  7. (* The following are chosen to ensure that data segments remain within the
  8. 64K limit imposed by Dos 16 bit systems. Manipulate them at your peril
  9. if you need to handle large grammars! *)
  10. maxSymbols = 500; (* max number of symbols
  11. (terminals+nonterminals+pragmas) *)
  12. maxTerminals = 400; (* max number of terminals *)
  13. maxNt = 210; (* max number of nonterminals *)
  14. maxNodes = 1500; (* max number of top-down graph nodes *)
  15. maxClasses = 250; (* max number of character classes *)
  16. maxSemLen = 64000; (* max length of a semantic text *)
  17. normTrans = 0; (* DFA transition during normal scanning *)
  18. contextTrans = 1; (* DFA transition during scanning of right context *)
  19. maxList = 150; (* max array size in FindCircularProductions *)
  20. maxLiterals = 127; (* max number of literal terminals *)
  21. (* node types *)
  22. unknown = 0;
  23. t = 1; (* terminal symbol *)
  24. pr = 2; (* pragma *)
  25. nt = 3; (* nonterminal symbol *)
  26. class = 4; (* character class *)
  27. char = 5; (* single character *)
  28. wt = 6; (* weak terminal symbol *)
  29. any = 7; (* symbol ANY *)
  30. eps = 8; (* empty alternative *)
  31. sync = 9; (* symbol SYNC *)
  32. sem = 10; (* semantic action *)
  33. alt = 11; (* alternative *)
  34. iter = 12; (* iteration *)
  35. opt = 13; (* option *)
  36. noSym = -1;
  37. eofSy = 0;
  38. (* token kinds *)
  39. classToken = 0; (* token class *)
  40. litToken = 1; (* literal (e.g. keyword) not recognized by DFA *)
  41. classLitToken = 2; (* token class that can also match a literal *)
  42. TYPE
  43. Name = ARRAY [0..39] OF CHAR;
  44. Position = RECORD (* position of stretch of source text *)
  45. beg: INT32; (* start relative to beginning of file *)
  46. len: CARDINAL; (* length *)
  47. col: INTEGER; (* column number of start position *)
  48. END;
  49. SymbolNode = RECORD (* node of symbol table *)
  50. typ: INTEGER; (* nt, t, pr, unknown *)
  51. name, (* symbol name *)
  52. constant: Name; (* named constant of symbol *)
  53. struct: INTEGER; (* typ = nt: index of first node of syntax graph *)
  54. (* typ = t: token kind: literal, class, ... *)
  55. deletable: BOOLEAN; (* typ = nt: TRUE, if nonterminal is deletable *)
  56. attrPos: Position; (* position of attributes in source text *)
  57. semPos: Position; (* typ = pr: pos of sem action in source text *)
  58. (* typ = nt: pos of local decls in source text *)
  59. line: INTEGER; (* source text line number of symbol in this node *)
  60. END;
  61. GraphNode = RECORD (* node of top-down graph *)
  62. typ: INTEGER; (* nt,sts,wts,char,class,any,eps,sem,sync,alt,
  63. iter,opt*)
  64. next: INTEGER; (* to successor node *)
  65. (* next < 0: to successor of enclosing structure *)
  66. p1: INTEGER; (* typ IN {nt, t, wt}: index to symbol table *)
  67. (* typ = any: index to anyset *)
  68. (* typ = sync: index to syncset *)
  69. (* typ = alt:
  70. index of first node of first alternative *)
  71. (* typ IN {iter, opt}: first node in subexpression *)
  72. (* typ = char: ordinal character value *)
  73. (* typ = class: index of character class *)
  74. p2: INTEGER; (* typ = alt:
  75. index of first node of second alternative *)
  76. (* typ IN {char, class}: transition code *)
  77. pos: Position; (* typ IN {nt, t, wt}:
  78. source pos of actual attributes *)
  79. (* typ = sem: source pos of sem action *)
  80. line: INTEGER; (* source text line number of item in this node *)
  81. END;
  82. Set = ARRAY [0 .. maxTerminals DIV Sets.size] OF BITSET;
  83. MarkList = ARRAY [0 .. maxNodes DIV Sets.size] OF BITSET;
  84. VAR
  85. maxT: INTEGER; (* terminals stored from 0 .. maxT in symbol table *)
  86. maxP: INTEGER; (* pragmas stored from maxT+1..maxP in symbol table *)
  87. firstNt: INTEGER; (* index of first nt: available after CompSymbolSets *)
  88. lastNt: INTEGER; (* index of last nt: available after CompSymbolSets *)
  89. maxC: INTEGER; (* index of last character class *)
  90. nNodes: INTEGER; (* index of last top-down graph node *)
  91. root: INTEGER; (* index of root node, filled by ATG *)
  92. semDeclPos: Position; (* position of global semantic declarations *)
  93. genScanner: BOOLEAN; (* TRUE: a scanner shall be generated *)
  94. ignoreCase: BOOLEAN; (* TRUE: scanner treats lower case as upper case *)
  95. symNames: BOOLEAN; (* TRUE: symbol names have to be assigned *)
  96. ignored: Set; (* characters ignored by the scanner *)
  97. ddt: ARRAY ["A" .. "Z"] OF BOOLEAN;
  98. (* parameter, debug and test switches *)
  99. PROCEDURE NewName (n: Name; s: ARRAY OF CHAR);
  100. (* Inserts the pair (n, s) in the token symbol name table *)
  101. PROCEDURE NewSym (t: INTEGER; n: Name; line: INTEGER): INTEGER;
  102. (* Generates a new symbol with type t and name n and returns its index *)
  103. PROCEDURE GetSym (sp: INTEGER; VAR sn: SymbolNode);
  104. (* Gets symbol node with index sp in sn. *)
  105. PROCEDURE PutSym (sp: INTEGER; sn: SymbolNode);
  106. (* Replaces symbol node with index sp by sn. *)
  107. PROCEDURE FindSym (n: Name): INTEGER;
  108. (* Gets symbol index for identifier with name n. *)
  109. PROCEDURE NewSet (s: Set): INTEGER;
  110. (* Stores s as a new set and returns its index. *)
  111. PROCEDURE CompFirstSet (gp: INTEGER; VAR first: Set);
  112. (* Computes start symbols of graph gp. *)
  113. PROCEDURE CompExpected (gp, sp: INTEGER; VAR exp: Set);
  114. (* Computes all symbols expected at location gp in graph of symbol sp. *)
  115. PROCEDURE CompDeletableSymbols;
  116. (* Marks deletable nonterminals and prints them. *)
  117. PROCEDURE CompSymbolSets;
  118. (* Collects first-sets, follow-sets, any-sets, and sync-sets. *)
  119. PROCEDURE PrintSymbolTable;
  120. (* Prints the symbol table (for tracing). *)
  121. PROCEDURE XRef;
  122. (* Produces a cross reference listing of all symbols. *)
  123. PROCEDURE NewClass (name: Name; set: Set): INTEGER;
  124. (* Defines a new character class and returns its index *)
  125. PROCEDURE ClassWithName (name: Name): INTEGER;
  126. (* Searches for a class with the given name. Returns its index or -1 *)
  127. PROCEDURE ClassWithSet (set: Set): INTEGER;
  128. (* Searches for a class with the given set. Returns its index or -1 *)
  129. PROCEDURE GetClass (n: INTEGER; VAR set: Set);
  130. (* Returns character class n *)
  131. PROCEDURE GetClassName (n: INTEGER; VAR name: Name);
  132. (* Returns the name of class n *)
  133. PROCEDURE GetSet (nr: INTEGER; VAR set: Set);
  134. (* Gives access to precomputed symbol sets *)
  135. PROCEDURE NewNode (typ, p1, line: INTEGER): INTEGER;
  136. (* Generates a new graph node with typ, p1, and source line number
  137. line and returns its index. *)
  138. PROCEDURE ClearMarkList (VAR m: MarkList);
  139. (* Clears all elements of m *)
  140. PROCEDURE GetNode (gp: INTEGER; VAR n: GraphNode);
  141. (* Gets graph node with index gp in n. *)
  142. PROCEDURE PutNode (gp: INTEGER; n: GraphNode);
  143. (* Replaces graph node with index gp by n. *)
  144. PROCEDURE ConcatAlt (VAR gL1, gR1: INTEGER; gL2, gR2: INTEGER);
  145. (* Makes (gL2, gR2) an alternative of the graph (gL1, gR1).
  146. The resulting graph is identified by (gL1, gR1). *)
  147. PROCEDURE ConcatSeq (VAR gL1, gR1: INTEGER; gL2, gR2: INTEGER);
  148. (* Concatenates graph (gL1, gR1) with graph (gL2, gR2) via next-chain.
  149. The resulting graph is identified by (gL1, gR1). *)
  150. PROCEDURE MakeFirstAlt (VAR gL, gR: INTEGER);
  151. (* Generates an alt-node with (gL, gR) as its first and only alternative *)
  152. PROCEDURE MakeIteration (VAR gL, gR: INTEGER);
  153. (* Encloses the graph (gL, gR) into an iteration construct.
  154. The resulting graph is identified by (gL, gR). *)
  155. PROCEDURE MakeOption (VAR gL, gR: INTEGER);
  156. (* Encloses the graph (gL, gR) into an option construct.
  157. The resulting graph is identified by (gL, gR). *)
  158. PROCEDURE CompleteGraph (gp: INTEGER);
  159. (* Lets right ends of graph gp be 0 *)
  160. PROCEDURE StrToGraph (s: ARRAY OF CHAR; VAR gL, gR: INTEGER);
  161. (* Generates linear graph from characters in s *)
  162. PROCEDURE DelGraph (gp: INTEGER): BOOLEAN;
  163. (* TRUE, if (sub) graph with root gp is deletable. *)
  164. PROCEDURE DelNode (gn: GraphNode): BOOLEAN;
  165. (* TRUE, if graph node gn is deletable, i.e. can be derived into the
  166. empty string. *)
  167. PROCEDURE PrintGraph;
  168. (* Prints the graph (for tracing). *)
  169. PROCEDURE FindCircularProductions (VAR ok: BOOLEAN);
  170. (* Finds and prints the circular part of the grammar.
  171. ok = TRUE means no circular part. *)
  172. PROCEDURE LL1Test (VAR ll1: BOOLEAN);
  173. (* Checks if the grammar satisfies the LL(1) conditions.
  174. ll1 = TRUE means no LL(1)-conflicts. *)
  175. PROCEDURE TestCompleteness (VAR ok: BOOLEAN);
  176. (* ok = TRUE, if all nonterminals have productions. *)
  177. PROCEDURE TestIfAllNtReached (VAR ok: BOOLEAN);
  178. (* ok = TRUE, if all nonterminals can be reached from the start symbol. *)
  179. PROCEDURE TestIfNtToTerm (VAR ok: BOOLEAN);
  180. (* ok = TRUE, if all nonterminals can be reduced to terminals. *)
  181. PROCEDURE AssignSymNames (default: BOOLEAN; VAR thereExists: BOOLEAN);
  182. PROCEDURE Restriction (n, limit: INTEGER);
  183. (* Signal compiler restriction and abort program *)
  184. END CRT.