📄 cr.atg
字号:
$C /* Generate Compiler Module */
COMPILER CR
/**************************************************************************
COCO/R for C grammar used to generate COCO/R itself
1.12 Last modified Tue 11-17-98
**************************************************************************/
#include "crt.h"
#include "crf.h"
#include "cra.h"
#include "crp.h"
#include "crs.h"
#include <string.h>
#include <stdlib.h>
static void FixString(char *name)
{
int i, len, dquote, spaces;
len = strlen(name);
if (len == 2) { SemError(129); return; }
if (ignore_case) upcase(name);
dquote = FALSE; spaces = FALSE;
for (i = 1; i <= len-2; i++) {
if (name[i] == '"') dquote = TRUE;
if (name[i] > 0 && name[i] <= ' ') spaces = TRUE;
}
if (!dquote) {
name[0] = '"'; name[len-1] = '"';
}
if (spaces) SemError(124);
}
static void MatchLiteral (int sp)
/* store string either as token or as literal */
{
PTermNode sn, sn1;
int matched_sp;
sn = GetTermP(sp);
matched_sp = MatchDFA((unsigned char *) sn->name, sp);
if (matched_sp != 0) {
sn1 = GetTermP(matched_sp);
sn1->type = T_CLASSLITTOKEN;
sn->type = T_LITTOKEN;
} else sn->type= T_CLASSTOKEN;
}
static void SetCtx (int gp)
/* set transition code to contextTrans */
{
PGraphNode gn;
while (gp > 0) {
gn = GetGraphP(gp);
if (gn->type == T_CHAR || gn->type == T_CLASS)
gn->CONTEXT = T_CONTEXT;
else
if (gn->type == T_OPT || gn->type == T_REP)
SetCtx(gn->INNER);
else
if (gn->type == T_ALT) {
SetCtx(gn->INNER); SetCtx(gn->ALT);
}
gp = gn->next;
}
}
static void StringClass(char *s, Set *items)
{
s[strlen(s)-1]=0; s++; /* Ignore First and Last character */
while (*s) Set_AddItem(items, *s++);
}
/**************************************************************************/
CHARACTERS
letter = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_" .
digit = "0123456789" .
cntl = CHR(0)..CHR(31).
tab = CHR(9) .
eol = CHR(13).
lf = CHR(10) .
noQuote1 = ANY - '"' - cntl.
noQuote2 = ANY - "'" - cntl.
IGNORE tab + eol + lf
TOKENS
ident = letter {letter | digit} .
string = '"' {noQuote1} '"' | "'" {noQuote2} "'" .
badstring = '"' {noQuote1} ( eol | lf) | "'" {noQuote2} (eol | lf) .
number = digit {digit} .
NAMES
Range = "..".
PRAGMAS
Options = "$" {letter}.
(. char s[100];
LookAheadString(s, sizeof(s)-1);
SetOptions(s); .)
COMMENTS
FROM "/*" TO "*/" NESTED
PRODUCTIONS
CR = (. Name name1;
int attr, sem, exp, is_new, sp, type;
PNTermNode sn; .)
"COMPILER"
Ident<compiler_name> (. global_defs.pos = S_NextPos;
global_defs.line = S_NextLine; .)
{ ANY } (. global_defs.len =
(int) (S_NextPos-global_defs.pos); .)
{ Declaration }
SYNC (. if (Successful()) { /* No Errors so far */
if (!MakeDeterministic()) SemError(127);
} .)
"PRODUCTIONS"
{ (. attr = NIL; sem = NIL; .)
Ident<name1> (. if ((sp = FindSym(name1, &type)) != UNDEF) {
is_new = FALSE;
if (type != T_NT) SemError(108); else {
sn = GetNTermP(sp);
if (sn->graph) SemError(107);
sn->line_dec = S_Line;
}
} else {
sp = NewSym(name1, T_NT);
sn = GetNTermP(sp); is_new = TRUE;
sn->line_dec = S_Line;
} .)
[ Attribs<&attr> ] (. if (!is_new) {
if (sn->has_attr && !attr) SemError(105);
if (!sn->has_attr && attr) SemError(105);
}
if (attr) {
sn->attr = attr; sn->has_attr = TRUE;
} .)
WEAK "="
[ SemText<&sem> ]
Expression<&exp> (. if (sem) {
(void) LinkGraph(sem, exp); exp = sem;
} .)
WEAK "." (. sn = GetNTermP(sp); /* reload */
sn->graph = exp; .)
SYNC
}
"END" Ident<name1> (. if (strcmp(name1, compiler_name)) SemError(117);
if((sp = FindSym(compiler_name, &type)) != UNDEF) {
if (type!=T_NT) SemError(108);
else {
sn = GetNTermP(sp);
if (sn->has_attr) SemError(112);
sn->reachable=TRUE;
}
} else SemError(111);
no_sym = NewSym("No", T_T); .)
"." .
Declaration =
(. Set ignore;
int n1, n2, nested = FALSE; .)
"CHARACTERS" { SetDecl }
| "TOKENS" { TokenDecl<T_T> }
| "NAMES" { NameDecl }
| "PRAGMAS" { TokenDecl<T_P> }
| "COMMENTS"
"FROM" TokenExpr<&n1>
"TO" TokenExpr<&n2>
[ "NESTED" (. nested = TRUE; .)
] (. NewComment(n1, n2, nested); .)
| "IGNORE"
( "CASE" (. ignore_case = TRUE; .)
| (. Set_Init(&ignore); .)
CompSet<&ignore> (. AddIgnore(&ignore);
if (Set_IsItem(&ignore,0)) SemError(119);
Set_Done(&ignore); .)
)
.
SetDecl =
(. Name name;
Set items;
Set_Init(&items); .)
Ident<name> (. if (FindClass(name) != UNDEF) SemError(107); .)
"=" CompSet<&items> "." (. (void) NewClass(name, &items);
Set_Done(&items); .)
.
CompSet<PSet items> =
(. Set set1, set2;
Set_Init(&set1); Set_Init(&set2); .)
SimSet<&set1>
{ ( "+" SimSet<&set2> (. Set_Union(&set1, &set2); .)
| "-" SimSet<&set2> (. Set_Diference(&set1, &set2); .)
) (. Set_Clean(&set2); .)
} (. Set_Union(items, &set1);
Set_Done(&set1); Set_Done(&set2); .)
.
SimSet<PSet items> =
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -