9

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?

sidanmor
  • 5,079
  • 3
  • 23
  • 31
voluminat0
  • 866
  • 2
  • 11
  • 21

2 Answers2

22

One or more:

declarations
    : declaration
    | declarations declaration
    ;

Zero or more:

declarations
    : /* empty */
    | declarations declaration
    ;
user207421
  • 305,947
  • 44
  • 307
  • 483
-1

Apparently,

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

How I solved it:

program     : declarations
        ;

declarations    : declaration declarations
        | declaration
        ;
voluminat0
  • 866
  • 2
  • 11
  • 21
  • 1
    That's not one or more, that's zero or more. For one or more you want to replace `/* empty */` with declaration. – sepp2k Mar 24 '15 at 15:26
  • 1
    This solution is right-recursive, which is not the correct way to use an LALR(1) parser. It should be left-recursive. – user207421 Jun 21 '19 at 11:45