且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

SQL 解析器库 - 从查询中获取表名

更新时间:2022-03-12 22:26:19

Antlr 可以为你生成一个不错的 SQL 解析器(解析器的源代码可以是 C++),并且可用的 SQL 语法很少:http://www.antlr3.org/grammar/list.html

Antlr can produce a nice SQL parser (the source of the parser can be C++) for you, and there is few SQL grammars for it available: http://www.antlr3.org/grammar/list.html

如果您只对表名感兴趣,那么采用其中一种语法并添加收集这些名称的语义操作应该相当容易.

If all you are interested in are the table names, then taking one of those grammars and adding a semantic action collecting those names should be fairly easy.

对 Antlr 和 Bison/Yacc 有一些经验 &Lex/Flex 我绝对推荐 Antlr.它是用 Java 编写的,但目标语言可以是 C++ - 生成的代码实际上是可读的,看起来像人写的.Antlr生成的解析器调试还算可以,Bison生成的就不能说了.

Having some experience with Antlr and Bison/Yacc & Lex/Flex I definitely recommend Antlr. It is written in Java, but the target language can be C++ - the generated code is actually readable, looks like written by a human. The debugging of Antlr generated parsers is quite OK, which cannot be said about those generated by Bison..

还有其他选项,例如 Lemon 和 sqlite 语法,如果您愿意,可以看看这个问题:C 中的 SQL 解析器

There are other options, like for example Lemon and sqlite grammar, have a look at this question if you like: SQL parser in C