⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 文法检测.java

📁 使用具递归功能的的高级语言来编制递归下降法的语法分析程序
💻 JAVA
字号:
package myprojects.wenfajiance;

import java.awt.*;
import java.awt.event.*;

class Wenfajiance extends Frame implements ActionListener{
 Button btn1;
 TextField txf1;
 char sym;
 String str;
 static int i;
 public Wenfajiance() {
  Panel pan1=new Panel();
  Panel pan2=new Panel();
  Label lb1=new Label("请输入你要语法检查的单词序列以$结尾:");
  pan1.setLayout(new FlowLayout(FlowLayout.LEFT));
  pan2.setLayout(new FlowLayout(FlowLayout.LEFT));
  btn1=new Button("检查");
  txf1=new TextField(30);
  pan1.add(lb1);
  pan2.add(txf1);
  pan2.add(btn1);
  this.setLayout(new BorderLayout());
  this.add(pan1,BorderLayout.NORTH);
  this.add(pan2,BorderLayout.CENTER);
  btn1.addActionListener(this);
  
  addWindowListener(new WindowAdapter() {
   public void windowClosing(WindowEvent e) {
    dispose();
    System.exit(0);
   }
  });
 }
 
 public void actionPerformed(ActionEvent e)
      {
       i=0;
       str=txf1.getText();
       scaner();
       S();
       if(sym=='$')
       txf1.setText("正确");
       else
       txf1.setText("不正确");
      }

 public static void main(String args[]) {
  //System.out.println("Starting Wenfajiance...");
  Wenfajiance mainFrame = new Wenfajiance();
  mainFrame.setSize(255, 160);
  mainFrame.setLocation(400,150);
  mainFrame.setTitle("Wenfajiance");
  mainFrame.setVisible(true);
 }
 
 
 public void scaner()//read the next sym;
 {
  if(i<str.length())
  {
  sym=str.charAt(i);
  i++;
     }
 }//end of scaner
 
 public void S()
 {
  E();
  S1();
 }//end of S
 
 public void S1()
 {
  if(sym=='+')
  {
   scaner();
   E();
   S1();
  }
  else if(sym=='-')
  {
   scaner();
   E();
   S1();
  }
  else if((sym !=')') && (sym !='$'))
  error();
 }//end of S1
 
 public void E()
 {
  T();
  E1();
 }//end of E
 
 public void E1()
 {
  if(sym=='*')
  {
   scaner();
   T();
   E1();
  }
  else if(sym=='/')
  {
   scaner();
   T();
   E1();
  }
  else if((sym !='+') && (sym !='-') && (sym !=')') && (sym !='$'))
  error();
 }//end of E1
 
 public void T()
 {
  if(sym=='(')
  {
   scaner();
   S();
   if(sym==')')
   scaner();
  }
  else
  F();
  
 }//end of T
 
 public void F()
 {
  N();
 }//end of F
 
 public void N()
 {
  if(((sym>='a') && (sym<='z')) ||((sym>='A') && (sym<='Z')))
  scaner();   
 }//end of N
 
 public void error()
 {
  txf1.setText("不正确");
 }
 
}//end of class

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -