title: Tracking Control Structures in Java for Static Code Analysis (Available Expressions)
body: <p>I am writing a program in Java that does Available expression Analysis on a given Java Program. I have managed to extract Generate and Kill set. To keep it simple, I am going with a constraint that will only analyze programs that declare ONE VARIABLE IN ONE LINE (The regex expression works for it with declaration and initialization but, the logic for redefinition of variables is still in progress. This is the reason why it won't correctly detect the kill set).</p>
  <p>The problem I am facing right now is that how would I track the loops? conditional statements? all the control structures? like While, for, and if-else statements.</p>
  <p>Can anyone please help? what to use for this? how to keep track of the paths? Below are the code files.</p>
  <p>Any assistance you can provide would be greatly appreciated. Thank you!</p>
  <p><strong>ReadFile.java</strong></p>
  <pre><code>   // Import the File class
  import java.io.File; 
   
  // Import this class to handle errors
  import java.io.FileNotFoundException;
  import java.util.ArrayList;
  // Import the Scanner class to read text files
  import java.util.Scanner; 
  import java.util.Stack;
  import java.util.regex.Matcher;
  import java.util.regex.Pattern;
  
  public class ReadFile 
  {
      public static void main(String[] args) {
          try {
          // Creating an object of the file for reading the stmt
          File myObj = new File(&quot;./Files/Test.txt&quot;);  
      
          ArrayList&lt;State&gt; stmts = new ArrayList&lt;State&gt;();
  
          String gen=&quot;{ }&quot;;
          String kill=&quot;{ }&quot;;
          Scanner myReader = new Scanner(myObj);
          int i=0;
          while (myReader.hasNextLine()) 
          {
              String stmt = myReader.nextLine();
              boolean def=true;
  
              if(stmt.contains(&quot;class&quot;) || stmt.contains(&quot;{&quot;) || stmt.contains(&quot;}&quot;) || stmt.contains(&quot;args[]&quot;) || stmt.startsWith(&quot;//&quot;))
              {
                  continue;
              }
               
                      
              Pattern p = Pattern.compile(&quot;^(int|long|float|double)\\s(([\\_\\w\\d])+)&quot;);
              
              Matcher m = p.matcher(stmt.trim());
  
  
              while(m.find())
              {
                  kill = m.group(3);
              }
  
              if(stmt.contains(&quot;=&quot;))
              {
                  String[] exp = stmt.split(&quot;=&quot;);
                  
                  
                  for(int l=0; l&lt;exp.length;l++)
                  {
                      
                      if(exp[l].contains(&quot;+&quot;) || exp[l].contains(&quot;-&quot;) || exp[l].contains(&quot;*&quot;) || exp[l].contains(&quot;/&quot;))
                      {
                          gen = exp[l];       
                      }
                  }
                  
              stmts.add(new State(new ArrayList&lt;String&gt;(), i, false, kill,gen,stmt));
              gen = &quot;{ }&quot;;
              kill = &quot;{ }&quot;;
              i++;
          }
          myReader.close();
          
          for (int j = 0; j &lt; stmts.size(); j++)
          {
              System.out.println(&quot;Line Number &quot; + stmts.get(j).lineNo);
              System.out.println(&quot;Statement &quot; + stmts.get(j).statement);
              System.out.println(&quot;Generated &quot; + stmts.get(j).genSet);
              System.out.println(&quot;Kill &quot; + stmts.get(j).killSet);
  
          }
  
  
          
          } catch (FileNotFoundException e) {
          System.out.println(&quot;An error occurred.&quot;);
          e.printStackTrace();
          }
          
      }
  
  }
  </code></pre>
  <p><strong>State.java</strong></p>
  <pre><code>import java.util.ArrayList;
  
  public class State 
  {
      //Elements
      public ArrayList&lt;String&gt; variables = new ArrayList&lt;String&gt;();
      public int lineNo;
      public boolean isDef;
      public String killSet;
      public String genSet;
      public String statement;
      
      //Constructor
      public State(ArrayList&lt;String&gt; al, int ln, boolean def, String kill, String gen, String stmt) 
      {
          for(int i=0;i&lt;al.size();i++)
          {
              variables.add(al.get(i));
          }
          lineNo = ln;
          isDef = def;
          killSet = kill;
          genSet = gen;
          statement = stmt;
      }
      
  }
  </code></pre>