I am working with Antlr4 and I want to parse and analyze any c++ code to detect any loops in any c++ source code to do dependency analysis on them, but I can't detect any loop in the source code.
What I have tried:
This is the rule that I used for "for loops" in Antlr:
forBlock: 'for' '(' (classicFor | forEach) ')' controlStructureBody ;
forExpression: primaryExpression (',' primaryExpression)* ;
I print out the tokens in this code:
<pre>#include <iostream>
using namespace std;
int main()
{
for (int i = 0; i <= 5; i++) {
for (int j = 0; j <= 5; j++) {
cout << i << j << " \t";
}
cout << "\n";
}
return 0;
}
using this code:
public void printToken(String inputFile) throws FileNotFoundException, IOException {
System.out.println("The tokens of the source code is: \n");
CharStream inputStream = CharStreams.fromFileName(inputFile);
TokensLexer tokensLexer = new TokensLexer(inputStream);
CommonTokenStream tokenStream = new CommonTokenStream(tokensLexer);
tokenStream.fill();
for (Token token : tokenStream.getTokens()) {
System.out.println("<" + token.getText() + "> " + "<" + token.getType() + ">");
}
}
and it gave me the type of each for loop as
<for> <45>
<for> <45>
I tried this code:
CharStream inputStream = CharStreams.fromFileName(inputFile);
TokensLexer tokensLexer = new TokensLexer(inputStream);
CommonTokenStream tokenStream = new CommonTokenStream(tokensLexer);
TokensParser tokensParser = new TokensParser(tokenStream);
tokenStream.fill();
for (Token token : tokenStream.getTokens()) {
if (token.getType() == 45)
System.out.println("loop is found");
}
}
When I put "45" it prints out
loop is found twice and when I change the number to "39" it prints out
loop is found only one.
I tried "39" because I have this value in my generated files from Antlr grammar.
Tokens.tokens -> for = 45.
Tokens.lexer.tokens -> for = 45.
TokensParser.java ->
Rule_forBlock = 39; Rule_forExpression = 40;
and when I try to add more loops:
#include <iostream>
using namespace std;
int main()
{
for (int i = 0; i <= 5; i++)
{
for (int j = 0; j <= 5; j++) {
cout << i << j << " \t";
}
cout << "\n";
}
for (int i = 0; i < 100; i++)
{
cout<<"Test"<<endl;
}
return 0;
}
and use the number 39 still detects only one loop.
Is there a way to detect the loops in the source code using Antlr and differentiate between the outer and inner loop?