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

📄 taste.atg

📁 cocorj09-一个Java语言分析器
💻 ATG
字号:
COMPILER Taste

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

CHARACTERS
  letter = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz".
  digit  = "0123456789".
  eol    = CHR(13).
  lf     = CHR(10).
  tab    = CHR(9).

TOKENS
  ident  = letter {letter | digit}.
  number = digit {digit}.

IGNORE eol + lf + tab

COMMENTS FROM "(*" TO "*)" NESTED

PRODUCTIONS

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

Taste                    (. String name, progName; Obj obj; .)
= "PROGRAM"              (. TC.Init(); TL.Init(); .)
  Ident<^progName>
  ";"                    (. TC.progStart = TC.pc; .)
  Body
  Ident<^name>
                         (. if (!name.equals(progName)) SemError(3);
                            TC.Emit(TC.HALTc); .)
  ".".

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

Body                     (. int fix, type; String name, name1; Obj obj; .)
=                        (. TL.EnterScope(); fix = TC.pc + 1; TC.Emit2(TC.JMP, 0); .)
  { "VAR"
    { Ident<^name>
      ":"                (. obj = TL.NewObj(name, TL.vars); .)
      TypeId<^obj.type>
      ";"             
    }

  | "PROCEDURE"
    Ident<^name>
    ";"                  (. obj = TL.NewObj(name, TL.procs); obj.adr = TC.pc; .)
    Body
    Ident<^name1>        (. TC.Emit(TC.RET);
                            if (!name.equals(name1)) SemError(3); .)
    ";"
  }
  "BEGIN"                (. TC.Fixup(fix); TC.Emit2(TC.RES, TL.DataSpace()); .)
  StatSeq
  "END"                  (. TL.LeaveScope(); .).

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

TypeId<^int type>
=                        (. type = TL.undef; .)
 ( "INTEGER"             (. type = TL.integer; .)
 | "BOOLEAN"             (. type = TL.bool; .)
 ).

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

Ident<^String name>
= ident                  (. name = token.val; .).

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

StatSeq = Stat {";" Stat}.

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

Stat                     (. int type;
                            String name;
                            Obj obj;
                            int fix, fix2, loopstart; .)
= [ Ident<^name>         (. obj = TL.This(name); .)
    ( ":" "="            (. if (obj.kind != TL.vars) SemError(7); .)
      Expression<^type>
                         (. if (type != obj.type) SemError(5);
                            TC.Emit3(TC.STO, TL.curLevel-obj.level, obj.adr); .)
  |                      (. if (obj.kind != TL.procs) SemError(8);
                            TC.Emit3(TC.CALL, TL.curLevel-obj.level, obj.adr); .)
    )
  | "IF"
    Expression<^type>
                         (. if (type != TL.bool) SemError(6);
                            fix = TC.pc + 1; TC.Emit2(TC.FJMP, 0); .)
    "THEN" StatSeq
    [ "ELSE"             (. fix2 = TC.pc + 1; TC.Emit2(TC.JMP, 0);
                            TC.Fixup(fix); fix = fix2; .)
      StatSeq
    ]
    "END"                (. TC.Fixup(fix); .)
  | "WHILE"              (. loopstart = TC.pc; .)
    Expression<^type>
                         (. if (type != TL.bool) SemError(6);
                            fix = TC.pc + 1; TC.Emit2(TC.FJMP, 0); .)
    "DO" StatSeq         (. TC.Emit2(TC.JMP, loopstart); TC.Fixup(fix); .)
    "END"
  | "READ"
    Ident<^name>         (. obj = TL.This(name);
                            if (obj.type != TL.integer) SemError(4);
                            TC.Emit3(TC.READ, TL.curLevel-obj.level, obj.adr); .)
  | "WRITE"
    Expression<^type>
                         (. if (type != TL.integer) SemError(4);
                            TC.Emit(TC.WRITE); .)
  ].

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

Expression<^int type>    (. int type1, op; .)
= SimExpr<^type>
  [ RelOp<^op>
    SimExpr<^type1>      (. if (type != type1) SemError(5);
                            TC.Emit(op); type = TL.bool; .)
  ].

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

SimExpr<^int type>       (. int type1, op; .)
= Term<^type>
  { AddOp<^op>
    Term<^type1>         (. if (type != TL.integer || type1 != TL.integer) SemError(4);
                            TC.Emit(op); .)
  }.

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

Term<^int type>          (. int type1, op; .)
= Factor<^type>
  { MulOp<^op>
    Factor<^type1>       (. if (type != TL.integer || type1 != TL.integer) SemError(4);
                            TC.Emit(op); .)
  }.

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

Factor<^int type>        (. int val, n; Obj obj; String name; .)
=                        (. type = TL.undef; .)
  ( Ident<^name>         (. obj = TL.This(name); type = obj.type;
                            if (obj.kind == TL.vars)
                              TC.Emit3(TC.LOAD, TL.curLevel-obj.level, obj.adr);
                            else SemError(7); .)
  | "TRUE"               (. TC.Emit2(TC.LIT, 1); type = TL.bool; .)
  | "FALSE"              (. TC.Emit2(TC.LIT, 0); type = TL.bool; .)
  | number               (. n = Integer.parseInt(token.val);
                            TC.Emit2(TC.LIT, n); type = TL.integer; .)
  | "-"
    Factor<^type>        (. if (type != TL.integer) {SemError(4); type = TL.integer;}
                            TC.Emit(TC.NEG); .)
  ).

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

MulOp<^int op>
=                        (. op = -1; .)
  ( "*"                  (. op = TC.MUL; .)
  | "/"                  (. op = TC.DIVI; .)
  ).

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

AddOp<^int op>
=                        (. op = -1; .)
  ( "+"                  (. op = TC.ADD; .)
  | "-"                  (. op = TC.SUB; .)
  ).

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

RelOp<^int op>
=                        (. op = -1; .)
  ( "="                  (. op = TC.EQU; .)
  | "<"                  (. op = TC.LSS; .)
  | ">"                  (. op = TC.GTR; .)
  ).

END Taste.

⌨️ 快捷键说明

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