這裡介紹一個極其簡單的編程語言的部分實現
Straight
Stm ::= Stm; Stm
CompoundStm
Stm ::= id := Exp
AssignStm
Stm ::= print(ExpList)
PrintStm
ExpList ::= Exp
PairExpList
ExpList ::= Exp
LastExpList
Exp ::= id
IdExp
Exp ::= num
NumExp
Exp ::= Exp Binop Exp
OpExp
Exp ::= (Stm
EseqExp
Binop::= + |
Arithmetic Operators
Straight
a :=
輸出
怎樣在編譯器中表達Straight
abstract class Stm {}
abstract class Exp {}
abstract class ExpList {}
class CompoundStm extends Stm {
Stm stm
CompoundStm(Stm s
}
class AssignStm extends Stm {
String id; Exp exp;
AssignStm(String i
}
class PrintStm extends Stm {
ExpList exps;
PrintStm(ExpList e) { exps = e; }
}
class PairExpList extends ExpList {
Exp head; ExpList tail;
PairExpList(Exp h
}
class LastExpList extends ExpList {
Exp exp;
LastExpList(Exp e) { exp = e; }
}
class IdExp extends Exp {
String id;
IdExp(String i) { id = i; }
}
class NumExp extends Exp {
int num;
NumExp(int n) { num = n; }
}
class OpExp extends Exp {
final static int Plus =
Exp left
int oper;
OpExp(Exp l
}
class EseqExp extends Exp {
Stm stm; Exp exp;
EseqExp(Stm s
}
上面這幾個Java類描述了Straight
Stm testprog = new CompoundStm(new AssignStm(
new NumExp(
new AssignStm(
new IdExp(
OpExp
new NumExp(
new PrintStm(new LastExpList(new IdExp(
在這裡
首先是maxargs
package tiger
import junit
public class TestCountMaxPrintStmArgs extends TestCase {
public TestCountMaxPrintStmArgs(String m) {
super(m);
}
public void testMaxargs() {
CountMaxPrintStmArgs counter = new CountMaxPrintStmArgs();
assertEquals(
}
}
TestProg
package tiger
import static java
/**
* This Interpreter is too in functional style
* no assignment<br>
* definition with initialization<br>
* <code>int i =
*
* @author pan
*/
class CountMaxPrintStmArgs {
/**
* Entry Point
*/
int maxargs(Stm stm) {
return _maxargs(stm);
}
/*
* Because ExpList can occurs only for PrintStm
* That is the same to count length of ExpList
* but here I use another approach to count only for
* PrintStm
*
* if you want to avoid instanceof
* pack maxargs methods in classes e
*/
private int _maxargs(Stm stm) {
if (stm instanceof CompoundStm) {
CompoundStm cstm = (CompoundStm) stm;
return max(_maxargs(cstm
} else if (stm instanceof AssignStm) {
AssignStm astm = (AssignStm) stm;
return _maxargs(astm
} else { // Then it can be only PrintStm
PrintStm pstm = (PrintStm) stm;
return max(countargs(pstm
}
}
private int _maxargs(ExpList exps) {
if (exps instanceof PairExpList) {
PairExpList pexps = (PairExpList) exps;
return max(_maxargs(pexps
} else { // Then it can be LastExpList
LastExpList lexps = (LastExpList) exps;
return _maxargs(lexps
}
}
private int _maxargs(Exp exp) {
if (exp instanceof IdExp)
return
else if (exp instanceof NumExp)
return
else if (exp instanceof OpExp) {
OpExp oexp = (OpExp) exp;
return max(_maxargs(oexp
} else { // Then it can be EseqExp
EseqExp eexp = (EseqExp) exp;
return max(_maxargs(eexp
}
}
private int countargs(ExpList exps) {
if (exps instanceof LastExpList)
return
else { // Then it is a PairExpList
PairExpList pexps = (PairExpList) exps;
return
}
}
}
這裡解釋一下int _maxargs(Stm stm)
上面的maxargs的實現中用了很多instanceof
From:http://tw.wingwit.com/Article/program/Java/hx/201311/26757.html