




已阅读5页,还剩13页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
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.swing.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 =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 String op(JTextArea file1,Lexical_Frame t,JTextArea file3)open=new FileDialog(t,打开,FileDialog.LOAD);open.setVisible(true);tryFile f12=new File(open.getDirectory(),open.getFile();t.frame.setTitle(+open.getDirectory()+open.getFile();FileReader fr=new FileReader(f12);BufferedReader br=new BufferedReader(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(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.empty()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.charAt(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);token=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(); else 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+行+)+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&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;elsecurrut=Character.toString(str.charAt(i);token=token+currut;i+;file3.append(+token+,+字符串常量+,+line+行+)+n);file3.append(+c+,+界定字符+,+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 FileDialog(t,另存为,FileDialog.SAVE);save.setVisible(true);tryFile 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.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);key_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=new 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_Word26=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 java.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.event.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 词法分析.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;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 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); frame.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(file_Save); menFile.add(file_Close); menFile.add(file_Exit); menHlep.add(contact_Us); menHlep.add(about); edit.add(font1); edit.add(erro); file_Save.setEnabled(false); edit.setEnabled(false); file_open.addActionListener(this); file_Save.addActionListener(this); file_Close.addActionListener(this); sure.addActionListener(this); not.addActionListener(this); file_Exit.addActionListener(this); font1.addActionListener(this); erro.addActionListener(this); about.addActionListener(this); contact_Us.addActionListener(this); menHlep.add(contact_Us); text_MenuBar.add(menFile); text_MenuBar.add(edit); text_MenuBar.add(menHlep); frame.setJMenuBar(text_MenuBar); frame.setSize(600,600); file.setSize(300, 300); file2.setSize(300, 300); file.setVisible(false); file2.setVisible(false); file10.setVisible(false); frame.setVisible(true);public void actionPerformed(ActionEvent e) function function1=new function();frame1.add(scrollee,BorderLayout.CENTER); file.setForeground(Color.red) ; file2.setForeground(Color.red) ;if(e.getActionCommand()=打开)file.setText();file2.setText(); i=0; s=function1.op(file, this,file2); file.setVisible(true); file2.setVisible(true); file_Save.setEnabled(true); edit.setEnabled(true);if(e.getActionCommand()=退出) if(file1.getText().equalsIgnoreCase(file2.getText()=false) Dg=new JDialog(frame,确认,false);JPanel p=new JPanel();p.add(sure);p.add(not);JLabel prompt=new JLabel(希望把结果保存到文件吗?);Dg.setLayout(new BorderLayout();Dg.add(prompt,BorderLayout.CENTER);Dg.add(p,BorderLayout.SOUTH);Dg.setSize(200, 200);Dg.setVisible(true);elsefile2.setVisible(false);file.setVisible(false); if(e.getActionCommand().equals(是) Dg.setVisible(false);scroll.setVisible(false); function1.sv(file2, this); file1.setText(file2.getText(); if(e.getActionCommand().equals(否)file.setVisible(false);file2.setVisible(false);Dg.setVisible(false);file1.setText(file2.getText();if(e.getActionCommand().equals(查看错误文件)frame1.setSize(300, 300);frame1.setVisible(true);if(e.getActionCommand()=关闭) if(file1.getText().equalsIgnoreCase(file2.getText()=false)Dg=new JDialog(frame,确认,true);JPanel p=new JPanel();p.add(sure);p.add(not); JTextArea prompt=new JTextArea(文本已经被修改,希望把词法分析结果保存到文件吗);Dg.setLayout(new BorderLayout();Dg.add(prompt,BorderLayout.CENTER);Dg.add(p,BorderLayout.SOUTH);Dg.setSize(500, 200);Dg.setVisible(true); System.exit(0);if(e.getActionCommand()=保存)function1.sv(file2, this); file1.setText(file2.getText();if(e.getActionCommand().equals(字体) JDialog Dg1=new JDialog(frame,设置字体,false);font=new Choice(); color=new Choice(); style=new Choice(); JLabel size=new JLabel(字体大小); JLabel f=new JLabel(样式); JLabel cl=new JLabel(颜色); font.add(12); font.add(15); font.add(18); font.add(25); font.add(38); color.add(红色); color.add(蓝色); color.add(绿色); style.add(正常); style.add(粗体); style.add(斜体); Dg1.setLayout(new GridLayout(3,2,3,8); Dg1.add(size); Dg1.add(font); Dg1.add(f); Dg1.add(style); Dg1.add(cl); Dg1.add(color); font.addItemListener(this); style.addItemListener(this); color.addItemListener(this); Dg1.setSize(300, 200); Dg1.setVisible(true);if(e.getActionCommand()=返回源文件) scroll2.setVisible(false);scroll.setVisible(true);scroll1.setVisible(true);if(e.getActionCommand()=查看帮助)scroll.setVisible(false);scroll1.setVisible(false);scroll2.setVisible(true);int i=0; frame.add(scroll2,BorderLayout.CENTER);file10.setVisible(true);file10.append(关键字如下:+n);for(i=0;i32;i+)file10.append(function1.k.key_Wordi+n);file10.append(运算符如下:+n);for(i=0;i44;i+)file10.append(+function1.op.leagl_operi+n);file10.append(界定符如下:+n);for(i=0;i;oper_Word6=!;oper_Word7=;oper_Word8=+;oper_Word9=*;oper_Word10=&;oper_Word11=*;oper_Word12=%;oper_Word13=;oper_Word16=;oper_Word17=|;oper_Word18=,;oper_Word19=.;oper_Word20=:;oper_Word21=?;oper_Word22=/;leagl_oper0=new String();leagl_oper1=new String();leagl_oper2=new String();leagl_oper3=new String();leagl_oper4=new String(.);leagl_oper5=new String(-);leagl_oper6=new String(-);leagl_oper7=new String(+);leagl_oper8=new String(-);leagl_oper9=new String(*);leagl_oper10=new String(&);leagl_oper11=new String(!);leagl_oper12=new String();leagl_oper13=new String(/)
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 工业厂房的绿色改造方案与实践
- 工业4.0背景下的物联网产品设计趋势
- 嵌入式系统的发展与应用领域拓展
- 展馆设计的环保理念与可持续发展
- 山区峡谷桥梁的特殊结构设计
- 工业4.0时代下的机器视觉技术应用
- 工业4.0下的供应链管理变革
- 少数民族文化传承策略
- 展览工程与实施细节解析
- 少儿动漫产业品牌建设探讨
- 主扇风机操作员培训课件
- 2025年福建省龙岩市中考数学模拟卷(含答案)
- 高考英语读后续写:三大主题语境结尾金句
- 微信授权协议书范本
- 中国糖尿病足诊治指南(2024版)解读课件
- 场地合作分成协议合同
- 2025年中国高吸水性树脂行业市场发展现状研究及投资战略咨询报告
- 中职电子商务基础理论试题及答案
- 驾驶员保密管理制度培训
- 市政工程沟槽开挖与沟槽回填专项施工方案
- 2025年吉林长春市轨道交通集团有限公司招聘笔试参考题库含答案解析
评论
0/150
提交评论