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

📄 coco.atg

📁 cocorj09-一个Java语言分析器
💻 ATG
📖 第 1 页 / 共 2 页
字号:
/* $0124678 */
/* Coco/R
   This grammar produces LL1 warnings for Attribs and AttrDecl, which can be ignored.
*/
COMPILER Coco

	private static final int ident = 0;
	private static final int string = 1;

	private static boolean genScanner = true, ignoreCase = false, genNames = false;

	private static void MatchLiteral(int sp) {
	// store string either as token or as literal
		Symbol sym, sym1;
		int matchedSp;
		sym = Tab.Sym(sp);
		matchedSp = DFA.MatchedDFA(sym.name, sp);
		if (matchedSp == Tab.noSym) sym.struct = Tab.classToken;
		else {
			sym1 = Tab.Sym(matchedSp); sym1.struct = Tab.classLitToken;
			sym.struct = Tab.litToken;
		}
	}

	private static void SetCtx(int p) {
	// set transition code to contextTrans
		GraphNode n;
		while (p > 0) {
			n = Tab.Node(p);
			if (n.typ == Tab.chr || n.typ == Tab.clas) {
				n.p2 = Tab.contextTrans;
			} else if (n.typ == Tab.opt || n.typ == Tab.iter) {
				SetCtx(n.p1);
			} else if (n.typ == Tab.alt) {
				SetCtx(n.p1); SetCtx(n.p2);
			}
			p = n.next;
		}
	}

	private static void SetDDT(String s) {
		char ch;
		for (int i = 1; i < s.length(); i++) {
			ch = s.charAt(i);
			if (Character.isDigit(ch)) Tab.ddt[Character.digit(ch, 10)] = true;
		}
	}

	private static String FixString(String s) {
		if (ignoreCase) s = s.toUpperCase();
		char[] a = s.toCharArray();
		int len = a.length;
		if (len == 2) SemError(29);
		boolean dbl = false, spaces = false;
		for (int i = 0; i < len-1; i++)
			if (a[i] == '"') dbl = true; else if (a[i] <= ' ') spaces = true;
		if (!dbl) {a[0] = '"'; a[len-1] = '"';}
		if (spaces) SemError(24);
		return new String(a, 0, len);
	}

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

CHARACTERS
  letter   = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_".
  digit    = "0123456789".
  eol      = CHR(13).
  lf       = CHR(10).
  tab      = CHR(9).
  noQuote1 = ANY - '"' - eol - lf.
  noQuote2 = ANY - "'" - eol - lf.

IGNORE eol + lf + tab

TOKENS
  ident     = letter {letter | digit}.
  string    = '"' {noQuote1} '"' | "'" {noQuote2} "'".
  badstring = '"' {noQuote1} (eol | lf) | "'" {noQuote2} (eol | lf).
  number    = digit {digit}.


PRAGMAS
  ddtSym   = "$" {letter | digit}. (. SetDDT(t.val); .)

COMMENTS FROM "/*" TO "*/" NESTED

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

PRODUCTIONS

Coco                              (. int sp, eofSy;
                                     boolean undef, noAttrs, noRet, ok, ok1;
                                     String gramName;
                                     Symbol sym;
                                     Graph g; .)
=
  "COMPILER"                      (. eofSy = Tab.NewSym(Tab.t, "EOF", 0);
                                     Tab.ignored = new BitSet(); .)
  ident                           (. gramName = token.val;
                                     Tab.semDeclPos = new Position();
                                     Tab.semDeclPos.beg = t.pos; .)
  {ANY}                           (. Tab.semDeclPos.len = t.pos - Tab.semDeclPos.beg;
                                     Tab.semDeclPos.col = 0; .)
  { Declaration }
  SYNC
  "PRODUCTIONS"                   (. ok = Successful();
                                     if (ok && genScanner) ok = DFA.MakeDeterministic();
                                     if (!ok) SemError(27);
                                     Tab.nNodes = 0; .)
  { ident                         (. sp = Tab.FindSym(token.val);
                                     undef = sp == Tab.noSym;
                                     if (undef) {
                                         sp = Tab.NewSym(Tab.nt, token.val, token.line);
                                         sym = Tab.Sym(sp);
                                     } else {
                                         sym = Tab.Sym(sp);
                                         if (sym.typ == Tab.nt) {
                                             if (sym.struct > 0) SemError(7);
                                         } else SemError(8);
                                           sym.line = token.line;
                                     }
                                     noAttrs = sym.attrPos == null; sym.attrPos = null;
                                     noRet = sym.retVar == null; sym.retVar = null; .)
    [   AttrDecl <sym>
      | AttrDecl1 <sym>
    ]                             (. if (!undef)
                                         if (noAttrs && sym.attrPos != null
                                         || noRet && sym.retVar != null
                                         || !noAttrs && sym.attrPos == null
                                         || !noRet && sym.retVar == null) SemError(5); .)
    [ SemText <^sym.semPos> ]
    WEAK "="
    Expression <^g>               (. sym.struct = g.l;
                                     Tab.CompleteGraph(g.r); .)
    WEAK "."
  }
  "END" ident                     (. if (Tab.ddt[2]) Tab.PrintGraph();
                                     Tab.gramSy = Tab.FindSym(gramName);
                                     if (Tab.gramSy == Tab.noSym) SemError(11);
                                     else {
                                         sym = Tab.Sym(Tab.gramSy);
                                         if (sym.attrPos != null) SemError(12);
                                     }
                                     if (!gramName.equals(token.val)) SemError(17);
                                     if (Scanner.err.count == 0) {
                                         System.out.println("Checking");
                                         Tab.CompSymbolSets();
                                         ok = Tab.NtsComplete();
                                         if (ok) {
                                             ok1 = Tab.AllNtReached();
                                             ok = Tab.NoCircularProductions();
                                         }
                                         if (ok) ok = Tab.AllNtToTerm();
                                         if (ok) ok1 = Tab.LL1();
                                         if (Tab.ddt[0] && genScanner) DFA.PrintStates();
                                         if (Tab.ddt[7]) Tab.XRef();
                                         if (ok) {
                                             ParserGen.WriteParser(genNames || Tab.ddt[4]);
                                             System.out.print("Parser"); System.out.flush();
                                             if (genScanner) {
                                                 System.out.print(" + Scanner");
                                                 System.out.flush();
                                                 DFA.WriteScanner(ignoreCase);
                                             }
                                             System.out.println(" generated");
                                             if (Tab.ddt[8]) ParserGen.WriteStatistics();
                                         }
                                         else System.out.println("Errors in grammar");
                                     }
                                     if (Tab.ddt[6]) Tab.PrintSymbolTable();
                                     System.out.println(); .)
  ".".

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

Declaration                       (. Graph g1, g2; boolean nested = false; .)
=
    "CHARACTERS" { SetDecl }
  | "TOKENS"     { TokenDecl <Tab.t> }
  | "NAMES"      { NameDecl }     (. genNames = true; .)
  | "PRAGMAS"    { TokenDecl <Tab.pr> }
  | "COMMENTS"
    "FROM" TokenExpr <^g1>
    "TO" TokenExpr <^g2>
    [ "NESTED"                    (. nested = true; .)
    ]                             (. new Comment(g1.l, g2.l, nested); .)
  | "IGNORE"
    ( "CASE"                      (. ignoreCase = true; .)
      | Set <^Tab.ignored>        (. Tab.ignored.set(32); /*' ' is always ignored*/
                                     if (Tab.ignored.get(0)) SemError(19); .)
    )
.

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

SetDecl                           (. int c; BitSet s; String name; .)
=
  ident                           (. name = token.val;
                                     c = Tab.ClassWithName(name);
                                     if (c >= 0) SemError(7); .)
  "=" Set <^s>                    (. c = Tab.NewClass(name, s); .)
  ".".

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

Set <^BitSet s>                   (. BitSet s2; .)
=
  SimSet <^s>
  {   "+" SimSet <^s2>            (. s.or(s2); .)
    | "-" SimSet <^s2>            (. Sets.Differ(s, s2); .)
  }.

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

SimSet <^BitSet s>                (. String name; int c, n1, n2; .)
=
                                  (. s = new BitSet(128); .)
  ( ident                         (. c = Tab.ClassWithName(token.val);
                                     if (c < 0) SemError(15); else s.or(Tab.Class(c)); .)
    | string                      (. name = token.val;
                                     for (int i = 1; name.charAt(i) != name.charAt(0); i++)
                                         if (ignoreCase) s.set((int) Character.toUpperCase(name.charAt(i)));
                                         else s.set((int) name.charAt(i)); .)
    | SingleChar<^n1>             (. s.set(n1); .)
      [ ".." SingleChar<^n2>      (. for (int i = n1; i <= n2; i++) s.set(i); .)
      ]
    | "ANY"                       (. s = Sets.FullSet(127); .)
  )
  .

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

SingleChar <^int n>
=
 "CHR" "(" number                 (. n = Integer.parseInt(token.val, 10);
                                     if (n > 127) SemError(18); n %= 128;
                                     if (ignoreCase && n >= 'a' && n <= 'z') n -= 32;  .)
 ")" .

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

TokenDecl <int typ>               (. SymInfo s; int sp; Position pos; Graph g; .)
=
  Symbol <^s>                     (. if (Tab.FindSym(s.name) != Tab.noSym) {SemError(7); sp = 0;}
                                     else {
                                         sp = Tab.NewSym(typ, s.name, token.line);
                                         Tab.Sym(sp).struct = Tab.classToken;
                                     } .)
  SYNC
  ( "=" TokenExpr <^g>            (. if (s.kind != ident) SemError(13);
                                     Tab.CompleteGraph(g.r);
                                     DFA.ConvertToStates(g.l, sp); .)
    "."
  |                               (. if (s.kind == ident) genScanner = false;
                                     else MatchLiteral(sp); .)
  )

⌨️ 快捷键说明

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