Search code examples
bnf

Is This Valid BNF Grammar?


I just wrote up some BNF and I'm a noobie at it, so I wanted to check with you guys if this is valid grammar, and if the input supplied can run?

BNF:

<expr> -> <id>  <id> + <id> +  | <id>  <id> + <id> - | <id>  <id> - <id> + | <id>  <id> - <id> -
<id> ->  A | B | C

My postfix input:

A B + C -

Would this work? Thanks in advance.


Solution

  • The valid separator between a symbol and an expression is ::= and not ->. Check out wikipedia for details.

    Anyway, a better grammar would look like this:

    <expr> ::= <id> [ <id> [ <operator> ]? ]{2}
    <operator> ::= '-' | '+'
    <id> ::= [ '0' .. '9' | 'A' .. 'Z' | 'a' .. 'z' ]+