Yacc parser not reducing specific production rules as intended

27 Views Asked by At

I am trying to implement a parser using yacc, and I have encountered an issue where the parser is not reducing a midline in the provided excerpt.

I understand that this a shift/reduce conflict, and that yacc's default is to shift. It is not clear to me how this can be amended, however -- all examples I have seen involve simple infix operators involving precedence and associativity. I have included the excerpt below, and the full code can be found with the appropriate commit permalinked on GitHub.

...

%token MINUS UNDERSCORE TEXT

%%

accept:
    | accept phrase
    ;

phrase:
      text
    | phrase text
    ;

text:
      literal
    | formatted
    ;

literal: TEXT ;

formatted:
      midline
    | underline
    ;

midline: MINUS phrase MINUS ;

underline: UNDERSCORE phrase UNDERSCORE ;

I have tested this code using the simple test case "The _cat -sat on- the_ mat", with the intention that in this case the midline nests within the underline. What happens is that neither midline nor underline are ever reduced to, and it fails with a syntax error. I would appreciate any insights or suggestions on what might be causing this issue and how to resolve it.

Thank you in advance for your help!

0

There are 0 best solutions below