Skip to content

clara-waseely/DynamicCodeAnalyzer-CompilerProject

Β 
Β 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

94 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Dynamic Java Code Analyzer

A group project for Compiler Course

contributors last update

Team Members

🌟 About the Project

This is a dynamic code analyzer for Java Programming Language to generate statement and branches code coverage reports.

Statement coverage VS. Branch coverage

if(a || b || (c && d && !e)) {
    test1 = true;
} else {
    test2 = false;
}
  • Statement Coverage: when our code says that test1 and test2 are hit.
  • Branch Coverage: to test the cases when a is true, when a is false but b is true, when a and b are false but c and d are true and e is false, and so on.

100% branch coverage => 100% statement coverage, while 100% statement coverage does not imply 100% branch coverage

System Flow

βš™οΈ Built With

  • Java
  • IntelliJ
  • ANTLR plug-in
  • CSS & HTML

🧭 Roadmap

βœ“ Delivery 1
  • Github repository creation

    <div align="center">
    
      <img src="https://github.com/Clara-Raef/Dynamic-Code-Analyzer--Compiler-Course-Project/blob/29d7ccda243cebb76c896872c9171a2abd8f4892/Delivery1/repo-qr-code.png" />
      
    </div>
    

  • ANTLR Java Lexer & Parser used


  • Testing the grammar & showing the parse tree using ANTLR with Intelli-J

    • πŸ§ͺ Simple If condition program that states Success/Failure for a certain grade
         public class IfCond {
          public static void main(String[] args) {
            int grade=72;
            if(grade>50){
                System.out.print("Succeeded");
            }
            else{
                System.out.println("Failed");
            }
        }
      }
      
    • πŸ“· If condition Parse Tree

    If condition test

    If condition Parse Tree


    • πŸ§ͺ Simple While loop that prints value of variable k of type integer while it's less than or equal to 10

      public class WhileLoop {
      public static void main(String[] args) {
          int k=4;
          while(k<=10){
              System.out.println(k);
          }
      }
      }
      
    • πŸ“· While loop Parse Tree

    While loop test

    While loop Parse Tree

  • πŸ“· While loop fault Parse Tree

    While loop fault test

    While loop fault Parse Tree


  • πŸ“· String Operation Parse Tree

    String Operation test

    String Operation test


  • Starting rule of the grammar: compilationUnit


  • A Java program based on Antlr (USING LISTENER) that takes a java file as an input and outputs a modified intermediate java file (injected code): a comment is added in each code block indicating the block number

πŸ“· Output file

πŸ“· Input file VS. Modified output file

βœ“ Delivery 2
  • A Java program based on Antlr that takes a java code (input.txt) and injects code into it, generating a modified java file (output1.java). When (output1.java) is run, the visited blocks from this code are detected and stated in a text file (output2.txt).

πŸ“· Output files

πŸ“· Input file VS. Output text file containing visited blocks numbers

βœ“ Delivery3
  • Generate an HTML where red-highlighted code blocks are the ones that have not been visited and the green-highlighted code blocks are the ones that have been visited
  • Branch coverage report: note the highlighted x == 0 || y == 1 || y == 5 in red

πŸ§ͺ Test 1

    class main {
    public static void main(String[] args) {
      int x = 0;
      int y = 0;
      if(x == 0 || y == 1 || y == 5){
        y = 10;
      }
      else {
        if(true){
          x++;
        }
      }
      for(int i = 0; i<3 ; i++) x++;
      while(y < 5) y = 5;
      while(y < 3 || y < 5) y = 4;
      if(true){
        y++;
      }
      int a[] = {1,2,3};
      for (int i = 0 ; i<4 ;i++){
      }
      {
      int z;
    }
    }
  }

πŸ“· Output HTML

πŸ“· Input code, Output modified code, text files containing the indeces of covered blocks & branches

  • Automated pipeline

πŸ§ͺ Running "Test.java" automatically generates all the files highlighted in blue

  • Testing diverse code cases

πŸ§ͺ Test 2

  public class Prime {
    public static void main(String[] args) {

        int low = 20, high = 50;

        while (low < high) {
            if(checkPrimeNumber(low))
                System.out.print(low + " ");

            ++low;
        }
    }

    public static boolean checkPrimeNumber(int num) {
        boolean flag = true;

        for(int i = 2; i <= num/2; ++i) {

            if(num % i == 0) {
                flag = false;
                break;
            }
        }

        return flag;
    }
}

πŸ“· Output HTML

πŸ“· Input code, Output modified code, text files containing the indeces of covered blocks

πŸ§ͺ Test 3

  public class PositiveNegative {

  public static void main(String[] args) {

      double number = 12.3;

      if (number < 0.0)
          System.out.println(number + " is a negative number.");

      else
          System.out.println(number + " is a positive number.");

  }
}

πŸ“· Output HTML

πŸ“· Input code, Output modified code, text files containing the indeces of covered blocks

References

About

A dynamic code analyzer for Java Language to generate statement and branches code coverage reports.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • ANTLR 78.5%
  • Java 19.3%
  • HTML 2.2%