|
|
||||||||||
|
|
SQL Grammars
The following SQL grammars are currently available:
The grammar definition shown below describes the
syntax for a subset of the SQL language. Using this grammar, the ProGrammar
parse engine can parse SQL statements and represent their contents as a parse
tree data structure.
// =================================================================
// ProGrammar Grammar Definition File
// -----------------------------------------------------------------
//
// SQL.GDL
//
// This example is intended for demonstration purposes only.
//
// (c) Copyright 1999, 2000 NorKen Technologies, Inc.
// All rights reserved.
//
// ==================================================================
grammar SQL <IGNORECASE,
SPACE = space_symbol,
HIDELITERALS,
SHOWDELIMITERS>
{
sql_list ::=
{ sql_def ";" };
sql_def ::=
schema
| base_table_def
| view_def
| privilege_def
| cursor_def
| manipulative_statement
;
// ================================================================
//
// SCHEMA DEFINITION LANGUAGE
//
// ================================================================
schema ::=
"create" "schema" "authorization" user
[schema_element_list];
schema_element_list ::= { schema_element };
schema_element ::=
base_table_def
| view_def
| privilege_def
;
// ---------------------------------------------------------------
// BASE TABLE DEFINITION
// ---------------------------------------------------------------
base_table_def ::=
"create" "table" table
"(" { base_table_element, "," } ")" ;
base_table_element ::=
column_def
| table_constraint_def
;
column_def ::=
column data_type [{ column_def_opt }]
;
column_def_opt ::=
"not" "null" ["unique" | "primary" "key"]
| "default" (literal | "null" | "user" )
| "check" "(" search_condition ")"
| "references" table [column_list]
;
table_constraint_def ::=
"unique" column_list
| "primary" "key" column_list
| "foreign" "key" column_list
"references" table [column_list]
| "check" "(" search_condition ")"
;
column_list ::= "(" { column, "," } ")" ;
// ---------------------------------------------------------------
// VIEW DEFINITION
// ---------------------------------------------------------------
view_def ::=
"create" "view" table [column_list]
"as" query_spec [with_check_option]
;
with_check_option ::=
"with" "check" "option"
;
// ---------------------------------------------------------------
// PRIVILEGE DEFINITION
// ---------------------------------------------------------------
privilege_def ::=
"grant" privileges "on" table "to" grantee_list
[with_grant_option]
;
with_grant_option ::=
"with" "grant" "option"
;
privileges ::=
"all" ["privileges"]
| { operation, "," }
;
operation ::=
"select"
| "insert"
| "delete"
| "update" [column_list]
| "references" [column_list]
;
grantee_list ::=
{ grantee, "," }
;
grantee ::=
"public"
| "user"
;
// ---------------------------------------------------------------
// CURSOR DEFINITION
// ---------------------------------------------------------------
cursor_def ::=
"declare" cursor "cursor" "for" query_exp
[order_by_clause]
;
order_by_clause ::=
"order" "by" { ordering_spec, "," }
;
ordering_spec ::=
intnum [asc_desc]
| column_ref [asc_desc]
;
asc_desc ::=
"asc"
| "desc"
;
// ================================================================
//
// MANIPULATIVE STATEMENTS
//
// ================================================================
manipulative_statement ::=
close_statement
| commit_statement
| delete_statement
| fetch_statement
| insert_statement
| open_statement
| rollback_statement
| select_statement
| update_statement
;
close_statement ::=
"close" cursor
;
commit_statement ::=
"commit" "work"
;
delete_statement ::=
"delete" "from" table
[ "where" "current" "of" cursor /* positioned */
| where_clause ] /* searched */
;
fetch_statement ::=
"fetch" cursor "into" target_list
;
insert_statement ::=
"insert" "into" table [column_list]
( values_spec | query_spec )
;
values_spec ::=
"values" "(" { insert_atom, "," } ")"
;
insert_atom ::=
"null"
| atom
;
open_statement ::=
"open" cursor
;
rollback_statement ::=
"rollback" "work"
;
select_statement ::=
"select" [all_or_distinct] selection
["into" target_list]
table_exp
;
all_or_distinct ::=
"all"
| "distinct"
;
update_statement ::=
"update" table "set" assignment_list
[ "where" "current" "of" cursor /* positioned */
| where_clause ] /* searched */
;
assignment_list ::=
{ assignment, "," }
;
assignment ::=
column "=" ("null" | scalar_exp)
;
target_list ::=
{ target, "," }
;
target ::=
parameter_ref
;
// ================================================================
//
// QUERY EXPRESSIONS
//
// ================================================================
query_exp ::=
{ query_term, "union" ["all"] }
;
query_term ::=
query_spec
| "(" query_exp ")"
;
query_spec ::=
"select" [all_or_distinct] selection table_exp
;
selection ::=
wildcard
| { scalar_exp , "," }
;
wildcard<TERMINAL> ::= "*";
table_exp ::=
from_clause
[where_clause]
[group_by_clause]
[having_clause]
;
from_clause ::=
"from" { table_ref, "," }
;
table_ref ::=
table /* [range_variable] */
;
where_clause ::=
"where" search_condition
;
group_by_clause ::=
"group" "by" { column_ref, "," }
;
having_clause ::=
"having" search_condition
;
// ================================================================
//
// SEARCH CONDITIONS
//
// ================================================================
search_condition ::=
{ search_term, "or" }
;
search_term ::=
{ search_factor, "and" }
;
search_factor ::=
"(" search_condition ")"
| not_tag search_condition
| predicate
;
not_tag ::= "not";
predicate ::=
test_for_null
| existence_test
| relational_exp
;
relational_exp ::=
scalar_exp
( comparison relational_rvalue
| [not_tag] relational_predicate
)
;
relational_rvalue ::=
scalar_exp
| [any_all_some] subquery ;
relational_predicate ::=
between_predicate
| like_predicate
| in_predicate
;
between_predicate ::=
"between" scalar1 "and" scalar2 ;
scalar1 ::= scalar_exp;
scalar2 ::= scalar_exp;
like_predicate ::=
"like" atom [escape] ;
in_predicate ::=
"in" (subquery | "(" atom_list ")") ;
test_for_null ::=
column_ref "is" [not_tag] "null"
;
existence_test ::=
"exists" subquery
;
escape ::=
"escape" atom
;
atom_list ::=
{ atom, "," }
;
any_all_some ::=
"any"
| "all"
| "some"
;
subquery ::=
"(" "select" [all_or_distinct] selection table_exp ")"
;
// ================================================================
//
// SCALAR EXPRESSIONS
//
// ================================================================
scalar_exp<SHOWDELIMITERS> ::=
{ scalar_term, scalar_term_op }
;
scalar_term ::=
{ scalar_factor, scalar_factor_op }
;
scalar_factor ::=
unary_op scalar_exp /* unary plus */
| atom
| function_ref
| column_ref
| "(" scalar_exp ")"
;
unary_op ::=
"+" | "-"
;
scalar_term_op ::=
"+" | "-"
;
scalar_factor_op ::=
"*" | "/"
;
atom ::=
parameter_ref
| literal
| "user"
;
parameter_ref ::=
parameter [["indicator"] parameter]
;
function_ref ::=
function_name "("
( wildcard
| distinct_literal column_ref
| [all_literal] scalar_exp
) ")"
;
distinct_literal ::=
"distinct" ;
all_literal ::=
"all" ;
function_name ::=
"avg"
| "min"
| "max"
| "sum"
| "count"
;
// ================================================================
//
// MISC
//
// ================================================================
literal ::=
string
| approxnum
| intnum
;
table ::=
name ["." name]
;
column_ref ::=
name ["." name ["." name]] ["." wildcard]
;
/* data types */
data_type ::=
char_type
| numeric_type
| decimal_type
| integer_type
| smallint_type
| float_type
| real_type
| double_type
;
char_type ::=
("character"|"char") [data_size]
;
numeric_type ::=
"numeric" [data_size]
;
decimal_type ::=
"decimal" [data_size]
;
integer_type ::=
"integer"
| "int"
;
smallint_type ::=
"smallint"
;
float_type ::=
"float" [data_size]
;
real_type ::=
"real"
;
double_type ::=
"double" "precision"
;
data_size ::=
"(" intnum ["," intnum] ")"
;
column ::=
name
;
cursor ::=
name
;
parameter ::=
":" name
;
range_variable ::=
name
;
user ::=
name
;
// ================================================================
//
// TOKENS
//
// ================================================================
comparison ::=
"=" | "<>" | "<" | ">" | "<=" | ">="
;
name ::=
'[A-Za-z][A-Za-z0-9_#]*'
;
intnum ::=
'[0-9]+'
;
string<TERMINAL,SPACE=""> ::=
'\'[^']*\''
| "\"" [{'[^"]' | "\"\""}] "\""
;
approxnum ::=
'-?(([0-9]*\.[0-9]+)([eE][-+]?[0-9]+)?|([0-9]+))'
;
space_symbol <TERMINAL,HIDDEN> ::=
{ ("\32" | "\r" |"\n"|"\t")
| "--" *("\n")
| "/*" *("*/") "*/",0 }
;
};
|
|||||||||
|
For comments or questions about this site, please contact webmaster@programmar.com Copyright © 1998-2008 NorKen Technologies, Inc. All rights reserved. |
||||||||||