Pol*_*ton 7 java matcher text-parsing
我已经写了一个程序来解析其中包含的样本C程序的文本文件if,else和while条件.
我有2 ArrayList秒,我的程序将解析文件.我使用Matcher,并指定模式StringS IN 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");
}
}
Run Code Online (Sandbox Code Playgroud)
我在这里要做的是,如果String找到我的话,输入行号ArrayList b,否则输入行号ArrayList a.因此,我知道,哪条线路有if,else和while语句.
我不知道我的代码是不正确还是什么,输入文件如下:
#include <stdio.h>
int main()
{
int i=1, sum = 0;
if( i = 1) {
sum += i;
} else
printf("sum = %d\n", sum);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
并且该程序的输出是:
run:
[2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
[1, 1, 1]
Run Code Online (Sandbox Code Playgroud)
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();
}
Run Code Online (Sandbox Code Playgroud)
上面一个工作正常(它只是代码的一部分,而不是整个程序.程序与上面的相同)并返回if找到的行号.我使用相同的逻辑并添加了else and while部分.
最后,我成功了(感谢您的精彩投入)。以下是我所做的更改:
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();
}
Run Code Online (Sandbox Code Playgroud)
输入文件相同,输出为:
[1, 2, 3, 4, 6, 7, 8, 10, 11, 12, 13]
[5, 9]
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
752 次 |
| 最近记录: |