Search code examples
bisonbnf

How to convert advanced EBNF construction (using braces) to BNF (to be used with Bison)


I am learning Flex/Bison by my own, and I am doing some experiments with VHDL93. I have problems with some grammatical "productions" (as called in the standard) which uses curly braces.

I understand the meaning and how to convert this example given in the standard:

term ::= factor { multiplying_operator factor }
term ::= factor | term multiplying_operator factor

But what about:

choices ::= choice { | choice }

And I do not figure how to convert this

block_configuration ::=
     for block_specification
          { use_clause }
          { configuration_item }
     end for ;

I read a lot of questions in stackoverflow and searched for some EBNF tutorial but I didn't see anything about this kind of construction.

Finally, I understand that this kind of construction:

configuration_declarative_part ::=
     { configuration_declarative_item }

Is translated as:

configuration_declarative_part ::= |
     configuration_declarative_part configuration_declarative_item

Is it ok?

Thanks in advance for any help.


Solution

  • The general case is that each {...} will create two new rules (call them n1 and n2) and be replaced by the second one:

    n1:  ...whatever was in the braces...
    n2: /* empty */ | n2 n1 ;
    

    so for choices ::= choice { | choice }, the becomes:

    n1 ::= /*empty*/ | choice
    n2 ::= /*empty*/ | n2 n1
    choices ::= choice n2
    

    you then refactor it to remove ambiguity and simplify it if you wish. The above reduces to the equivalent:

    choices ::= choice | choices choice