If the grammar is ambiguous, there is conflict between rules of the grammar: either between reducing with two different rules or between reducing by a rule and shifting an input symbol. Such a conflict is resolved at parser generation time by selecting the highest priority action, where the priority of a reduce action is determined by the closeness of the rule to the beginning of the grammar. A priority is assigned to a shift by associating it with the rule that results in the shift being performed.
For example, if the grammar contains the two rules:
:if
expression
:then
statement
:else
statement
:if
expression
:then
statement
this results in a conflict in the parser between a shift of
:else
, for rule a, and a reduce by rule b. This conflict may be resolved by listing rule a earlier in the grammar than rule b. This ensures that the shift is always done.
Note that ambiguities cannot always be resolved successfully in this way. In this example, if the ambiguity is resolved the other way around, by listing rule b first, this results in the
if ... then ...
part of an
if ... then ... else ...
statement being reduced, and a syntax error is produced for the
else
part.
During parser generation, any conflicts between rules are reported, together with information about how the conflict was resolved.