I need a simple lexical analyzer that reports for-loop errors in C/C++.
-
1I need more details to answer this question – John Weldon Jun 09 '09 at 23:39
-
2I need a beer. Hey, it's 1640, 20 more minutes. Woo HOo! – Adrien Jun 09 '09 at 23:40
-
1:) - to clarify: can you clarify your question? – John Weldon Jun 09 '09 at 23:40
-
Actually, I need more pork chop to understand the question. – none Jun 09 '09 at 23:43
-
Can't you just run it through the compiler and see if it produces an error? – Zifre Jun 09 '09 at 23:47
-
1i am starting to construct a simple compiler /lexical analizer code using c/c++ that reports for-loop error – Jun 10 '09 at 00:21
-
gcc code is free you can download it. There is the full lexer and parser for C++ builtin. All you need to do as add some extra code to the for loop analysis. – Martin York Jun 10 '09 at 00:52
4 Answers
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
.

- 19,717
- 4
- 46
- 69
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:
flex/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!

- 842
- 9
- 17