摘自COCO/R手册,代码具有启发性
Sources of the Sample Compiler
B.1 Taste.ATG
COMPILER Taste
const int // types
undef = 0, integer = 1, boolean = 2;
const int // object kinds
var = 0, proc = 1;
public SymbolTable tab;
public CodeGenerator gen;
CHARACTERS
letter = 'A'..'Z' + 'a'..'z'.
digit = '0'..'9'.
TOKENS
ident = letter {letter | digit}.
number = digit {digit}.
COMMENTS FROM "/*" TO "*/" NESTED
COMMENTS FROM "//" TO '\n'
IGNORE '\r' + '\n' + '\t'
PRODUCTIONS
AddOp<out Op op>
= (. op = Op.ADD; .)
( '+'
| '-' (. op = Op.SUB; .)
).
/*------------------------------------------------------------------------*/
Expr<out int type> (. int type1; Op op; .)
= SimExpr<out type>
[ RelOp<out op>
SimExpr<out type1> (. if (type != type1) SemErr("incompatible types");
gen.Emit(op); type = boolean; .)
].
/*------------------------------------------------------------------------*/
Factor<out int type> (. int n; Obj obj; string name; .)
= (. type = undef; .)
( Ident<out name> (. obj = tab.Find(name); type = obj.type;
if (obj.kind == var) {
if (obj.level == 0) gen.Emit(Op.LOADG, obj.adr);
else gen.Emit(Op.LOAD, obj.adr);
} else SemErr("variable expected"); .)
| number (. n = Convert.ToInt32(t.val);
gen.Emit(Op.CONST, n); type = integer; .)
| '-'
Factor<out type> (. if (type != integer) {
SemErr("integer type expected"); type = integer;
}
gen.Emit(Op.NEG); .)
| "true" (. gen.Emit(Op.CONST, 1); type = boolean; .)
| "false" (. gen.Emit(Op.CONST, 0); type = boolean; .)
).
/*------------------------------------------------------------------------*/
Ident<out string name>
= ident (. name = t.val; .).
/*------------------------------------------------------------------------*/
35
MulOp<out Op op>
= (. op = Op.MUL; .)
( '*'
| '/' (. op = Op.DIV; .)
).
/*------------------------------------------------------------------------*/
ProcDecl (. string name; Obj obj; int adr; .)
= "void"
Ident<out name> (. obj = tab.NewObj(name, proc, undef); obj.adr = gen.pc;
if (name == "Main") gen.progStart = gen.pc;
tab.OpenScope(); .)
'(' ')'
'{' (. gen.Emit(Op.ENTER, 0); adr = gen.pc - 2; .)
{ VarDecl | Stat }
'}' (. gen.Emit(Op.LEAVE); gen.Emit(Op.RET);
gen.Patch(adr, tab.topScope.nextAdr);
tab.CloseScope(); .).
/*------------------------------------------------------------------------*/
RelOp<out Op op>
= (. op = Op.EQU; .)
( "=="
| '<' (. op = Op.LSS; .)
| '>' (. op = Op.GTR; .)
).
/*------------------------------------------------------------------------*/
SimExpr<out int type> (. int type1; Op op; .)
= Term<out type>
{ AddOp<out op>
Term<out type1> (. if (type != integer || type1 != integer)
SemErr("integer type expected");
gen.Emit(op); .)
}.
/*------------------------------------------------------------------------*/
Stat (. int type; string name; Obj obj;
int adr, adr2, loopstart; .)
= Ident<out name> (. obj = tab.Find(name); .)
( '=' (. if (obj.kind != var) SemErr("cannot assign to procedure"); .)
Expr<out type> ';' (. if (type != obj.type) SemErr("incompatible types");
if (obj.level == 0) gen.Emit(Op.STOG, obj.adr);
else gen.Emit(Op.STO, obj.adr); .)
| '(' ')' ';' (. if (obj.kind != proc) SemErr("object is not a procedure");
gen.Emit(Op.CALL, obj.adr); .)
)
| "if"
'(' Expr<out type> ')' (. if (type != boolean) SemErr("boolean type expected");
gen.Emit(Op.FJMP, 0); adr = gen.pc - 2; .)
Stat
[ "else" (. gen.Emit(Op.JMP, 0); adr2 = gen.pc - 2;
gen.Patch(adr, gen.pc);
adr = adr2; .)
Stat
] (. gen.Patch(adr, gen.pc); .)
| "while" (. loopstart = gen.pc; .)
'(' Expr<out type> ')' (. if (type != boolean) SemErr("boolean type expected");
gen.Emit(Op.FJMP, 0); adr = gen.pc - 2; .)
Stat (. gen.Emit(Op.JMP, loopstart); gen.Patch(adr, gen.pc); .)
| "read"
Ident<out name> ';' (. obj = tab.Find(name);
if (obj.type != integer) SemErr("integer type expected");
gen.Emit(Op.READ);
if (obj.level == 0) gen.Emit(Op.STOG, obj.adr);
else gen.Emit(Op.STO, obj.adr); .)
36
| "write"
Expr<out type> ';' (. if (type != integer) SemErr("integer type expected");
gen.Emit(Op.WRITE); .)
| '{' { Stat | VarDecl } '}' .
/*------------------------------------------------------------------------*/
Taste (. string name; .)
= "program" (. gen.Init(); tab.Init(); .)
Ident<out name> (. tab.OpenScope(); .)
'{'
{ VarDecl | ProcDecl }
'}' (. tab.CloseScope();
if (gen.progStart == -1) SemErr("main function never defined");
.).
/*------------------------------------------------------------------------*/
Term<out int type> (. int type1; Op op; .)
= Factor<out type>
{ MulOp<out op>
Factor<out type1> (. if (type != integer || type1 != integer)
SemErr("integer type expected");
gen.Emit(op); .)
}.
/*------------------------------------------------------------------------*/
Type<out int type>
= (. type = undef; .)
( "int" (. type = integer; .)
| "bool" (. type = boolean; .)
).
/*------------------------------------------------------------------------*/
VarDecl (. string name; int type; .)
= Type<out type>
Ident<out name> (. tab.NewObj(name, var, type); .)
{ ',' Ident<out name> (. tab.NewObj(name, var, type); .)
} ';'.
END Taste.
37
B.2 SymTab.cs (symbol table)
using System;
namespace Taste {
public class Obj { // object decribing a declared name
public string name; // name of the object
public int type; // type of the object (undef for procs)
public Obj next; // to next object in same scope
public int kind; // var, proc, scope
public int adr; // address in memory or start of proc
public int level; // nesting level; 0=global, 1=local
public Obj locals; // scopes: to locally declared objects
public int nextAdr; // scopes: next free address in this scope
}
public class SymbolTable {
const int // types
undef = 0, integer = 1, boolean = 2;
const int // object kinds
var = 0, proc = 1, scope = 2;
public int curLevel; // nesting level of current scope
public Obj undefObj; // object node for erroneous symbols
public Obj topScope; // topmost procedure scope
Parser parser;
// open a new scope and make it the current scope (topScope)
public void OpenScope () {
Obj scop = new Obj();
scop.name = ""; scop.kind = scope;
scop.locals = null; scop.nextAdr = 0;
scop.next = topScope; topScope = scop;
curLevel++;
}
// close the current scope
public void CloseScope () {
topScope = topScope.next; curLevel--;
}
// create a new object node in the current scope
public Obj NewObj (string name, int kind, int type) {
Obj p, last, obj = new Obj();
obj.name = name; obj.kind = kind; obj.type = type;
obj.level = curLevel;
p = topScope.locals; last = null;
while (p != null) {
if (p.name == name) parser.SemErr("name declared twice");
last = p; p = p.next;
}
if (last == null) topScope.locals = obj; else last.next = obj;
if (kind == var) obj.adr = topScope.nextAdr++;
return obj;
}
38
// search the name in all open scopes and return its object node
public Obj Find (string name) {
Obj obj, scope;
scope = topScope;
while (scope != null) { // for all scopes
obj = scope.locals;
while (obj != null) { // for all objects in this scope
if (obj.name == name) return obj;
obj = obj.next;
}
scope = scope.next;
}
parser.SemErr(name + " is undeclared");
return undefObj;
}
public SymbolTable (Parser parser) {
this.parser = parser;
topScope = null;
curLevel = -1;
undefObj = new Obj();
undefObj.name = "undef"; undefObj.type = undef; undefObj.kind = var;
undefObj.adr = 0; undefObj.level = 0; undefObj.next = null;
}
} // end SymbolTable
} // end namespace
39
B.3 CodeGen.cs (code generator)
using System;
using System.IO;
namespace Taste {
public enum Op { // opcodes
ADD, SUB, MUL, DIV, EQU, LSS, GTR, NEG,
LOAD, LOADG, STO, STOG, CONST,
CALL, RET, ENTER, LEAVE, JMP, FJMP, READ, WRITE
}
public class CodeGenerator {
string[] opcode =
{"ADD ", "SUB ", "MUL ", "DIV ", "EQU ", "LSS ", "GTR ", "NEG ",
"LOAD ", "LOADG", "STO ", "STOG ", "CONST", "CALL ", "RET ", "ENTER",
"LEAVE", "JMP ", "FJMP ", "READ ", "WRITE"};
public int progStart; // address of first instruction of main program
public int pc; // program counter
byte[] code = new byte[3000];
// data for Interpret
int[] globals = new int[100];
int[] stack = new int[100];
int top; // top of stack
int bp; // base pointer
//----- code generation methods -----
public void Put(int x) { code[pc++] = (byte)x; }
public void Emit (Op op) { Put((int)op); }
public void Emit (Op op, int val) { Emit(op); Put(val>>8); Put(val); }
public void Patch (int adr, int val) {
code[adr] = (byte)(val>>8); code[adr+1] = (byte)val;
}
public void Decode() {
int maxPc = pc; pc = 1;
while (pc < maxPc) {
Op code = (Op)Next();
Console.Write("{0,3}: {1} ", pc-1, opcode[(int)code]);
switch(code) {
case Op.LOAD: case Op.LOADG: case Op.CONST: case Op.STO: case Op.STOG:
case Op.CALL: case Op.ENTER: case Op.JMP: case Op.FJMP:
Console.WriteLine(Next2()); break;
case Op.ADD: case Op.SUB: case Op.MUL: case Op.DIV: case Op.NEG:
case Op.EQU: case Op.LSS: case Op.GTR: case Op.RET: case Op.LEAVE:
case Op.READ: case Op.WRITE:
Console.WriteLine(); break;
}
}
}
//----- interpreter methods -----
int Next () {
return code[pc++];
}
int Next2 () {
int x, y;
x = (sbyte)code[pc++]; y = code[pc++];
return (x << 8) + y;
}
40
int Int (bool b) {
if (b) return 1; else return 0;
}
void Push (int val) {
stack[top++] = val;
}
int Pop() {
return stack[--top];
}
int ReadInt(FileStream s) {
int ch, sign, n = 0;
do {ch = s.ReadByte();} while (!(ch >= '0' && ch <= '9' || ch == '-'));
if (ch == '-') {sign = -1; ch = s.ReadByte();} else sign = 1;
while (ch >= '0' && ch <= '9') {
n = 10 * n + (ch - '0');
ch = s.ReadByte();
}
return n * sign;
}
public void Interpret (string data) {
int val;
try {
FileStream s = new FileStream(data, FileMode.Open);
Console.WriteLine();
pc = progStart; stack[0] = 0; top = 1; bp = 0;
for (;;) {
switch ((Op)Next()) {
case Op.CONST: Push(Next2()); break;
case Op.LOAD: Push(stack[bp+Next2()]); break;
case Op.LOADG: Push(globals[Next2()]); break;
case Op.STO: stack[bp+Next2()] = Pop(); break;
case Op.STOG: globals[Next2()] = Pop(); break;
case Op.ADD: Push(Pop()+Pop()); break;
case Op.SUB: Push(-Pop()+Pop()); break;
case Op.DIV: val = Pop(); Push(Pop()/val); break;
case Op.MUL: Push(Pop()*Pop()); break;
case Op.NEG: Push(-Pop()); break;
case Op.EQU: Push(Int(Pop()==Pop())); break;
case Op.LSS: Push(Int(Pop()>Pop())); break;
case Op.GTR: Push(Int(Pop()<Pop())); break;
case Op.JMP: pc = Next2(); break;
case Op.FJMP: val = Next2(); if (Pop()==0) pc = val; break;
case Op.READ: val = ReadInt(s); Push(val); break;
case Op.WRITE: Console.WriteLine(Pop()); break;
case Op.CALL: Push(pc+2); pc = Next2(); break;
case Op.RET: pc = Pop(); if (pc == 0) return; break;
case Op.ENTER: Push(bp); bp = top; top = top + Next2(); break;
case Op.LEAVE: top = bp; bp = Pop(); break;
default: throw new Exception("illegal opcode");
}
}
} catch (IOException) {
Console.WriteLine("--- Error accessing file {0}", data);
System.Environment.Exit(0);
}
}
public CodeGenerator () { pc = 1; progStart = -1; }
} // end CodeGen
} // end namespace
41
B.4 Taste.cs (main program)
using System;
namespace Taste {
class Taste {
public static void Main (string[] arg) {
if (arg.Length > 0) {
Scanner scanner = new Scanner(arg[0]);
Parser parser = new Parser(scanner);
parser.tab = new SymbolTable(parser);
parser.gen = new CodeGenerator();
parser.Parse();
if (parser.errors.count == 0) {
parser.gen.Decode();
parser.gen.Interpret("Taste.IN");
}
} else {
Console.WriteLine("-- No source file specified");
}
}
}
} // end namespace