CR.mod 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  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 CR;
  68. FROM CRS IMPORT lst, src, errors, directory, Error, CharAt;
  69. FROM CRP IMPORT Parse;
  70. IMPORT CRC, CRT, CRA, CRP, CRS, CRX, FileIO;
  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. FROM FileIO IMPORT CR, LF,EOF, WriteString, Write, WriteLn, WriteInt, Long0;
  89. FROM Storage IMPORT ALLOCATE;
  90. FROM SYSTEM IMPORT TSIZE;
  91. IMPORT lst, CharAt, errors, INT32;
  92. EXPORT StoreError, PrintListing;
  93. TYPE
  94. Err = POINTER TO ErrDesc;
  95. ErrDesc = RECORD
  96. nr, line, col: INTEGER;
  97. next: Err
  98. END;
  99. CONST
  100. tab = 11C;
  101. VAR
  102. firstErr, lastErr: Err;
  103. Extra: INTEGER;
  104. PROCEDURE StoreError (nr, line, col: INTEGER; pos: INT32);
  105. (* Store an error message for later printing *)
  106. VAR
  107. nextErr: Err;
  108. BEGIN
  109. ALLOCATE(nextErr, TSIZE(ErrDesc));
  110. nextErr^.nr := nr; nextErr^.line := line; nextErr^.col := col;
  111. nextErr^.next := NIL;
  112. IF firstErr = NIL
  113. THEN firstErr := nextErr
  114. ELSE lastErr^.next := nextErr
  115. END;
  116. lastErr := nextErr;
  117. INC(errors)
  118. END StoreError;
  119. PROCEDURE GetLine (VAR pos: INT32;
  120. VAR line: ARRAY OF CHAR;
  121. VAR eof: BOOLEAN);
  122. (* Read a source line. Return empty line if eof *)
  123. VAR
  124. ch: CHAR;
  125. i: CARDINAL;
  126. BEGIN
  127. i := 0; eof := FALSE; ch := CharAt(pos); INC(pos);
  128. WHILE (ch # CR) & (ch # LF) & (ch # EOF) DO
  129. line[i] := ch; INC(i); ch := CharAt(pos); INC(pos);
  130. END;
  131. eof := (i = 0) & (ch = EOF); line[i] := 0C;
  132. IF ch = CR THEN (* check for MsDos *)
  133. ch := CharAt(pos);
  134. IF ch = LF THEN INC(pos); Extra := 0 END
  135. END
  136. END GetLine;
  137. PROCEDURE PrintErr (line: ARRAY OF CHAR; nr, col: INTEGER);
  138. (* Print an error message *)
  139. PROCEDURE Msg (s: ARRAY OF CHAR);
  140. BEGIN
  141. WriteString(lst, s)
  142. END Msg;
  143. PROCEDURE Pointer;
  144. VAR
  145. i: INTEGER;
  146. BEGIN
  147. WriteString(lst, "***** ");
  148. i := 0;
  149. WHILE i < col + Extra - 2 DO
  150. IF line[i] = tab
  151. THEN Write(lst, tab)
  152. ELSE Write(lst, ' ')
  153. END;
  154. INC(i)
  155. END;
  156. WriteString(lst, "^ ")
  157. END Pointer;
  158. BEGIN
  159. Pointer;
  160. CASE nr OF
  161. 0: Msg("EOF expected")
  162. | 1: Msg("ident expected")
  163. | 2: Msg("string expected")
  164. | 3: Msg("badstring expected")
  165. | 4: Msg("number expected")
  166. | 5: Msg("'COMPILER' expected")
  167. | 6: Msg("'PRODUCTIONS' expected")
  168. | 7: Msg("'=' expected")
  169. | 8: Msg("'.' expected")
  170. | 9: Msg("'END' expected")
  171. | 10: Msg("'CHARACTERS' expected")
  172. | 11: Msg("'TOKENS' expected")
  173. | 12: Msg("'NAMES' expected")
  174. | 13: Msg("'PRAGMAS' expected")
  175. | 14: Msg("'COMMENTS' expected")
  176. | 15: Msg("'FROM' expected")
  177. | 16: Msg("'TO' expected")
  178. | 17: Msg("'NESTED' expected")
  179. | 18: Msg("'IGNORE' expected")
  180. | 19: Msg("'CASE' expected")
  181. | 20: Msg("'+' expected")
  182. | 21: Msg("'-' expected")
  183. | 22: Msg("'..' expected")
  184. | 23: Msg("'ANY' expected")
  185. | 24: Msg("'CHR' expected")
  186. | 25: Msg("'(' expected")
  187. | 26: Msg("')' expected")
  188. | 27: Msg("'|' expected")
  189. | 28: Msg("'WEAK' expected")
  190. | 29: Msg("'[' expected")
  191. | 30: Msg("']' expected")
  192. | 31: Msg("'{' expected")
  193. | 32: Msg("'}' expected")
  194. | 33: Msg("'SYNC' expected")
  195. | 34: Msg("'CONTEXT' expected")
  196. | 35: Msg("'<' expected")
  197. | 36: Msg("'>' expected")
  198. | 37: Msg("'<.' expected")
  199. | 38: Msg("'.>' expected")
  200. | 39: Msg("'(.' expected")
  201. | 40: Msg("'.)' expected")
  202. | 41: Msg("not expected")
  203. | 42: Msg("invalid TokenFactor")
  204. | 43: Msg("invalid Factor")
  205. | 44: Msg("invalid Factor")
  206. | 45: Msg("invalid Term")
  207. | 46: Msg("invalid Symbol")
  208. | 47: Msg("invalid SingleChar")
  209. | 48: Msg("invalid SimSet")
  210. | 49: Msg("invalid NameDecl")
  211. | 50: Msg("this symbol not expected in TokenDecl")
  212. | 51: Msg("invalid TokenDecl")
  213. | 52: Msg("invalid Attribs")
  214. | 53: Msg("invalid Declaration")
  215. | 54: Msg("invalid Declaration")
  216. | 55: Msg("invalid Declaration")
  217. | 56: Msg("this symbol not expected in CR")
  218. | 57: Msg("invalid CR")
  219. | 101: Msg("character set may not be empty")
  220. | 102: Msg("string literal may not extend over line end")
  221. | 103: Msg("a literal must not have attributes")
  222. | 104: Msg("this symbol kind not allowed in production")
  223. | 105: Msg("attribute mismatch between declaration and use")
  224. | 106: Msg("undefined string in production")
  225. | 107: Msg("name declared twice")
  226. | 108: Msg("this type not allowed on left side of production")
  227. | 109: Msg("earlier semantic action was not terminated")
  228. | 111: Msg("no production found for grammar name")
  229. | 112: Msg("grammar symbol must not have attributes")
  230. | 113: Msg("a literal must not be declared with a structure")
  231. | 114: Msg("semantic action not allowed here")
  232. | 115: Msg("undefined name")
  233. | 116: Msg("attributes not allowed in token declaration")
  234. | 117: Msg("name does not match grammar name")
  235. | 118: Msg("unacceptable constant value")
  236. | 119: Msg("may not ignore CHR(0)")
  237. | 120: Msg("token might be empty")
  238. | 121: Msg("token must not start with an iteration")
  239. | 122: Msg("comment delimiters may not be structured")
  240. | 123: Msg("only terminals may be weak")
  241. | 124: Msg("literal tokens may not contain white space")
  242. | 125: Msg("comment delimiter must be 1 or 2 characters long")
  243. | 126: Msg("character set contains more than one character")
  244. | 127: Msg("could not make deterministic automaton")
  245. | 128: Msg("semantic action text too long - please split it")
  246. | 129: Msg("literal tokens may not be empty")
  247. | 130: Msg("IGNORE CASE must appear earlier")
  248. ELSE Msg("Error: "); WriteInt(lst, nr, 1);
  249. END;
  250. WriteLn(lst)
  251. END PrintErr;
  252. PROCEDURE PrintListing;
  253. (* Print a source listing with error messages *)
  254. VAR
  255. nextErr: Err;
  256. eof: BOOLEAN;
  257. lnr, errC: INTEGER;
  258. srcPos: INT32;
  259. line: ARRAY [0 .. 255] OF CHAR;
  260. BEGIN
  261. WriteString(lst, "Listing:");
  262. WriteLn(lst); WriteLn(lst);
  263. srcPos := Long0;
  264. nextErr := firstErr;
  265. GetLine(srcPos, line, eof); lnr := 1; errC := 0;
  266. WHILE ~ eof DO
  267. WriteInt(lst, lnr, 5);WriteString(lst, " ");
  268. WriteString(lst, line); WriteLn(lst);
  269. WHILE (nextErr # NIL) & (nextErr^.line = lnr) DO
  270. PrintErr(line, nextErr^.nr, nextErr^.col); INC(errC);
  271. nextErr := nextErr^.next
  272. END;
  273. GetLine(srcPos, line, eof); INC(lnr);
  274. END;
  275. IF nextErr # NIL THEN
  276. WriteInt(lst, lnr, 5); WriteLn(lst);
  277. WHILE nextErr # NIL DO
  278. PrintErr(line, nextErr^.nr, nextErr^.col); INC(errC);
  279. nextErr := nextErr^.next
  280. END
  281. END;
  282. WriteLn(lst);
  283. WriteInt(lst, errC, 5); WriteString(lst, " error");
  284. IF errC # 1 THEN Write(lst, "s") END;
  285. WriteLn(lst); WriteLn(lst);WriteLn(lst);
  286. END PrintListing;
  287. BEGIN
  288. firstErr := NIL; Extra := 1;
  289. END ListHandler;
  290. PROCEDURE SetOption (s: ARRAY OF CHAR);
  291. (* Set compiler options *)
  292. VAR
  293. i: CARDINAL;
  294. BEGIN
  295. i := 1;
  296. WHILE s[i] # 0C DO
  297. s[i] := CAP(s[i]);
  298. IF (s[i] >= "A") AND (s[i] <= "Z") THEN CRT.ddt[s[i]] := TRUE END;
  299. INC(i);
  300. END;
  301. END SetOption;
  302. PROCEDURE Msg (S: ARRAY OF CHAR);
  303. BEGIN
  304. FileIO.WriteString(FileIO.StdOut, S); FileIO.WriteLn(FileIO.StdOut);
  305. END Msg;
  306. (* --------------------------- Help ------------------------------- *)
  307. PROCEDURE Help;
  308. BEGIN
  309. Msg("Usage: COCOR [-Options] [Grammar[.atg]] [-Options]");
  310. Msg("Example: COCOR -mcs Test");
  311. Msg("");
  312. Msg("Options are");
  313. Msg("a - Trace automaton");
  314. Msg("c - Generate compiler module");
  315. Msg("d - Suppress generation of Definition Modules");
  316. Msg("f - Give Start and Follower sets");
  317. Msg("g - Print top-down graph");
  318. Msg("i - Trace start set computations");
  319. Msg("l - Force listing");
  320. Msg("m - (Multipass) Suppress FORWARD declarations");
  321. Msg("n - Generate symbolic names");
  322. Msg("p - Generate parser only");
  323. Msg("s - Print symbol table");
  324. Msg("t - Grammar tests only - no code generated");
  325. Msg("x - Print cross reference list");
  326. Msg("COMPILER.FRM, SCANNER.FRM and PARSER.FRM must be in the working directory,");
  327. Msg("or on the path specified by the environment variable CRFRAMES");
  328. END Help;
  329. BEGIN (* CR *)
  330. FileIO.WriteString(FileIO.StdOut, "Coco/R (WinTel) - Compiler-Compiler V");
  331. FileIO.WriteString(FileIO.StdOut, Version);
  332. FileIO.WriteLn(FileIO.StdOut);
  333. FileIO.WriteString(FileIO.StdOut, "Released by Pat Terry ");
  334. FileIO.WriteString(FileIO.StdOut, ReleaseDate);
  335. FileIO.WriteLn(FileIO.StdOut);
  336. FileIO.NextParameter(GrammarName);
  337. IF (GrammarName[0] = "?")
  338. OR (GrammarName[0] = "/") AND (GrammarName[1] = "?") THEN
  339. Help; FileIO.QuitExecution
  340. END;
  341. IF GrammarName[0] = 0C THEN
  342. FileIO.WriteString(FileIO.StdOut, "(COCOR ? gives short help screen)");
  343. FileIO.WriteLn(FileIO.StdOut);
  344. END;
  345. WHILE (GrammarName[0] = "-") OR (GrammarName[0] = "/") DO
  346. (* accept options before filename *)
  347. SetOption(GrammarName); FileIO.NextParameter(GrammarName)
  348. END;
  349. ok := GrammarName[0] # 0C;
  350. REPEAT
  351. IF ~ ok THEN
  352. FileIO.WriteString(FileIO.StdOut, "Grammar[.atg] ? : ");
  353. FileIO.ReadString(FileIO.StdIn, GrammarName);
  354. IF ~ FileIO.Okay THEN FileIO.QuitExecution END;
  355. FileIO.ReadLn(FileIO.StdIn);
  356. END;
  357. FileIO.AppendExtension(GrammarName, ATGExt, ATGFileName);
  358. GrammarName := ATGFileName;
  359. FileIO.Open(src, GrammarName, FALSE);
  360. ok := FileIO.Okay;
  361. IF ~ ok THEN
  362. FileIO.WriteString(FileIO.StdOut, "File <");
  363. FileIO.WriteString(FileIO.StdOut, GrammarName);
  364. FileIO.WriteString(FileIO.StdOut, "> not found.");
  365. FileIO.WriteLn(FileIO.StdOut);
  366. END
  367. UNTIL ok;
  368. FileIO.NextParameter(Options);
  369. IF Options[0] # 0C THEN SetOption(Options) END;
  370. FileIO.ExtractDirectory(GrammarName, directory);
  371. FileIO.ChangeExtension(GrammarName, LSTExt, lstFileName);
  372. FileIO.Open(lst, lstFileName, TRUE);
  373. FileIO.WriteString(lst, "Coco/R - Compiler-Compiler V");
  374. FileIO.WriteString(lst, Version);
  375. FileIO.WriteLn(lst);
  376. FileIO.WriteString(lst, "Released by Pat Terry ");
  377. FileIO.WriteString(lst, ReleaseDate);
  378. FileIO.WriteLn(lst);
  379. FileIO.WriteString(lst, "Source file: ");
  380. FileIO.WriteString(lst, GrammarName);
  381. FileIO.WriteLn(lst); FileIO.WriteLn(lst);
  382. FileIO.WriteLn(FileIO.StdOut);
  383. FileIO.WriteString(FileIO.StdOut, "parsing file ");
  384. FileIO.WriteString(FileIO.StdOut, GrammarName);
  385. FileIO.WriteLn(FileIO.StdOut);
  386. CRS.Error := StoreError;
  387. CRP.Parse;
  388. IF errors = 0 THEN
  389. Msg("testing grammar");
  390. FileIO.WriteString(lst, "Grammar Tests:");
  391. FileIO.WriteLn(lst); FileIO.WriteLn(lst);
  392. CRT.CompSymbolSets;
  393. CRT.TestCompleteness(ok);
  394. IF ok THEN CRT.TestIfAllNtReached(ok) END;
  395. IF ok THEN CRT.FindCircularProductions(ok) END;
  396. IF ok THEN CRT.TestIfNtToTerm(ok) END;
  397. IF ok THEN CRT.LL1Test(ll1) END;
  398. FileIO.WriteLn(lst);
  399. IF ~ ok OR ~ ll1 OR CRT.ddt["L"] OR CRT.ddt["X"] THEN
  400. Msg("listing");
  401. PrintListing; IF CRT.ddt["X"] THEN CRT.XRef; END;
  402. END;
  403. IF CRT.ddt["N"] OR CRT.symNames THEN
  404. Msg("symbol name assignment");
  405. CRT.AssignSymNames(CRT.ddt["N"], CRT.symNames);
  406. END;
  407. IF ok AND ~ CRT.ddt["T"] THEN
  408. Msg("generating parser");
  409. CRX.GenCompiler;
  410. IF CRT.genScanner AND ~ CRT.ddt["P"] THEN
  411. Msg("generating scanner");
  412. CRA.WriteScanner(ok);
  413. IF CRT.ddt["A"] THEN CRA.PrintStates END;
  414. END;
  415. IF CRT.ddt["C"] THEN
  416. Msg("generating compiler");
  417. CRC.WriteDriver;
  418. END;
  419. CRX.WriteStatistics;
  420. END;
  421. IF ~ ok THEN Msg("Compilation ended with errors in grammar tests.");
  422. ELSIF ~ ll1 THEN Msg("Compilation ended with LL(1) errors.");
  423. ELSE Msg("Compilation completed. No errors detected.");
  424. END;
  425. ELSE
  426. Msg("listing");
  427. PrintListing; IF CRT.ddt["X"] THEN CRT.XRef END;
  428. Msg("*** errors detected ***");
  429. END;
  430. IF CRT.ddt["G"] THEN CRT.PrintGraph END;
  431. IF CRT.ddt["S"] THEN CRT.PrintSymbolTable END;
  432. FileIO.Close(lst); FileIO.Close(src);
  433. END CR.