pattern.compile帮助java程序

时间:2014-05-02 06:38:58

标签: java text-parsing matcher

我编写了一个程序来解析一个文本文件,其中包含一个带有ifelsewhile条件的示例C程序。

我有2个ArrayList,我的程序将解析整个文件。我使用Matcher并在String中指定了模式Pattern.compile()。我正在尝试为特定程序绘制控制流图;但是,我现在只是找到节点,稍后会将它们连接起来。

这是我的代码:

//import static LineMatcher.ENCODING;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.LineNumberReader;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public final class CFG {

  public void findLines(String aFileName) {
    List<Integer> a = new ArrayList<Integer>();
    List<Integer> b = new ArrayList<Integer>();
    // int [] a = new int[10000];
    // int [] b = new int[10000];
    Pattern regexp = Pattern.compile("if|else|while");
    Matcher exp1 = regexp.matcher("if");
    Matcher exp2 = regexp.matcher("else");
    Matcher exp3 = regexp.matcher("while");

    Path path = Paths.get(aFileName);
    try (BufferedReader reader = Files.newBufferedReader(path, ENCODING);
        LineNumberReader lineReader = new LineNumberReader(reader);) {
      String line = null;
      while ((line = lineReader.readLine()) != null) {
        // exp1.reset(line); //reset the input
        int counter = 1;
        if (exp1.find()) {
          int l = lineReader.getLineNumber();

          b.add(l);
        }
        if (exp2.find()) {
          int l = lineReader.getLineNumber();

          b.add(l);
        }
        if (exp3.find()) {
          int l = lineReader.getLineNumber();

          b.add(l);
        } else {
          int l = lineReader.getLineNumber();
          a.add(l);
        }
      }
      // counter++;

      System.out.println(a);
      System.out.println(b);
    }

    catch (IOException ex) {
      ex.printStackTrace();
    }
  }

  final static Charset ENCODING = StandardCharsets.UTF_8;

  public static void main(String... arguments) {
    CFG lineMatcher = new CFG();
    lineMatcher.findLines("C:Desktop\\test.txt");
  }
}

我在这里尝试的是,如果找到我的String,请在ArrayList b中输入行号,否则在ArrayList a中输入行号。因此,我知道,哪些行包含ifelsewhile语句。

我不知道我的代码是不正确的还是什么,输入文件如下:

#include <stdio.h>

int main()
{
  int i=1, sum = 0;  
  if( i = 1)  {
    sum += i;
  }  else
    printf("sum = %d\n", sum);

  return 0;
}

,程序的输出是:

run: 
[2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
[1, 1, 1]

PS:我是业余爱好者,这个程序在逻辑上可能不正确。

如果需要更多信息,请与我们联系。

编辑:

只对一个字符串搜索有效的代码:

Pattern regexp = Pattern.compile("if");
    Matcher matcher = regexp.matcher("if");


    Path path = Paths.get(aFileName);
    try (
      BufferedReader reader = Files.newBufferedReader(path, ENCODING);
      LineNumberReader lineReader = new LineNumberReader(reader);
    ){
      String line = null;
      while ((line = lineReader.readLine()) != null) {
       matcher.reset(line); //reset the input


       if(matcher.find())
       {

         int a= lineReader.getLineNumber();
         System.out.println(a);
                }

      }      
    }    
    catch (IOException ex){
      ex.printStackTrace();
    }

上面一个工作正常(它只是代码的一部分,而不是整个程序。程序与上面的程序相同)并返回找到if的行号。我使用了相同的逻辑并添加了else and while部分。

3 个答案:

答案 0 :(得分:1)

最后,我得到了这个工作(感谢惊人的输入)。以下是我所做的更改:

public void findLines(String aFileName) {

     List<Integer> a = new ArrayList<Integer>();
     List<Integer> b = new ArrayList<Integer>();

    Pattern regexp = Pattern.compile("(if|else|while).*");
    Matcher exp1 = regexp.matcher("if|else|while");
    Path path = Paths.get(aFileName);
        try (
          BufferedReader reader = Files.newBufferedReader(path, ENCODING);
          LineNumberReader lineReader = new LineNumberReader(reader);
        ){
          String line = null;
          while ((line = lineReader.readLine()) != null) {
          exp1.reset(line); 


            if(exp1.find())
            {
                int l= lineReader.getLineNumber();


                b.add(l);

                     }

            else
            {int l= lineReader.getLineNumber();
                  a.add(l);


               }   

          } 


        System.out.println(a);
        System.out.println(b);
                }


       catch (IOException ex){
         ex.printStackTrace();
        }

输入文件相同,输出为:

[1, 2, 3, 4, 6, 7, 8, 10, 11, 12, 13]
[5, 9]

答案 1 :(得分:0)

听起来你正在努力识别和使用另一种语言的语法。我前段时间尝试过这样做,最后抓取我的自定义代码并决定使用ANTLR API。它确实减少了完成我的项目所花费的时间。如果适用,我建议你去那条路线。

以下是ANTLR网站:http://www.antlr.org/

答案 2 :(得分:0)

当您尝试匹配“if”时,您正在进行模式匹配,期望整行等于“if”,我认为您需要做的是“。 if。 “这将查看该行是否包含”if“。既然如此,请使用字符串的.contains()方法,而不是使用正则表达式来查找各种语句。它更有效率。