Is it possible to use Prolog to define a grammar for a non-natural language, for example, SQL?
If so, please guide me to some starting point.
Asked
Active
Viewed 144 times
0
-
how can I say that, for example: select - from - where are a key word, and they can be upcase or lowercase. thanks – Great Oct 07 '16 at 14:55
-
Is there any links or documents that explain the syntax and the use of the DCG in a simple way. please. I'm googling now but I'm little lost. Thanks in advance. – Great Oct 07 '16 at 15:03
1 Answers
2
Prolog supports Definite Clause Grammars (DCG). With this you can write context-free grammars like this:
sql --> select_statement.
select_statement --> select, from_statement.
from --> [from].
select --> [select].
This can be directly interpreted by a Prolog interpreter. I am not sure what kind of grammar SQL needs. It should be possible to write some kind of context-sensitive grammars, too.

marli
- 529
- 10
- 19
-
-
They are the [terminal symbols](https://en.wikipedia.org/wiki/Terminal_and_nonterminal_symbols) of the grammar. That are simple constants in Prolog, so writing them capitalized is wrong here. – marli Oct 07 '16 at 14:44
-
Alright thank you @marli. The comma means "and" I think ? "OR" can be expressed by "|" . yeah? And how can express that something can be repeated 0 or many times? – Great Oct 07 '16 at 15:27
-
Yes, the comma means "and". But "or" is expressed with a semicolon ";". This is handled like in Prolog. Actually the grammar style is just syntactic sugar. – marli Oct 07 '16 at 15:58
-
This is a grammar and not a regular expression. So you can only use recursion. E.g. moreThanOne --> moreThanOne; one. – marli Oct 07 '16 at 16:02
-
Try this: http://www.learnprolognow.org/lpnpage.php?pagetype=html&pageid=lpn-htmlse29 for dokumentation. – marli Oct 07 '16 at 16:04