notationbnfebnf

How can I write a sentence in BNF?


What is the syntax for writing a sentence in BNF? The structure or correctness of it doesn't matter. I only care whether it has 1 or more words, which may or may not be separated by spaces, and which may or may not contain symbols or numbers.


Solution

  • What is the syntax for writing a sentence in BNF?

    Backus-Naur form (BNF) isn't a data format like JSON or PDF, it's a description of the grammar that defines a given format. You wouldn't write a sentence in BNF, bu you might use BNF to describe what a sentence is. That description would probably enumerate the characters acceptable in a sentence, tell you that words are made up up non-whitespace characters, and that sentences are sequences of words separated by spaces and ending with a sentence-terminating punctuation mark. Of course, there are many rules about what makes a valid sentence in a natural language like English, so if you were going down that route you'd probably also need to create entities for things like subject-phrase, verb-phrase, object-phrase, etc.

    A complete English grammar expressed in BNF would surely be exceedingly complex. BNF is better suited to grammars of computer languages and formats. You might have a BNF description of JSON, for example, or of allowable syntax in C or Java.

    I only care whether it has 1 or more words, which may or may not be separated by spaces, and which may or may not contain symbols or numbers.

    The rules for BNF are available on the Wikipedia page among other places. Again, you wouldn't write a specific sentence with BNF, but you'd say what's allowable for a sentence. So you might have rules like:

    <char> ::= "A" | "B" | "C" | "D" ...
    <word> ::= <char>*
    <terminator> ::= "." | "!" | "?"
    <phrase> ::= <word> | <word> " " <phrase>
    <sentence> ::= <phrase> <terminator>
    

    Each rule is built up from other rules, or sometimes built recursively... e.g. the rule for phrase says that a phrase is a word or a word plus a space plus a phrase, so you could have any number of space-separated words.