BNF for Query of Queries

The Backus Naur Form (BNF) is a formal notation to describe programming syntax. The following is the BNF for Query of Queries:

Input ::= select_statement 
select_statement ::= select_expression ( <ORDER> <BY> order_by_list )? 

select_expression ::= ( <OPENPAREN> select_expression <CLOSEPAREN> |
    select_specification ) ( <UNION> ( <ALL> )? select_expression )? 

select_specification ::= <SELECT> ( <ALL> | <DISTINCT> )? select_list <FROM>
    from_table_list ( <WHERE> cond_exp )? ( <GROUP> <BY> group_by_list )? 
    ( <HAVING> cond_exp )? 

order_by_list ::= order_by_column ( <COMMA> order_by_column )* 

order_by_column ::= ( <IDENTIFIER> | <INTEGER_LITERAL> ) ( <ASC> | <DESC> )? 

group_by_list ::= column_ref ( <COMMA> column_ref )* 

from_table_list ::= <IDENTIFIER> ( <COMMA> <IDENTIFIER> )* 

select_list ::= select_column ( <COMMA> select_column )* 

select_column ::= <ASTERISK> 
 | <IDENTIFIER> <DOT> ( <ASTERISK> | <IDENTIFIER> ( alias )? ) 
 | expression ( alias )? 

alias ::= ( <AS> )? <IDENTIFIER> 

cond_exp ::= cond_term ( <OR> cond_exp )? 

cond_term ::= cond_factor ( <AND> cond_term )? 

cond_factor ::= ( <NOT> )? cond_test 

cond_test ::= cond_primary ( <IS> ( <NOT> )? ( <TRUE> | <FALSE> | <UNKNOWN> ) )? 

cond_primary ::= simple_cond 
 | <OPENPAREN> cond_exp <CLOSEPAREN> 

simple_cond ::= like_cond 
 | null_cond 
 | between_cond 
 | in_cond 
 | comparison_cond 

null_cond ::= row_constructor <IS> ( <NOT> )? <NULL> 

comparison_cond ::= row_constructor comparison_operator row_constructor 

between_cond ::= row_constructor ( <NOT> )? <BETWEEN> row_constructor 
    <AND> row_constructor 

in_cond ::= row_constructor ( <NOT> )? <IN> <OPENPAREN> ( expression_list )
    <CLOSEPAREN> 

row_constructor ::= expression 

comparison_operator ::= <LESSEQUAL> 
 | <GREATEREQUAL> 
 | <NOTEQUAL> 
 | <NOTEQUAL2> 
 | <EQUAL> 
 | <LESS> 
 | <GREATER> 

like_cond ::= string_exp ( <NOT> )? <LIKE> string_exp 

expression_list ::= expression ( <COMMA> expression )? 

expression ::= <STRING_LITERAL> 
 | <OPENPAREN> <STRING_LITERAL> <CLOSEPAREN> 
 | numeric_exp 

numeric_exp ::= numeric_term ( ( <PLUS> | <MINUS> ) numeric_exp )? 

numeric_term ::= numeric_factor ( ( <ASTERISK> | <SLASH> ) numeric_term )? 

numeric_factor ::= ( <PLUS> | <MINUS> )? numeric_primary 

numeric_primary ::= <INTEGER_LITERAL> 
 | <FLOATING_POINT_LITERAL> 
 | aggregate_func 
 | column_ref 
 | <OPENPAREN> numeric_exp <CLOSEPAREN> 

aggregate_func ::= <COUNT> <OPENPAREN> count_param <CLOSEPAREN> 
 | ( <AVG> | <SUM> | <MIN> | <MAX> ) <OPENPAREN> ( <ALL> | <DISTINCT> )?
    numeric_exp <CLOSEPAREN> 

count_param ::= <ASTERISK> 
 | ( <ALL> | <DISTINCT> )? numeric_exp 

string_exp ::= <STRING_LITERAL> 
 | column_ref 
 | <OPENPAREN> string_exp <CLOSEPAREN> 

column_ref ::= <IDENTIFIER> ( <DOT> <IDENTIFIER> )?

Comments