[?] Shouldn't precedence rules fix this shift/reduce error?

jchen@netcom.com (Jeffrey Chen)
Tue, 27 Jun 1995 14:52:22 GMT

          From comp.compilers

Related articles
[?] Shouldn't precedence rules fix this shift/reduce error? jchen@netcom.com (1995-06-27)
Re: Shouldn't precedence rules fix this shift/reduce error? umrigar@cs.binghamton.edu (Zerksis D. Umrigar) (1995-07-04)
| List of all articles for this month |
Newsgroups: comp.compilers
From: jchen@netcom.com (Jeffrey Chen)
Keywords: yacc, question
Organization: NETCOM On-line Communication Services (408 261-4700 guest)
Date: Tue, 27 Jun 1995 14:52:22 GMT
Status: RO

I need to parse simple arithmetic expressions involving numbers
(VALUE_TOKs), unary minus, and plus. Plus, optionally may have an
"instance name" appended to the operator:


Examples: 2 foo_name:+ 2 (with an instance name appended)
                    2 + 2 (no instance name appended)
                    -4 bar_name:+ 2 + 3 (two pluses, one w/inst.name, one w/o)


My yacc grammar is below, along with PCYACC's yy.lrt output which
shows a shift/reduce and reduce/reduce conflict in state 6.


QUESTION: If rule B gets the precedence of '+' and rule A gets the
precedence of UNARY_MINUS. Shouldn't the precedence rules I have
specified do the trick and favor the reduce over the shift?
What concept am I missing here? (I understand the reduce/reduce error,
I don't understand the shift/reduce error).


Thanks,
Jeffrey Chen
jchen@netcom.com


----------------------------------------------------------------------






%token VALUE_TOK
%token IDENTIFIER_TOK
%token ERROR_TOK


%left '+'
%left UNARY_MINUS


%start expn


%%


instance.opt
        : /* empty */
        | IDENTIFIER_TOK ':'
        ;


expn
: VALUE_TOK
| '-' expn %prec UNARY_MINUS /* rule A */
| expn instance.opt '+' expn /* rule B */
        ;


%%


-*-=-*-=-*-=-*-=-*- LALR PARSING TABLE -*-=-*-=-*-=-*-=-*-




+------------------------- STATE 0 -------------------------+


+ CONFLICTS:




+ RULES:


                $accept : ^expn $end


+ ACTIONS AND GOTOS:


            VALUE_TOK : shift & new state 2
                            - : shift & new state 3
                                : error


                      expn : goto state 1


+------------------------- STATE 1 -------------------------+


+ CONFLICTS:




+ RULES:


                $accept : expn^$end
                      expn : expn^instance.opt + expn
      instance.opt : ^ (rule 1)


+ ACTIONS AND GOTOS:


                      $end : accept
  IDENTIFIER_TOK : shift & new state 5
                            + : reduce by rule 1
                                : error


      instance.opt : goto state 4


+------------------------- STATE 2 -------------------------+


+ CONFLICTS:




+ RULES:


                      expn : VALUE_TOK^ (rule 3)


+ ACTIONS AND GOTOS:


                                : reduce by rule 3




+------------------------- STATE 3 -------------------------+


+ CONFLICTS:




+ RULES:


                      expn : -^expn


+ ACTIONS AND GOTOS:


            VALUE_TOK : shift & new state 2
                            - : shift & new state 3
                                : error


                      expn : goto state 6


+------------------------- STATE 4 -------------------------+


+ CONFLICTS:




+ RULES:


                      expn : expn instance.opt^+ expn


+ ACTIONS AND GOTOS:


                            + : shift & new state 7
                                : error




+------------------------- STATE 5 -------------------------+


+ CONFLICTS:




+ RULES:


      instance.opt : IDENTIFIER_TOK^:


+ ACTIONS AND GOTOS:


                            : : shift & new state 8
                                : error




+------------------------- STATE 6 -------------------------+


+ CONFLICTS:


? sft/red (shift & new state 5, rule 4) on IDENTIFIER_TOK
? red/red conflict (rules 4 and 1) on +


+ RULES:


                      expn : - expn^ (rule 4)
                      expn : expn^instance.opt + expn
      instance.opt : ^ (rule 1)


+ ACTIONS AND GOTOS:


  IDENTIFIER_TOK : shift & new state 5
                            + : reduce by rule 1
                                : reduce by rule 4


      instance.opt : goto state 4


+------------------------- STATE 7 -------------------------+


+ CONFLICTS:




+ RULES:


                      expn : expn instance.opt +^expn


+ ACTIONS AND GOTOS:


            VALUE_TOK : shift & new state 2
                            - : shift & new state 3
                                : error


                      expn : goto state 9


+------------------------- STATE 8 -------------------------+


+ CONFLICTS:




+ RULES:


      instance.opt : IDENTIFIER_TOK :^ (rule 2)


+ ACTIONS AND GOTOS:


                                : reduce by rule 2




+------------------------- STATE 9 -------------------------+


+ CONFLICTS:


? sft/red (shift & new state 5, rule 5) on IDENTIFIER_TOK
? red/red conflict (rules 5 and 1) on +


+ RULES:


                      expn : expn^instance.opt + expn
                      expn : expn instance.opt + expn^ (rule 5)
      instance.opt : ^ (rule 1)


+ ACTIONS AND GOTOS:


  IDENTIFIER_TOK : shift & new state 5
                            + : reduce by rule 1
                                : reduce by rule 5


      instance.opt : goto state 4


-*-=-*-=-*-=-*-=-*- END OF TABLE -*-=-*-=-*-=-*-=-*-


--


Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.