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

📄 coco.atg

📁 cocorj09-一个Java语言分析器
💻 ATG
📖 第 1 页 / 共 2 页
字号:
  [ SemText <^pos>                (. if (typ == Tab.t) SemError(14);
                                     Tab.Sym(sp).semPos = pos; .)
  ].

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

AttrDecl <Symbol sym>             (. int beg, col, dim; StringBuffer buf; .)
=
  "<"
  [ "^"
    ident                         (. buf = new StringBuffer(token.val); dim = 0; .)
    { "[" "]"                     (. dim++; .)
    }
    ident                         (. sym.retVar = token.val; .)
    { "[" "]"                     (. dim++; .)
    }                             (. while (dim > 0) { buf.append("[]"); dim--; }
                                     sym.retType = buf.toString(); .)
    [","]
  ]                               (. beg = t.pos; col = t.col; .)
  { ANY | badstring               (. SemError(2); .)
  }
  ">"                             (. if (token.pos > beg) {
                                         sym.attrPos = new Position();
                                         sym.attrPos.beg = beg; sym.attrPos.col = col;
                                         sym.attrPos.len = token.pos - beg;
                                     } .).

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

AttrDecl1 <Symbol sym>            (. int beg, col, dim; StringBuffer buf; .)
=
  "<."
  [ "^"
    ident                         (. buf = new StringBuffer(token.val); dim = 0; .)
    { "[" "]"                     (. dim++; .)
    }
    ident                         (. sym.retVar = token.val; .)
    { "[" "]"                     (. dim++; .)
    }                             (. while (dim > 0) { buf.append("[]"); dim--; }
                                     sym.retType = buf.toString(); .)
    [","]
  ]                               (. beg = t.pos; col = t.col; .)
  { ANY | badstring               (. SemError(2); .)
  }
  ".>"                            (. if (token.pos > beg) {
                                         sym.attrPos = new Position();
                                         sym.attrPos.beg = beg; sym.attrPos.col = col;
                                         sym.attrPos.len = token.pos - beg;
                                     } .).

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

Expression <^Graph g>             (. Graph g2; boolean first; .)
=
  Term <^g>                       (. first = true; .)
  { WEAK "|"
    Term <^g2>                    (. if (first) {g = Tab.FirstAlt(g); first = false;}
                                     g = Tab.Alternative(g, g2); .)
  }.

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

Term<^Graph g>                    (. Graph g2; .)
=                                 (. g = null; .)
  ( Factor <^g>
    { Factor <^g2>                (. g = Tab.Sequence(g, g2); .)
    }
  |                               (. g = new Graph();
                                     g.l = Tab.NewNode(Tab.eps, 0, 0);
                                     g.r = g.l; .)
  ).

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

Factor <^Graph g>                 (. GraphNode n;
                                     SymInfo s;
                                     Symbol sym;
                                     Position pos;
                                     BitSet set;
                                     int sp, typ;
                                     boolean undef, weak = false; .)
=
                                  (. g = new Graph(); weak = false; .)
  ( [ "WEAK"                      (. weak = true; .)
    ]
    Symbol <^s>                   (. sp = Tab.FindSym(s.name); undef = sp == Tab.noSym;
                                     if (undef) {
                                         if (s.kind == ident)
                                             sp = Tab.NewSym(Tab.nt, s.name, 0); // forward nt
                                         else if (genScanner) {
                                             sp = Tab.NewSym(Tab.t, s.name, token.line);
                                             MatchLiteral(sp);
                                         } else { // undefined string in production
                                             SemError(6); sp = 0;
                                         }
                                     }
                                     sym = Tab.Sym(sp); typ = sym.typ;
                                     if (typ != Tab.t && typ != Tab.nt) SemError(4);
                                     if (weak)
                                         if (sym.typ == Tab.t) typ = Tab.wt; else SemError(23);
                                     g.l = Tab.NewNode(typ, sp, token.line); g.r = g.l;
                                     n = Tab.Node(g.l); .)
    [ (   Attribs <n>
        | Attribs1 <n> )          (. if (typ != Tab.nt) SemError(3); .)
    ]                             (. if (undef) {
                                         sym.attrPos = n.pos; sym.retVar = n.retVar; // dummies
                                     } else
                                         if (n.pos != null && sym.attrPos == null
                                         || n.retVar != null && sym.retVar == null
                                         || n.pos == null && sym.attrPos != null
                                         || n.retVar == null && sym.retVar != null) SemError(5);
                                     .)
  | "(" Expression <^g> ")"
  | "[" Expression <^g> "]"       (. g = Tab.Option(g); .)
  | "{" Expression <^g> "}"       (. g = Tab.Iteration(g); .)
  | SemText <^pos>                (. g.l = Tab.NewNode(Tab.sem, 0, 0);
                                     g.r = g.l;
                                     n = Tab.Node(g.l); n.pos = pos; .)
  | "ANY"                         (. set = Sets.FullSet(Tab.maxTerminals);
                                     set.clear(Tab.eofSy);
                                     g.l = Tab.NewNode(Tab.any, Tab.NewSet(set), 0);
                                     g.r = g.l; .)
  | "SYNC"                        (. g.l = Tab.NewNode(Tab.sync, 0, 0);
                                     g.r = g.l; .)
  ).

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

TokenExpr <^Graph g>              (. Graph g2; boolean first; .)
=
  TokenTerm <^g>                  (. first = true; .)
  { WEAK "|"
    TokenTerm <^g2>               (. if (first) {g = Tab.FirstAlt(g); first = false;}
                                     g = Tab.Alternative(g, g2); .)
  }.

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

TokenTerm <^Graph g>              (. Graph g2; .)
=
  TokenFactor <^g>
  { TokenFactor <^g2>             (. g = Tab.Sequence(g, g2); .)
  }
  [ "CONTEXT"
    "(" TokenExpr <^g2>           (. SetCtx(g2.l); g = Tab.Sequence(g, g2); .)
    ")"
  ].

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

TokenFactor <^Graph g>            (. String name; int kind, c; SymInfo s; .)
=
                                  (. g = new Graph(); .)
  ( Symbol <^s>                   (. if (s.kind == ident) {
                                         c = Tab.ClassWithName(s.name);
                                         if (c < 0) {
                                             SemError(15);
                                             c = Tab.NewClass(s.name, new BitSet());
                                         }
                                         g.l = Tab.NewNode(Tab.clas, c, 0);
                                         g.r = g.l;
                                     } else /*string*/ g = Tab.StrToGraph(s.name); .)
  | "(" TokenExpr <^g> ")"
  | "[" TokenExpr <^g> "]"        (. g = Tab.Option(g); .)
  | "{" TokenExpr <^g> "}"        (. g = Tab.Iteration(g); .)
  ).

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

Symbol <^SymInfo s>
=
                                  (. s = new SymInfo(); .)
  ( ident                         (. s.kind = ident; .)
  | string                        (. s.kind = string; .)
  )                               (. s.name = token.val;
                                     if (s.kind == string) s.name = FixString(s.name); .).

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

Attribs <GraphNode n>             (. int beg, col; .)
=
  "<"
  ( "^"                           (. beg = t.pos; .)
    { ANY | badstring             (. SemError(2); .)
    }                             (. n.retVar = ParserGen.GetString(beg, t.pos); .)
  ( "," AttrRest<n>
  | ">"
  )
  | AttrRest<n>
  ).

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

AttrRest <GraphNode n>            (. int beg, col; .)
=
                                  (. beg = t.pos; col = t.col; .)
  { ANY | badstring               (. SemError(2); .)
  }
  ">"                             (. if (token.pos > beg) {
                                         n.pos = new Position();
                                         n.pos.beg = beg; n.pos.col = col;
                                         n.pos.len = token.pos - beg;
                                     } .).

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

Attribs1 <GraphNode n>            (. int beg, col; .)
=
  "<."
  ( "^"                           (. beg = t.pos; .)
    { ANY | badstring             (. SemError(2); .)
    }                             (. n.retVar = ParserGen.GetString(beg, t.pos); .)
  ( "," AttrRest1<n>
  | ".>"
  )
  | AttrRest1<n>
  ).

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

AttrRest1 <GraphNode n>           (. int beg, col; .)
=
                                  (. beg = t.pos; col = t.col; .)
  { ANY | badstring               (. SemError(2); .)
  }
  ".>"                            (. if (token.pos > beg) {
                                         n.pos = new Position();
                                         n.pos.beg = beg; n.pos.col = col;
                                         n.pos.len = token.pos - beg;
                                     } .).

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

SemText <^Position pos>
=
  "(."                            (. pos = new Position();
                                     pos.beg = t.pos; pos.col = t.col; .)
  {   ANY
    | badstring                   (. SemError(2); .)
    | "(."                        (. SemError(9); .)
  }
  ".)"                            (. pos.len = token.pos - pos.beg; .).

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

NameDecl                          (. String alias, str = new String(); .)
  = ident                         (. alias = token.val; .)
    "="
    (   ident                     (. str = token.val; .)
      | string                    (. str = FixString(token.val); .)
    )                             (. Tab.NewName(alias, str); .)
  ".".

END Coco.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -