Question

I have below yacc grammar:

OPTIONS:OPTIONS OPTION                  {printf("%s\n", "Options    enabled");}
        | OPTION                        {printf("%s\n",     "First option");}
        |
        ;

OPTION:   DEBUG                         {printf("%s\n",   "debug enabled");}
        | NESTING                       {printf("%s\n", "nesting enabled");}
        | '(' STACK '=' NAME ')'        {printf("%s\n", "stack size given");}
    | NOLIST                        {printf("%s\n", "nolist enabled");}
    | VIEW EQ NAME                  {printf("%s\n", "this is a view, first name is view name");}
    ;

For empty rule, it is giving me shift/reduce error. Y.output file for the error looks like:

17 OPTIONS: . OPTIONS OPTION
18        | . OPTION
19        | .  [SEMICOLON, VIEW, DEBUG, NESTING, NOLIST, '(']
20 OPTION: . DEBUG
21       | . NESTING
22       | . '(' STACK '=' NAME ')'
23       | . NOLIST
24       | . VIEW EQ NAME

NAME     shift, and go to state 4
VIEW     shift, and go to state 11
DEBUG    shift, and go to state 12
NESTING  shift, and go to state 13
NOLIST   shift, and go to state 14
'('      shift, and go to state 15

VIEW      [reduce using rule 19 (OPTIONS)]
DEBUG     [reduce using rule 19 (OPTIONS)]
NESTING   [reduce using rule 19 (OPTIONS)]
NOLIST    [reduce using rule 19 (OPTIONS)]
'('       [reduce using rule 19 (OPTIONS)]
$default  reduce using rule 19 (OPTIONS)

Can someone suggest, how to solve the issue?

Was it helpful?

Solution

You have a recursion on rule OPTIONS. The conflit exists because there are two ways to stop that recursion. Look, for instance, if you have just One option, there are two different parsing trees.

OPTIONS         or                OPTIONS
   |                               |    \
 OPTION                        OPTIONS  OPTION
   |                               |
  ...                       "empty rule"

So, remove the empty rule or the OPTIONS : OPTION (maintaining the empty rule) and the problem should be gone.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top