<pre name="code" class="java">import java.awt.*; import java.awt.event.*; import java.io.*; import javax.swing.*; public class WordAnalysis extends JFrame{ FileInputStream fis; JFileChooser jfc; JButton jb1; JButton jb2; JButton jb3; final JTextArea jt1 = new JTextArea(15,35); final JTextArea jt2 = new JTextArea(15,35); /*关键字*/ String key[] = {"if","then","else","while","do"}; /* 定义分界符 */ char border[] = { '+', '-', '*', '/', '>', '<', '=', '(', ')', ';' }; String border1[]={ "+", "-", "*", "/", ">", "<", "=", "(", ")", ";" }; /* 定义关系运算符 */ public WordAnalysis(){ init(); } public void init(){ this.setTitle("WordAnalysis @author xjp"); this.setSize(450,650); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); jt1.setLineWrap(true); jt2.setLineWrap(true); //jt1 = new JTextArea(10,30); //jt2 = new JTextArea(10,30); jt1.setLineWrap(true); jt2.setLineWrap(true); JScrollPane scrol1 = new JScrollPane(jt1); JScrollPane scrol2 = new JScrollPane(jt2); jb1 = new JButton("开始词法分析"); jb2 = new JButton("导入文件"); jb3 = new JButton("清除文本框"); JPanel jp = new JPanel(); jp.add(scrol1); jp.add(jb1); jp.add(jb2); jp.add(jb3); jp.add(scrol2); this.getContentPane().add(jp); this.setVisible(true); jb1.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent arg0) { analysis(); } }); jb2.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent arg0) { try { jfc=new JFileChooser();//文件选择器 jfc.showDialog(null, "文件选择"); fis = new FileInputStream(jfc.getSelectedFile().toString()); byte[] args = new byte[10000]; int len=fis.read(args); System.out.println(new String(args,0,len)); jt1.setText(""); jt1.setText(new String(args,0,len)); } catch (Exception e) { e.printStackTrace(); } } }); jb3.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent arg0) { jt1.setText(""); jt2.setText(""); } }); } /**词法分析*/ public void analysis(){ if(jt2.getText()!=null){ jt2.setText("结果如下:\n"); } String text= jt1.getText(); String text1=""; int flag=1; for(int i=0;i<text.length();i++){ flag=1; for(int j=0;j<border.length;j++){ if(text.charAt(i)==border[j]){ flag=0; text1=text1+" "+text.charAt(i)+" "; break; } } if (flag == 1) { text1 = text1 + text.charAt(i); } } System.out.println(text1); /** 以空格分隔字符串 (split方法参数为正则表达式)*/ String s[]=text1.split(",|\\s|\\.|/n|:"); for(int i=0;i<s.length;i++){ System.out.println("--"+s[i]+"--"); if(!isKeyWord(s[i])){ if(!isBorder(s[i])){ if(!isNumber1(s[i])){ if(!isNumber2(s[i])){ if(!isNumber3(s[i])){ isIdentifier( s[i]); } } } } } } System.out.println("----"); } public boolean isKeyWord(String s){ for(int i1=0;i1<key.length;i1++){ if(s.equals(key[i1])){ jt2.append("<"+s+" , ->\n"); return true; } } return false; } public boolean isNumber1(String s){ if(s.equals("")) return false; if(s.matches("\\d")){ jt2.append("< 1 , "+s+" >\n"); return true;} if(s.charAt(0)!='0') if(s.matches("\\d*")){ jt2.append("< 1 , "+s+" >\n"); return true; } return false; } public boolean isNumber2(String s){ if(s.equals("")) return false; if(s.charAt(0)=='0'){ s=s.substring(1, s.length()); if(s.matches("\\d")){ jt2.append("< 2 , "+s+" >\n"); return true;} if(s.charAt(0)!='0') if(s.matches("\\d*")){ jt2.append("< 2 , "+s+" >\n"); return true; } } return false; } public boolean isNumber3(String s){ if(s.equals("")) return false; if(s.charAt(0)=='0'&&s.charAt(1)=='x'){ s=s.substring(2, s.length()); jt2.append("< 3 , "+s+" >\n"); return true; } //} return false; } public boolean isBorder(String s){ for(int i1=0;i1<border1.length;i1++){ if(s.equals(border1[i1])){ //jt2.append(s+"是分界符和运算符"+"\n"); jt2.append("<"+s+" , ->\n"); return true; } } return false; } public boolean isIdentifier(String s) { //如果字符串为空或者长度为0,返回false if(s == null || s.length() == 0) { return false; } //字符串中每一个字符都必须是java标识符的一部分 for(int i = 0; i < s.length(); i++){ if (!Character.isJavaIdentifierPart(s.charAt(i))) { return false; } } jt2.append("< 0 , "+s+" >\n"); return true; } public static void main(String []args){ WordAnalysis wordAnalysis=new WordAnalysis(); } }
java 逆波兰算法
最新推荐文章于 2024-09-15 19:46:31 发布