编译原理语法分析 算术表达式

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
{
i=x.i;
i++;
ch=x.lex(i);
if(!T_Production())returnfalse;
if(!G_Production())returnfalse;
returntrue;
}
elseif(ch=='-')
{
i=x.i;
i++;
ch=x.lex(i);
if(!T_Production())returnfalse;
if(!S_Production())returnfalse;
returntrue;
}
publicbooleanF_Production()
{
if(ch=='(')
{i=x.i;
i++;
ch=x.lex(i);
if(E_Production())
returnfalse;
if(ch==')')
{
i=x.i;
if(!F_Production())returnfalse;
if(!S_Production())returnfalse;
returntrue;
}
elseif(ch=='/')
{
i=x.i;
i++;
ch=x.lex(i);
if(!F_Production())returnfalse;
if(!S_Production())returnfalse;
if(G_Production())returnfalse;
returntrue;
}
elseif(ch==')'||ch=='#')
{
returntrue;
}
returnfalse;
}
publicbooleanS_Production()
{
if(ch=='*')
{
i=x.i;
i++;
ch=x.lex(i);
L2=new JLabel("结果是:");
bt=new JButton("语法分析");
bt2=new JButton("关闭");
frame1.setTitle("递归下降子程序分析语法");
frame1.setLayout(new GridLayout(3,1));
frame1.add(L1);
i++;
ch=x.lex(i);
returntrue;
}
elsereturnfalse;
}
if(ch=='1'||ch=='2')
{
i=x.i;
i++;
ch=x.lex(i);
returntrue;
}
elsereturnfalse;
}
publicbooleanG_Production()
{
if(ch=='+')
import java.awt.event.ActionListener;
import javax.swing.*;
public class frame implements ActionListener{
JFrame frame1;
JLabel L1,L2;
JButton bt,bt2;
JTextField input,result;
else result.setText("不符合语法要求");
}
if(e.getActionCommand()=="关闭")
{
frame1.dispose();
}
}
}
package语法分析;
public class Lex
{
String str="";
int i;
//int j=0;
public char lex(int j)
{
return true;
}
return false;
}
}
package语法分析;
publicclasstop_down_grammar {
charch;
inti=0;
Lexx=newLex();
publicbooleanE_Production()
{
ch=x.lex(i);
if(ch=='+'||ch=='-')
{ i=j;
if(is_identifiers_key())
{
return '2';
}
if(is_digital())
return '1';
if(is_oper())
return str.charAt(i);
return '0';
}பைடு நூலகம்
public boolean is_identifiers_key()
top_down_grammar a =new top_down_grammar();
public frame()
{
frame1=new JFrame("");
input=new JTextField(20);
result=new JTextField(20);
L1=new JLabel("请输入表达式以#结束");
{
i++;
f=true;
}
if(f)
i--;
return f;
}
public boolean is_digital()
{ boolean f=false;
if(i<str.length()&&str.charAt(i)>=48&&str.charAt(i)<=57)
{
while(((str.charAt(i)>=48&&str.charAt(i)<=57)||str.charAt(i)=='.'))
returntrue;
}
elseif(ch=='+'||ch=='-'||ch=='#'||ch==')')
{if(i<x.str.length()&&ch!='#')
{
if(x.str.charAt(i+1)!='#')
returntrue;
elsereturnfalse;
}
returntrue;
frame1.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
a.i=0;
a.x.str=input.getText();
if(e.getActionCommand()=="语法分析")
{
if(a.E_Production()) result.setText("符合语法要求");
package语法分析;
publicclassdisplymain {
publicstaticvoidmain(String args[])
{
newframe();
}
}
package语法分析;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
frame1.add(input);
frame1.add(L2);
frame1.add(result);
frame1.add(bt);
frame1.add(bt2);
bt.addActionListener(this);
bt2.addActionListener(this);
frame1.setSize(500, 500);
{i=x.i;
i++;
ch=x.lex(i);
}
if(!T_Production())returnfalse;
if(!G_Production())returnfalse;
returntrue;
}
publicbooleanT_Production()
{
if( !F_Production())returnfalse;
}
returnfalse;
}
}
{
boolean f=false;
//c2=str.charAt(i);
while((i<str.length()&&((str.charAt(i)>=65&&str.charAt(i)<=90)||(str.charAt(i)>=97&&str.charAt(i)<=122)||(str.charAt(i)=='_'))))
{
i++;
f=true;
}
//c=(str.charAt(i));
if(f)
i--;
return f;
}
return false;
}
public boolean is_oper()
{ // c2=str.charAt(i);
if(i<str.length())
if(str.charAt(i)=='('||str.charAt(i)==')'||str.charAt(i)=='+'||str.charAt(i)=='-'||str.charAt(i)=='*'||str.charAt(i)=='/'||str.charAt(i)=='#')
相关文档
最新文档