Straight Line Interpreter
Hi All
I am doing exercise of book "Modern Compiler Implementation in JAVA" by Andrew Appel. It is first exercise and have to develop two functions maxargs to find the maximum number of arguments for print statements ..I am not able to understand the logic to use for recursive print statements.
like if there is print inside print ..how should i go ahead for developing this function ?
Till now i have tried this
static int maxargs(Stm s)
if (s instanceof CompoundStm)
{
CompoundStm stm = (CompoundStm) s;
return Math.max(maxargs(stm.stm2, stm.stm1));
}
else if (s instance of AssignStm)
here is the case where the recursive prints are possible.
[720 byte] By [
yogsmaa] at [2007-11-26 13:23:05]

# 1
Hi yogsma,
I made it:
public static int maxargs(Stm s) {
if (s instanceof CompoundStm) {
CompoundStm stm = (CompoundStm) s;
return Math.max(maxargs(stm.stm1), maxargs(stm.stm2));
} else if (s instanceof AssingStm) {
AssingStm astm = (AssingStm) s;
return (maxargs(astm.exp));
} else if (s instanceof PrintStm) {
PrintStm pstm = (PrintStm) s;
return Math.max(maxargs(pstm.exps), lenght_exp_list(pstm.exps));
} else {
return 0;
}
}
private static int maxargs(Exp e) {
if (e instanceof IdExp) {
return 1;
} else if (e instanceof NumExp) {
return 1;
} else if (e instanceof OpExp) {
OpExp oexp = (OpExp) e;
return Math.max(maxargs(oexp.left), maxargs(oexp.right));
} else if (e instanceof EseqExp) {
EseqExp esq = (EseqExp) e;
return Math.max(maxargs(esq.stm), maxargs(esq.exp));
} else {
return 0;
}
}
private static int maxargs(ExpList e) {
if(e instanceof PairExpList){
PairExpList expl = (PairExpList) e;
return Math.max(maxargs(expl.head), maxargs(expl.tail));
} else if (e instanceof LastExpList) {
LastExpList lexp = (LastExpList) e;
return maxargs(lexp.head);
} else {
return 0;
}
}
private static int lenght_exp_list(ExpList e){
if(e instanceof PairExpList){
return 1 + lenght_exp_list(((PairExpList)e).tail);
} else {
return 1;
}
}