2

I'm not quite sure how to answer a question for my computer languages class. I am to convert the following statement from EBNF form to BNF form:

EBNF: expr --> [-] term {+ term}

I understand that expressions included within curly braces are to be repeated zero or more times, and that things included within right angle braces represents zero or one options. If my understanding is correct, would this be a correct conversion?

My BNF:

expr --> expr - term
       | expr + term
       | term

Bonus Reading

Ian Boyd
  • 246,734
  • 253
  • 869
  • 1,219
user2105927
  • 21
  • 1
  • 2
  • This question is not a duplicate. The linked question asks for the list of general rules. This question is about a specific expression. And while the rules *might* be useful in answering this question: the linked answer does not answer *this* question. – Ian Boyd Jul 10 '20 at 15:05
  • @IanBoyd General rules are better, though, because the answer to the question should be useful to more than one person. And, the rules in that other question are sufficient to solve this question. – Brilliand Jul 11 '20 at 08:33
  • 1
    @Brilliand I disagree. I tried my stab at applying the rules to this practical example. The rules are written in an abstract 3rd notation, that isn't BNF or EBNF *(e.g. What is **`ε`**? What is **`.`**?)*. And it seems to have implied steps (i.e. i *think* the rules are trying to indicate that you have to introduce intermediate definitions). So, if it was me, i'm left asking the question. Perhaps the rules quoted need to be edited use some form of Backus-Naur grammar. – Ian Boyd Jul 11 '20 at 16:37

1 Answers1

2

I don't think that's correct. In fact, I don't think the EBNF is actually valid EBNF. The answer to the question How to convert BNF to EBNF shows how valid EBNF is constructed, quoting from ISO/IEC 14977:1996, the Extended Backus-Naur Form standard.

I think the expression:

expr --> [-] term {+ term}

should be written:

expr = [ '-' ] term { '+', term };

This means that an expression consists of an optional minus sign, followed by a term, followed by a sequence of zero of more occurrences of a plus sign and a term.

Next question: which dialect of BNF are you targeting? Things get tricky here; there are many dialects. However, here's one possible translation:

<expr> ::= [ MINUS ] <term> <opt_add_term_list>

<opt_add_term_list> ::= /* Nothing */
     | <opt_add_term_list> <opt_add_term>

<add_term> ::= PLUS term

Where MINUS and PLUS are terminals (for '-' and '+'). This is a very austere but minimal BNF. Another possible translation would be:

<expr> ::= [ MINUS ] <term> { PLUS <term> }*

Where the { ... }* part means zero or more of the contained pattern ... (so PLUS <term> in this example). Or you could use quoted characters:

<expr> ::= [ '-' ] <term> { '+' <term> }*

And so the list of possible alternatives goes on. You'll have to look at the definition of BNF you were given to work to, and you should complain about the very sloppy EBNF you were given, if it was meant to be ISO standard EBNF. If it was just some random BNF-style language called EBNF, I guess it is just the name that is confusing. Private dialects are fine as long as they're defined, but it isn't possible for people not privy to the dialect to know what the correct answer is.

Community
  • 1
  • 1
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278