[antlr-interest] Re: Semantic predicates that aren't & hoisting
David Jung
jungdl at ornl.gov
Thu Mar 10 12:53:17 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:
> [...]
>
>>> 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.
>
> Read my grammar fragment again. The first alternative of exprListOrStmt is
> a recursive call to exprList. That will match the nested exprLists.
>
> What I didn't do was make the semi-colon optional: no semi after exprList
> blocks but a semi is require after an expr.
John,
I think you misunderstood the problem (or I still misunderstand you).
Your fragment above didn't make rule expr explicit, but if I add it in
we have something like:
expr: ruleA
| 'if' expr 'then' expr
| ...
ruleA: ruleB ( ('+'|'-') expr )?
ruleB ...
...
ruleZ : exprList | number | ...
exprList: : '{' ( exprListOrStmt )* '}' ;
exprListOrStmt : ('{')=>exprList | expr ';' ;
Firstly, I added the ('{')=> syntactic predicate to
eliminate the ambiguity when it starts with an exprList.
Secondly, and importantly, expr won't match:
"{if {a>0} then {f();g();} x();}", will it? (or am I going blind?)
This is what I'm trying to solve. (note the absence of the ';'
before the x. "{if {a>0} then {f();g();}; x();}" should be
invalid syntax).
If I promote the exprListOrStmt rule to be the root of
the grammar and replace some of the expr references
to exprListOrStmt instead, I still think we've trouble.
i.e.
exprListOrStmt : ('{')=>exprList | expr ';' ;
exprList: : '{' ( exprListOrStmt )* '}' ;
expr: ruleA
| 'if' exprListOrStmt 'then' exprListOrStmt
| ...
ruleA: ruleB ( ('+'|'-') expr)?
ruleB ...
...
ruleZ : exprList | number | ...
This can't match "1+{2;};" as an exprListOrStmt/expr.
If we changed RuleA to
ruleA: ruleB ( ('+'|'-') exprListOrStmt )?
when it would, but it wouldn't match "1+2+3;" then
(but would match "1+2+3;;;")
Am I going crazy?
> Check out e.g., the C or Java grammars for examples of how the various
> parts of an expr hang together.
Both C & Java make the distinction between statements and expressions,
which makes the problem simple. You can't write f({g(),h();}()) in Java.
(g is called, h is called & returns a function, which is called and its
return value passed to f)
Thanks again,
-David.
More information about the antlr-interest
mailing list