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

David Jung jungdl at ornl.gov
Thu Mar 10 10:21:37 PST 2005


John D. Mitchell said:
>>>>>> "David" == David Jung <jungdl at ornl.gov> writes:
>> John D. Mitchell said:
>>>>>>>> "David" == David Jung <jungdl at ornl.gov> writes:
> [...]
>
>>>> expressionList : '{' expr ( ';' expr )* '}' ;
>
>>>> The important rule for this discussion being expressionList.  So, a
>>>> valid expr can be "5" or "{5; 7; 8}" or "{ { 2; 3; }; <-- notice this
>>>> ';' 5; } "
>
>>> Make the exprList rule a bit more complicated and have it explicitly
>>> understand the recursive nesting of the exprLists.
>
>> I'm not exactly sure how that would work.
>
> Add the handling of the recursive exprList directly into exprList. E.g.:
>
> exprList
>         : '{' ( exprListOrStmt )* '}'
>         ;
>
> exprListOrStmt
>         : exprList
>         | expr ';'
>         ;

Perhaps I'm confused.  Here as you have expr ';', that means that
an expression that ends with an exprList (but isn't one directly),
like the 'if' case, can have a ';' following it - which is exactly
what I'm trying to avoid requiring.
That is "if (a) {f();g();h();}" is a valid expr, but isn't
an exprList, so if embedded as part of an exprList like:
{ if(a) {f();g();h();}; x(); y(); } requires that ';' before
the x.
Note that I can't place all the rules with training expr in
the exprListOrStmt rule (like the 'if' rule), because the
expr rule is the root of the whole grammar, but exprList
is part of a primaryExpr, which is way near the 'bottom' of the
grammar.
(So you can write (if(a)then b())+{g();h();}*5, for example.
Even {if(a)then f else g}() - which calls func f or g depending on c)
That is, everything is an expression (and both functions and types
are first-class values)

> Ah, the old separator vs. terminator argument.  Well, there are certainly
> plenty of examples of that idea floating around. :-)
>
> Since you seem to have some control over the language, I strongly suggest
> implementing your grammar with semi-colons as terminators first and then
> go
> back after you have that well debugged and tweak it to be a separator.

Well, this was intended to the the tweaking stage :)
I've implemented an interpreter for the language already and am
now starting a compiler to target the JVM.  So I though I'd try
to fix the nagging ';' problem that's been bugging me :)
I have no problem with terminators, so long as the last one in
an exprList isn't required :)

> Hope this helps,
Yes, you're certainly helping... :)

Thanks,
-David.




More information about the antlr-interest mailing list