Bison one or more occurrences in grammar file

Deadly 提交于 2021-01-21 08:00:15

问题


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?


回答1:


One or more:

declarations
    : declaration
    | declarations declaration
    ;

Zero or more:

declarations
    : /* empty */
    | declarations declaration
    ;



回答2:


Apparently,

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

How I solved it:

program     : declarations
        ;

declarations    : declaration declarations
        | declaration
        ;


来源:https://stackoverflow.com/questions/29235967/bison-one-or-more-occurrences-in-grammar-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!