简体   繁体   English

在哪里可以找到能够报告C或C ++中的循环错误的词法分析器?

[英]Where might I obtain a lexical analyzer capable of reporting for-loop errors in C or C++?

我需要一个简单的词法分析器来报告C / C ++中的循环错误。

The compiler will complain very loudly if you write an illegal for loop: 如果编写非法的for循环,编译器将大声抱怨:

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. 但是,很多for循环的“错误”都是完全合法的。

I'm assuming you're trying to flag legal for loops that don't mean what you want them to mean. 我假设您正在尝试标记合法的for循环,这些循环并不意味着您想要它们表示什么。 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 <= 10时,写i < 10 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: 查看以下问题:

flex /lex and bison /yacc are two good tools to develop such things. flex / lex和bison / yacc是开发此类工具的两个很好的工具。 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. 报告for循环错误似乎是您需要的非常具体的内容,因此您可能必须编写自己的词法分析器和解析器才能使其执行所需的操作。

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循环错误?

For purely lexical analysis, you could use regular expressions, or any of dozens scanner generators (flex/lex, ANTLR). 对于纯词法分析,您可以使用正则表达式或数十种扫描仪生成器(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. 但是,据我了解,大多数C ++解析器都是手写的。 I'm not sure if even an LALR parser would do the trick; 我不确定,即使是LALR解析器也能解决问题。 you might need to bring out the big guns and use something like Bison's GLR support. 您可能需要拿出大手笔,并使用诸如Bison的GLR支持之类的工具。 Also, for a ton more information on lexical/syntactic analysis, I'd recommend 'The Dragon Book' . 另外,有关词汇/句法分析的更多信息,我建议使用“ The Dragon Book” Good luck! 祝好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM