Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Solving Shift/Reduce conflict

149 views
Skip to first unread message

Fabrice Leal

unread,
Jun 16, 2013, 1:46:09 PM6/16/13
to
I'm trying to solve a shift/reduce conflict (an occurrence of the "dangling else" situation, I believe) in a toy parser made with cl-yacc.

From some googling, I tried to add

(:precedence ((:left :elseif)
(:left :else)
))

but I must confess I have no formal education in lexing or parsing, so I'm kinda shooting in the dark here.


Could I get some help in this issue? This is my parser so far:

(yacc:define-parser ifparser
(:start-symbol start)
(:terminals (:if :else :elseif
:cmp-1 :cmp-2 :cmp-3
:do-smt :do-otr
:ob :cb))

(expr
(:cmp-1)
(:cmp-2)
(:cmp-3)
(:do-smt)
(:do-otr)
ifclause)

(elseif
nil
(elseif :elseif expr expr))

(single-else
nil
(:else expr))

(ifclause
(:if expr expr elseif single-else))

(start
ifclause))

George Neuner

unread,
Jun 16, 2013, 10:30:54 PM6/16/13
to
On Sun, 16 Jun 2013 10:46:09 -0700 (PDT), Fabrice Leal
<fabrice...@gmail.com> wrote:

>I'm trying to solve a shift/reduce conflict (an occurrence of the "dangling else" situation, I believe) in a toy parser made with cl-yacc.
>
>From some googling, I tried to add
>
> (:precedence ((:left :elseif)
> (:left :else)
> ))
>
>but I must confess I have no formal education in lexing or parsing, so I'm kinda shooting in the dark here.
>
>Could I get some help in this issue?


Playing with precedence won't help. You can't eliminate the
shift/reduce conflict without disallowing chaining of IF "statements"
... which I presume you do want to permit.

Shift/reduce is merely a warning that the grammar is ambiguous - and
minor ambiguity such as posed by "dangling else" isn't a problem in
practice. yacc's default response is to shift the token, which is the
correct behavior to enable chaining. You may be able to silence the
warning if cl-yacc permits doing so [I haven't any experience with
it].

George

George Neuner

unread,
Jun 16, 2013, 11:21:48 PM6/16/13
to
On Sun, 16 Jun 2013 22:30:54 -0400, George Neuner
<gneu...@comcast.net> wrote:

>You can't eliminate the shift/reduce conflict without disallowing
>chaining of IF "statements"
^^^^^^^

Sorry, that should have been "nesting" IFs.

[Aside: posting to CLL is really slow tonight.]

George

Kenneth Tilton

unread,
Jun 17, 2013, 1:49:27 PM6/17/13
to
On Sunday, June 16, 2013 1:46:09 PM UTC-4, Fabrice Leal wrote:
> I'm trying to solve a shift/reduce conflict (an occurrence of the "dangling else" situation, I believe) in a toy parser made with cl-yacc.

Why are you using yacc?

-hk

Kenneth Tilton

unread,
Jun 19, 2013, 9:52:53 AM6/19/13
to
On Monday, June 17, 2013 1:49:27 PM UTC-4, Kenneth Tilton wrote:
> On Sunday, June 16, 2013 1:46:09 PM UTC-4, Fabrice Leal wrote:
>
> > I'm trying to solve a shift/reduce conflict (an occurrence of the "dangling else" situation, I believe) in a toy parser made with cl-yacc.
>
>
>
> Why are you using yacc?
>

I asked because my experience related in this post (no need to read it) http://smuglispweeny.blogspot.com/2008/03/my-biggest-lisp-project.html was that I found with Lisp that I could create a DSL without a parser -- as long as I did not mind the parens and of course as a Lisper... this approach also meant my DSL swam nicely with Lisp, so not everything had to be in the DSL, just the boilerplate I wanted the DSL to hide.


If you /really/ want to parse a language, have you looked at Smug? https://github.com/drewc/smug#readme

btw, another issue as stated in my post is that I was well aware that Lex and Yacc were a PITA to work with (and I think Smug has some of the same issues in re difficulties writing/debugging the parser).

hth, hk

Dimitri Fontaine

unread,
Jun 19, 2013, 10:43:46 AM6/19/13
to
Kenneth Tilton <kti...@mcna.net> writes:
> I asked because my experience related in this post (no need to read it)
> http://smuglispweeny.blogspot.com/2008/03/my-biggest-lisp-project.html was
> that I found with Lisp that I could create a DSL without a parser -- as long
> as I did not mind the parens and of course as a Lisper... this approach also
> meant my DSL swam nicely with Lisp, so not everything had to be in the DSL,
> just the boilerplate I wanted the DSL to hide.

Agreed.

> If you /really/ want to parse a language, have you looked at Smug?
> https://github.com/drewc/smug#readme

I'm using esrap these days for providing a non-lispy command language in
the new version of pgloader:

http://nikodemus.github.io/esrap/

HTH,
--
dim
0 new messages