[antlr-interest] The right way to walk if/then/else clauses that contain multiple statements
Anthony Urso
anthonyu at cs.ucla.edu
Wed Apr 14 15:53:56 PDT 2010
I have a translator from a DSL to Java that mostly works.
I peeked behind the scenes and used trial and error to make a way to
get a working if/then/else walker that could handle multiple elseif
clauses. I ended up passing the magic list_xxx variables into my
string template, and then doing parallel iteration over them. I don't
think that is the correct way, unfortunately, and now that I want to
upgrade the walker to handle multiple statements per clause, I have no
idea how to do it.
What is the correct way to do this?
Thanks,
Anthony
===
grammar G;
statement:
ifStatement;
ifStatement :
'if'^ expression 'then'! statement+
('elseif' expression 'then'! statement+)*
('else' statement+)?
'end'! 'if'!;
tree grammar Gtw;
statement:
^('if' tst=expression stmt=statement
('elseif' eitst+=expression eistmt+=statement)*
('else' estmt=statement)?) -> ifelseifelse(tst={tst}, stmt={stmt},
eitst={list_eitst},
eistmt={list_eistmt}, estmt={estmt});
group Gstg;
ifelseifelse(tst, stmt, eitst, eistmt, estmt) ::= <<
if (<tst>) {
<stmt>
<if (eistmt)>
<eitst, eistmt: { t, s | \} else if (<t>) \{
<s>
}>
<endif>
<if (estmt)>
} else {
<estmt>
<endif>
}
>>
More information about the antlr-interest
mailing list