📄 cr.mod
字号:
(* CR Main Module of Coco/R
== =====================
This is a compiler generator that produces a scanner and a parser
from an attributed grammar, and optionally a complete small compiler.
Original code in Oberon by Hanspeter Moessenboeck, ETH Zurich
Ported at ETH to Apple Modula, and thence to JPI-2 Modula.
JPI version of 27 January 1991 was then modified to make more
portable by Pat Terry, January - October 1992
This is the WinTel version
Usage:
COCOR [-options] GrammarName[.atg] [$options]
Input:
attributed grammar input grammar
scanner.frm frame file
parser.frm frame file
compiler.frm frame file (optional)
(Frame files must be in the sme directory as the grammar, or may be
found on a path specified by DOS environment variable CRFRAMES).
Output:
<GrammarName>S.def + mod generated scanner
<GrammarName>P.def + mod generated parser
<GrammarName>.err error numbers and corresponding error messages
<GrammarName>.lst source listing with error messages and trace output
Optionally
<GrammarName>G.def + mod generated symbolic names
<GrammarName>.mod generated compiler main module
Implementation restrictions
1 too many nodes in graph (>1500) CRT.NewNode
2 too many symbols (>500) CRT.NewSym, MovePragmas
3 too many sets (>256 ANY-syms or SYNC syms) CRT.NewSet,
4 too many character classes (>250) CRT.NewClass
5 too many conditions in generated code (>100) CRX.NewCondSet
6 too many token names in "NAMES" (>100) CRT.NewName
7 too many states in automata (>500) CRA.NewState
Trace output
(To activate a trace switch, write "${letter}" in the input grammar, or
invoke Coco with a second command line parameter)
A Prints states of automaton
C Generates complete compiler module
D Suppresses Def Mod generation
F Prints start symbols and followers of nonterminals.
G Prints the top-down graph.
I Trace of start symbol set computation.
L Forces a listing (otherwise a listing is only printed if errors are found).
M Suppresses FORWARD declarations in parser (for multipass compilers).
N Uses default names for symbol value constants. This generates an
extra module <grammar name>G, and corresponding import statements
using constant names instead of numbers for symbols in parser and
scanner.
The constants are used unqualified and hence all needed constants
have to be imported; so a complete import list for these constants
is generated.
There is no decision whether a constant is actually needed.
The default conventions are (only terminals or pragmas can have names):
single character --> <ASCII name (lowercase)>Sym
eg. "+" --> plusSym
character string --> <string>Sym
eg. "PROGRAM" --> PROGRAMSym
scanner token --> <token name>Sym
eg. ident --> identSym
O Trace of follow set computation (not yet implemented).
P Generates parser only
S Prints the symbol list.
T Suppresses generation of def and mod files (grammar tests only).
X Prints a cross reference list.
==========================================================================*)
MODULE CR;
FROM CRS IMPORT lst, src, errors, directory, Error, CharAt;
FROM CRP IMPORT Parse;
IMPORT CRC, CRT, CRA, CRP, CRS, CRX, FileIO, Storage;
IMPORT SYSTEM (* for TSIZE only *);
CONST
ATGExt = ".atg";
LSTExt = ".lst";
Version = "1.48";
ReleaseDate = "9 July 1999";
TYPE
INT32 = FileIO.INT32;
VAR
Options,
GrammarName,
ATGFileName,
lstFileName: ARRAY [0 .. 63] OF CHAR;
ll1: BOOLEAN; (* TRUE, if grammar is LL(1) *)
ok: BOOLEAN; (* TRUE, if grammar tests ok so far *)
MODULE ListHandler;
(* ------------------- Source Listing and Error handler -------------- *)
IMPORT FileIO, Storage, SYSTEM;
IMPORT lst, CharAt, errors, INT32;
EXPORT StoreError, PrintListing;
TYPE
Err = POINTER TO ErrDesc;
ErrDesc = RECORD
nr, line, col: INTEGER;
next: Err
END;
CONST
tab = 11C;
VAR
firstErr, lastErr: Err;
Extra: INTEGER;
PROCEDURE StoreError (nr, line, col: INTEGER; pos: INT32);
(* Store an error message for later printing *)
VAR
nextErr: Err;
BEGIN
Storage.ALLOCATE(nextErr, SYSTEM.TSIZE(ErrDesc));
nextErr^.nr := nr; nextErr^.line := line; nextErr^.col := col;
nextErr^.next := NIL;
IF firstErr = NIL
THEN firstErr := nextErr
ELSE lastErr^.next := nextErr
END;
lastErr := nextErr;
INC(errors)
END StoreError;
PROCEDURE GetLine (VAR pos: INT32;
VAR line: ARRAY OF CHAR;
VAR eof: BOOLEAN);
(* Read a source line. Return empty line if eof *)
VAR
ch: CHAR;
i: CARDINAL;
BEGIN
i := 0; eof := FALSE; ch := CharAt(pos); INC(pos);
WHILE (ch # FileIO.CR) & (ch # FileIO.LF) & (ch # FileIO.EOF) DO
line[i] := ch; INC(i); ch := CharAt(pos); INC(pos);
END;
eof := (i = 0) & (ch = FileIO.EOF); line[i] := 0C;
IF ch = FileIO.CR THEN (* check for MsDos *)
ch := CharAt(pos);
IF ch = FileIO.LF THEN INC(pos); Extra := 0 END
END
END GetLine;
PROCEDURE PrintErr (line: ARRAY OF CHAR; nr, col: INTEGER);
(* Print an error message *)
PROCEDURE Msg (s: ARRAY OF CHAR);
BEGIN
FileIO.WriteString(lst, s)
END Msg;
PROCEDURE Pointer;
VAR
i: INTEGER;
BEGIN
FileIO.WriteString(lst, "***** ");
i := 0;
WHILE i < col + Extra - 2 DO
IF line[i] = tab
THEN FileIO.Write(lst, tab)
ELSE FileIO.Write(lst, ' ')
END;
INC(i)
END;
FileIO.WriteString(lst, "^ ")
END Pointer;
BEGIN
Pointer;
CASE nr OF
0: Msg("EOF expected")
| 1: Msg("ident expected")
| 2: Msg("string expected")
| 3: Msg("badstring expected")
| 4: Msg("number expected")
| 5: Msg("'COMPILER' expected")
| 6: Msg("'PRODUCTIONS' expected")
| 7: Msg("'=' expected")
| 8: Msg("'.' expected")
| 9: Msg("'END' expected")
| 10: Msg("'CHARACTERS' expected")
| 11: Msg("'TOKENS' expected")
| 12: Msg("'NAMES' expected")
| 13: Msg("'PRAGMAS' expected")
| 14: Msg("'COMMENTS' expected")
| 15: Msg("'FROM' expected")
| 16: Msg("'TO' expected")
| 17: Msg("'NESTED' expected")
| 18: Msg("'IGNORE' expected")
| 19: Msg("'CASE' expected")
| 20: Msg("'+' expected")
| 21: Msg("'-' expected")
| 22: Msg("'..' expected")
| 23: Msg("'ANY' expected")
| 24: Msg("'CHR' expected")
| 25: Msg("'(' expected")
| 26: Msg("')' expected")
| 27: Msg("'|' expected")
| 28: Msg("'WEAK' expected")
| 29: Msg("'[' expected")
| 30: Msg("']' expected")
| 31: Msg("'{' expected")
| 32: Msg("'}' expected")
| 33: Msg("'SYNC' expected")
| 34: Msg("'CONTEXT' expected")
| 35: Msg("'<' expected")
| 36: Msg("'>' expected")
| 37: Msg("'<.' expected")
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -