[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Change to shift instead of reduce
From: |
Akim Demaille |
Subject: |
Re: Change to shift instead of reduce |
Date: |
Mon, 3 Jun 2013 09:09:54 +0200 |
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