1
#! /usr/local/bin/perl -ws
6
my $grammar = <<'EOGRAMMAR';
11
| { "Could you rephase that?\n" }
13
select: ask_select qualifier(?) field /of/ qualifier(?) table
14
{ "SELECT DISTINCT $item[3]\nFROM $item[6]\n" }
15
| ask_select qualifier(?) table /'?/ qualifier(?) field
16
{ "SELECT DISTINCT $item[6]\nFROM $item[3]\n" }
17
| ask_select qualifier(?) table
18
{ "SELECT *\nFROM $item[3]\n" }
20
sum: ask_count table prep qualifier field prep(?) value
21
{ "SELECT COUNT(*)\nFROM $item[2]\n" .
22
"WHERE $item[5] = $item[7]\n" }
24
{ "SELECT COUNT(*)\nFROM $item[2]\n" }
26
identify: ask_select(?) /who supplies/ qualifier value
27
{ "SELECT supplier\nFROM merchandise\n" .
28
"WHERE name = $item[4]\n" }
30
| whats qualifier field /of/ qualifier /suppliers? of/ value
31
{ "SELECT suppliers.$item[3]\n" .
32
"FROM suppliers, products\n" .
33
"WHERE products.name = $item[-1]\n" .
34
"AND suppliers.name = products.supplier \n" }
39
field: /(name)s?/ { $1 }
40
| /(product)s?/ { $1 }
41
| /(id)(entit(y|ies))?/ { $1 }
42
| /(quantit(y|ies))/ { $1 }
43
| /(received)s?/ { $1 }
44
| /(supplier)s?/ { $1 }
46
| /(address)(es)?/ { $1 }
48
table: /suppliers?/ { 'suppliers' }
50
| /orders?/ { 'orders' }
52
qualifier: /the|every|all( the)?|any|our/
54
ask_select: reply to_me
56
ask_count: ask_select(?) /how (many|much)/
58
reply: /tell|show|list/
60
value: /\w+/ { qq{'$item[1]'} }
62
to_me: /((to )?(me|us))?/
64
whats: /what's|what (is|are)/
66
prep: /for|of|with|by/
70
my $parser = Parse::RecDescent->new($grammar)
79
my $SQL = $parser->translate($_);
84
how many orders for the product spam are there?
85
tell me how many suppliers by the name of Jones we have
86
what are the names of our suppliers of trinitrotoluene?
87
tell me our suppliers' names
89
show us the suppliers of the mechanise
90
list all supplier names
91
how many orders are there?
92
how much merchandise do we carry?
93
who supplies our nitrocelluose?
94
tell me who supplies our ethylacetate?
95
what are the addresses of our suppliers of trinitrotoluene?