started work on a lemon sql parser

This commit is contained in:
Pedro 2014-10-17 16:26:06 +02:00
parent 75a4bed720
commit 0a8cced386
6 changed files with 140 additions and 0 deletions

11
src/lemon/Makefile Normal file
View File

@ -0,0 +1,11 @@
all: lemon_parser.c flex_lexer.c
lemon_parser.c: lemon_parser.y
lemon lemon_parser.y
flex_lexer.c: flex_lexer.l
flex --outfile=flex_lexer.c --header-file=flex_lexer.h flex_lexer.l
clean:
rm -f lemon_parser.c flex_lexer.c lemon_parser.h flex_lexer.h lemon_parser.out

37
src/lemon/SQLParser.cpp Normal file
View File

@ -0,0 +1,37 @@
#include "SQLParser.h"
#include "lemon_parser.h"
#include "flex_lexer.h"
#include <stdio.h>
void *ParseAlloc(void *(*mallocProc)(size_t));
void ParseFree(void *p, void (*freeProc)(void*));
void Parse(void *yyp, int yymajor, const char* text, Statement**);
SQLParser::SQLParser() {
fprintf(stderr, "SQLParser only has static methods atm! Do not initialize!\n");
}
Statement* SQLParser::parseSQL(const char *text) {
yyscan_t scanner;
yylex_init(&scanner);
// Scan the provided string
YY_BUFFER_STATE state = yy_scan_string(text, scanner);
void* lemonParser = ParseAlloc(malloc);
int tokenCode;
Statement* result;
do {
tokenCode = yylex(scanner);
Parse(lemonParser, tokenCode, yyget_text(scanner), &result);
// printf("Token %d\n", tokenCode);
} while (tokenCode > 0);
yy_delete_buffer(state, scanner);
yylex_destroy(scanner);
return result;
}

16
src/lemon/SQLParser.h Normal file
View File

@ -0,0 +1,16 @@
#ifndef __SQLPARSER_H_
#define __SQLPARSER_H_
#include "Statement.h"
class SQLParser {
public:
static Statement* parseSQL(const char* sql);
private:
SQLParser();
};
#endif

52
src/lemon/flex_lexer.l Normal file
View File

@ -0,0 +1,52 @@
%{
/**
* flex_lexer.l file
*/
#include "lemon_parser.h"
#include <stdlib.h>
#define TOK(name) { return name; }
%}
%option reentrant
%option noyywrap
%%
[ \t\n]+ ; /* skip whitespace */
SELECT TOK(SELECT)
FROM TOK(FROM)
GROUP TOK(GROUP)
BY TOK(BY)
WHERE TOK(WHERE)
NOT TOK(NOT)
AND TOK(AND)
OR TOK(OR)
"=" |
"<>" |
"<" |
">" |
"<=" |
">=" TOK(COMPARISON)
[-+*/(),.;] TOK(yytext[0])
[0-9]+ |
[0-9]+"."[0-9]* |
"."[0-9]* TOK(INTNUM)
[A-Za-z][A-Za-z0-9_]* TOK(NAME)
'[^'\n]*' TOK(STRING)
%%
int yyerror(const char *msg) {
fprintf(stderr,"Error:%s\n",msg); return 0;
}

24
src/lemon/lemon_parser.y Normal file
View File

@ -0,0 +1,24 @@
%include {
#include <assert.h>
#include <cstdlib>
#include "lib/Statement.h"
}
%syntax_error { printf("Lemon syntax error\n"); }
%extra_argument { Statement** result }
%token_type {const char*}
%type expr {Statement*}
%left PLUS MINUS .
start ::= prog.
prog ::= prog print NL .
prog ::= prog print .
prog ::= .
print ::= expr(a) . { *result = a; }
expr(a) ::= NUMBER . { a = new Statement(eSelect); }
expr(a) ::= expr(b) PLUS expr . { a = b; }

0
src/lemon/token_def.h Normal file
View File