Re: Yacc - nesting of if - else if - else - end ifstatements

822 views
Skip to first unread message

A.T.Hofkamp

unread,
Jul 31, 2012, 3:08:02 AM7/31/12
to ply-...@googlegroups.com
On 07/12/2012 06:19 PM, Joshua Boshi wrote:
> Below is my sucessfull approach without else-if part (I listed only relevant
> rules):
>

> block_if_statement: RULE_OPEN IF CONDITION RULE_CLOSE statements RULE_OPEN
> END RULE_CLOSE
>
> block_if_statement: RULE_OPEN IF CONDITION RULE_CLOSE statements
> block_else_statement

You didn't say where the conflict happens, bu my guess is here.

After "statements", the RULE_OPEN token decides whether or not it should start a
'block_else_staetment', but it cannot do that since the same token is also in the first rule.

The solution is probably not to make two if statements, but just one, with an 'if' part, optional
'elsif' parts, and finally an optional 'else' part.


BTW is making "(END)" a single token possible? It would make the grammar a lot simpler.

> I think that this is very common problem in parsing and I hope that somebody
> here have already solved it :-)

You have to be careful where to split grammar rules; at the end of a rule the algorithm needs to
make a decision how to continue which may not always be possible.

Otherwise, your problem is very simple in terms of LALR(1).


Albert
Reply all
Reply to author
Forward
0 new messages