Hi Sebastian,
What about the following:
Productions
start = stmt+;
stmt =
{assign} ... |
{case} case identifier case_part+ endcase;
case_part =
{choice} identifier colon |
{stmt} stmt;
Yes, this grammar does accept invalid constructs. You simply need to
weed them out after parsing. In other words, you visit the AST and
reject any invalid construct. This even allows you to issue more
informative error messages than the parser does. The AST shape is not
perfect, but it is better. To get a perfect AST, you would need to:
1. Add the necessary additional alternatives/productions in the AST
part of your grammar.
2. Write the code that walks the automatically generated AST and
changes the tree when appropriate (by creating new nodes).
Something like:
Abstract Syntax Tree
...
{parse_case} case identifier case_part+ endcase |
{case} case choice+ endcase;
...
choice =
identifier colon stmt+;
Your AST walker would create a new "case stmt" node to replace each
"parse_case stmt" node.
Have fun!
Etienne
In this case it's ok:
===================================================
Productions
start = stmt+;
stmt =
{assign} [lhs]:identifier assign [rhs]:identifier semi
| {case} case_stmt semi
;
case_stmt =
{case}
case [pivot]:identifier
identifier colon
case_elem*
stmt+
endcase
;
case_elem = stmt+ identifier colon;
===================================================
Unfortunately this results in an ugly AST. Do you have any idea to get this
right?
--
Etienne M. Gagnon, Ph.D.
SableCC: http://sablecc.org
_______________________________________________
SableCC-Discussion mailing list
[email protected]
http://lists.sablecc.org/listinfo/sablecc-discussion