I want to parse text for single query. This query will end with semicolon. It will be like sql. Ex: CREATE TABLE 'somename';
My y file is
%{
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <stdbool.h>
#include "ast.h"
extern int yylex(void);
extern void yyerror(char const *msg);
QueryNode *queryNode;
%}
%union {
int integer;
char *str;
char chr;
bool boolean;
int intval;
char *strval;
ObjectTypeNode *objectTypeNode;
CreateClauseNode *createClauseNode;
QueryNode *queryNode;
}
%token NUMBER
%token INTNUM
%token<str> CREATE_KEYWORD
%token<str> DATABASE_KEYWORD
%token<str> TABLE_KEYWORD
%token<str> LETTER
%token<str> STRING
%token<str> IDENTIFIER
%token<chr> LEFT_BRACKET RIGHT_BRACKET COMMA SEMICOLON EOL
%type<objectTypeNode> object_type
%type<createClauseNode> create_clause
%type<queryNode> query
%start input
%%
input: SEMICOLON EOL { queryNode = NULL; }
| query SEMICOLON EOL { queryNode = $1; }
;
query: create_clause { $$ = CreateQueryNode($1, CREATE_CLAUSE_TYPE); }
;
create_clause: CREATE_KEYWORD object_type STRING { $$ = CreateCreateClauseNode($2, $3); }
;
object_type: DATABASE_KEYWORD { $$ = CreateObjectTypeNode(DATABASE_OBJECT); }
| TABLE_KEYWORD { $$ = CreateObjectTypeNode(TABLE_OBJECT); }
;
%%
void yyerror(char const *msg) {
printf("Error: %s\n", msg);
}
And my l file is
%{
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <stdarg.h>
#include "ast.h"
#include "y.tab.h"
%}
%option noyywrap nodefault yylineno case-insensitive
%%
CREATE { yylval.strval = "create"; return CREATE_KEYWORD; }
DATABASE { return DATABASE_KEYWORD; }
TABLE { return TABLE_KEYWORD; }
"(" { return LEFT_BRACKET; }
")" { return RIGHT_BRACKET; }
";" { return SEMICOLON; }
-?[0-9]+ { yylval.intval = atoi(yytext); return INTNUM; }
L?'(\\.|[^\\'])+' |
L?\"(\\.|[^\\"])*\" { yylval.strval = yytext; return STRING; }
[a-zA-Z]+[0-9]* { return IDENTIFIER; }
[a-zA-Z]+ { return LETTER; }
[\n] { printf("eol\n"); return EOL; }
[ \t\f\v] { ; }
. { return *yytext; }
%%
I using yyparse() function in my other main function. main.c file is
#include <stdio.h>
#include <stdlib.h>
#include "ast.h"
#include "y.tab.h"
extern QueryNode *queryNode;
int main(int argc, char *argv[]) {
int result = yyparse();
if(result == 0 && queryNode != NULL) {
printf("AST created\n");
} else {
printf("Problem!\n");
}
return 0;
}
When I input as CREATE TABLE 'testo'; yyparse don't terminate and program waiting in int result = yyparse(); line. How can I fix it? I using flex and bison. I want to terminate with this input.