Home > Back-end >  Doing BNF for a simplified SQL “where” clause
Doing BNF for a simplified SQL “where” clause

Time:10-24

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?

CodePudding user response:

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.

CodePudding user response:

One option would be as follow, with examples in-line:

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

condition:
    expression [= != < <= > >= IN] expression     # Revenue > 0
    expression IS [NOT] NULL                      # Revenue IS NOT NULL
    condition [AND | OR ] condition               # Revenue > 0 AND Profit < 100
  • Related