您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 质量控制/管理 > C语言词法分析程序java版
1附录:核心代码清单C语言词法分析代码算法实习--------------------------------------------------------ByNLLWRQ功能类:packageconversion;importjava.io.*;publicclassWork{publicStringfilename;publicintflage=1;publicinterror[]={0,0,0,0,0};publicinterror1[]={0,0,0,0,0,0};StringBufferbuffer=newStringBuffer();privateStringKeychar[]={int,char,long,float,double,short,unsigned,struct,union,enum,auto,extern,static,register,typedef,void,if,else,switch,case,default,do,while,for,break,continue,return,goto,define,include,undef,ifdef,endif,line,sizeof,asm,fortran,ada,pascal};privateStringKeyfile[]={stdio,windows,stdlib,ctype,math,time,string};publicWork(Stringfilename){this.filename=filename;}booleanisKeyfile(Stringch){for(inti=0;iKeyfile.length;i++)if(Keyfile[i].equals(ch))returntrue;returnfalse;2}booleanisKeychar(Stringch){for(inti=0;iKeychar.length;i++)if(Keychar[i].equals(ch))returntrue;returnfalse;}booleanisDigit(charch){if(ch='0'&&ch='9')returntrue;elsereturnfalse;}booleanisLeter(charch){if((ch='A'&&ch='Z')||(ch='a'&&ch='z')||ch=='_')returntrue;elsereturnfalse;}booleanisendline(charch){if(ch=='@')returntrue;elsereturnfalse;}publicvoidreadFile()throwsFileNotFoundException{try{FileReaderfr=newFileReader(this.filename);3BufferedReaderbr=newBufferedReader(fr);Stringtemp=null;charend='@';while((temp=br.readLine())!=null){buffer.append(temp);buffer.append(end);}}catch(Exceptione){System.out.println(文件操作错误:+e.toString());}}publicStringAnalysis(){inti=0;charch;Stringstr1=;ViewFilea=null;while(ibuffer.length()){ch=buffer.charAt(i);if(isLeter(ch)){StringBuffertemp=newStringBuffer();temp.append(ch);ch=buffer.charAt(++i);while(isLeter(ch)||isDigit(ch)){temp.append(ch);ch=buffer.charAt(++i);}if(isKeyfile(temp.toString())){4ch=buffer.charAt(i++);if(ch!='.')error[2]++;else{temp.append(ch);ch=buffer.charAt(i++);if(ch!='h'){error[3]++;}elsetemp.append(ch);}str1+=(+temp+,+“+line=+flage+”+)+\t是头文件+\n;}elseif(isKeychar(temp.toString()))str1+=(+temp+,+“+line=+flage+”+)+\t是关键字+\n;elsestr1+=(+temp+,+“+line=+flage+”+)+\t是标识符+\n;}elseif((ch=='[')||(ch==']')){str1+=(+ch+,+”+line=+flage+“+)+\t是运算符+\n;i++;if(ch=='[')error[0]++;elseerror[1]++;}5elseif(ch=='.'){str1+=(+ch+,+”+line=+flage+“+)+\t是运算符+\n;i++;}elseif((ch=='{')||(ch=='}')){str1+=(+ch+,+”+line=+flage+“+)+\n;i++;if(ch=='{')error1[0]++;elseerror1[1]++;}elseif((ch=='(')||(ch==')')){str1+=(+ch+,+”+line=+flage+“+)+\n;i++;if(ch=='(')error1[2]++;elseerror1[3]++;}elseif((ch==';')||(ch==',')||(ch=='')){if(ch==','){str1+=(+ch+,+”+line=+flage+“+)+\t是分隔符+\n;i++;}6elseif(ch==';'){error1[4]++;str1+=(+ch+,+”+line=+flage+“+)+\t是分隔符+\n;i++;}elseif(ch==''){error1[5]++;str1+=(+ch+,+”+line=+flage+“+)+\n;i++;}}elseif(isDigit(ch)){StringBuffertemp=newStringBuffer();while(isDigit(ch)){temp.append(ch);ch=buffer.charAt(++i);}str1+=(+temp+,+”+line=+flage+”+)+\t是数字+\n;}elseif(isendline(ch)){flage++;i++;}else{if((ch!='')){StringBuffertemp=newStringBuffer();if(ch=='+'){temp.append(ch);ch=buffer.charAt(++i);7if(ch=='+'){temp.append(ch);i++;}if(ch=='='){temp.append(ch);i++;}}elseif(ch=='-'){temp.append(ch);ch=buffer.charAt(++i);if(ch=='-'){temp.append(ch);i++;}if(ch==''){temp.append(ch);i++;}if(ch=='='){temp.append(ch);i++;}}elseif(ch=='*')8{temp.append(ch);ch=buffer.charAt(++i);if(ch=='*'){temp.append(ch);i++;}if(ch=='='){temp.append(ch);i++;}}elseif(ch=='/'){temp.append(ch);ch=buffer.charAt(++i);if(ch=='/'){temp.append(ch);i++;}if(ch=='='){temp.append(ch);i++;}}elseif(ch=='%'){temp.append(ch);9ch=buffer.charAt(++i);if(ch=='%'){temp.append(ch);i++;}if(ch=='='){temp.append(ch);i++;}}elseif(ch=='&'){temp.append(ch);ch=buffer.charAt(++i);if(ch=='&'){temp.append(ch);i++;}if(ch=='='){temp.append(ch);i++;}}elseif(ch=='^'){temp.append(ch);ch=buffer.charAt(++i);if(ch=='^')10{temp.append(ch);i++;}if(ch=='='){temp.append(ch);i++;}}elseif(ch=='|'){temp.append(ch);ch=buffer.charAt(++i);if(ch=='|'){temp.append(ch);i++;}if(ch=='='){temp.append(ch);i++;}}elseif(ch==''){temp.append(ch);ch=buffer.charAt(++i);if(ch==''){temp.append(ch);11ch=buffer.charAt(++i);if(ch=='='){temp.append(ch);i++;}}elseif(ch=='='){temp.append(ch);i++;}}elseif(ch==''){temp.append(ch);ch=buffer.charAt(++i);if(ch==''){temp.append(ch);ch=buffer.charAt(++i);if(ch=='='){temp.append(ch);i++;}}elseif(ch=='='){temp.append(ch);i++;}}else12{temp.append(ch);ch=buffer.charAt(++i);if(ch=='='){temp.append(ch);i++;}}str1+=(+temp+,+”+line=+flage+“+)+\t是运算符+\n;}elsei++;}}returnstr1;}}界面实现:packageconversion;importjava.awt.Dimension;importjava.awt.Font;importjava.awt.event.ActionEvent;importjava.awt.event.ActionListener;importjava.io.BufferedReader;importjava.io.BufferedWriter;importjava.io.File;importjava.io.FileNotFoundException;importjava.io.FileReader;importjava.io.FileWriter;importjava.io.IOException;importjavax.swing.JButton;importjavax.swing.JFileChooser;importjavax.swing.JFrame;importjavax.swing.JLabel;13importjavax.swing.JMenu;importjavax.swing.JMenuBar;importjavax.swing.JMenuItem;importjavax.swing.JOptionPane;importjavax.swing.JScrollPane;importjavax.swing.JTextArea;importjavax
本文标题:C语言词法分析程序java版
链接地址:https://www.777doc.com/doc-3970506 .html