📄 crp.c
字号:
static void SemText(int *n)
{
long P;
int Len, Line, Col;
Expect(LparenPointSym);
P = S_Pos+2; Line = S_Line; Col = S_Col;
while (Sym >= identSym && Sym <= LparenPointSym ||
Sym == No_Sym) {
if (Sym >= identSym && Sym <= stringSym ||
Sym >= numberSym && Sym <= PointGreaterSym ||
Sym == No_Sym) {
Get();
} else if (Sym == badstringSym) {
Get();
SemError(102);
} else if (Sym == LparenPointSym) {
Get();
SemError(109);
} else GenError(50);
}
Expect(PointRparenSym);
Len = (int) (S_Pos - P);
*n = MakeSemGraph(T_SEM, P, Len, Line, Col);
}
static void Expression(int *n)
{
int n0 = NIL, n1, n2, SX_Line;
Term(&n1);
while (WeakSeparator(BarSym,4,3)) {
if (n0 == NIL)
n0 = n1 = MakeGraph(T_ALT, n1);
SX_Line = S_Line;
Term(&n2);
n2 = MakeGraph(T_ALT, n2);
SetGraphLine(n2,SX_Line);
n1 = LinkAltGraph(n1, n2);
}
*n = (n0 ? n0 : n1);
}
static void SetDecl(void)
{
Name name;
Set items;
Set_Init(&items);
Ident(name);
if (FindClass(name) != UNDEF) SemError(107);
Expect(EqualSym);
CompSet(&items);
Expect(PointSym);
(void) NewClass(name, &items);
Set_Done(&items);
}
static void TokenDecl(int sec_type)
{
char name[MAX_STR_LEN];
int p = 0, sp, type;
if (Sym == identSym) {
Ident(name);
if ((sp = FindSym(name, &type)) != UNDEF) SemError(107);
else sp = NewSym(name, sec_type);
while (!(Sym >= EOF_Sym && Sym <= stringSym ||
Sym >= PRODUCTIONSSym && Sym <= EqualSym ||
Sym >= CHARACTERSSym && Sym <= COMMENTSSym ||
Sym == IGNORESym ||
Sym == LparenPointSym)) { GenError(51); Get(); }
if (Sym == EqualSym) {
Get();
TokenExpr(&p);
if (sec_type == T_T) ConvertToStates(p, sp);
else ConvertToStates(p, sp+FIRST_PRAGMA);
Expect(PointSym);
} else if (Sym >= identSym && Sym <= stringSym ||
Sym == PRODUCTIONSSym ||
Sym >= CHARACTERSSym && Sym <= COMMENTSSym ||
Sym == IGNORESym ||
Sym == LparenPointSym) {
P_option = TRUE;
} else GenError(52);
} else if (Sym == stringSym) {
String(name);
P_option = TRUE;
if ((sp = FindSym(name, &type)) != UNDEF) SemError(107);
else sp = NewSym(name, sec_type);
} else GenError(53);
if (Sym == LparenPointSym) {
SemText(&p);
if (sec_type == T_T) SemError(114);
else SetPragmaText(sp, p);
}
}
static void NameDecl(void)
{
Name username, name;
Ident(username);
Expect(EqualSym);
if (Sym == identSym) {
Ident(name);
} else if (Sym == stringSym) {
String(name);
} else GenError(54);
Expect(PointSym);
NewName(name, username);
}
static void TokenExpr(int *n)
{
int n0 = NIL, n1, n2;
TokenTerm(&n1);
while (WeakSeparator(BarSym,6,5)) {
if (n0 == NIL)
n0 = n1 = MakeGraph(T_ALT, n1);
TokenTerm(&n2);
n2 = MakeGraph(T_ALT, n2);
n1 = LinkAltGraph(n1, n2);
}
*n = (n0 ? n0 : n1);
}
static void CompSet(PSet items)
{
Set set1, set2;
Set_Init(&set1); Set_Init(&set2);
SimSet(&set1);
while (Sym >= PlusSym && Sym <= MinusSym) {
if (Sym == PlusSym) {
Get();
SimSet(&set2);
Set_Union(&set1, &set2);
} else if (Sym == MinusSym) {
Get();
SimSet(&set2);
Set_Diference(&set1, &set2);
} else GenError(55);
Set_Clean(&set2);
}
Set_Union(items, &set1);
Set_Done(&set1); Set_Done(&set2);
}
static void SimSet(PSet items)
{
Name name;
char str[MAX_STR_LEN];
int n1, n2;
switch (Sym) {
case identSym:
Ident(name);
if (FindClass(name) == UNDEF) SemError(115);
GetClassWithName(name, items);
break;
case stringSym:
String(str);
StringClass(str, items);
break;
case CHRSym:
ChrSet(&n1);
if (Sym == Range) {
Get();
ChrSet(&n2);
Set_AddRange(items, n1, n2);
} else if (Sym == PRODUCTIONSSym ||
Sym == PointSym ||
Sym >= CHARACTERSSym && Sym <= COMMENTSSym ||
Sym == IGNORESym ||
Sym >= PlusSym && Sym <= MinusSym) {
Set_AddItem(items, n1);
} else GenError(56);
break;
case ANYSym:
Get();
Set_Union(items, &ANY_SET);
break;
default :GenError(57); break;
}
}
static void String(char *s)
{
Expect(stringSym);
LexString(s, MAX_STR_LEN-1);
FixString(s);
}
static void ChrSet(int *n)
{
char str[5]; int x;
Expect(CHRSym);
Expect(LparenSym);
if (Sym == numberSym) {
Get();
LexString(str, sizeof(str)-1);
x = atoi(str);
if (x > 255) { SemError(118); x = 0; }
*n = x;
} else if (Sym == stringSym) {
Get();
LexString(str, sizeof(str)-1);
if (strlen(str) != 3) SemError(118);
*n = str[1];
} else GenError(58);
Expect(RparenSym);
}
static void Term(int *n)
{
int n0 = NIL, n1, n2;
if (Sym >= identSym && Sym <= stringSym ||
Sym == ANYSym ||
Sym == LparenSym ||
Sym >= WEAKSym && Sym <= LbrackSym ||
Sym == LbraceSym ||
Sym == SYNCSym ||
Sym == LparenPointSym) {
Factor(&n1);
n0 = n1;
while (Sym >= identSym && Sym <= stringSym ||
Sym == ANYSym ||
Sym == LparenSym ||
Sym >= WEAKSym && Sym <= LbrackSym ||
Sym == LbraceSym ||
Sym == SYNCSym ||
Sym == LparenPointSym) {
Factor(&n2);
n1 = LinkGraph(n1, n2);
}
} else if (Sym == PointSym ||
Sym >= RparenSym && Sym <= BarSym ||
Sym == RbrackSym ||
Sym == RbraceSym) {
n0 = MakeSemGraph(T_SEM, -1, 0, S_Line, S_Col);
}
*n = n0;
}
static void Factor(int *n)
{
char name1[MAX_STR_LEN];
int weak = 0, SX_Line;
int n1, n2 = NIL;
int sp, is_new, type;
PNTermNode snt;
switch (Sym) {
case identSym:
case stringSym:
case WEAKSym:
if (Sym == WEAKSym) {
Get();
weak = 1;
}
Symbol(name1);
sp = FindSym(name1, &type);
if (type == T_CLASS) SemError(104);
if (weak && type == T_T) type = T_WT;
if (weak && type == T_NT) SemError(123);
n1 = MakeGraph(type, sp);
if (type == T_NT) {
snt = GetNTermP(sp);
is_new = snt->graph == 0;
snt->line_use = S_Line;
snt->reachable = TRUE;
};
if (Sym == LessSym ||
Sym == LessPointSym) {
Attribs(&n2);
(void) LinkAltGraph(n1, n2);
if (type != T_NT) SemError(103);
else {
if(!is_new && !snt->has_attr) SemError(105);
if (is_new) snt->has_attr = TRUE;
};
} else if (Sym >= identSym && Sym <= stringSym ||
Sym == PointSym ||
Sym == ANYSym ||
Sym >= LparenSym && Sym <= SYNCSym ||
Sym == LparenPointSym) {
if (type == T_NT)
if (!is_new && snt->has_attr) SemError(105);
} else GenError(59);
break;
case LparenSym:
Get();
Expression(&n1);
Expect(RparenSym);
break;
case LbrackSym:
Get();
SX_Line = S_Line;
Expression(&n1);
Expect(RbrackSym);
n1 = MakeGraph(T_OPT, n1);
SetGraphLine(n1,SX_Line);
break;
case LbraceSym:
Get();
SX_Line = S_Line;
Expression(&n1);
Expect(RbraceSym);
n1 = MakeGraph(T_REP, n1);
SetGraphLine(n1,SX_Line);
break;
case LparenPointSym:
SemText(&n1);
break;
case ANYSym:
Get();
n1 = MakeGraph(T_ANY, 0);
break;
case SYNCSym:
Get();
n1 = MakeGraph(T_SYNC, 0);
break;
default :GenError(60); break;
}
*n = n1;
}
static void Symbol(char *name)
{
int sp, type;
if (Sym == identSym) {
Ident(name);
sp = FindSym(name, &type);
if (sp == UNDEF) sp = NewSym(name, T_NT);
} else if (Sym == stringSym) {
String(name);
sp = FindSym(name, &type);
if (sp == UNDEF) {
sp = NewSym(name, T_T);
MatchLiteral(sp);
};
} else GenError(61);
}
static void TokenTerm(int *n)
{
int n0 = NIL, n1, n2;
TokenFactor(&n1);
n0 = n1;
while (Sym >= identSym && Sym <= stringSym ||
Sym == LparenSym ||
Sym == LbrackSym ||
Sym == LbraceSym) {
TokenFactor(&n2);
n1 = LinkGraph(n1, n2);
}
if (Sym == CONTEXTSym) {
Get();
Expect(LparenSym);
TokenExpr(&n2);
Expect(RparenSym);
SetCtx(n2); n1 = LinkGraph(n1, n2);
}
*n = n0;
}
static void TokenFactor(int *n)
{
char name[MAX_STR_LEN];
int p = 0;
switch (Sym) {
case identSym:
Ident(name);
if ((p = FindClass(name)) == UNDEF) {
/* Just Create a valid node */
p = MakeGraph(T_CHAR, 0);
SemError(115);
} else p = MakeGraph(T_CLASS, p);
break;
case stringSym:
String(name);
p = StrToGraph((unsigned char *) name);
break;
case LparenSym:
Get();
TokenExpr(&p);
Expect(RparenSym);
break;
case LbrackSym:
Get();
TokenExpr(&p);
Expect(RbrackSym);
p = MakeGraphOp(T_OPT, p);
break;
case LbraceSym:
Get();
TokenExpr(&p);
Expect(RbraceSym);
p = MakeGraphOp(T_REP, p);
break;
default :GenError(62); break;
}
*n = p;
}
void Parse(void)
{ S_Reset(); Get();
CR();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -