package myCompiler;
import java.util.ArrayList;
import java.util.Stack;
import java.util.Vector;
public class Recursive2 {
int CONST = 1;
int VAR = 2;
int PROCEDURE = 3;
int IF = 4;
int THEN = 5;
int WHILE = 6;
int DO = 7;
int READ = 8;
int WRITE = 9;
int BEGIN = 10;
int END = 11;
int CALL = 12;
int ODD = 13;
int ADD = 14;
int SUB = 15;
int MUL = 16;
int DIV = 17;
int EQU = 18;
int NEQ = 19;
int LES = 20;
int LEQ = 21;
int LAG = 22;
int GEQ = 23;
int DOT = 24;
int COM = 25;
int SEM = 26;
int ASS = 27;
int LBR = 28;
int RBR = 29;
int INT = 30;
int ID = 31;
int EIN = 32;
GetWords getwords;
SYM sym;
String name;
String previous;
Stack tbl=new Stack();
Vector alltbls=new Vector();
ArrayList alist;
Header hdr;
int LEV = -1;
int DX;
int line;
Construct cstruct;
Varstruct vstruct;
Procstruct pstruct;
public Recursive2(String SourceFileName) {
getwords = new GetWords(SourceFileName);
}
ArrayList makeTable(String previous,String name){
Header header=new Header(previous,name);
ArrayList arraylist=new ArrayList();
arraylist.add(header);
return arraylist;
}
Construct makeConstruct(String name,int value){
Construct cstruct=new Construct(name,value);
return cstruct;
}
Varstruct makeVarstruct(String name,int level,int adr){
Varstruct vstruct=new Varstruct(name,level,adr);
return vstruct;
}
Procstruct makeProcstruct(String name,int level){
Procstruct pstruct=new Procstruct(name,level);
return pstruct;
}
void adrclear(){
DX=0;
}
void adrincrease(){
DX++;
}
void levincrease(){
LEV++;
}
void levdiscrease(){
LEV--;
}
void setline(int line){
this.line=line;
}
public void getword() {
getwords.getsym();
sym = getwords.sym;
setline(getwords.line);
System.out.print(sym.name);
System.out.print(" " + sym.code);
System.out.println(" "+line);
}
public void print(Object object) {
if (object instanceof Construct) {
System.out.println(((Construct) object).name + " "
+ ((Construct) object).kind + " "
+ ((Construct) object).value);
} else if (object instanceof Varstruct) {
System.out.println(((Varstruct) object).name + " "
+ ((Varstruct) object).kind + " "
+ ((Varstruct) object).level + " "
+ ((Varstruct) object).adr);
} else if (object instanceof Procstruct) {
System.out.println(((Procstruct) object).name + " "
+ ((Procstruct) object).kind + " "
+ ((Procstruct) object).level + " "
+ ((Procstruct) object).adr);
}
}
public static void main(String args[]) {
String s = "e:/pl0_1.txt";
Recursive2 rr = new Recursive2(s);
rr.getword();
rr.program();
Object object;
ArrayList aa;
Header h;
System.out.println("Name Kind Value/Level Address");
for(int i=0;i<rr.alltbls.size();i++){
aa=(ArrayList) rr.alltbls.get(i);
h=(Header) aa.get(0);
System.out.println("==========================================");
System.out.println(h.name+" "+h.previous);
for (int j=1;j<aa.size();j++){
object=aa.get(j);
rr.print(object);
}
}
}
public void program() {
alist=this.makeTable(null, "program");
tbl.push(alist);
alltbls.add(alist);
block();
if (sym.code == DOT) {
getword();
}else {
Error2.error(line,"缺少 .");
}
}
public void block() {
levincrease();
adrclear();
conststatement();
varstatement();
procstatement();
sentence();
}
public void conststatement() {
if (sym.code == CONST) {
getword();
constdefine();
constsuffix();
if (sym.code == SEM) {
getword();
}
}
}
public void constdefine() {
if (sym.code == ID) {
name=sym.name;
getword();
if (sym.code == EQU) {
getword();
if (sym.code == INT) {
cstruct=makeConstruct(name,sym.code);
alist=(ArrayList) tbl.pop();
alist.add(cstruct);
tbl.push(alist);
//constvalue();
getword();
} else
Error2.error(line,"缺少整数!");
} else
Error2.error(line,"缺少标识符!");
} else
Error2.error(line,"多余的CONST!");
}
public void constsuffix() {
if (sym.code == COM) {
getword();
constdefine();
constsuffix();
if (sym.code == SEM) {
getword();
} else
Error2.error(line,"缺少分号!");
}
}
public void varstatement() {
if (sym.code == VAR) {
getword();
if (sym.code == ID) {
adrincrease();
vstruct=makeVarstruct(sym.name,LEV,DX);
alist=(ArrayList) tbl.pop();
alist.add(vstruct);
tbl.push(alist);
//varadd();
getword();
identsuffix();
if (sym.code == SEM)
getword();
else
Error2.error(line,"缺少分号!");
} else
Error2.error(line,"多余的VAR");
}
}
public void identsuffix() {
if (sym.code == COM) {
getword();
if (sym.code == ID) {
//varadd();
adrincrease();
vstruct=makeVarstruct(sym.name,LEV,DX);
alist=(ArrayList) tbl.pop();
alist.add(vstruct);
tbl.push(alist);
getword();
identsuffix();
} else
Error2.error(line,"多余的逗号!");
}
}
public void procstatement() {
if (sym.code == PROCEDURE) {
prochead();
block();
if (sym.code == SEM) {
tbl.pop();
levdiscrease();
getword();
procsuffix();
} else
Error2.error(line,"缺少分号!");
}
}
public void prochead() {
if (sym.code == PROCEDURE) {
getword();
if (sym.code == ID) {
pstruct=this.makeProcstruct(sym.name, LEV);
alist=(ArrayList) tbl.pop();
alist.add(pstruct);
tbl.push(alist);
hdr=(Header) alist.get(0);
previous=hdr.name;
name=sym.name;
alist=this.makeTable(previous, name);
tbl.push(alist);
alltbls.add(alist);
//procadd();
getword();
if (sym.code == SEM) {
getword();
} else
Error2.error(line,"缺少分号!");
} else
Error2.error(line,"缺少过程名称!");
}
else Error2.error(line,"缺少过程声明!");
}
public void procsuffix() {
if (sym.code == PROCEDURE) {
prochead();
block();
if (sym.code == SEM) {
tbl.pop();
levdiscrease();
getword();
procsuffix();
} else
Error2.error(line,"缺少分号!");
}
}
public void sentence() {
if (sym.code == ID)
assinsent();
else if (sym.code == IF)
ifsent();
else if (sym.code == WHILE)
whilesent();
else if (sym.code == CALL)
callsent();
else if (sym.code == READ)
readsent();
else if (sym.code == WRITE)
writesent();
else if (sym.code == BEGIN)
complsent();
}
public void assinsent() {
if (sym.code == ID) {
getword();
if (sym.code == ASS) {
getword();
express();
} else
Error2.error(line,"缺少赋值号!");
} else
Error2.error(line,"多余的赋值号!");
}
public void ifsent() {
if (sym.code == IF) {
getword();
condition();
if (sym.code == THEN) {
getword();
sentence();
} else
Error2.error(line,"缺少then");
} else
Error2.error(line,"多余的IF!");
}
public void whilesent() {
if (sym.code == WHILE) {
getword();
condition();
if (sym.code == DO) {
getword();
sentence();
} else
Error2.error(line,"缺少while!");
} else
Error2.error(line,"多余的while!");
}
public void callsent() {
if (sym.code == CALL) {
getword();
if (sym.code == ID)
getword();
else
Error2.error(line,"call语句缺少参数!");
} else
评论0