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?
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.
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.
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.
One or more:
declarations
: declaration
| declarations declaration
;
Zero or more:
declarations
: /* empty */
| declarations declaration
;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With