[antlr-interest] Re: Semantic predicates that aren't & hoisting

Sebastian Kaliszewski sk at z.pl
Tue Mar 15 04:53:29 PST 2005


On Monday 14 March 2005 19:15, David Jung wrote:
> John,
>   Don't waste any more of your time.  You've failed to
> grasp the fundamental problem.  Your grammar, like Java/C++,
> breaks the orthogonality rule of language design; in that
> your statements/blocks are not primary.
> That's why it can't parse the input I provided in my last post:
> "{if ({a();}++ < 0) then {b; c(); {;}+1 dd();} e; f();}*2"
> (notice the "++", "+1" and "*2")
>
> That could be fixed by putting statementList in primaryExpr, but
> would introduce ambiguity.  Doing that would give something very
> similar to the solution I proposed in my original post in this
> thread.  However, my original question was if there was a better
> way; one that avoids the ambiguity.  Now I know the answer is no.


I thonk not. Try using syntactic predicate instead of semantic one. You 
wrote that it's impossible in your case, but IMHO it is. Just notice, that 
every closing brace is matched be an openig one -- so make ANTLR decide 
upon that.

So... In your original grammar instead of...


expressionList : '{' expr ( ';' expr )* '}' ;


Try using...


expressionList: '{' exprListBody '}'

exprListBody:
  ('{')=> expressionList exprListBody
 | expr (';' exprListBody)?
 | (';')*
 ;


> Perhaps ANTLR 3 will support the hoisting I need.

Try the above proposal -- or something about the lines (I didn't check if 
it's 100% right, but I had to somewhat do similar stuff in my grammar (my 
case is more complex, as using separator is dictated not only by preceding 
construct but also the following one -- in your case it would be something 
like making semicolon optional not only directly after closing brace, but 
also directly before opening brace -- this is also possible to solve

rgds
-- 
Sebastian Kaliszewski


More information about the antlr-interest mailing list