首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Eclipse开发 >

有没有帮帮小弟我这个新手

2012-06-28 
有没有帮帮我这个新手C语言词法分析算法设计与实现编制一个读单词过程,从输入的源程序中,识别出各个具有独

有没有帮帮我这个新手
C语言词法分析算法设计与实现编制一个读单词过程,从输入的源程序中,识别出各个具有独立意义的单词,即基本保留字、标识符、常数、运算符、分隔符五大类。
输入:一段C语言程序
输出:每个单词以及每个单词所在行号
比如:输入如下一段C程序
main()  
{
int a,b;
}
输出为:
( main, ”line=1”);
( ( , ”line=1“);
( ) , ”line=1”);
({ , ”line=2” );
( int, “line=2”);
……………用Java语言,或者C语言,推荐用Java语言。完成所要有的C语言词法分析器。
要求:读文件,或者命令行的形式读取C源程序,输出源程序中每个单词以及每个单词所在行号。
要求:开发出图形化界面,读文件,把结果输出到界面上。

package test;

import java.io.*;
public class WordAnalysis {

  private String Keyword[]={"if","int","for","while","do","return","break","continue","main","printf","scanf"};
  public String filename;
  StringBuffer buffer = new StringBuffer(); 
  public WordAnalysis(String filename) {
  this.filename=filename; 
  }
   
  boolean isKeyword(String ch){
  for(int i=0;i<Keyword.length;i++)
  if(Keyword[i].equals(ch))
  return true;
  return false;  
  }
  boolean isDigit(char ch){
  if(ch>47&&ch<58)
  return true;
  else
  return false; 
  }
  boolean isLeter(char ch){
  if((ch>64&&ch<91)||(ch>96&&ch<123))
  return true;
  else
  return false; 
  }
  public void readFile()throws FileNotFoundException{
  try{
  FileReader fr = new FileReader(this.filename);  
  BufferedReader br = new BufferedReader(fr);  
  String temp = null;  
  while((temp = br.readLine()) != null)
  {  
  buffer.append(temp); 
  }  
  } 
  catch (Exception e) {
  System.out.println("文件操作错误:" + e.toString());
  } 
   
  }
  public void Analysis(){
  for(int n=1;n<50;n++)
  {
  int i=0;
  char ch;
  while(i<buffer.length()){
  ch=buffer.charAt(i);
  if(isLeter(ch))
  {
  StringBuffer temp=new StringBuffer() ;
  temp.append(ch);
  ch=buffer.charAt(++i);
  while(isLeter(ch)||isDigit(ch))
  {
  temp.append(ch);
  ch=buffer.charAt(++i); 
  }
  if(isKeyword(temp.toString()))
  System.out.println("( line="+n+",“ "+temp+" ”)");
  else
  System.out.println("( line="+n+",“ "+temp+" ”)");
  }
  else if((ch==';')||(ch==',')||(ch=='(')||(ch==')')||(ch=='{')||(ch=='}')||(ch=='"'))//判断是否为分隔符
  {
  System.out.println("( line="+n+",“ "+ch+" ”)");
  i++;
  }
  else if(isDigit(ch))
  {
  StringBuffer temp=new StringBuffer();
  while(isDigit(ch))
  {
  temp.append(ch);
  ch=buffer.charAt(++i); 
  }
  System.out.println("( line="+n+",“ "+temp+" ”)");
  }
  else
  {  


  if((ch!=' '))
  { 
  StringBuffer temp=new StringBuffer();
  temp.append(ch);
  ch=buffer.charAt(++i);
  if(ch=='=')
  {
  temp.append(ch);
  i++;
  } 
  System.out.println("( line="+n+",“ "+temp+" ”)");
  }
  else
  i++;
   
  } 
  }
   
  }
  } 

public static void main(String[] args)throws IOException {
  // TODO code application logic here
  WordAnalysis fr=new WordAnalysis("c://1.txt");
  fr.readFile();
  fr.Analysis();
}
}
没有错误,但是运行的结果就是不对,看了好多还是不会啊,求指点

[解决办法]

Java code
import java.io.BufferedReader;import java.io.FileNotFoundException;import java.io.FileReader;import java.io.IOException;import java.util.ArrayList;import java.util.List;public class WordAnalysis {    private String Keyword[] = { "if", "int", "for", "while", "do", "return",            "break", "continue", "main", "printf", "scanf" };    public String filename;//    StringBuffer buffer = new StringBuffer();    List list = new ArrayList() ; //用List来存放每一行代码    public WordAnalysis(String filename) {        this.filename = filename;    }    boolean isKeyword(String ch) {        for (int i = 0; i < Keyword.length; i++)            if (Keyword[i].equals(ch))                return true;        return false;    }    boolean isDigit(char ch) {        if (ch > 47 && ch < 58)            return true;        else            return false;    }    boolean isLeter(char ch) {        if ((ch > 64 && ch < 91) || (ch > 96 && ch < 123))            return true;        else            return false;    }    public void readFile() throws FileNotFoundException {        try {            FileReader fr = new FileReader(this.filename);            BufferedReader br = new BufferedReader(fr);            String temp = null;                        while ((temp = br.readLine()) != null) {                list.add(temp) ;//把每一行代码放入list中                //buffer.append(temp); //用StringBuffer只是追加连接,当然得不到每一行的啦 ,会把所有的代码都放到一行上            }        } catch (Exception e) {            System.out.println("文件操作错误:" + e.toString());        }    }    public void Analysis() {        System.out.println("-----analysis begin -----");        String str = "" ;        for (int n = 0; n < list.size(); n++) {            int i = 0;            char ch;            str = (String)list.get(n) ;  //取出每一行的代码            while (i < str.length()) {                ch = str.charAt(i);                if (isLeter(ch)) {                    StringBuffer temp = new StringBuffer();                    temp.append(ch);                    ch = str.charAt(++i);                    while (isLeter(ch) || isDigit(ch)) {                        temp.append(ch);                        ch = str.charAt(++i);                    }                    if (isKeyword(temp.toString()))                        System.out.println("( line=" +  (n+1 ) + ",“ " + temp + " ”)");                    else                        System.out.println("( line=" +  (n+1 ) + ",“ " + temp + " ”)");                } else if ((ch == ';') || (ch == ',') || (ch == '(')                        || (ch == ')') || (ch == '{') || (ch == '}')                        || (ch == '"'))// 判断是否为分隔符                {                    System.out.println("( line=" + (n+1 ) + ",“ " + ch + " ”)");                    i++;                } else if (isDigit(ch)) {                    StringBuffer temp = new StringBuffer();                    while (isDigit(ch)) {                        temp.append(ch);                        ch = str.charAt(++i);                    }                    System.out.println("( line=" +  (n+1 ) + ",“ " + temp + " ”)");                } else {                    if ((ch != ' ')) {                        StringBuffer temp = new StringBuffer();                        temp.append(ch);                        ch = str.charAt(++i);                        if (ch == '=') {                            temp.append(ch);                            i++;                        }                        System.out.println("( line=" +  (n+1 ) + ",“ " + temp + " ”)");                    } else                        i++;                }            }        }        System.out.println("-----analysis end -----");    }    public static void main(String[] args) throws IOException {        // TODO code application logic here        WordAnalysis fr = new WordAnalysis("c://1.txt");        fr.readFile();        fr.Analysis();    }} 

热点排行