Help.com program template

I wrote a program for parsing a text file that contains an example C program with if , else and while conditions.

I have 2 ArrayList and my program will parse the file. I use Matcher and set the String pattern in Pattern.compile() . I am trying to draw a graph of control flow for a specific program; however, I just find the nodes at the moment and link them later.

Here is my code:

 //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"); } } 

What I'm trying to do here, if my String found, enter the line number in ArrayList b , otherwise enter the line number in ArrayList a . Therefore, I know which lines the if , else and while expressions have.

I do not know if my code is incorrect or what, the input file looks like this:

 #include <stdio.h> int main() { int i=1, sum = 0; if( i = 1) { sum += i; } else printf("sum = %d\n", sum); return 0; } 

and program exit:

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

PS: I'm an amateur, this program may be logically incorrect.

Please let me know if you need more information.

EDIT:

Code that works just fine for one string search:

 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(); } 

It works fine on one (it’s just part of the code, not the whole program. The program is the same as above) and returns the line number where if found. I used the same logic and added the else and while .

+7
java text-parsing matcher
source share
3 answers

Finally, I got this job (thanks for the awesome stuff). The following are the changes I made:

 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(); } 

The input file is the same and the output is:

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

It looks like you are trying to recognize and work with the grammar of another language. I tried to do this a while ago and ended up clearing my own code and decided to use the ANTLR API. This really reduced the time needed to complete my project. I would recommend you go this route, if applicable.

Here is the ANTLR website: http://www.antlr.org/

0
source share

You perform pattern matching when you try to match an if, which expects the entire string to be equal to if. I think you need to do ".if". which will search if the string contains "if". Since this is the case, use the .contains () method of the string in which you are looking for various operators instead of using regex. It is more efficient.

0
source share

All Articles