Home > Software design >  Production Never Reducued Menhir
Production Never Reducued Menhir

Time:01-03

I've been at this for a good two hours now but I can't get the compiler to stop giving me this warning.

Warning: production type_anno -> is never reduced.

Below is my parser.mly code.

(*Optional type annotation*)
type_anno:
        | {None}
        | COL;name = ID {Some name}
        ;

statement:
         |(*SEND stmt*) SEND; chn = ID; O_TBRACE; var1 = ID; COMMA; var2 = ID; C_TBRACE {Send(chn,var1,var2)}
         |(*RECV stmt*) O_TBRACE; var1 = ID; COMMA; var2=ID; C_TBRACE; ARW; RECV; chn = ID; S_COL ; stmt = statement {Recv(var1,var2,chn, stmt)}
         |(*SELECT stmt*)  chn=ID; DOT; lbl=ID; O_TBRACE; l1 = ID; C_TBRACE; S_COL; stmt = statement {Select(chn, lbl, l1, stmt)}
         |(*CASE stmt*) CASE; chn =ID; O_PAREN; branches =  branch_statement C_PAREN {Case(chn,branches)}
         |(*CAST stmt*) CAST; chn = ID;O_TBRACE;l1=ID;C_TBRACE; {Cast(chn,l1)}
         |(*SHIFT stmt*) l1 = ID ; ARW; SHIFT; chn=ID; S_COL;stmt=statement {Shift(chn, l1, stmt)}
         |(*ACCEPT stmt*) l1 = ID ; ARW; ACC; chn=ID; S_COL;stmt=statement {Accept(chn, l1, stmt)}
         |(*ACQUIRE stmt*) l1 = ID ; ARW; ACQ; chn=ID; S_COL;stmt=statement {Acquire(chn, l1, stmt)}
         |(*DETACH stmt*) DET; chn=ID; O_TBRACE; l1=ID;C_TBRACE;{Detach(chn,l1)}
         |(*RELEASE stmt*) l1 = ID ; ARW; REL; chn=ID; S_COL;stmt=statement {Release(chn, l1, stmt)}
         |(*CLOSE stmt*) CLOSE; chn=ID;{Close(chn)}
         |(*WAIT stmt*) WAIT; chn=ID;stmt=statement;{Wait(chn,stmt)}
         |(*SPAWN stmt*) SPAWN; ref =ID; ARW;  fn_id =ID ;inp = params; S_COL; stmt=statement {Spawn(ref,fn_id,inp,stmt)}
         |(*NEW stmt*)  chn =ID;anno=type_anno; ARW; NEW;   stmt =statement ; S_COL ;stmt0 = statement {match anno with Some a -> NewA(chn,a,stmt, stmt0) | None -> New(chn,stmt,stmt0)}
         |(*OUTPUT stmt*) PRINT; O_PAREN;var= ID; C_PAREN {Out(var)}
         |(*FWD stmt*) FWD; O_PAREN; chn1=ID; COMMA;chn2 = ID; C_PAREN {Fwd(chn1,chn2)}
         |(*SPL stmt*) O_TBRACE; var1 = ID; COMMA; var2=ID; C_TBRACE; ARW; SPL; chn = ID; S_COL ; stmt = statement {Split(var1,var2,chn, stmt)}

CodePudding user response:

The more important part of the warning emitted by menhir is that your grammar has a shift/reduce conflict that was resolved in favor of the shift.

Due to this conflict, the production

type_anno:
        | {None}

can never be reduced since it is overridden by all productions that accepts ARW after an ID in the statement rule

         |(*SHIFT stmt*) l1 = ID ; ARW; ...
         |(*ACCEPT stmt*) l1 = ID ; ARW; ...
         |(*ACQUIRE stmt*) l1 = ID ; ARW; ...
         |(*RELEASE stmt*) l1 = ID ; ARW; ...
         ...
         |(*NEW stmt*)  chn =ID;anno=type_anno; ARW; NEW; ...

You can check the issue in menhir interpret mode that your parser as written rejects

ID ARW NEW

The smallest fix for the shift/reduce conflict is to make the rule type_anno an inline rule:

%inline type_anno:
        | {None}
        | COL;name = ID {Some name}
        ;
  • Related