[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Change to shift instead of reduce
From: |
Adam Smalin |
Subject: |
Re: Change to shift instead of reduce |
Date: |
Mon, 3 Jun 2013 16:01:40 -0400 |
Could you give me a better example? I tried the below and neither helped. I
got the error
Ambiguity detected.
Option 1,
mainLoop -> <Rule 5, tokens 1 .. 8>
mainLoop -> <Rule 5, tokens 1 .. 7>
mainLoop -> <Rule 5, tokens 1 .. 6>
mainLoop <tokens 1 .. 4>
mainElement -> <Rule 6, tokens 5 .. 6>
'$' <tokens 5 .. 5>
VarName <tokens 6 .. 6>
mainElement -> <Rule 7, tokens 7 .. 7>
mainElement2 -> <Rule 9, tokens 7 .. 7>
Token -> <Rule 10, tokens 7 .. 7>
'.' <tokens 7 .. 7>
mainElement -> <Rule 7, tokens 8 .. 8>
mainElement2 -> <Rule 8, tokens 8 .. 8>
VAR <tokens 8 .. 8>
Option 2,
mainLoop -> <Rule 5, tokens 1 .. 8>
mainLoop <tokens 1 .. 4>
mainElement -> <Rule 6, tokens 5 .. 8>
'$' <tokens 5 .. 5>
VarName -> <Rule 12, tokens 6 .. 8>
VarName <tokens 6 .. 6>
'.' <tokens 7 .. 7>
VAR <tokens 8 .. 8>
>From both. Input is $a.b $a.b
%%
program: mEOS main
main: | mainLoop mEOS
mainLoop:
mainElement %dprec 5
| mainLoop mainElement %dprec 4
mainElement:
%nonassoc '$' VarName %dprec 6 { printf("varname\n"); }
| mainElement2 %dprec 1
mainElement2:
VAR %dprec 3 { printf("var\n"); }
| Token %dprec 2 { printf("token\n"); }
Token: '.' %dprec 1
VarName: VAR %dprec 7
| VarName '.' VAR %dprec 8
EOS: '\n' | ';'
mEOS: | mEOS EOS
------
%right '$' '.'
%%
program: mEOS main
main: | mainLoop mEOS
mainLoop:
mainElement %prec 5
| mainLoop mainElement %prec 4
mainElement:
'$' VarName %dprec 6 { printf("varname\n"); }
| mainElement2 %dprec 1
mainElement2:
VAR %dprec 3 { printf("var\n"); }
| Token %dprec 2 { printf("token\n"); }
Token: '.' %dprec 1
VarName: VAR %dprec 7
| VarName '.' VAR %dprec 8
On Mon, Jun 3, 2013 at 3:09 AM, Akim Demaille <address@hidden> wrote:
>
> Le 1 juin 2013 à 20:49, Adam Smalin <address@hidden> a écrit :
>
> > I have another thread but here is a simple version of my problem
> >
> > My text input is
> > a.b c.d
> > What I would like is
> > varname varname
> > My grammar is
> > %glr-parser
> > //...
> > mainLoop:
> > mainElement %dprec 5
> > | mainLoop mainElement %dprec 4
> > mainElement:
> > '$' VarName %dprec 6 { printf("varname\n"); }
> > | VAR %dprec 3 { printf("var\n"); }
> > | Token %dprec 2 { printf("token\n"); }
> >
> > Token: '.' %dprec 1
> > VarName: VAR %dprec 7
> > | VarName '.' VAR %dprec 8
> >
> > The conflict file shows
> > 6 mainElement: '$' VarName . [$end, VAR, '.', '$', '\n', ';']
> > 11 VarName: VarName . '.' VAR
> >
> > '.' shift, and go to state 18
> >
> > '.' [reduce using rule 6 (mainElement)]
> > $default reduce using rule 6 (mainElement)
> >
> > Now I see $default reduce. I'd like it to shift. I threw in a bunch of
> > precedence hoping something would trigger a shift instead of a reduce but
> > no. What can I do?
>
> You have a competition between r6, which wants to be reduced,
> and token '.', which wants to be shifted. r6 is "tagged" by
> '$', its rightmost token (use %prec to use another associativity
> / precedence carrier). So you have a competition between "$" and ".".
> To make the latter win, several choices:
>
> - use precedence (e.g., %nonassoc '$' %nonassoc '.')
>
> - use associativity (to promote shift, make it right
> associative: %right '$' '.').
>
> HTH.
Re: Change to shift instead of reduce, Ron Burk, 2013/06/03