0

I have a line with ORDER BY DESC(?year) where ?year can be anything followed by a question mark (?) such as ?name, ?address etc. I have tried ORDER BY DESC\(\?[a-z]+\) to capture the whole string as ORDER BY DESC(?year) but not working.

lex file:

%{
#include <cstdio>
#include <iostream>
#include "grammar.tab.h"
%}

%option case-insensitive

%%
[1-9][0-9]*         { yylval.i = atoi(yytext); return INT_NUM; }
"ORDER BY DESC\(\?[a-z]+\)"     { yylval.s = strdup(yytext); return ORDER_BY_DESC; }            
"\n"                { yylineno++; }
\.                  { return DOT; }             
[ \t]+              { /* ignore white space */ }                                                                                                            
"#".*               { /* ignore comments */ }
[ \t\v\f\r]+            { }
.               { std::cerr << "Lexical Error!\n"; }        
%%

int yywrap() {
    return 1;
}

bison file:

    %{
    #include <iostream>
    #include <cstdlib>
    #include <stdio.h>
    #include <stdlib.h>
    #include <sstream>
    #include <string>
    #include <regex>

    using namespace std;

    extern int yylineno;
    extern int yylex();

    void yyerror(char *s, ...);
    %}

    %error-verbose

    %union
    {
        int i;
        char *s;
    }
    %token<i> INT_NUM;
    %token<s> ORDER_BY_DESC;

    %%
    order_by:
            | ORDER_BY_DESC
            {
                   string s($<s>1); 
                   string str = s.substr(0, s.size() - 1);
                   char *x = new char[str.length() + 1];
                   order_by_stmt = str;
                   strcpy(x, str.c_str());
                   $<s>$ = x;
            }
        ;

    %%

    void yyerror(char *s, ...) {
        va_list ap;
        va_start(ap, s);
        fprintf(stderr, "%d: error: ", yylineno);
        vfprintf(stderr, s, ap);
        fprintf(stderr, "\n");
    }


int main() {

    yyparse();

    return 0;
}
splash
  • 13,037
  • 1
  • 44
  • 67
Abir Chokraborty
  • 1,695
  • 4
  • 15
  • 23

1 Answers1

0
"ORDER BY DESC"([^)]*) could be a solution for this question.
Abir Chokraborty
  • 1,695
  • 4
  • 15
  • 23