added documentation to lexer and grammar files

This commit is contained in:
Pedro 2014-10-22 15:43:27 +02:00
parent b92bbd9157
commit d2b5e10945
2 changed files with 133 additions and 51 deletions

View File

@ -1,23 +1,43 @@
%{
/*
* parser.y file
* To generate the parser run: "bison parser.y"
/**
* bison_parser.y
* defines bison_parser.h
* outputs bison_parser.c
*
* Grammar File Spec: http://dinosaur.compilertools.net/bison/bison_6.html
*
*/
/*********************************
** Section 1: C Declarations
*********************************/
#include "Statement.h"
#include "List.h"
#include "bison_parser.h"
#include "flex_lexer.h"
#include <iostream>
#include <stdio.h>
int yyerror(Statement **expression, yyscan_t scanner, const char *msg) {
// Add error handling routine as needed
fprintf(stderr, "[Error] SQL Parser: %s\n", msg);
return 0;
}
%}
/*********************************
** Section 2: Bison Parser Declarations
*********************************/
// Define the names of the created files
%output "bison_parser.c"
%defines "bison_parser.h"
// Tell bison to create a reentrant parser
%define api.pure full
// Specify code that is included in the generated .h and .c files
%code requires {
#ifndef YYtypeDEF_YY_SCANNER_T
@ -27,14 +47,18 @@ typedef void* yyscan_t;
}
%output "bison_parser.c"
%defines "bison_parser.h"
%define api.pure
// Define additional parameters for yylex (http://www.gnu.org/software/bison/manual/html_node/Pure-Calling.html)
%lex-param { yyscan_t scanner }
// Define additional parameters for yyparse
%parse-param { Statement **statement }
%parse-param { yyscan_t scanner }
/*********************************
** Define all data-types (http://www.gnu.org/software/bison/manual/html_node/Union-Decl.html)
*********************************/
%union {
float number;
char* sval;
@ -48,39 +72,58 @@ typedef void* yyscan_t;
List<Expr*>* explist;
}
%token SELECT FROM GROUP BY WHERE NOT AND OR
/*********************************
** Token Definition
*********************************/
%token SELECT FROM GROUP BY WHERE NOT AND OR
%token <sval> NAME STRING COMPARISON
%token <number> INTNUM
/*********************************
** Non-Terminal types (http://www.gnu.org/software/bison/manual/html_node/Type-Decl.html)
*********************************/
%type <statement> statement
%type <select_statement> select_statement
%type <sval> table_name
%type <table> from_clause table_exp
%type <expr> expr column_name scalar_exp literal
%type <expr> comparison_predicate predicate search_condition where_clause
%type <slist> table_ref_commalist
%type <explist> expr_list group_clause
%%
/*********************************
** Section 3: Grammar Definition
*********************************/
// Defines our general input.
// TODO: Support list of statements
input:
statement opt_semicolon { *statement = $1; }
;
// All types of statements
// Atm: only select statements (future: insert, delete, etc...)
statement:
select_statement { $$ = $1; }
| /* empty */ { $$ = NULL; }
;
////// TODO:
// join
// limit
// order by
/******************************
** Select Statements
******************************/
// TODO: join
// TODO: limit
// TODO: order by
select_statement:
SELECT expr_list from_clause where_clause group_clause
{
@ -99,18 +142,20 @@ from_clause:
FROM table_exp { $$ = $2; }
;
where_clause:
WHERE search_condition { $$ = $2; }
| /* empty */ { $$ = NULL; }
;
// TODO: having
group_clause:
GROUP BY expr_list { $$ = $3; }
| /* empty */ { $$ = NULL; }
;
// TODO: comma_list with mixed table names and select statements
table_exp:
table_ref_commalist {
TableRef* t = new TableRef(eTableName);
@ -125,41 +170,34 @@ table_exp:
;
// TODO: multiple predicates
search_condition:
predicate
;
predicate:
comparison_predicate
;
comparison_predicate:
scalar_exp COMPARISON scalar_exp { $$ = makePredicate($1, $2, $3); }
;
// TODO: Expression can also be scalar_exp
expr:
column_name
| NAME '(' expr ')' { $$ = makeFunctionRef($1, $3); }
;
/* Lists */
expr_list:
expr { $$ = new List<Expr*>($1); }
| expr_list ',' expr { $1->push_back($3); $$ = $1; }
// TODO: Scalar expressions can also be arithmetic
scalar_exp:
column_name
| literal
;
// TODO: add table_ref to include names and select queries
table_ref_commalist:
table_name { $$ = new List<char*>($1); }
| table_ref_commalist ',' table_name { $1->push_back($3); $$ = $1; }
;
/* / Lists */
column_name:
NAME { $$ = makeColumnRef($1); }
;
@ -174,13 +212,33 @@ literal:
| INTNUM { $$ = makeFloatLiteral($1); }
;
scalar_exp:
column_name
| literal
;
opt_semicolon:
';'
| /* empty */
;
/******************************
** Lists
******************************/
expr_list:
expr { $$ = new List<Expr*>($1); }
| expr_list ',' expr { $1->push_back($3); $$ = $1; }
;
// TODO: add table_ref to include names and select queries
table_ref_commalist:
table_name { $$ = new List<char*>($1); }
| table_ref_commalist ',' table_name { $1->push_back($3); $$ = $1; }
;
%%
/*********************************
** Section 4: Additional C code
*********************************/
/* empty */

View File

@ -1,32 +1,50 @@
%{
/*
* lexer.l file
* To generate the lexical analyzer run: "flex lexer.l"
/**
* lexer
*
*
*/
/***************************
** Section 1: Definitions
***************************/
%{
#include "Statement.h"
#include "List.h"
#include "bison_parser.h"
#include <stdio.h>
#include <iostream>
using namespace std;
#define TOK(name) { return name; }
%}
/***************************
** Section 2: Rules
***************************/
/* Define the output files */
%option header-file="flex_lexer.h"
%option outfile="flex_lexer.c"
%option outfile="flex_lexer.c" header-file="flex_lexer.h"
%option warn nodefault
%option reentrant noyywrap never-interactive nounistd
%option bison-bridge
/* Make reentrant */
%option reentrant bison-bridge
/* performance tweeks */
%option never-interactive batch
/* other flags */
%option noyywrap warn
%option case-insensitive
/* %option nodefault */
/***************************
** Section 3: Rules
***************************/
%%
[ \t\n]+ ;
[ \t\n]+ /* skip whitespace */;
SELECT TOK(SELECT)
FROM TOK(FROM)
@ -69,8 +87,14 @@ OR TOK(OR)
return STRING;
}
%%
/***************************
** Section 3: User code
***************************/
int yyerror(const char *msg) {
fprintf(stderr,"Error:%s\n",msg); return 0;
fprintf(stderr, "[Error] SQL Lexer: %s\n",msg); return 0;
}