forked from dkackman/SqlLinq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SQL-ANSI-89.4.grm
213 lines (165 loc) · 7.14 KB
/
SQL-ANSI-89.4.grm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
! -----------------------------------------------------------------------------------
! SQL '89
!
! SQL (Structured Query Language)
!
! The SQL programming language was developed as a uniform means of modifying and
! querying relational databases. By using a single abstract language to interact
! with the database, programs can be written that are independent of the vender and
! format of the database itself. Variations are used by Oracle, Microsoft and most
! other developers
!
! In 1992, a new version SQL as released but has yet to be implemented by any major
! developer. The reason to this lies in the sheer complexity of the grammar. SQL 92
! contains over 300 rules and a myraid of new features. For instance, in SQL 92, the
! developer can create types using COBOL syntax rather than the normal data types of
! SQL 89. This reason combined with the fact that SQL 89 is a time-tested and
! ample tool maintains it as the standard of the database industry.
!
! Update:
! 02/17/2005
! Added "NULL" to the <Value> rule, I also added more comments to the grammar
!
! v.2 added boolean literal
! v.4 allow <Expression> to contain an aggregate
! v.5 made the FROM clause in DELETE statements a non terminal
!
! Note: This is an ad hoc version of the language. If there are any flaws, please
! visit www.devincook.com/goldparser
! -----------------------------------------------------------------------------------
"Name" = 'SQL 89'
"Version" = '1989'
"About" = 'This is the ANSI 89 version of SQL. Variations are used by'
| 'Oracle, Microsoft and most other database developers'
"Start Symbol" = <Query>
! =============================================================================
! Comments
! =============================================================================
Comment Start = '/*'
Comment End = '*/'
Comment Line = '--'
! =============================================================================
! Terminals
! =============================================================================
!{String Ch 1} = {Printable} - ["]
{String Ch 2} = {All Valid} - [''] - {Control Codes}
{String Ch 3} = {Printable} - [#]
{Id Ch Standard} = {Alphanumeric} + [_]
{Id Ch Extended} = {Printable} - ['['] - [']']
!StringLiteral = '"'{String Ch 1}*'"' | ''{String Ch 2}*''
StringLiteral = ''{String Ch 2}*''
IntegerLiteral = {Digit}+
RealLiteral = {Digit}+'.'{Digit}+
BooleanLiteral = 'TRUE' | 'FALSE'
DateLiteral = '#'{String Ch 3}*'#'
Function = 'Value()'
!----- Identifiers in SQL are very complex.
Id = ({Letter}{Id Ch Standard}* | '['{Id Ch Extended}+']') ('.'({Letter}{Id Ch Standard}* | '['{Id Ch Extended}+']'))?
! =============================================================================
! Rules
! =============================================================================
<Query> ::= <Select Stm>
! =============================================================================
! Select Statement
! =============================================================================
<Select Stm> ::= SELECT <Columns> <From Clause> <Where Clause> <Group Clause> <Having Clause> <Order Clause>
<Columns> ::= <Restriction> '*'
| <Restriction> <Column List>
<Column List> ::= <Column Item> ',' <Column List>
| <Column Item>
<Column Item> ::= <Column Source>
<Column Source> ::= <Aggregate>
| Id
| <Aggregate> <Column Alias>
| Id <Column Alias>
<Column Alias> ::= AS Id
<Restriction> ::= ALL
| DISTINCT
|
<Aggregate> ::= Count '(' '*' ')'
| Count '(' <Expression> ')'
| Count '(' 'DISTINCT' <Expression> ')'
| Count '(' 'ALL' <Expression> ')'
| Avg '(' <Expression> ')'
| Min '(' <Expression> ')'
| Max '(' <Expression> ')'
| StDev '(' <Expression> ')'
| StDevP '(' <Expression> ')'
| Sum '(' <Expression> ')'
| Var '(' <Expression> ')'
| VarP '(' <Expression> ')'
<From Clause> ::= FROM <Id List> <Join Chain>
<Join Chain> ::= <Join> <Join Chain>
|
<Join> ::= INNER JOIN <Id List> ON Id '=' Id
| LEFT JOIN <Id List> ON Id '=' Id
| RIGHT JOIN <Id List> ON Id '=' Id
| JOIN <Id List> ON Id '=' Id
<Where Clause> ::= WHERE <Expression>
|
<Group Clause> ::= GROUP BY <Id List>
|
<Order Clause> ::= ORDER BY <Order List>
|
<Order List> ::= ID <Order Type> ',' <Order List>
| ID <Order Type>
| Function <Order Type>
<Order Type> ::= ASC
| DESC
|
<Having Clause> ::= HAVING <Expression>
|
! =============================================================================
! Expressions
! =============================================================================
<Expression> ::= <And Exp> OR <Expression>
| <And Exp>
<And Exp> ::= <Not Exp> AND <And Exp>
| <Not Exp>
<Not Exp> ::= NOT <Pred Exp>
| <Pred Exp>
<Pred Exp> ::= <Add Exp> BETWEEN <Add Exp> AND <Add Exp>
| <Add Exp> NOT BETWEEN <Add Exp> AND <Add Exp>
| <Value> IS NOT NULL
| <Value> IS NULL
| <Add Exp> LIKE StringLiteral
| <Add Exp> NOT LIKE StringLiteral
| <Add Exp> IN <In List>
| <Add Exp> NOT IN <In List>
| <Add Exp> '=' <Add Exp>
| <Add Exp> '<>' <Add Exp>
| <Add Exp> '!=' <Add Exp>
| <Add Exp> '>' <Add Exp>
| <Add Exp> '>=' <Add Exp>
| <Add Exp> '<' <Add Exp>
| <Add Exp> '<=' <Add Exp>
| <Add Exp>
<Add Exp> ::= <Add Exp> '+' <Mult Exp>
| <Add Exp> '-' <Mult Exp>
| <Mult Exp>
| <Aggregate>
<Mult Exp> ::= <Mult Exp> '*' <Negate Exp>
| <Mult Exp> '/' <Negate Exp>
| <Negate Exp>
<Negate Exp> ::= '-' <Value>
| <Value>
<Value> ::= <Tuple>
| ID
| IntegerLiteral
| RealLiteral
| StringLiteral
| NULL
| BooleanLiteral
| DateLiteral
| Function
<Tuple> ::= '(' <Select Stm> ')'
| '(' <Expr List> ')'
<In List> ::= '(' <Value List> ')'
<Value List> ::= <Value> ',' <Value List>
| <Value>
<Expr List> ::= <Expression> ',' <Expr List>
| <Expression>
<Id List> ::= <Id Member> ',' <Id List>
| <Id Member>
<Id Member> ::= Id
| Id Id