[antlr-interest] question about variable assignment in grammars

Mark Volkmann r.mark.volkmann at gmail.com
Tue Jan 29 04:52:58 PST 2008


On Jan 28, 2008 3:17 PM, Olivier Lefevre <lefevrol at yahoo.com> wrote:
> I am going through the book and looking at ExprEval.g.

I assume you're talking about section 3.2.

> I am puzzled by the use of auxiliary variable names in
> the actions, i.e., under the expr and multExpr rules
> we have these assignments to 'e' but under the stat
> rule expr is "accessed" directly, as in '$expr.value'
> Thus why couldn't we write, e.g., $atom.value?

You can. In some cases it's just personal preference. However, if a
rule refers to another rule more than once then you need to use
variables to differentiate between the occurrences. You can see this
in the expr rule of Eval.g in section 3.3 where it uses the following.

: ^('+' a=expr b=expr) { $value = a + b; }

> Also, re. the atom rule, something that can correspond
> to an entire expression (third option in the rule) does
> not match my understanding of the word "atom", atomic
> usually meaning "without a fine structure". Is it really
> correct to use it here or is it a legerdemain?

I agree that it doesn't fit the definition of an atom. I'd never heard
the word "legerdemain" before. You caused me to look it up, so now I
know. ;-) "skillful use of one's hands when performing conjuring
tricks; deception; trickery"



-- 
R. Mark Volkmann
Object Computing, Inc.


More information about the antlr-interest mailing list