Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 878
voluminat0 Avatar asked Mar 24 '15 15:03

voluminat0


People also ask

What is a Bison grammar?

In a Bison grammar, a grammar rule can have an action made up of C statements. Each time the parser recognizes a match for that rule, the action is executed. See section Actions. Most of the time, the purpose of an action is to compute the semantic value of the whole construct from the semantic values of its parts.

How does Bison parser work?

The Bison parser is a bottom-up parser. It tries, by shifts and reductions, to reduce the entire input down to a single grouping whose symbol is the grammar's start-symbol. Steps to use Bison: Write a lexical analyzer to process input and pass tokens to the parser (calc.

What is type in Bison?

Here nonterminal is the name of a nonterminal symbol, and type is the name given in the %union to the alternative that you want (see The Union Declaration). You can give any number of nonterminal symbols in the same %type declaration, if they have the same value type.


1 Answers

One or more:

declarations
    : declaration
    | declarations declaration
    ;

Zero or more:

declarations
    : /* empty */
    | declarations declaration
    ;
like image 192
user207421 Avatar answered Sep 22 '22 09:09

user207421