⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 coco.atg

📁 全功能c#编译器
💻 ATG
📖 第 1 页 / 共 2 页
字号:
                                   Graph.Finish(g);
                                   DFA.ConvertToStates(g.l, sym);
                                 .)
  | '.'                         (. if (typ != Node.rslv) SemErr("resolver is only allowed in RESOLVERS section"); .) /* ML-AW */
  |                             (. if (kind == id) genScanner = false;
                                   else DFA.MatchLiteral(sym);
                                 .)
  )
  ( SemText<out sym.semPos>     (. if (typ == Node.t) SemErr("semantic action not allowed here"); .)
  |                             (. if (typ == Node.rslv) SemErr("resolvers must have a semantic action"); .) /* ML-AW */
  )
.

/*------------------------------------------------------------------------------------*/

AttrDecl<Symbol sym>
=
  '<'                           (. int beg = la.pos; int col = la.col; .)
  { ANY
  | badString                   (. SemErr("bad string in semantic action"); .)
  }
  '>'                           (. 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; Position pos; Node rslv = null; 
                                   g = null;
                                 .)
= [                             (. rslv = new Node(Node.rslv, null, la.line); .)
    ResolveExpr<out pos>        (. rslv.pos = 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)); .)
.

/*------------------------------------------------------------------------------------*/

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);
                                   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);
                                     } 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 && typ != Node.rslv) /* ML */
                                     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);
                                 .)
.

/*------------------------------------------------------------------------------------*/

ResolveExpr<out Position pos>   
=
  "IF" "("                       (. int beg = la.pos; int col = la.col; .)
  ( ("=" | "!=") CondPart        /* indicate the beginning of a syntax snippet.
                                    The condition is true if the actual input matches
                                    the given syntax snippet (or does not match for "!=")
                                  */
  | "(" CondPart ")"
  | ANY CondPart
  )				                 (. pos = new Position(beg, t.pos - beg, col); .)
.

/* ConPart exists to guarantee an equal number of opening and *
 * closing parentheses inside the conditional expression.     */
CondPart = { "(" CondPart | 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 = new Graph(); .)
( 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);
                                   } else g = Graph.StrToGraph(name);  // str
                                 .)
| '(' TokenExpr<out g> ')'
| '[' TokenExpr<out g> ']'      (. Graph.MakeOption(g); .)
| '{' TokenExpr<out g> '}'      (. Graph.MakeIteration(g); .)
)
.

/*------------------------------------------------------------------------------------*/

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; .)
)
.

/*------------------------------------------------------------------------------------*/

Attribs<Node p>
=
  '<'                           (. int beg = la.pos; int col = la.col; .)
  { ANY 
  | badString                   (. SemErr("bad string in attributes"); .)
  }
  '>'                           (. 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 + -