Java 1.5 grammar with CST -> AST; issue with default case in switch statements
Thomas Schwinge <thomas <at> schwinge.name>
2011-11-21 16:00:26 GMT
Hi!
In my diploma thesis/project I'm using the Java 1.5 grammar as originally
published by Stefan Mandel in
<http://lists.sablecc.org/pipermail/sablecc-user/binrUhxOff8TC.bin> in
message <http://lists.sablecc.org/pipermail/sablecc-user/msg00435.html>,
then picked up by Janus Nielsen in
<http://janusnielsen.wordpress.com/2007/04/18/java-5-grammar-for-sablecc-32/>,
<http://janusnielsen.wordpress.com/2008/06/08/java-5-grammar-for-sablecc-32-now-with-copyright/>,
and, after me noticing that the links are 404 again, published in a
public repository, <http://bitbucket.org/jdn/java5grammar>.
That advantage over the grammar on
<http://sablecc3.sablecc.org/wiki/Java-1.5> is that it already contains
CST -> AST transformation rules.
To make it work with stock SableCC 3.2, I'm using the following tiny
patch on top of this version. (The change log comment on top of the file
would also need updating.)
--- /home/thomas/tmp/source/sablecc/java5grammar/master/grammar/java5.sablecc 2011-03-23
17:59:45.000000000 +0100
+++ src/ikr/compl/jmm/parser.sablecc 2011-03-23 22:12:36.000000000 +0100
@@ -1491,7 +1491,7 @@
{super} question super reference_type {-> New wildcard.super(reference_type.type)};
type_parameters {-> [elements]:type_parameter*} =
- lt type_parameter_list_gt {-> type_parameter_list_gt.elements};
+ lt type_parameter_list_gt {-> [type_parameter_list_gt.elements]};
type_parameter_list_gt {-> [elements]:type_parameter*} =
{one} type_parameter_gt {-> [type_parameter_gt.type_parameter]} |
So far, so good -- big thanks, Stefan and Janus -- I didn't have to spend
much time on the grammar itself, but could instead concentrate on just
using it.
Today I found an issue with the default case in switch statements:
switch_statement {-> statement} =
switch condition switch_block {-> New statement.switch(condition.expression, [switch_block.statement])};
switch_block {-> statement*} =
l_brc switch_block_statement_group* switch_label* r_brc {->
[switch_block_statement_group.statement, New
statement.switch_block([switch_label.expression], [])]} ;
switch_block_statement_group {-> statement} =
switch_label+ block_statement+ {-> New statement.switch_block([switch_label.expression], [block_statement.statement])};
switch_label {-> expression?} =
{expression} case constant_exp colon {-> constant_exp.expression} |
{default} default colon {-> Null};
(Please see
<https://bitbucket.org/jdn/java5grammar/src/eb90e50d0139/grammar/java5.sablecc>
for the whole grammar.)
Briefly, this means that a switch label is either a (constant) expression
(which remains in the AST), or the default token (which is mapped to
Null).
Then, if you have:
switch (x)
{
case 10:
case 100:
a = 1;
break;
default:
a = 0;
break;
case 20:
case 200:
a = 2;
break;
}
..., you'd get a switch_block_statement_group with a switch_label list of
10, 100, an empty switch_label list (default case), and a switch_label
list of 20, 200. This is still detectable unambiguously.
But, in the following case:
switch (x)
{
case 10:
case 100:
a = 1;
break;
case 30:
default:
case 300:
a = 0;
break;
case 20:
case 200:
a = 2;
break;
}
..., the 30 and 300 cases will build a switch_label list, but the default
case will no longer be detectable: due to its mapping to Null, it will
simply not be added to this switch_label list, and will thus no longer be
detectable, contrary to the empty list case in the example before.
Before I begin researching all the theory about CST -> AST
transformations -- any quick suggestion?
Grüße,
Thomas
_______________________________________________
SableCC-Discussion mailing list
SableCC-Discussion <at> lists.sablecc.org
http://lists.sablecc.org/listinfo/sablecc-discussion