I am new to bison, I have some basic questions if you could help me with them:
-
Which one is right from the following:
%left ’*’ ’/’or
%left '*' '/'that means instead of getting the token I use it to define it in the parser file
-
Can I define a rule like this:
EXP -> EXP "and" EXPinstead of
EXP -> EXP AND EXP //AND here is a token -
If I have LEX and BISON files for building a parser which one should include the other and if
I have used a common header file in which one of them should the file be defined? -
If the BISON algorithm found a match according to one of the rules what happens first it makes reduce then it does the action defined for the rule that matched or first it does the action and after that makes reduce to the stack?
Its tough to tell what you are asking due to your formatting, but think the answer is no.
%leftjust defines a a token (exactly like%tokendoes) and in addition sets a precedence level for that token. You still have to “get” the token by recognizing it in your lexer and returning the appropriate token value.While you can use
"and", you don’t want to because its almost impossible to get right. Its much better to useANDorand(no quotes). The difference is that using quotes creates a token that is not output as a#definein the.tab.hfile, so there’s no easy way to generate that token in your lexer.There a a number of ways to do it. The easiest is to have neither include the other and have the lex file include the header generated by bison’s
-dflag — this is what most examples do. It is also possible to directly include thelex.yy.cfile in the 3rd section of the.yfile OR include the.tab.cin the top section of the.lfile (but not both!) in which case you’ll only compile the one file.Its executes the action for the rule first (so the values for the items on the RHS are available while the action is executing), and then does the stack reduction, replacing the RHS items with the value the action put int
$$.