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 SEMIInput ENDReduce [cmd_list ::= cmd SEMI].Shift 
8Stack: WHILE expr DO cmd_list ENDInput SEMIReduce [cmd ::= WHILE expr DO 
cmd_list END].Shift 410Stack: cmdShift 515Stack: cmd SEMI...
But with the CASE construct, I get:
...Stack: CASE case_list case_cond cmd SEMIInput ENDReduce [cmd_list ::= cmd 
SEMI].Shift 10Stack: CASE case_list case_cond cmd_listFALLBACK END => IDShift 
713Stack: CASE case_list case_cond cmd_list ENDInput SEMISyntax 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                                      
_________________________________________________________________
Do you have a story that started on Hotmail? Tell us now
http://clk.atdmt.com/UKM/go/195013117/direct/01/
_______________________________________________
sqlite-users mailing list
sqlite-users@sqlite.org
http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users

Reply via email to