added documentation to lexer and grammar files
This commit is contained in:
		
							parent
							
								
									b92bbd9157
								
							
						
					
					
						commit
						d2b5e10945
					
				| @ -1,23 +1,43 @@ | |||||||
| %{ | %{ | ||||||
| 
 | /** | ||||||
| /* |  * bison_parser.y | ||||||
|  * parser.y file |  * defines bison_parser.h | ||||||
|  * To generate the parser run: "bison parser.y" |  * outputs bison_parser.c | ||||||
|  |  *  | ||||||
|  |  * Grammar File Spec: http://dinosaur.compilertools.net/bison/bison_6.html | ||||||
|  |  * | ||||||
|  */ |  */ | ||||||
|  | /********************************* | ||||||
|  |  ** Section 1: C Declarations | ||||||
|  |  *********************************/ | ||||||
| 
 | 
 | ||||||
| #include "Statement.h" | #include "Statement.h" | ||||||
| #include "List.h" | #include "List.h" | ||||||
| #include "bison_parser.h" | #include "bison_parser.h" | ||||||
| #include "flex_lexer.h" | #include "flex_lexer.h" | ||||||
| 
 | 
 | ||||||
| #include <iostream> | #include <stdio.h> | ||||||
| 
 | 
 | ||||||
| int yyerror(Statement **expression, yyscan_t scanner, const char *msg) { | 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 { | %code requires { | ||||||
| 
 | 
 | ||||||
| #ifndef YYtypeDEF_YY_SCANNER_T | #ifndef YYtypeDEF_YY_SCANNER_T | ||||||
| @ -27,14 +47,18 @@ typedef void* yyscan_t; | |||||||
| 
 | 
 | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| %output  "bison_parser.c" | // Define additional parameters for yylex (http://www.gnu.org/software/bison/manual/html_node/Pure-Calling.html) | ||||||
| %defines "bison_parser.h" |  | ||||||
| 
 |  | ||||||
| %define api.pure |  | ||||||
| %lex-param   { yyscan_t scanner } | %lex-param   { yyscan_t scanner } | ||||||
|  | 
 | ||||||
|  | // Define additional parameters for yyparse | ||||||
| %parse-param { Statement **statement } | %parse-param { Statement **statement } | ||||||
| %parse-param { yyscan_t scanner } | %parse-param { yyscan_t scanner } | ||||||
| 
 | 
 | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | /********************************* | ||||||
|  |  ** Define all data-types (http://www.gnu.org/software/bison/manual/html_node/Union-Decl.html) | ||||||
|  |  *********************************/ | ||||||
| %union { | %union { | ||||||
| 	float number; | 	float number; | ||||||
| 	char* sval; | 	char* sval; | ||||||
| @ -48,39 +72,58 @@ typedef void* yyscan_t; | |||||||
| 	List<Expr*>* explist; | 	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 <sval> NAME STRING COMPARISON | ||||||
| %token <number> INTNUM | %token <number> INTNUM | ||||||
| 
 | 
 | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | /********************************* | ||||||
|  |  ** Non-Terminal types (http://www.gnu.org/software/bison/manual/html_node/Type-Decl.html) | ||||||
|  |  *********************************/ | ||||||
| %type <statement> statement | %type <statement> statement | ||||||
| %type <select_statement> select_statement | %type <select_statement> select_statement | ||||||
| %type <sval> table_name | %type <sval> table_name | ||||||
| %type <table> from_clause table_exp | %type <table> from_clause table_exp | ||||||
| 
 |  | ||||||
| %type <expr> expr column_name scalar_exp literal | %type <expr> expr column_name scalar_exp literal | ||||||
| %type <expr> comparison_predicate predicate search_condition where_clause | %type <expr> comparison_predicate predicate search_condition where_clause | ||||||
| 
 |  | ||||||
| %type <slist> table_ref_commalist | %type <slist> table_ref_commalist | ||||||
| %type <explist> expr_list group_clause | %type <explist> expr_list group_clause | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
| %% | %% | ||||||
|  | /********************************* | ||||||
|  |  ** Section 3: Grammar Definition | ||||||
|  |  *********************************/ | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
|  | // Defines our general input. | ||||||
|  | // TODO: Support list of statements | ||||||
| input: | input: | ||||||
| 		statement opt_semicolon { *statement = $1; } | 		statement opt_semicolon { *statement = $1; } | ||||||
| 	; | 	; | ||||||
| 
 | 
 | ||||||
|  | // All types of statements | ||||||
|  | // Atm: only select statements (future: insert, delete, etc...) | ||||||
| statement: | statement: | ||||||
| 		select_statement { $$ = $1; } | 		select_statement { $$ = $1; } | ||||||
| 	|	/* empty */ { $$ = NULL; } | 	|	/* empty */ { $$ = NULL; } | ||||||
| 	; | 	; | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| ////// TODO: | /****************************** | ||||||
| // join |  ** Select Statements | ||||||
| // limit |  ******************************/ | ||||||
| // order by |  | ||||||
| 
 | 
 | ||||||
|  | // TODO: join | ||||||
|  | // TODO: limit | ||||||
|  | // TODO: order by | ||||||
| select_statement: | select_statement: | ||||||
| 		SELECT expr_list from_clause where_clause group_clause | 		SELECT expr_list from_clause where_clause group_clause | ||||||
| 		{ | 		{ | ||||||
| @ -99,18 +142,20 @@ from_clause: | |||||||
| 		FROM table_exp { $$ = $2; } | 		FROM table_exp { $$ = $2; } | ||||||
| 	; | 	; | ||||||
| 
 | 
 | ||||||
|  | 
 | ||||||
| where_clause: | where_clause: | ||||||
| 		WHERE search_condition { $$ = $2; } | 		WHERE search_condition { $$ = $2; } | ||||||
| 	|	/* empty */ { $$ = NULL; } | 	|	/* empty */ { $$ = NULL; } | ||||||
| 	; | 	; | ||||||
| 
 | 
 | ||||||
|  | // TODO: having | ||||||
| group_clause: | group_clause: | ||||||
| 		GROUP BY expr_list { $$ = $3; } | 		GROUP BY expr_list { $$ = $3; } | ||||||
| 	|	/* empty */ { $$ = NULL; } | 	|	/* empty */ { $$ = NULL; } | ||||||
| 	; | 	; | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| 
 | // TODO: comma_list with mixed table names and select statements | ||||||
| table_exp: | table_exp: | ||||||
| 		table_ref_commalist { | 		table_ref_commalist { | ||||||
| 			TableRef* t = new TableRef(eTableName); | 			TableRef* t = new TableRef(eTableName); | ||||||
| @ -125,41 +170,34 @@ table_exp: | |||||||
| 	; | 	; | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
|  | // TODO: multiple predicates | ||||||
| search_condition: | search_condition: | ||||||
| 		predicate | 		predicate | ||||||
| 	; | 	; | ||||||
| 
 | 
 | ||||||
|  | 
 | ||||||
| predicate: | predicate: | ||||||
| 		comparison_predicate | 		comparison_predicate | ||||||
| 	; | 	; | ||||||
| 
 | 
 | ||||||
|  | 
 | ||||||
| comparison_predicate: | comparison_predicate: | ||||||
| 		scalar_exp COMPARISON scalar_exp { $$ = makePredicate($1, $2, $3); } | 		scalar_exp COMPARISON scalar_exp { $$ = makePredicate($1, $2, $3); } | ||||||
| 	; | 	; | ||||||
| 
 | 
 | ||||||
| 
 | // TODO: Expression can also be scalar_exp | ||||||
| expr: | expr: | ||||||
| 		column_name | 		column_name | ||||||
| 	|	NAME '(' expr ')' { $$ = makeFunctionRef($1, $3); } | 	|	NAME '(' expr ')' { $$ = makeFunctionRef($1, $3); } | ||||||
| 	; | 	; | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| 
 | // TODO: Scalar expressions can also be arithmetic | ||||||
| /* Lists */ | scalar_exp: | ||||||
| expr_list: | 		column_name | ||||||
| 		expr { $$ = new List<Expr*>($1); } | 	|	literal | ||||||
| 	|	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; } |  | ||||||
| 	; |  | ||||||
| /* / Lists */ |  | ||||||
| 
 |  | ||||||
| 
 |  | ||||||
| column_name: | column_name: | ||||||
| 		NAME { $$ = makeColumnRef($1); } | 		NAME { $$ = makeColumnRef($1); } | ||||||
| 	; | 	; | ||||||
| @ -174,13 +212,33 @@ literal: | |||||||
| 	|	INTNUM { $$ = makeFloatLiteral($1); } | 	|	INTNUM { $$ = makeFloatLiteral($1); } | ||||||
| 	; | 	; | ||||||
| 
 | 
 | ||||||
| scalar_exp: |  | ||||||
| 		column_name |  | ||||||
| 	|	literal |  | ||||||
| 	; |  | ||||||
| 
 | 
 | ||||||
| opt_semicolon: | opt_semicolon: | ||||||
| 		';' | 		';' | ||||||
| 	|	/* empty */ | 	|	/* 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 */ | ||||||
|  | 
 | ||||||
|  | |||||||
| @ -1,32 +1,50 @@ | |||||||
| %{ | /** | ||||||
| 
 |  * lexer | ||||||
| /* |  * | ||||||
|  * lexer.l file |  * | ||||||
|  * To generate the lexical analyzer run: "flex lexer.l" |  | ||||||
|  */ |  */ | ||||||
| 
 | 
 | ||||||
|  | 
 | ||||||
|  | /*************************** | ||||||
|  |  ** Section 1: Definitions | ||||||
|  |  ***************************/ | ||||||
|  | %{ | ||||||
|  | 
 | ||||||
| #include "Statement.h" | #include "Statement.h" | ||||||
| #include "List.h" | #include "List.h" | ||||||
| #include "bison_parser.h" | #include "bison_parser.h" | ||||||
| 
 |  | ||||||
| #include <stdio.h> | #include <stdio.h> | ||||||
| #include <iostream> |  | ||||||
| using namespace std; |  | ||||||
| 
 | 
 | ||||||
| #define TOK(name) { return name; } | #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" | /* Make reentrant */ | ||||||
| %option warn nodefault | %option reentrant bison-bridge | ||||||
| %option reentrant noyywrap never-interactive nounistd | 
 | ||||||
| %option bison-bridge | /* performance tweeks */ | ||||||
|  | %option never-interactive batch | ||||||
|  | 
 | ||||||
|  | /* other flags */ | ||||||
|  | %option noyywrap warn | ||||||
| %option case-insensitive | %option case-insensitive | ||||||
|  | /* %option nodefault */ | ||||||
| 
 | 
 | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | /*************************** | ||||||
|  |  ** Section 3: Rules | ||||||
|  |  ***************************/ | ||||||
| %% | %% | ||||||
| 
 | 
 | ||||||
| [ \t\n]+	; | [ \t\n]+	/* skip whitespace */; | ||||||
| 
 | 
 | ||||||
| SELECT	TOK(SELECT) | SELECT	TOK(SELECT) | ||||||
| FROM	TOK(FROM) | FROM	TOK(FROM) | ||||||
| @ -69,8 +87,14 @@ OR		TOK(OR) | |||||||
| 	return STRING; | 	return STRING; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
| %% | %% | ||||||
|  | /*************************** | ||||||
|  |  ** Section 3: User code | ||||||
|  |  ***************************/ | ||||||
| 
 | 
 | ||||||
| int yyerror(const char *msg) { | int yyerror(const char *msg) { | ||||||
|     fprintf(stderr,"Error:%s\n",msg); return 0; |     fprintf(stderr, "[Error] SQL Lexer: %s\n",msg); return 0; | ||||||
| } | } | ||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user
	 Pedro
						Pedro