I need a simple lexical analyzer that reports for-loop errors in C/C++.
views:
265answers:
4flex/lex and bison/yacc are two good tools to develop such things. Reporting for-loop errors would seem to be a very specific thing you need, so you may have to write your own lexer and parser to have it do what you want.
Bison's manual is pretty comprehensive.
With that said, why not just use your compiler's error messages to find out if you've written the for-loop wrong?
For purely lexical analysis, you could use regular expressions, or any of dozens scanner generators (flex/lex, ANTLR). For syntactic analysis on the other hand, you'd probably need a parser generator that could read a context-free grammar. However, from what I understand, most C++ parsers are hand written. I'm not sure if even an LALR parser would do the trick; you might need to bring out the big guns and use something like Bison's GLR support. Also, for a ton more information on lexical/syntactic analysis, I'd recommend 'The Dragon Book'. Good luck!
The compiler will complain very loudly if you write an illegal for loop:
for (int i)
will get a big loud error on every compiler on the market.
However, a lot of for-loop "mistakes" are perfectly legal.
I'm assuming you're trying to flag legal for loops that don't mean what you want them to mean. The problem, of course, is that the compiler has no way of knowing what you mean. You can legitimately leave out any of the three parts in the loop, and it's common to leave out all three parts. Additionally, you can do more than one thing in each part as well:
for (int i = 0, MAX_ITERS = 20; ; ++i, --MAX_ITERS) {
if (MAX_ITERS == 0 || i > MAX_ITERS)
break;
if (i % 2 == 0)
continue;
std::cout << i << ',' << MAX_ITERS << '\n';
}
And, of course, most loop errors are completely impossible for a compiler to find, such as writing i < 10
when you mean i <= 10
.
I suspect that what you want is not a lexical analyser which just looks at individual tokens and is part of a compiler, but rather a static analyzer which looks at code and can suggest potential bugs. Check out these questions: