Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert BNF to EBNF

Tags:

bnf

ebnf

How can I convert this BNF to EBNF?

<vardec> ::= var <vardeclist>;
<vardeclist> ::= <varandtype> {;<varandtype>}
<varandtype> ::= <ident> {,<ident>} : <typespec>
<ident> ::= <letter> {<idchar>}
<idchar> ::= <letter> | <digit> | _
like image 670
Deshi Basara Avatar asked Feb 17 '13 14:02

Deshi Basara


People also ask

What is the difference between EBNF and BNF?

BNF syntax can only represent a rule in one line, whereas in EBNF a terminating character, the semicolon, marks the end of a rule. Furthermore, EBNF includes mechanisms for enhancements, defining the number of repetitions, excluding alternatives, comments, etc.

What is EBNF?

In computer science, extended Backus–Naur form (EBNF) is a family of metasyntax notations, any of which can be used to express a context-free grammar. EBNF is used to make a formal description of a formal language such as a computer programming language.


1 Answers

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.

like image 196
Jonathan Leffler Avatar answered Oct 01 '22 02:10

Jonathan Leffler