我正在C++ database query engine project
工作。目前,我应该能够解析为 schema sql.data(例如create table
)和 query sql.data(例如select ... from ...
) 。所以我有每个SQL 2解析器。
我遇到的问题:我只能使用一次解析器,并且效果很好。如果我尝试同时解析 schema 和 query ,那么我会遇到以下冲突:
(这只是即将发生的冲突的一部分,但我认为这可能是主要原因和主要冲突)
> ninja
[9/10] Linking CXX executable imlabdb
FAILED: imlabdb
: && /usr/bin/c++ -g -O0 -fsanitize=address CMakeFiles/imlabdb.dir/tools/imlabdb.cc.o -o imlabdb libimlab.a libschema.a libquery.a vendor/gflags/lib/libgflags.a -pthread && :
libquery.a(query_scanner.cc.o):(.bss+0x140): multiple definition of `yyleng'
libschema.a(schema_scanner.cc.o):(.bss+0x140): first defined here
libquery.a(query_scanner.cc.o):(.bss+0x280): multiple definition of `yyin'
libschema.a(schema_scanner.cc.o):(.bss+0x280): first defined here
libquery.a(query_scanner.cc.o):(.bss+0x2c0): multiple definition of `yyout'
libschema.a(schema_scanner.cc.o):(.bss+0x2c0): first defined here
libquery.a(query_scanner.cc.o):(.data+0x0): multiple definition of `yylineno'
libschema.a(schema_scanner.cc.o):(.data+0x0): first defined here
libquery.a(query_scanner.cc.o):(.data+0x40): multiple definition of `yy_flex_debug'
libschema.a(schema_scanner.cc.o):(.data+0x40): first defined here
libquery.a(query_scanner.cc.o):(.bss+0x380): multiple definition of `yytext'
libschema.a(schema_scanner.cc.o):(.bss+0x380): first defined here
如您所见,我使用 ninja 进行编译。我认为local.make
和CMakeList.txt
没问题,因此我在这里跳过以显示它们。
我尝试以简洁的方式显示我的代码。
imlabdb.cc
int main(int argc, char *argv[]) {
imlab::schemac::SchemaParseContext schema_parse_context;
std::ifstream in_schema("../data/schema.sql"); // schema sql
auto schema = schema_parse_context.Parse(in_schema);
in_schema.close();
imlab::queryc::QueryParseContext query_parse_context;
std::ifstream in_query("../data/queryc_2.sql"); // query sql
auto query = query_parse_context.Parse(in_query);
in_query.close();
}
schema_parse_context.cc
Schema SchemaParseContext::Parse(std::istream &in) {
beginScan(in);
imlab::schemac::SchemaParser parser(*this);
parser.set_debug_level(trace_parsing_);
parser.parse();
endScan();
return {mySchema}; // a container for the create table nokens
}
query_parse_context.cc
Query QueryParseContext::Parse(std::istream &in) {
beginScan(in);
imlab::queryc::QueryParser parser(*this);
parser.set_debug_level(trace_parsing_);
parser.parse();
endScan();
return {myQuery}; // a container for querys
}
然后显示架构的flex and bison
。
schema_scanner.l ,而没有多余的令牌。
%{
// Header
#include <cerrno>
#include <climits>
#include <cstdlib>
#include <string>
#include <istream>
#include "imlab/schemac/schema_parse_context.h"
#include "./schema_parser.h"
namespace imlab {
namespace schemac {
// The location of the current token
extern imlab::schemac::location loc;
// The input stream of the scanner
extern std::istream *in;
} // namespace schemac
} // namespace imlab
using namespace imlab::schemac;
// Work around an incompatibility in flex (at least versions
// 2.5.31 through 2.5.33): it generates code that does
// not conform to C89. See Debian bug 333231
// <http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=333231>.
#undef yywrap
#define yywrap() 1
// Declare the yylex function
#define YY_DECL SchemaParser::symbol_type yylex(SchemaParseContext& sc)
// Configure the scanner to use istreams
#define YY_INPUT(buffer, result, max_size) \
result = 0; \
while (true) { \
int c = in->get(); \
if (in->eof()) break; \
buffer[result++] = c; \
if (result == max_size || c == '\n') break; \
}
%}
%{
// ---------------------------------------------------------------------------------------------------
// Options
// ---------------------------------------------------------------------------------------------------
%}
%{
// noyywrap: Disable yywrap (EOF == end of parsing)
// nounput: Disable manipulation of input stream
// noinput: Disable explicit fetch of the next character
// batch: Scanner in batch-mode (vs. interactive)
// debug: Write debug info to stderr
// caseless: Case-insensitive pattern matching
%}
%option noyywrap
%option nounput
%option noinput
%option batch
%option debug
%option caseless
%{
// Code run each time a token is matched.
// We just update the location of the token.
#define YY_USER_ACTION { loc.columns(yyleng); }
%}
%%
%{
// Code runs each time yylex is called.
// Set the beginning of the token to the end of the previous token.
loc.step ();
%}
[ \t]+ { loc.step(); }
"\n" { loc.lines (yyleng); loc.step (); }
";" { return SchemaParser::make_SEMICOLON(loc); }
%%
// ---------------------------------------------------------------------------------------------------
// Code
// ---------------------------------------------------------------------------------------------------
// The input stream
imlab::schemac::location imlab::schemac::loc;
// The input stream of the scanner
std::istream *imlab::schemac::in = nullptr;
// Begin a scan
void imlab::schemac::SchemaParseContext::beginScan(std::istream &is) {
yy_flex_debug = trace_scanning_;
in = &is;
}
// End a scan
void imlab::schemac::SchemaParseContext::endScan() {
in = nullptr;
}
schema_parser.y 也删除了不必要的标记和案例
%skeleton "lalr1.cc"
%require "3.0.4"
// ---------------------------------------------------------------------------------------------------
// Write a parser header file
%defines
// Define the parser class name
%define parser_class_name {SchemaParser}
// Create the parser in our namespace
%define api.namespace { imlab::schemac }
// Use C++ variant to store the values and get better type warnings (compared to "union")
%define api.value.type variant
// With variant-based values, symbols are handled as a whole in the scanner
%define api.token.constructor
// Prefix all tokens
%define api.token.prefix {SCHEMA_}
// Check if variants are constructed and destroyed properly
%define parse.assert
// Trace the parser
%define parse.trace
// Use verbose parser errors
%define parse.error verbose
// Enable location tracking.
%locations
// Pass the compiler as parameter to yylex/yyparse.
%param { imlab::schemac::SchemaParseContext &sc }
// ---------------------------------------------------------------------------------------------------
// Added to the header file and parser implementation before bison definitions.
// We include string for string tokens and forward declare the SchemaParseContext.
%code requires {
#include <string>
#include <vector>
#include "imlab/schemac/schema_parse_context.h"
}
// ---------------------------------------------------------------------------------------------------
// Import the compiler header in the implementation file
%code {
imlab::schemac::SchemaParser::symbol_type yylex(imlab::schemac::SchemaParseContext& sc);
}
%code {
std::string insertTableId;
int positionToInsert;
}
// ---------------------------------------------------------------------------------------------------
// Token definitions but deleted the most of them
%token <int> INTEGER_VALUE "integer_value"
%token <std::string> IDENTIFIER "identifier"
// ---------------------------------------------------------------------------------------------------
// Define error function
void imlab::schemac::SchemaParser::error(const location_type& l, const std::string& m) {
sc.Error(l.begin.line, l.begin.column, m);
}
将flex and bison
替换为query
,但几乎以相同的方式编写。
query_scanner.l
%{
// Header
#include <cerrno>
#include <climits>
#include <cstdlib>
#include <string>
#include <istream>
#include "imlab/queryc/query_parse_context.h"
#include "./query_parser.h"
namespace imlab {
namespace queryc {
// The location of the current token
extern imlab::queryc::location loc;
// The input stream of the scanner
extern std::istream *in;
} // namespace queryc
} // namespace imlab
using namespace imlab::queryc;
// Work around an incompatibility in flex (at least versions
// 2.5.31 through 2.5.33): it generates code that does
// not conform to C89. See Debian bug 333231
// <http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=333231>.
#undef yywrap
#define yywrap() 1
// Declare the yylex function
#define YY_DECL QueryParser::symbol_type yylex(QueryParseContext& sc)
// Configure the scanner to use istreams
#define YY_INPUT(buffer, result, max_size) \
result = 0; \
while (true) { \
int c = in->get(); \
if (in->eof()) break; \
buffer[result++] = c; \
if (result == max_size || c == '\n') break; \
}
%}
%{
// ---------------------------------------------------------------------------------------------------
// Options
// ---------------------------------------------------------------------------------------------------
%}
%option noyywrap
%option nounput
%option noinput
%option batch
%option debug
%option caseless
%{
#define YY_USER_ACTION { loc.columns(yyleng); }
%%
%{
loc.step ();
%}
[ \t]+ { loc.step(); }
"\n" { loc.lines (yyleng); loc.step (); }
";" { return QueryParser::make_SEMICOLON(loc); }
%%
// ---------------------------------------------------------------------------------------------------
// Code
// ---------------------------------------------------------------------------------------------------
// The input stream
imlab::queryc::location imlab::queryc::loc;
// The input stream of the scanner
std::istream *imlab::queryc::in = nullptr;
// Begin a scan
void imlab::queryc::QueryParseContext::beginScan(std::istream &is) {
yy_flex_debug = trace_scanning_;
in = &is;
}
// End a scan
void imlab::queryc::QueryParseContext::endScan() {
in = nullptr;
}
query_parser.y
%skeleton "lalr1.cc"
%require "3.0.4"
// ---------------------------------------------------------------------------------------------------
%defines
%define parser_class_name {QueryParser}
%define api.namespace { imlab::queryc }
%define api.value.type variant
%define api.token.constructor
%define api.token.prefix {QUERY_}
%define parse.assert
%define parse.trace
%define parse.error verbose
%locations
%param { imlab::queryc::QueryParseContext &sc }
// ---------------------------------------------------------------------------------------------------
%code requires {
#include <string>
#include <vector>
#include "imlab/queryc/query_parse_context.h"
}
// ---------------------------------------------------------------------------------------------------
%code {
imlab::queryc::QueryParser::symbol_type yylex(imlab::queryc::QueryParseContext& sc);
}
// %code {
// std::string insertTableId;
// int positionToInsert;
// }
// ---------------------------------------------------------------------------------------------------
// Token definitions -- most of them deleted
%token <int> INTEGER_VALUE "integer_value"
%token <std::string> IDENTIFIER "identifier"
// ---------------------------------------------------------------------------------------------------
// Define error function
void imlab::queryc::QueryParser::error(const location_type& l, const std::string& m) {
sc.Error(l.begin.line, l.begin.column, m);
}
// ---------------------------------------------------------------------------------------------------
简而言之:
我可以肯定的:如果我仅在flex and bison
中使用其中之一,那么这两个imlabdb.cc
和其他类似imlabdb.cc
的东西都可以正常工作。当我尝试同时使用两个解析器时,只会发生我添加到顶部的冲突。
我想要的:要同时使用两个解析器,并避免命名冲突。
谢谢!
(如果我删除了太多代码或当前代码不足,请发布。我将对其进行修复。)
答案 0 :(得分:1)
模式(DDL)和查询(DML)都是SQL语言的一部分。您不需要两个词法分析器/解析器,而仅需要一个基于SQL语法定义的语法分析器/解析器。例如,看看“ YACC SQL Grammar Reference”。
答案 1 :(得分:1)
尽管@serge对于您的特定问题是正确的,但我会回答更笼统的问题。
Flex和bison创建了一个C文件,其中包含一堆公用的函数和变量,例如yylex
和yyparse
。如果在一个程序中有两个解析器或词法分析器,这是不幸的。程序不知道调用哪个yylex
和yyparse
。因此,您需要使用不同的名称。
要在Flex中执行此操作,请使用选项
%option prefix="foo"
这会将具有yy prefex的所有符号重命名为foo,因此foolex
。因此,在您的情况下,您可以在一个词法分析器中使用一个前缀,在另一个词法分析器中使用另一个前缀。
在野牛上,事物的语法不同。根据您使用的Bision版本,您可以使用以下任一(新功能):
%define api.prefix {foo}
或(旧):
%name-prefix "foo"
然后将生成fooparse
。
有关参考,请参见文档: