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

John D. Mitchell johnm-antlr at non.net
Thu Mar 10 14:34:35 PST 2005


>>>>> "David" == David Jung <jungdl at ornl.gov> writes:
>>>>>> John D. Mitchell said:
>>>>>>> "David" == David Jung <jungdl at ornl.gov> writes:
[...]

> 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 ';' ;

Note that since you called it an exprList, I've presumed that the value of
an exprList is the value of the last expression (that's pretty
standard). In that case, e.g., 5 + {a(); b + g()} does actually make sense.
If that's not your intent then you need to figure out a difference between
e.g., statements and expressions for your language.

> 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?)

Of course it will:

expr will match exprList.

exprList will match expr will match if followed by an expr matching an
exprList followed by then followed by an expr matching an exprList
containing two exprs matching a list of function call expressions; followed
by a function call expression.


> 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).

That's exactly what my fragment supports.  As noted, it forbids the semi
colon after the exprList (unless, of course, you allow empty statements
:-).

I don't know the rest of your grammar/language so I can't speak
specifically about what other wacky things you're doing and how this
interacts.


> 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.

You keep trying to make things more complicated. :-(


>> 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)

Indeed, and now you can see why such a distinction is useful. :-)

Take care,
	John


More information about the antlr-interest mailing list