sqlparsinggrammarcontext-free-grammarbnf

Doing BNF for a simplified SQL “where” clause


I have the following SQL statement with a rudimentary BNF applied:

SELECT
    from_expression [, from_expression] ...
FROM
    from_source
WHERE
    condition


from_source:
    table_name

from_expression:
    literal              # 'abc', 1, 
    column               # Table.Field1, Table.Profit
    function             # ABS(...)
    operator invocation  # NOT field1, 2+3, Genres[0], Genres[1:2], Address.name

condition:
    ???

For now the WHERE condition is the same as the from_expression but evaluated as a boolean. What would be the proper way to show that?


Solution

  • The grammar doesn't care about semantics.

    Syntactically, an expression is an expression, nothing more. If you later do some kind of semantic analysis, that is when you'll need to deal with the difference. You might do that in the reduction actions for condition and from_expression but it would be cleaner to just build an AST while parsing and do the semantic analysis later on the tree.