📄 coco.atg
字号:
if (tokenString == null || tokenString.Equals(noString))
DFA.ConvertToStates(g.l, sym);
else { // TokenExpr is a single string
if (Tab.literals[tokenString] != null)
SemErr("token string declared twice");
Tab.literals[tokenString] = sym;
DFA.MatchLiteral(tokenString, sym);
}
.)
| (. if (kind == id) genScanner = false;
else DFA.MatchLiteral(sym.name, sym);
.)
)
[ SemText<out sym.semPos> (. if (typ != Node.pr) SemErr("semantic action not allowed here"); .)
]
.
/*------------------------------------------------------------------------------------*/
AttrDecl<Symbol sym>
=
'<' (. int beg = la.pos; int col = la.col; .)
{ ANY
| badString (. SemErr("bad string in attributes"); .)
}
'>' (. if (t.pos > beg)
sym.attrPos = new Position(beg, t.pos - beg, col); .)
.
/*------------------------------------------------------------------------------------*/
Expression<out Graph g> (. Graph g2; .)
=
Term<out g> (. bool first = true; .)
{ WEAK
'|'
Term<out g2> (. if (first) { Graph.MakeFirstAlt(g); first = false; }
Graph.MakeAlternative(g, g2);
.)
}
.
/*------------------------------------------------------------------------------------*/
Term<out Graph g> (. Graph g2; Node rslv = null; g = null; .)
=
( [ (. rslv = new Node(Node.rslv, null, la.line); .)
Resolver<out rslv.pos> (. g = new Graph(rslv); .)
]
Factor<out g2> (. if (rslv != null) Graph.MakeSequence(g, g2);
else g = g2;
.)
{ Factor<out g2> (. Graph.MakeSequence(g, g2); .)
}
| (. g = new Graph(new Node(Node.eps, null, 0)); .)
) (. if (g == null) // invalid start of Term
g = new Graph(new Node(Node.eps, null, 0));
.)
.
/*------------------------------------------------------------------------------------*/
Factor<out Graph g> (. string name; int kind; Position pos; bool weak = false;
g = null;
.)
=
( [ "WEAK" (. weak = true; .)
]
Sym<out name, out kind> (. Symbol sym = Symbol.Find(name);
if (sym == null && kind == str)
sym = Tab.literals[name] as Symbol;
bool undef = sym == null;
if (undef) {
if (kind == id)
sym = new Symbol(Node.nt, name, 0); // forward nt
else if (genScanner) {
sym = new Symbol(Node.t, name, t.line);
DFA.MatchLiteral(sym.name, sym);
} else { // undefined string in production
SemErr("undefined string in production");
sym = Tab.eofSy; // dummy
}
}
int typ = sym.typ;
if (typ != Node.t && typ != Node.nt)
SemErr("this symbol kind is not allowed in a production");
if (weak)
if (typ == Node.t) typ = Node.wt;
else SemErr("only terminals may be weak");
Node p = new Node(typ, sym, t.line);
g = new Graph(p);
.)
[ Attribs<p> (. if (kind != id) SemErr("a literal must not have attributes"); .)
] (. if (undef)
sym.attrPos = p.pos; // dummy
else if ((p.pos == null) != (sym.attrPos == null))
SemErr("attribute mismatch between declaration and use of this symbol");
.)
| '(' Expression<out g> ')'
| '[' Expression<out g> ']' (. Graph.MakeOption(g); .)
| '{' Expression<out g> '}' (. Graph.MakeIteration(g); .)
| SemText<out pos> (. Node p = new Node(Node.sem, null, 0);
p.pos = pos;
g = new Graph(p);
.)
| "ANY" (. Node p = new Node(Node.any, null, 0); // p.set is set in Tab.SetupAnys
g = new Graph(p);
.)
| "SYNC" (. Node p = new Node(Node.sync, null, 0);
g = new Graph(p);
.)
) (. if (g == null) // invalid start of Factor
g = new Graph(new Node(Node.eps, null, 0));
.)
.
/*------------------------------------------------------------------------------------*/
Resolver<out Position pos>
=
"IF" "(" (. int beg = la.pos; int col = la.col; .)
Condition (. pos = new Position(beg, t.pos - beg, col); .)
.
/*------------------------------------------------------------------------------------*/
Condition = { "(" Condition | ANY } ")" .
/*------------------------------------------------------------------------------------*/
TokenExpr<out Graph g> (. Graph g2; .)
=
TokenTerm<out g> (. bool first = true; .)
{ WEAK
'|'
TokenTerm<out g2> (. if (first) { Graph.MakeFirstAlt(g); first = false; }
Graph.MakeAlternative(g, g2);
.)
}
.
/*------------------------------------------------------------------------------------*/
TokenTerm<out Graph g> (. Graph g2; .)
=
TokenFactor<out g>
{ TokenFactor<out g2> (. Graph.MakeSequence(g, g2); .)
}
[ "CONTEXT"
'(' TokenExpr<out g2> (. Graph.SetContextTrans(g2.l); Graph.MakeSequence(g, g2); .)
')'
]
.
/*------------------------------------------------------------------------------------*/
TokenFactor<out Graph g> (. string name; int kind; .)
=
(. g = null; .)
( Sym<out name, out kind> (. if (kind == id) {
CharClass c = CharClass.Find(name);
if (c == null) {
SemErr("undefined name");
c = new CharClass(name, new BitArray(CharClass.charSetSize));
}
Node p = new Node(Node.clas, null, 0); p.val = c.n;
g = new Graph(p);
tokenString = noString;
} else { // str
g = Graph.StrToGraph(name);
if (tokenString == null) tokenString = name;
else tokenString = noString;
}
.)
| '(' TokenExpr<out g> ')'
| '[' TokenExpr<out g> ']' (. Graph.MakeOption(g); .)
| '{' TokenExpr<out g> '}' (. Graph.MakeIteration(g); .)
) (. if (g == null) // invalid start of TokenFactor
g = new Graph(new Node(Node.eps, null, 0)); .)
.
/*------------------------------------------------------------------------------------*/
Sym<out string name, out int kind>
= (. name = "???"; kind = id; .)
( ident (. kind = id; name = t.val; .)
| (string (. name = t.val; .)
| char (. name = "\"" + t.val.Substring(1, t.val.Length-2) + "\""; .)
) (. kind = str;
if (DFA.ignoreCase) name = name.ToLower();
if (name.IndexOf(' ') >= 0)
SemErr("literal tokens must not contain blanks"); .)
)
.
/*------------------------------------------------------------------------------------*/
Attribs<Node p>
=
'<' (. int beg = la.pos; int col = la.col; .)
{ Attribs<p>
|ANY
| badString (. SemErr("bad string in attributes"); .)
}
'>' (. if (t.pos > beg) p.pos = new Position(beg, t.pos - beg, col); .)
.
/*------------------------------------------------------------------------------------*/
SemText<out Position pos>
=
"(." (. int beg = la.pos; int col = la.col; .)
{ ANY
| badString (. SemErr("bad string in semantic action"); .)
| "(." (. SemErr("missing end of previous semantic action"); .)
}
".)" (. pos = new Position(beg, t.pos - beg, col); .)
.
/*------------------------------------------------------------------------------------*/
UsingDecl<out Position pos>
=
"using" (. int beg = t.pos; .)
{ ANY } ';' (. int end = t.pos; .)
{ "using" { ANY } ';' (. end = t.pos; .)
} (. pos = new Position(beg, end - beg + 1, 0); .)
.
END Coco.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -