Hi, sorry to bump this thread, but I thought I'd just try once more to see
whether anybody might have some insight on the problem I'm having with
Lemon, as detailed in the original post below.  I'm sure posts that languish
on this mailing list quickly get snowed under!  I appreciate its not an
Sqlite question, but I don't know where else to post questions about
Lemon.

Thanks!!!
Andy


----- Original Message ----- 
From: "Andy Gibbs"
Sent: Wednesday, March 10, 2010 10:54 AM
Subject: Question about lemon

> Hello,
>
> Is it alright to ask a quick question about the lemon parser in this
> mailing list, or is there a dedicated one which I should post this to
> instead?
>
> I'm using lemon to create a parser for a simple c/basic-like grammar,
> and have among other rules, the following defined (I've trimmed it down
> to what I believe are the important bits for this email, so hopefully
> its all there):
>
> cmd       ::= WHILE expr DO cmd_list END.
> cmd       ::= CASE case_list END.
> cmd       ::= SET set_list.
> cmd_list  ::= cmd_list cmd SEMI.
> cmd_list  ::= cmd SEMI.
> case_list ::= case_list case_cond cmd_list.
> case_list ::= case_cond cmd_list.
> case_cond ::= WHEN expr THEN.
> case_cond ::= ELSE.
> set_list  ::= set_list COMMA set_item.
> set_list  ::= set_item.
> set_item  ::= ID ASSIGN expr.
>
> This in itself works correctly, parsing such code as...
>
> SET a := 0;
> WHILE a < 12 DO
>  some_other_statement;
>  CASE WHEN a = 5 THEN
>    another_statement;
>    yet_another_statement;
>  WHEN a = 11 THEN
>    do_something;
>  END;
>  SET a := a+1;
> END;
>
> However, I would like to be able to add the rule:
>
> cmd ::= set_item.
>
> so that the "SET" keyword becomes optional in the above code (i.e. so
> that I can write "a := 0;" instead of "SET a := 0;").  When I do this
> however, the parser then fails to parse CASE constructs.
>
> I believe I know where the problem lies: I am using the %fallback
> directive which includes a number of keywords that fallback to "ID",
> and in this list is the "END" keyword.  Ideally I would like to leave
> the fallback list as it is, if possible, and in fact I'd much rather
> not add the additional rule than change this!
>
> What interests me is that the WHILE construct still parses correctly.
> An output from the parser trace gives the following:
>
> ...
> Stack: WHILE expr DO cmd SEMI
> Input END
> Reduce [cmd_list ::= cmd SEMI].
> Shift 8
> Stack: WHILE expr DO cmd_list END
> Input SEMI
> Reduce [cmd ::= WHILE expr DO cmd_list END].
> Shift 410
> Stack: cmd
> Shift 515
> Stack: cmd SEMI
> ...
>
> But with the CASE construct, I get:
>
> ...
> Stack: CASE case_list case_cond cmd SEMI
> Input END
> Reduce [cmd_list ::= cmd SEMI].
> Shift 10
> Stack: CASE case_list case_cond cmd_list
> FALLBACK END => ID
> Shift 713
> Stack: CASE case_list case_cond cmd_list END
> Input SEMI
> Syntax error!
>
> My understanding is that it simply isn't able to reduce "CASE case_list
> case_cond cmd_list END" to "CASE case_list END" and then "cmd" and so
> instead takes "END" to be the left-hand-side of the "set_item" rule as
> a continuation of "cmd_list", gets a semi-colon as the next character
> which according to that rule would be a syntax error.
>
> My question -- sorry this is such a long post -- is, what is it about
> the grammar for WHILE which allows it to work, and CASE that causes it
> to fail?  It is because there is an extra "list" level in the grammar
> ("case_list" -> "cmd_list", instead of just "cmd_list")?  Is there any
> way I can change the grammar to do what I hope to do, or is this a
> limitation of lemon?  I've tried adding a rule "cmd ::= CASE case_cond
> cmd_list END" but this causes a parsing conflict error message in lemon.
> I've also had a look into the lemon source code to see if I could find
> an answer there, but I'm afraid its engineering brilliance is beyond me!
>
> Thank you very much for any help that can be offered.
> Andy
> 

_______________________________________________
sqlite-users mailing list
sqlite-users@sqlite.org
http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users

Reply via email to