CR.frm 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. (* CR Main Module of Coco/R
  2. == =====================
  3. This is a compiler generator that produces a scanner and a parser
  4. from an attributed grammar, and optionally a complete small compiler.
  5. Original code in Oberon by Hanspeter Moessenboeck, ETH Zurich
  6. Ported at ETH to Apple Modula, and thence to JPI-2 Modula.
  7. JPI version of 27 January 1991 was then modified to make more
  8. portable by Pat Terry, January - October 1992
  9. This is the WinTel version
  10. Usage:
  11. COCOR [-options] GrammarName[.atg] [$options]
  12. Input:
  13. attributed grammar input grammar
  14. scanner.frm frame file
  15. parser.frm frame file
  16. compiler.frm frame file (optional)
  17. (Frame files must be in the sme directory as the grammar, or may be
  18. found on a path specified by DOS environment variable CRFRAMES).
  19. Output:
  20. <GrammarName>S.def + mod generated scanner
  21. <GrammarName>P.def + mod generated parser
  22. <GrammarName>.err error numbers and corresponding error messages
  23. <GrammarName>.lst source listing with error messages and trace output
  24. Optionally
  25. <GrammarName>G.def + mod generated symbolic names
  26. <GrammarName>.mod generated compiler main module
  27. Implementation restrictions
  28. 1 too many nodes in graph (>1500) CRT.NewNode
  29. 2 too many symbols (>500) CRT.NewSym, MovePragmas
  30. 3 too many sets (>256 ANY-syms or SYNC syms) CRT.NewSet,
  31. 4 too many character classes (>250) CRT.NewClass
  32. 5 too many conditions in generated code (>100) CRX.NewCondSet
  33. 6 too many token names in "NAMES" (>100) CRT.NewName
  34. 7 too many states in automata (>500) CRA.NewState
  35. Trace output
  36. (To activate a trace switch, write "${letter}" in the input grammar, or
  37. invoke Coco with a second command line parameter)
  38. A Prints states of automaton
  39. C Generates complete compiler module
  40. D Suppresses Def Mod generation
  41. F Prints start symbols and followers of nonterminals.
  42. G Prints the top-down graph.
  43. I Trace of start symbol set computation.
  44. L Forces a listing (otherwise a listing is only printed if errors are found).
  45. M Suppresses FORWARD declarations in parser (for multipass compilers).
  46. N Uses default names for symbol value constants. This generates an
  47. extra module <grammar name>G, and corresponding import statements
  48. using constant names instead of numbers for symbols in parser and
  49. scanner.
  50. The constants are used unqualified and hence all needed constants
  51. have to be imported; so a complete import list for these constants
  52. is generated.
  53. There is no decision whether a constant is actually needed.
  54. The default conventions are (only terminals or pragmas can have names):
  55. single character --> <ASCII name (lowercase)>Sym
  56. eg. "+" --> plusSym
  57. character string --> <string>Sym
  58. eg. "PROGRAM" --> PROGRAMSym
  59. scanner token --> <token name>Sym
  60. eg. ident --> identSym
  61. O Trace of follow set computation (not yet implemented).
  62. P Generates parser only
  63. S Prints the symbol list.
  64. T Suppresses generation of def and mod files (grammar tests only).
  65. X Prints a cross reference list.
  66. ==========================================================================*)
  67. MODULE -->Grammar;
  68. FROM -->Scanner IMPORT lst, src, errors, directory, Error, CharAt;
  69. FROM -->Parser IMPORT Parse;
  70. IMPORT CRC, CRT, CRA, CRP, CRS, CRX, FileIO, Storage;
  71. IMPORT SYSTEM (* for TSIZE only *);
  72. CONST
  73. ATGExt = ".atg";
  74. LSTExt = ".lst";
  75. Version = "1.53";
  76. ReleaseDate = "17 September 2002";
  77. TYPE
  78. INT32 = FileIO.INT32;
  79. VAR
  80. Options,
  81. GrammarName,
  82. ATGFileName,
  83. lstFileName: ARRAY [0 .. 63] OF CHAR;
  84. ll1: BOOLEAN; (* TRUE, if grammar is LL(1) *)
  85. ok: BOOLEAN; (* TRUE, if grammar tests ok so far *)
  86. MODULE ListHandler;
  87. (* ------------------- Source Listing and Error handler -------------- *)
  88. IMPORT FileIO, Storage, SYSTEM;
  89. IMPORT lst, CharAt, errors, INT32;
  90. EXPORT StoreError, PrintListing;
  91. TYPE
  92. Err = POINTER TO ErrDesc;
  93. ErrDesc = RECORD
  94. nr, line, col: INTEGER;
  95. next: Err
  96. END;
  97. CONST
  98. tab = 11C;
  99. VAR
  100. firstErr, lastErr: Err;
  101. Extra: INTEGER;
  102. PROCEDURE StoreError (nr, line, col: INTEGER; pos: INT32);
  103. (* Store an error message for later printing *)
  104. VAR
  105. nextErr: Err;
  106. BEGIN
  107. Storage.ALLOCATE(nextErr, SYSTEM.TSIZE(ErrDesc));
  108. nextErr^.nr := nr; nextErr^.line := line; nextErr^.col := col;
  109. nextErr^.next := NIL;
  110. IF firstErr = NIL
  111. THEN firstErr := nextErr
  112. ELSE lastErr^.next := nextErr
  113. END;
  114. lastErr := nextErr;
  115. INC(errors)
  116. END StoreError;
  117. PROCEDURE GetLine (VAR pos: INT32;
  118. VAR line: ARRAY OF CHAR;
  119. VAR eof: BOOLEAN);
  120. (* Read a source line. Return empty line if eof *)
  121. VAR
  122. ch: CHAR;
  123. i: CARDINAL;
  124. BEGIN
  125. i := 0; eof := FALSE; ch := CharAt(pos); INC(pos);
  126. WHILE (ch # FileIO.CR) & (ch # FileIO.LF) & (ch # FileIO.EOF) DO
  127. line[i] := ch; INC(i); ch := CharAt(pos); INC(pos);
  128. END;
  129. eof := (i = 0) & (ch = FileIO.EOF); line[i] := 0C;
  130. IF ch = FileIO.CR THEN (* check for MsDos *)
  131. ch := CharAt(pos);
  132. IF ch = FileIO.LF THEN INC(pos); Extra := 0 END
  133. END
  134. END GetLine;
  135. PROCEDURE PrintErr (line: ARRAY OF CHAR; nr, col: INTEGER);
  136. (* Print an error message *)
  137. PROCEDURE Msg (s: ARRAY OF CHAR);
  138. BEGIN
  139. FileIO.WriteString(lst, s)
  140. END Msg;
  141. PROCEDURE Pointer;
  142. VAR
  143. i: INTEGER;
  144. BEGIN
  145. FileIO.WriteString(lst, "***** ");
  146. i := 0;
  147. WHILE i < col + Extra - 2 DO
  148. IF line[i] = tab
  149. THEN FileIO.Write(lst, tab)
  150. ELSE FileIO.Write(lst, ' ')
  151. END;
  152. INC(i)
  153. END;
  154. FileIO.WriteString(lst, "^ ")
  155. END Pointer;
  156. BEGIN
  157. Pointer;
  158. CASE nr OF
  159. -->Errors
  160. | 101: Msg("character set may not be empty")
  161. | 102: Msg("string literal may not extend over line end")
  162. | 103: Msg("a literal must not have attributes")
  163. | 104: Msg("this symbol kind not allowed in production")
  164. | 105: Msg("attribute mismatch between declaration and use")
  165. | 106: Msg("undefined string in production")
  166. | 107: Msg("name declared twice")
  167. | 108: Msg("this type not allowed on left side of production")
  168. | 109: Msg("earlier semantic action was not terminated")
  169. | 111: Msg("no production found for grammar name")
  170. | 112: Msg("grammar symbol must not have attributes")
  171. | 113: Msg("a literal must not be declared with a structure")
  172. | 114: Msg("semantic action not allowed here")
  173. | 115: Msg("undefined name")
  174. | 116: Msg("attributes not allowed in token declaration")
  175. | 117: Msg("name does not match grammar name")
  176. | 118: Msg("unacceptable constant value")
  177. | 119: Msg("may not ignore CHR(0)")
  178. | 120: Msg("token might be empty")
  179. | 121: Msg("token must not start with an iteration")
  180. | 122: Msg("comment delimiters may not be structured")
  181. | 123: Msg("only terminals may be weak")
  182. | 124: Msg("literal tokens may not contain white space")
  183. | 125: Msg("comment delimiter must be 1 or 2 characters long")
  184. | 126: Msg("character set contains more than one character")
  185. | 127: Msg("could not make deterministic automaton")
  186. | 128: Msg("semantic action text too long - please split it")
  187. | 129: Msg("literal tokens may not be empty")
  188. | 130: Msg("IGNORE CASE must appear earlier")
  189. ELSE Msg("Error: "); FileIO.WriteInt(lst, nr, 1);
  190. END;
  191. FileIO.WriteLn(lst)
  192. END PrintErr;
  193. PROCEDURE PrintListing;
  194. (* Print a source listing with error messages *)
  195. VAR
  196. nextErr: Err;
  197. eof: BOOLEAN;
  198. lnr, errC: INTEGER;
  199. srcPos: INT32;
  200. line: ARRAY [0 .. 255] OF CHAR;
  201. BEGIN
  202. FileIO.WriteString(lst, "Listing:");
  203. FileIO.WriteLn(lst); FileIO.WriteLn(lst);
  204. srcPos := FileIO.Long0; nextErr := firstErr;
  205. GetLine(srcPos, line, eof); lnr := 1; errC := 0;
  206. WHILE ~ eof DO
  207. FileIO.WriteInt(lst, lnr, 5); FileIO.WriteString(lst, " ");
  208. FileIO.WriteString(lst, line); FileIO.WriteLn(lst);
  209. WHILE (nextErr # NIL) & (nextErr^.line = lnr) DO
  210. PrintErr(line, nextErr^.nr, nextErr^.col); INC(errC);
  211. nextErr := nextErr^.next
  212. END;
  213. GetLine(srcPos, line, eof); INC(lnr);
  214. END;
  215. IF nextErr # NIL THEN
  216. FileIO.WriteInt(lst, lnr, 5); FileIO.WriteLn(lst);
  217. WHILE nextErr # NIL DO
  218. PrintErr(line, nextErr^.nr, nextErr^.col); INC(errC);
  219. nextErr := nextErr^.next
  220. END
  221. END;
  222. FileIO.WriteLn(lst);
  223. FileIO.WriteInt(lst, errC, 5); FileIO.WriteString(lst, " error");
  224. IF errC # 1 THEN FileIO.Write(lst, "s") END;
  225. FileIO.WriteLn(lst); FileIO.WriteLn(lst); FileIO.WriteLn(lst);
  226. END PrintListing;
  227. BEGIN
  228. firstErr := NIL; Extra := 1;
  229. END ListHandler;
  230. PROCEDURE SetOption (s: ARRAY OF CHAR);
  231. (* Set compiler options *)
  232. VAR
  233. i: CARDINAL;
  234. BEGIN
  235. i := 1;
  236. WHILE s[i] # 0C DO
  237. s[i] := CAP(s[i]);
  238. IF (s[i] >= "A") AND (s[i] <= "Z") THEN CRT.ddt[s[i]] := TRUE END;
  239. INC(i);
  240. END;
  241. END SetOption;
  242. PROCEDURE Msg (S: ARRAY OF CHAR);
  243. BEGIN
  244. FileIO.WriteString(FileIO.StdOut, S); FileIO.WriteLn(FileIO.StdOut);
  245. END Msg;
  246. (* --------------------------- Help ------------------------------- *)
  247. PROCEDURE Help;
  248. BEGIN
  249. Msg("Usage: COCOR [-Options] [Grammar[.atg]] [-Options]");
  250. Msg("Example: COCOR -mcs Test");
  251. Msg("");
  252. Msg("Options are");
  253. Msg("a - Trace automaton");
  254. Msg("c - Generate compiler module");
  255. Msg("d - Suppress generation of Definition Modules");
  256. Msg("f - Give Start and Follower sets");
  257. Msg("g - Print top-down graph");
  258. Msg("i - Trace start set computations");
  259. Msg("l - Force listing");
  260. Msg("m - (Multipass) Suppress FORWARD declarations");
  261. Msg("n - Generate symbolic names");
  262. Msg("p - Generate parser only");
  263. Msg("s - Print symbol table");
  264. Msg("t - Grammar tests only - no code generated");
  265. Msg("x - Print cross reference list");
  266. Msg("COMPILER.FRM, SCANNER.FRM and PARSER.FRM must be in the working directory,");
  267. Msg("or on the path specified by the environment variable CRFRAMES");
  268. END Help;
  269. BEGIN (* CR *)
  270. FileIO.WriteString(FileIO.StdOut, "Coco/R (WinTel) - Compiler-Compiler V");
  271. FileIO.WriteString(FileIO.StdOut, Version);
  272. FileIO.WriteLn(FileIO.StdOut);
  273. FileIO.WriteString(FileIO.StdOut, "Released by Pat Terry ");
  274. FileIO.WriteString(FileIO.StdOut, ReleaseDate);
  275. FileIO.WriteLn(FileIO.StdOut);
  276. FileIO.NextParameter(GrammarName);
  277. IF (GrammarName[0] = "?")
  278. OR (GrammarName[0] = "/") AND (GrammarName[1] = "?") THEN
  279. Help; FileIO.QuitExecution
  280. END;
  281. IF GrammarName[0] = 0C THEN
  282. FileIO.WriteString(FileIO.StdOut, "(COCOR ? gives short help screen)");
  283. FileIO.WriteLn(FileIO.StdOut);
  284. END;
  285. WHILE (GrammarName[0] = "-") OR (GrammarName[0] = "/") DO
  286. (* accept options before filename *)
  287. SetOption(GrammarName); FileIO.NextParameter(GrammarName)
  288. END;
  289. ok := GrammarName[0] # 0C;
  290. REPEAT
  291. IF ~ ok THEN
  292. FileIO.WriteString(FileIO.StdOut, "Grammar[.atg] ? : ");
  293. FileIO.ReadString(FileIO.StdIn, GrammarName);
  294. IF ~ FileIO.Okay THEN FileIO.QuitExecution END;
  295. FileIO.ReadLn(FileIO.StdIn);
  296. END;
  297. FileIO.AppendExtension(GrammarName, ATGExt, ATGFileName);
  298. GrammarName := ATGFileName;
  299. FileIO.Open(src, GrammarName, FALSE);
  300. ok := FileIO.Okay;
  301. IF ~ ok THEN
  302. FileIO.WriteString(FileIO.StdOut, "File <");
  303. FileIO.WriteString(FileIO.StdOut, GrammarName);
  304. FileIO.WriteString(FileIO.StdOut, "> not found.");
  305. FileIO.WriteLn(FileIO.StdOut);
  306. END
  307. UNTIL ok;
  308. FileIO.NextParameter(Options);
  309. IF Options[0] # 0C THEN SetOption(Options) END;
  310. FileIO.ExtractDirectory(GrammarName, directory);
  311. FileIO.ChangeExtension(GrammarName, LSTExt, lstFileName);
  312. FileIO.Open(lst, lstFileName, TRUE);
  313. FileIO.WriteString(lst, "Coco/R - Compiler-Compiler V");
  314. FileIO.WriteString(lst, Version);
  315. FileIO.WriteLn(lst);
  316. FileIO.WriteString(lst, "Released by Pat Terry ");
  317. FileIO.WriteString(lst, ReleaseDate);
  318. FileIO.WriteLn(lst);
  319. FileIO.WriteString(lst, "Source file: ");
  320. FileIO.WriteString(lst, GrammarName);
  321. FileIO.WriteLn(lst); FileIO.WriteLn(lst);
  322. FileIO.WriteLn(FileIO.StdOut);
  323. FileIO.WriteString(FileIO.StdOut, "parsing file ");
  324. FileIO.WriteString(FileIO.StdOut, GrammarName);
  325. FileIO.WriteLn(FileIO.StdOut);
  326. CRS.Error := StoreError;
  327. CRP.Parse;
  328. IF errors = 0 THEN
  329. Msg("testing grammar");
  330. FileIO.WriteString(lst, "Grammar Tests:");
  331. FileIO.WriteLn(lst); FileIO.WriteLn(lst);
  332. CRT.CompSymbolSets;
  333. CRT.TestCompleteness(ok);
  334. IF ok THEN CRT.TestIfAllNtReached(ok) END;
  335. IF ok THEN CRT.FindCircularProductions(ok) END;
  336. IF ok THEN CRT.TestIfNtToTerm(ok) END;
  337. IF ok THEN CRT.LL1Test(ll1) END;
  338. FileIO.WriteLn(lst);
  339. IF ~ ok OR ~ ll1 OR CRT.ddt["L"] OR CRT.ddt["X"] THEN
  340. Msg("listing");
  341. PrintListing; IF CRT.ddt["X"] THEN CRT.XRef; END;
  342. END;
  343. IF CRT.ddt["N"] OR CRT.symNames THEN
  344. Msg("symbol name assignment");
  345. CRT.AssignSymNames(CRT.ddt["N"], CRT.symNames);
  346. END;
  347. IF ok AND ~ CRT.ddt["T"] THEN
  348. Msg("generating parser");
  349. CRX.GenCompiler;
  350. IF CRT.genScanner AND ~ CRT.ddt["P"] THEN
  351. Msg("generating scanner");
  352. CRA.WriteScanner(ok);
  353. IF CRT.ddt["A"] THEN CRA.PrintStates END;
  354. END;
  355. IF CRT.ddt["C"] THEN
  356. Msg("generating compiler");
  357. CRC.WriteDriver;
  358. END;
  359. CRX.WriteStatistics;
  360. END;
  361. IF ~ ok THEN Msg("Compilation ended with errors in grammar tests.");
  362. ELSIF ~ ll1 THEN Msg("Compilation ended with LL(1) errors.");
  363. ELSE Msg("Compilation completed. No errors detected.");
  364. END;
  365. ELSE
  366. Msg("listing");
  367. PrintListing; IF CRT.ddt["X"] THEN CRT.XRef END;
  368. Msg("*** errors detected ***");
  369. END;
  370. IF CRT.ddt["G"] THEN CRT.PrintGraph END;
  371. IF CRT.ddt["S"] THEN CRT.PrintSymbolTable END;
  372. FileIO.Close(lst); FileIO.Close(src);
  373. END -->Grammar.