I am trying to write a mini parser for a simple language whose specification is as follows:
A search term is represented by. Both searchkey
and searchvalue
is a word formed by using alphanumeric characters.
<searchkey> <searchoperator> <search value>
: represents the exact match(similar to "=").
ex :
response:success
multiple search terms can be combined with AND
& OR
operator.
ex.
response:sucess AND extension:php
By default AND
has higher precedence than OR
but this can be changed by grouping the search terms in a round bracket.
ex:
response:sucess AND (extension:php OR extension:css)
along with :
other valid operators search operators are >, >=, <, <=
.
ex.
time >= 2020-01-09 AND response:success OR os:windows
A search term could be preceded with NOT
keyword to do the negation.
ex.
response:success AND NOT os:windows
I have written this grammar in ebnf notation :
<exp> ::= <exp> " OR " <exp> | <logical_and>
<logical_and> ::= <exp> " AND " <exp> | <logical_not>
<logical_not> ::= " NOT " <logical_not> | <term>
<term> ::= "(" <exp> ")" | <atom>
<atom> ::= <word> <op> <word>
<op> ::= ">" | ">=" | ":" | "<" | "<="
<word> ::= ("a" | "b" | "c" | "d" | "e" | "f" | "g"
| "h" | "i" | "j" | "k" | "l" | "m" | "n"
| "o" | "p" | "q" | "r" | "s" | "t" | "u"
| "v" | "w" | "x" | "y" | "z")+
But originally I had come up with this grammar.
<expr> ::= <or>
<or> ::= <and> (" OR " <and>)*
<and> ::= <unary> ((" AND ") <unary>)*
<unary> ::= " NOT " <unary> | <equality>
<equality> ::= (<word> ":" <word>) | <comparison>
<comparison> ::= "(" <expr> ")" | (<word> (" > " | " >= " | " < " | " <= ") <word>)+
<word> ::= ("a" | "b" | "c" | "d" | "e" | "f" | "g"
| "h" | "i" | "j" | "k" | "l" | "m" | "n"
| "o" | "p" | "q" | "r" | "s" | "t" | "u"
| "v" | "w" | "x" | "y" | "z")+
is there any difference if I choose one over another? Can there exist multiple grammars for a language? Is there any formal way to generate grammar for a language? this is a small language so its easy to get the grammar intuitively by tweaking it but what if the langue specification is huge?