📄 grammer.java
字号:
import com.ms.wfc.ui.*;
import java.util.*;
public class Grammer
{
Worder worder;
Form1 main_form;
WordInfo current_word;
//TableArea tbarea;
IntStack tbarea;
public Grammer()
{
worder=new Worder();
}
public Grammer(String source,Form1 mform)
{
worder=new Worder(source);
this.main_form=mform;
}
public void setText(String code)
{
this.worder.setText(code);
}
public boolean startwork()
{
Glb.reset();
worder.current=0;
if(a_proc())
{
this.Message("编译成功!");
return true;
}
else
{
this.Message("编译因错误而中断!");
this.Message("错误位置——Line"+Glb.err_line);
this.Message("未能生成目标代码!");
return false;
}
}
boolean a_proc()
{
Glb.LEV++;
//tbarea=new TableArea();
//tbarea.start=Glb.Table.size();
tbarea=new IntStack();
Glb.S_Fathers.push(tbarea);
//变量和常量的定义
if(!this.get_word())
return false;
for(;;)
{
//这里只处理const和var
if(current_word.name.equals("var"))
if(!this.var_declare())
return false;
else;
else
if(current_word.name.equals("const"))
if(!this.const_declare())
return false;
else;
else
{
//目前过程中常量,变量定义完成,所以应该完成当前数据段范围信息,并将其入栈
//tbarea.end=Glb.Table.size();
//Glb.S_Fathers.push(tbarea);
break;
}
//再取一个单词
if(!this.get_word())
return false;
}
//变量和常量的定义结束
//procedure的定义
for(;;)
{
if(current_word.name.equals("procedure"))
{
//->ID
if(!this.get_word())
return false;
if(current_word.type!=WordType.ID)
{
return false;
}
//已经是一个ID了,现在是重名检查
if(Glb.ID_samename(current_word))
{
return false;
}
//已经做过重名检查
current_word.type=WordType.PROCEDURE;
current_word.lever=Glb.LEV;
current_word.addr=-1;
current_word.val=-1;
Glb.Table.addElement(current_word);
Glb.S_Unconfirm_proc_start.push(Glb.Table.size()-1);
tbarea.push(Glb.Table.size()-1);
//->";"
if(!this.get_word())
return false;
if(!current_word.name.equals(";"))
{
return false;
}
a_proc();
}
else break;
if(!this.get_word())
return false;
}
//过程的内部代码
//->begin
if(!current_word.name.equals("begin"))
{
return false;
}
else
{
//看到begin了,所以Table中的最近的未确定过程首地址现在已经确定了
if(!Glb.S_Unconfirm_proc_start.isEmpty())
{
int i=Glb.S_Unconfirm_proc_start.pop();
((WordInfo)Glb.Table.elementAt(i)).addr=Glb.ObjCode.size();
}
if(!this.get_word())
return false;
//如果当前是第0层,则整个程序的入口也就确定了
((Injunction)Glb.ObjCode.elementAt(1)).cmd=InType.JMP;
((Injunction)Glb.ObjCode.elementAt(1)).A2=Glb.ObjCode.size();
}
//->";"
if(!current_word.name.equals(";"))
{
return false;
}
else
{
if(!this.get_word())
return false;
}
while(!current_word.name.equals("end"))
{
if(!inner_code())
{
return false;
}
else
{
if(!this.get_word())
return false;
}
}
//->end
if(!current_word.name.equals("end"))
{
return false;
}
else
{
//一个过程定义结束!
if(!Glb.S_Fathers.isEmpty())
{
Glb.S_Fathers.pop();
}
Glb.LEV--;
if(!this.get_word())
return false;
}
//->";"
if(!current_word.name.equals(";"))
{
return false;
}
else
{
Glb.ObjCode.addElement(new Injunction(InType.OPR,0,0));
if(Glb.LEV==-1)
{
((Injunction)Glb.ObjCode.elementAt(0)).cmd=InType.INT;
((Injunction)Glb.ObjCode.elementAt(0)).A2=Glb.space_needed;
}
}
return true;
}
boolean var_declare()
{
if(!this.get_word())
return false;
for(;;)
{
//目前的输入单词已经是var了
//现在要求输入一个标志符
if(current_word.type!=WordType.ID)
{
return false;
}
//重名检查
if(Glb.ID_samename(current_word))
{
return false;
}
//重名检查已通过,可以加到表Tabel中去了
current_word.addr=Glb.DX++;
current_word.lever=Glb.LEV;
current_word.type=WordType.VAEIABLE;
current_word.val=0;
Glb.Table.addElement(current_word);
Glb.space_needed++;
tbarea.push(Glb.Table.size()-1);
//看看下一个单词
if(!this.get_word())
return false;
//是不是“,”?
if(current_word.name.equals(","))
{
//下面还应该是一个标志符
if(!this.get_word())
return false;
continue;
}
//是不是“;”?
if(current_word.name.equals(";"))
break;
return false;
}
return true;
}
boolean const_declare()
{
WordInfo the_const;
if(!this.get_word())
return false;
for(;;)
{
//目前的输入单词已经是const了
//现在要求输入一个标志符
if(current_word.type!=WordType.ID)
{
return false;
}
//重名检查
if(Glb.ID_samename(current_word))
{
return false;
}
//重名检查已通过
the_const=current_word;
//->"="
if(!this.get_word())
return false;
if(current_word.name!="=")
{
return false;
}
//->NUMBER
if(!this.get_word())
return false;
if(current_word.type!=WordType.NUM)
{
return false;
}
the_const.addr=Glb.DX++;
the_const.lever=Glb.LEV;
the_const.type=WordType.CONSTANT;
the_const.val=Integer.parseInt(current_word.name);
Glb.Table.addElement(the_const);
Glb.space_needed++;
tbarea.push(Glb.Table.size()-1);
//看看下一个单词
if(!this.get_word())
return false;
//是不是“,”?
if(current_word.name.equals(","))
{
//下面还应该是一个标志符
if(!this.get_word())
return false;
continue;
}
//是不是“;”?
if(current_word.name.equals(";"))
break;
return false;
}
return true;
}
boolean inner_code()
{
//内部代码,比如begin-end之间的代码
//完事以后,并不预读下一个单词(;后面的单词)
if(current_word.name.equals(";"))
return true;
//done!
if(current_word.type==WordType.ID)
{
if(!give_val())
return false;
else return true;
}
//done!
if(current_word.name.equals("if"))
{
if(!if_then())
return false;
else return true;
}
//done!
if(current_word.name.equals("while"))
{
if(!while_do())
return false;
else return true;
}
//done.
if(current_word.name.equals("read"))
{
if(!read())
return false;
else return true;
}
//done
if(current_word.name.equals("write"))
{
if(!write())
return false;
else return true;
}
if(current_word.name.equals("call"))
{
if(!call())
return false;
else return true;
}
return false;
}
boolean give_val()
{
WordInfo w_tobedo;
//现在的输入是一个标志符,接下来的应该是一个赋值语句
if(!Glb.is_var(current_word))
{
return false;
}
//先把当前标志符的地址记录下来,以备后面的sto语句用
w_tobedo=Glb.word_in_table(current_word);
//然后是一个“=”
if(!this.get_word())
return false;
if(!this.current_word.name.equals(":="))
{
return false;
}
else
{
if(!this.get_word())
return false;
}
//接下来应该是一个数学表达式
if(!math_exp())
return false;
//最后是";"
if(!current_word.name.equals(";"))
{
return false;
}
//层层检查,一个赋值语句终于通过了
//最后添加一条STO语句
Glb.ObjCode.addElement(new Injunction(InType.STO,w_tobedo.lever,w_tobedo.addr));
return true;
}
boolean if_then()
{
/*OK,到了这份上,已经进入一个if-then语句了!
if-then语句的结构是:
if [布尔表达式] then
begin
[语句]
[语句]
……
end
当然也可以是:
if [布尔表达式] then [语句]
*/
//第一个棘手的问题就是布尔表达式的判定问题
//现在的输入已经是if了,下面就是布尔表达式
if(!bool_exp())
return false;
//->then
if(!current_word.name.equals("then"))
{
return false;
}
else
{
//有then了,可以添加JPC语句了,注意要压栈
Glb.ObjCode.addElement(new Injunction(InType.JPC,0,-1));
Glb.S_Unconfirm_JPC.push(Glb.ObjCode.size()-1);
//分有begin-end和无begin-end两种情况讨论
if(!this.get_word())
return false;
if(current_word.name.equals("begin"))
{
//begin-end形式
//首先得给begin配个;吧!
if(!this.get_word())
return false;
if(!current_word.name.equals(";"))
{
}
else
{
if(!this.get_word())
return false;
}
while(!current_word.name.equals("end"))
{
if(!inner_code())
{
return false;
}
else
{
if(!this.get_word())
return false;
}
}
//->end
if(!current_word.name.equals("end"))
{
return false;
}
else
{
//一个if-then定义结束!
if(!this.get_word())
return false;
//地址回填
int back_pos=Glb.S_Unconfirm_JPC.pop();
((Injunction)Glb.ObjCode.elementAt(back_pos)).A2=Glb.ObjCode.size();
}
//->";"
if(!current_word.name.equals(";"))
{
return false;
}
else
{}
}
else
{
//一句话形式
if(!inner_code())
{
return false;
}
else
{}
//一共就一句话,现在已经读完了
//地址回填
int back_pos=Glb.S_Unconfirm_JPC.pop();
((Injunction)Glb.ObjCode.elementAt(back_pos)).A2=Glb.ObjCode.size();
//->";"
if(!current_word.name.equals(";"))
{
return false;
}
else
{}
}
}
return true;
}
boolean while_do()
{
/*OK,到了这份上,已经进入一个while-do语句了!
while-do语句的结构是:
while [布尔表达式] do
begin
[语句]
[语句]
……
end
当然也可以是:
while [布尔表达式] do [语句]
*/
//第一个棘手的问题还是布尔表达式的判定问题
//现在的输入已经是while了,下面就是布尔表达式
//首先把返回地址压栈
Glb.S_Unconfirm_do.push(Glb.ObjCode.size());
if(!bool_exp())
return false;
//->do
if(!current_word.name.equals("do"))
{
return false;
}
else
{
//有do了,可以添加JPC语句了,注意要压栈
Glb.ObjCode.addElement(new Injunction(InType.JPC,0,-1));
Glb.S_Unconfirm_JPC.push(Glb.ObjCode.size()-1);
//分有begin-end和无begin-end两种情况讨论
if(!this.get_word())
return false;
if(current_word.name.equals("begin"))
{
//begin-end形式
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -