Johan Danforth
Johan Danforth

Reputation: 4579

EBNF for simple conditions

I'm trying to write a EBNF specification for a simple expression of condition-statements similar to that within if statement tests, for example: aaa = "xxx" or aaa != "xxx" AND bbb = "yyy" or more complex with parentheses like aaa = "xxx" AND bbb = "yyy" AND ( ccc = "zzz" OR ddd = "www" )

The spec I've come up with so far is this, and my question - is it correct?

<cond> ::= <id> <rel_op> <value>
    | <cond> <op> <cond>
    | "(" <cond> ")"

<op> ::= AND
    | OR

<rel_op> ::= "=" 
    | "!="

<value> ::= <quot> <char>* <quot>

<quot> ::= "

Side-note - I'm new to E/BNF and planning on implementing a parser in C# based on this specification to replace an old parser that is spaghetti-like and stinks.

Upvotes: 1

Views: 1814

Answers (1)

Johan Danforth
Johan Danforth

Reputation: 4579

This is what I came up with, and has also implemented successfully:

<cond> := <rel_cond> {<op> <cond>} 
    | <parenth_cond> {<op> <cond>}

<rel_cond> := <id> <rel_op> <value>

<rel_op> := "=" | "!=" | "~" | "!~"

<parenth_cond> := "(" <cond> ")"

<op> := AND | OR

<id> := <char>* 

<value> := <quot> <char>* <quot>

<quot> := "

Upvotes: 2

Related Questions