voluminat0
voluminat0

Reputation: 906

Bison one or more occurrences in grammar file

My program that needs to be parsed should be of the form:

program   : [declaration]+
          ;

Which should mean: The program consists of one or more declarations. Declaration on its turn is of course defined in a similar way, and so on...

Currently, I'm getting an error on the + from the Bison parser. How do I define the one or more condition in a correct way with bison?

Upvotes: 9

Views: 3982

Answers (2)

voluminat0
voluminat0

Reputation: 906

Apparently,

Bison does not support the + or * symbols to denote these things.

How I solved it:

program     : declarations
        ;

declarations    : declaration declarations
        | declaration
        ;

Upvotes: -1

user207421
user207421

Reputation: 310909

One or more:

declarations
    : declaration
    | declarations declaration
    ;

Zero or more:

declarations
    : /* empty */
    | declarations declaration
    ;

Upvotes: 22

Related Questions