How to convert BNF to EBNF

后端 未结 2 1730
别跟我提以往
别跟我提以往 2020-11-28 14:46

How can I convert this BNF to EBNF?

 ::= var ;
 ::=  {;}


        
相关标签:
2条回答
  • 2020-11-28 15:15

    Remove the angle brackets and put all terminals into quotes:

    vardec ::= "var" vardeclist;
    vardeclist ::= varandtype { ";" varandtype }
    varandtype ::= ident { "," ident } ":" typespec
    ident ::= letter { idchar }
    idchar ::= letter | digit | "_"
    
    0 讨论(0)
  • 2020-11-28 15:35

    EBNF or Extended Backus-Naur Form is ISO 14977:1996, and is available in PDF from ISO for free*. It is not widely used by the computer language standards. There's also a paper that describes it, and that paper contains this table summarizing EBNF notation.

             Table 1: Extended BNF
    Extended BNF    Operator  Meaning
    -------------------------------------------------------------
    unquoted words            Non-terminal symbol
    " ... "                   Terminal symbol
    ' ... '                   Terminal symbol
    ( ... )                   Brackets
    [ ... ]                   Optional symbols
    { ... }                   Symbols repeated zero or more times
    { ... }-                  Symbols repeated one or more times†
    =               in        Defining symbol
    ;               post      Rule terminator
    |               in        Alternative
    ,               in        Concatenation
    -               in        Except
    *               in        Occurrences of
    (* ... *)                 Comment
    ? ... ?                   Special sequence
    

    The * operator is used with a preceding (unsigned) integer number; it does not seem to allow for variable numbers of repetitions — such as 1-15 characters after an initial character to make identifiers up to 16 characters long. This lis

    In the standard, open parenthesis ( is called start group symbol and close parenthesis ) is called end group symbol; open square bracket [ is start option symbol and close square bracket is end option symbol; open brace { is start repeat symbol and close brace } is end repeat symbol. Single quotes ' are called first quote symbol and double quotes " are second quote symbol.

    * Yes, free — even though you can also pay 74 CHF for it if you wish. Look at the Note under the box containing the chargeable items.


    The question seeks to convert this 'BNF' into EBNF:

    <vardec> ::= var <vardeclist>;
    <vardeclist> ::= <varandtype> {;<varandtype>}
    <varandtype> ::= <ident> {,<ident>} : <typespec>
    <ident> ::= <letter> {<idchar>}
    <idchar> ::= <letter> | <digit> | _
    

    The BNF is not formally defined, so we have to make some (easy) guesses as to what it means. The translation is routine (it could be mechanical if the BNF is formally defined):

    vardec     = 'var', vardeclist, ';';
    vardeclist = varandtype, { ';', varandtype };
    varandtype = ident, { ',', ident }, ':', typespec;
    ident      = letter, { idchar };
    idchar     = letter | digit | '_';
    

    The angle brackets have to be removed around non-terminals; the definition symbol ::= is replaced by =; the terminals such as ; and _ are enclosed in quotes; concatenation is explicitly marked with ,; and each rule is ended with ;. The grouping and alternative operations in the original happen to coincide with the standard notation. Note that explicit concatenation with the comma means that multi-word non-terminals are unambiguous.


    Casual study of the standard itself suggests that the {...}- notation is not part of the standard, just of the paper. However, as jmmut notes in a comment, the standard does define the meaning of {…}-:

    §5.8 Syntactic term

    When a syntactic-term is a syntactic-factor followed by an except-symbol followed by a syntactic-exception it represents any sequence of symbols that satisfies both of the conditions:

    a) it is a sequence of symbols represented by the syntactic-factor,

    b) it is not a sequence of symbols represented by the syntactic-exception.

    NOTE - { "A" } - represents a sequence of one or more A's because it is a syntactic-term with an empty syntactic-exception.

    0 讨论(0)
提交回复
热议问题