Brian Keng
2004-06-27 20:24:49 UTC
Hi,
I'm having a problem with this sequence of tokens:
if expr then block_statement else if expr then block_statement else if
expr then block_statement endif; endif;
can be interpreted in two ways:
if expr then
block_statement
else if expr then
block_statement
else
if expr then
block_statement
endif;
endif;
or
if expr then
block_statement
else
if expr then
block_statement
else if expr then
block_statement
endif;
endif;
Now I think these both ways produce the same logic, but they definitely
get expanded in two ways. Firstly, which way should they be expanded?
Secondly, how do we specify a CFG to do that?
I'm been working on this problem for a long time and haven't come up
with a solution. We talked about Java's dangling "if" in class but we
didn't really write a CFG for it, as far as I know we haven't talked
about anything similar to this problem. I found a website on the
dangling else (http://www.parsifalsoft.com/ifelse.html) but it's not
exactly the same problem (but I think it's similar). I think it has
something to do with "else if".
So, can we get a few hints about how to go about handling this problem?
I'm guessing we're not allowed to write a ambiguous grammar (and let
CUP shift for us). If I'm missing anything blatantly obivous, please
let me know.
Thanks,
Brian Keng
I'm having a problem with this sequence of tokens:
if expr then block_statement else if expr then block_statement else if
expr then block_statement endif; endif;
can be interpreted in two ways:
if expr then
block_statement
else if expr then
block_statement
else
if expr then
block_statement
endif;
endif;
or
if expr then
block_statement
else
if expr then
block_statement
else if expr then
block_statement
endif;
endif;
Now I think these both ways produce the same logic, but they definitely
get expanded in two ways. Firstly, which way should they be expanded?
Secondly, how do we specify a CFG to do that?
I'm been working on this problem for a long time and haven't come up
with a solution. We talked about Java's dangling "if" in class but we
didn't really write a CFG for it, as far as I know we haven't talked
about anything similar to this problem. I found a website on the
dangling else (http://www.parsifalsoft.com/ifelse.html) but it's not
exactly the same problem (but I think it's similar). I think it has
something to do with "else if".
So, can we get a few hints about how to go about handling this problem?
I'm guessing we're not allowed to write a ambiguous grammar (and let
CUP shift for us). If I'm missing anything blatantly obivous, please
let me know.
Thanks,
Brian Keng