实验1《词法分析程序设计与实现》一、实验目的加深对词法分析器的工作过程的理解;加强对词法分析方法的掌握;能够采用一种编程语言实现简单的词法分析程序;能够使用自己编写的分析程序对简单的程序段进行词法分析。二、实验内容自定义一种程序设计语言,或者选择已有的一种高级语言,编制它的词法分析程序。词法分析程序的实现可以采用任何一种编程语言和编程工具。从输入的源程序中,识别出各个具有独立意义的单词,即关键字、标识符、常数、运算符、界符。并依次输出各个单词的内部编码及单词符号自身值。(遇到错误时可显示“Error”,然后跳过错误部分继续显示)三、实验方法通过自定义一组符号表,存储到属性文件中,然后使用一些if和else语句来判断所获取的字符的类型,并输出相应的码。四、实验步骤1.定义目标语言的可用符号表和构词规则;2.依次读入源程序符号,对源程序进行单词切分和识别,直到源程序结束;3.对正确的单词,按照它的种别以种别码,值的形式保存在符号表中;4.对不正确的单词,做出错误处理。五、实验结果项目截图:六、实验结论源代码如下:这是Lexer类:package词法分析程序;importjava.awt.BorderLayout;importjava.awt.Dimension;importjava.awt.Font;importjava.awt.Graphics;importjava.awt.event.ActionEvent;importjava.awt.event.ActionListener;importjava.awt.image.BufferedImage;importjava.io.File;importjava.io.FileNotFoundException;importjava.io.FileOutputStream;importjava.io.IOException;importjava.util.Properties;importjavax.swing.BorderFactory;importjavax.swing.ImageIcon;importjavax.swing.JButton;importjavax.swing.JFrame;importjavax.swing.JPanel;importjavax.swing.JScrollPane;importjavax.swing.JTextArea;publicclassLexer{privatestaticStringTITLE=欢迎使用本系统!;booleanflag=false;intx=-250,y=50;privateintcode=0;privatechar[]ch;privateStringstrToken=;privateStringBuffersb=newStringBuffer(strToken);privateJTextAreainputArea,statusArea;/***构造器创建swing操作界面*/publicLexer(){//创建符号表,初次运行时请执行下面的函数//createProperty();JFrameframe=newJFrame(词法分析器);frame.setDefaultCloseOperation(3);frame.setSize(500,550);frame.setLocationRelativeTo(null);finalJPanelnorth=newJPanel();north.setPreferredSize(newDimension(0,100));inputArea=newJTextArea();inputArea.setFont(newFont(,Font.BOLD,18));inputArea.setBorder(BorderFactory.createTitledBorder(代码框));JScrollPanejs1=newJScrollPane(inputArea);statusArea=newJTextArea();statusArea.setEditable(false);statusArea.setBorder(BorderFactory.createTitledBorder(状态框));statusArea.setFont(newFont(,Font.PLAIN,15));JScrollPanejs2=newJScrollPane(statusArea);js2.setPreferredSize(newDimension(150,500));JPaneljPanel=newJPanel();JButtonstart=newJButton(开始分析);JButtonclear=newJButton(清空);jPanel.add(start);jPanel.add(clear);frame.add(north,BorderLayout.NORTH);frame.add(js1,BorderLayout.CENTER);frame.add(js2,BorderLayout.EAST);frame.add(jPanel,BorderLayout.SOUTH);frame.setVisible(true);finalGraphicsg=north.getGraphics();newThread(){publicvoidrun(){BufferedImagebf=newBufferedImage(north.getWidth(),north.getHeight(),BufferedImage.TYPE_INT_RGB);Graphicsgg=bf.getGraphics();gg.setFont(newFont(华文行楷,Font.ITALIC,25));while(true){try{Thread.sleep(10);}catch(InterruptedExceptionex){ex.printStackTrace();}if(flag){continue;}gg.drawImage(newImageIcon(images/sky.jpg).getImage(),0,0,null);gg.drawString(TITLE,x,y);x++;if(xnorth.getWidth())x=-250;g.drawImage(bf,0,0,null);}}}.start();start.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){if(inputArea.getText().isEmpty()){statusArea.setText(请在左边输入代码!);}else{statusArea.setText();initLexer(inputArea.getText());}}});clear.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){inputArea.setText();statusArea.setText();}});}/****@paramsrc*/publicvoidinitLexer(Stringsrc){//去掉大多无用的换行,减少循环次数src=src.replace('\n','\0');src=src.replace('\t','\0');ch=src.toCharArray();intlen=ch.length;for(inti=0;ilen;i++){while(ch[i]==''){i++;}if(isLetter(ch[i])){while(ilen&&(isLetter(ch[i])||isDigit(ch[i]))){sb.append(ch[i]);i++;}i--;code=reserve(sb);if(code==0){statusArea.append(1,+insertId(sb)+\n);}else{statusArea.append(+code+,+insertId(sb)+\n);}clear();}elseif(isDigit(ch[i])){while(isDigit(ch[i])){sb.append(ch[i]);i++;}i--;statusArea.append(2,+insertConst(sb)+\n);clear();}elseif(ch[i]=='+')statusArea.append(3,+\n);elseif(ch[i]=='-')statusArea.append(4,-\n);elseif(ch[i]=='*')statusArea.append(5,*\n);elseif(ch[i]=='/')statusArea.append(6,/\n);elseif(ch[i]=='=')statusArea.append(7,=\n);elseif(ch[i]==',')statusArea.append(8,,\n);elseif(ch[i]=='(')statusArea.append(9,(\n);elseif(ch[i]==')')statusArea.append(10,)\n);elseif(ch[i]=='{')statusArea.append(11,{\n);elseif(ch[i]=='}')statusArea.append(12,}\n);elseif(ch[i]=='[')statusArea.append(13,[\n);elseif(ch[i]==']')statusArea.append(14,]\n);elseif(ch[i]==';')statusArea.append(15,;\n);elseif(ch[i]=='.')statusArea.append(16,.\n);elseprocError(i);}}privatevoidclear(){this.sb=newStringBuffer();}privatevoidprocError(inti){statusArea.append(ch[i]+不在符号表中!\n);}privateStringinsertConst(StringBuffersb){Stringcontent=newString(sb);returncontent;}privateStringinsertId(StringBuffersb){Stringcontent=newString(sb);returncontent;}privateintreserve(StringBuffersb){Stringcontent=newString(sb);PropertieschartPro=newProperties();Filefile=newFile(Chart.properties);LexerUtil.loadPro(chartPro,file);if(chartPro.containsKey(content)){returnInteger.parseInt(chartPro.getProperty(content));}return0;}privatebooleanisDigit(charch){if(String.valueOf(ch).isEmpty()){returnfalse;}elseif(ch='0'&&ch='9'){returntrue;}returnfalse;}privatebooleanisLetter(charch){if(String.valueOf(ch).isEmpty()){returnfalse;}elseif(ch='a'&&