1、编译原理 词法分析package 词法分析;import java.awt.FileDialog;import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.File;import java.io.FileReader;import java.io.FileWriter;import java.util.Stack;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JTextArea;import javax.s
2、wing.JTextField;import 词法分析.key;import 词法分析.operator;import 词法分析.symbol;public class function extends JFramestatic final long serialVersionUID =1L; int start,end,i=0,j=0,line=0,flag=0,flag1=0; Stack e=new Stack();/()括号的栈 Stack e1=new Stack();/的栈 Stack e2=new Stack();/的栈 char c,c8; Lexical_Frame f1 =
3、new Lexical_Frame(5); JTextArea f11=new JTextArea(); key k=new key(); operator op=new operator(); symbol sy=new symbol(); String str=,currut=,token=; FileDialog open,save; JTextField string_Rearch,string_Replace; JButton btn_Search,btn_Search_Next,btn_Replace,btn_Replace_All; function() ; public Str
4、ing op(JTextArea file1,Lexical_Frame t,JTextArea file3) open=new FileDialog(t,打开,FileDialog.LOAD); open.setVisible(true); try File f12=new File(open.getDirectory(),open.getFile(); t.frame.setTitle(+open.getDirectory()+open.getFile(); FileReader fr=new FileReader(f12); BufferedReader br=new BufferedR
5、eader(fr); while(str=br.readLine()!=null) line+; j=str.length(); file1.append(line+ +str+n); for(i=0;ij;i+) c=str.charAt(i); flag=0; currut=;token=; if(c=32) continue; if(str.charAt(i)=/&(str.charAt(i+1)=*|str.charAt(i+1)=/) if(str.charAt(i)=/&str.charAt(i+1)=*) flag1=1; else break; if(flag1=1) for(
6、i=0;ij;i+) if(str.charAt(i)=*&str.charAt(i+1)=/) flag1=0; i=i+2; if(ij) if(is_identifiers_key(file3) continue; if(is_digital(file3) continue; if(is_oper(file3) continue; if(is_sym_String(file3) continue; if(c255) f11.append(存在非法字符:+c+在第+line+行+n); if(flag1=1) System.out.println(未找到注释结尾符号); if(!e.emp
7、ty() f11.append(少了 )括号+n); if(!e1.empty() f11.append(少了 括号+n); if(!e2.empty() f11.append(少了 括号+n); t.filee.setText(f11.getText(); fr.close(); catch(Exception e1) file1.setVisible(true); return open.getDirectory()+open.getFile(); public boolean is_identifiers_key(JTextArea file3) if(flag1=0&(str.char
8、At(i)=65&str.charAt(i)=97&str.charAt(i)=122)|str.charAt(i)=_) c=(str.charAt(i); while(i=48&str.charAt(i)=57) while(i=48&str.charAt(i)=57)|str.charAt(i)=.) c=(str.charAt(i); currut=Character.toString(str.charAt(i); token=token+currut; i+; file3.append(+token+,+数字常量+,+line+行+)+n); /c=(str.charAt(i); t
9、oken=1; flag=1; i-; return true; return false; public boolean is_oper(JTextArea file3) if(flag1=0&op.isoperator(str.charAt(i) while(ij&op.isoperator(str.charAt(i) c=(str.charAt(i); currut=Character.toString(str.charAt(i); i+; if(c=(|c=|c=)|c=|c=,) if(c=() e.push(c); if(c=) if(!e.empty() e.pop(); els
10、e System.out.println(少了 ( 括号); if(c=) e1.push(c); if(c=) if(!e1.empty() e1.pop(); else f11.append(少了 括号+line+n); file3.append(+c+,+操作运算符+,+line+行+)+n); else token=token+currut; if(token.equals(+)|token.equals(-) break; if(!token.equals() if(op.is_leagl_oper(token) file3.append(+token+,+操作运算符+,+line+
11、行+)+n); else f11.append(第+line+行+i+列存在非法操作运算符+token); i-; token=1; flag=1; return true; return false; public boolean is_sym_String(JTextArea file3) if(flag1=0&sy.is_sym(str.charAt(i) c=(str.charAt(i); if(str.charAt(i)=|str.charAt(i)=) c=str.charAt(i); file3.append(+,+界定字符+,+line+行+)+n); i+; while(ij
12、&str.charAt(i)!=&str.charAt(i)!=) if(str.charAt(i)=&str.charAt(i+1)=n)|(str.charAt(i)=%&(str.charAt(i+1)=f|str.charAt(i+1)=d|str.charAt(i+1)=c|str.charAt(i+1)=s) i=i+2; else currut=Character.toString(str.charAt(i); token=token+currut; i+; file3.append(+token+,+字符串常量+,+line+行+)+n); file3.append(+c+,+
13、界定字符+,+line+行+)+n); /i+; flag=1; else if(str.charAt(i)=) e2.push(str.charAt(i); if(str.charAt(i)=) if(!e2.empty() e2.pop(); else f11.append(少了 括号+n); file3.append(+str.charAt(i)+,+界定字符+,+line+行+)+n); flag=1; return true; return false; public void sv(JTextArea file1,Lexical_Frame t) save=new FileDial
14、og(t,另存为,FileDialog.SAVE); save.setVisible(true); try File f1=new File(save.getDirectory(),save.getFile(); FileWriter fw=new FileWriter(f1); BufferedWriter bw=new BufferedWriter(fw); String gt=file1.getText(); String lines = gt.split(n); for (int j = 0; j lines.length; j+) bw.write(linesj + rn); bw.
15、flush(); fw.close(); catch ( Exception e2) package 词法分析;public class key int i=0; String key_Word=new String32; public key() key_Word0=new String(auto); key_Word1=new String(break); key_Word2=new String(case); key_Word3=new String(char); key_Word4=new String(const); key_Word5=new String(continue); k
16、ey_Word6=new String(default); key_Word7=new String(do); key_Word8=new String(double); key_Word9=new String(else); key_Word10=new String(enum); key_Word11=new String(extern); key_Word12=new String(float); key_Word13=new String(for); key_Word14=new String(goto); key_Word15=new String(if); key_Word16=n
17、ew String(int); key_Word17=new String(long); key_Word18=new String(register); key_Word19=new String(return); key_Word20=new String(short); key_Word21=new String(signed); key_Word22=new String(static); key_Word23=new String(sizeof); key_Word24=new String(struct); key_Word25=new String(switch); key_Wo
18、rd26=new String(typedef); key_Word27=new String(union); key_Word28=new String(unsigned); key_Word29=new String(void); key_Word30=new String(volatile); key_Word31=new String(while); public int iskey(String s) for(i=0;i32;i+) if(s.equalsIgnoreCase(key_Wordi) return 1; return 0; package 词法分析;import jav
19、a.awt.BorderLayout;import java.awt.Choice;import java.awt.Color;import java.awt.Font;import java.awt.GridLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.ItemEvent;import java.awt.event.ItemListener;import java.awt.event.TextEvent;import java.awt.ev
20、ent.TextListener;import javax.swing.JButton;import javax.swing.JDialog;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JMenu;import javax.swing.JMenuBar;import javax.swing.JMenuItem;import javax.swing.JPanel;import javax.swing.JScrollPane;import javax.swing.JTextArea;import 词法
21、分析.function;SuppressWarnings(serial)public class Lexical_Frame extends JFrame implements ActionListener ,ItemListener,TextListener JButton sure=new JButton(是);JButton not=new JButton(否);JDialog Dg; JMenuBar text_MenuBar; JMenu menFile,menHlep,edit; JMenuItem file_open,file_Close,file_Exit,file_Save;
22、 JMenuItem contact_Us,about; JMenuItem font1,erro; Choice font,color,style; JLabel f; String s=,t; JFrame frame; int i=0,j,k=0; JTextArea file=new JTextArea(); JTextArea file1=new JTextArea(); JTextArea file2=new JTextArea(); JTextArea file10=new JTextArea(); JFrame frame1 = new JFrame(); JTextArea
23、filee=new JTextArea(); JScrollPane scrollee=new JScrollPane(filee); JScrollPane scroll2=new JScrollPane(file10); JScrollPane scroll=new JScrollPane(file); JScrollPane scroll1=new JScrollPane(file2); public Lexical_Frame(int i) public Lexical_Frame() frame = new JFrame(); frame.setResizable(false); f
24、rame.setTitle(词法分析程序); file.setLineWrap(true); frame.add(scroll1,BorderLayout.CENTER); frame.add(scroll,BorderLayout.WEST); menFile=new JMenu(文件); edit=new JMenu(编辑); font1=new JMenuItem(字体); menHlep=new JMenu(帮助); file_open=new JMenuItem(打开); erro=new JMenuItem(查看错误文件); file_Close=new JMenuItem(关闭); file_Exit=new JMenuItem(退出); file_Save=new JMenuItem(保存); contact_Us=new JMenuItem(返回源文件); about=new JMenuItem(查看帮助); text_MenuBar=new JMenuBar(); menFile.add(file_open); menFile.add(fi
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1