0

I am writing a SQL grammar which is straightforward for the most part, but ran into a snag when writing the rule for the CASE statement.

It would be trivial if CASE and WHEN were reserved tokens in the SQL language, but they aren't. Either can be used as normal identifiers for column names, aliases, etc.

I considered using lex to determine if it should return the CASE or IDENTIFIER token, but I can't figure out how to do that. The rules would be pretty complex if at all possible.

At the same time I don't know if it is even possible to write a non-terminal rule that uses the value of the IDENTIFIER token in the rule itself (as opposed to the action).

I know it's possible as its already been done by Oracle's parser (though now that I think about it they may not be using lex/yacc).

EDIT: Can I assume CASE is a keyword and return it as a token, but push it back to lex and have it return CASE as an IDENTIFIER with a value of "CASE" if the current rule fails?

I have the luxury of knowing the statement's syntax is correct (an already working query), so if the CASE rule fails I know that it's an IDENTIFIER.

If lex and yacc can't do it, can flex and bison?

  • I think ANSI SQL is pretty flexible in the definition of reserved words. Each implementation defines its own particular words, so I see no problem with defining `case` and `when` to be reserved for this purpose. For instance, `case` and `when` are reserved keywords for MySQL (and cannot be used for identifiers unless escaped). – Gordon Linoff Dec 18 '15 at 17:25
  • Unfortunately I need to parse Oracle SQL so I must handle it as both an IDENTIFIER and keyword. – user5695926 Dec 18 '15 at 18:00
  • 1
    @user5695926: That is precisely what the duplicate question is about. And I answered it there. It should solve your problem as well (although the precise production is slightly different, the idea is precisely the same). – rici Dec 18 '15 at 18:54
  • Thanks user5695926, I missed your link the first time through. I will have to see how many rules would have to be modified for that approach and ensure it doesn't generate a shift/reduce conflict. – user5695926 Dec 18 '15 at 19:12

0 Answers0