Take a look at state 7 from the debug output. It describes the case when the parser has already accepted the following set of tokens:
ifstatement ::= If Number A statement B *
Here are two parameters that the parser can select when the Newline token appears in this case:
Remember this and switch to state 6. This shift is prescribed by the following rule from your grammar:
ifstatement ::= If Number A statement B Newline Else A statement B.
Consider the current rule as completed and return to the top-level rule. This abbreviation is determined by this rule from your grammar:
ifstatement ::= If Number A statement B.
With LALR (1), the parser has no other option to fail in this case due to the fact that it cannot look forward to the next tokens in the stream. He cannot predict that Else will appear after Newline.
Revise your grammar to avoid this conflict. I can only add that new line characters are usually not included in the grammar of a language. Tokenizers typically treat them as a marker border, similar to other space characters.
source share