[antlr-interest] Python target @members problem

Matt Palmer mattpalms at gmail.com
Thu Sep 18 11:25:41 PDT 2008


Hi Henrique,

I think I got it wrong!  Your rule is actually a parser rule, not a lexer
rule, so my suggestion won't help at all!

Actually, I think it's because you're using the Python language target.
Unlike Java, you have to use "self":

self.names

or Python won't see it as a member of the class.

Matt



On Thu, Sep 18, 2008 at 6:38 PM, Henrique <hjrnunes at student.dei.uc.pt>wrote:

> Hi!Thanks for your reply Matt!
> I've replaced my members section:
>
> members {
> names=[];
> rule_name = [];
> }
>
> by your suggestion:
>
> @lexer::members {
> names=[];
> rule_name = [];
> }
>
> Still, I'm having the same problem... The problem is only with "names". It
> all seems fine with "rule_name".. I suppose it's because I initialize it in
> rule rule:
>
> rule
> @init{
> rule_name=[]
> print "NEW RULE"
> }
> @after{
> names.append(rule_name);
> print "END RULE: " + str(names);
> }
> : RULE USER_STRING (USER_RATIONAL)? LEFTBRACKET buncha_replacements?
> RIGHTBRACKET {rule_name.append($USER_STRING.text);}
>  ;
>
> Any thoughts? Or perhaps I'm doing something wrong?
> Thanks,
>
> Henrique Nunes
>
>
>
> On Thu, Sep 18, 2008 at 10:42 AM, Matt Palmer <mattpalms at gmail.com> wrote:
>
>> Hi Henrique,
>>
>> you need to specify:
>>
>> @lexer::members
>>
>> as members on its own puts them in the parser, not the lexer.
>>
>> Matt.
>>
>> On Thu, Sep 18, 2008 at 3:35 AM, Henrique <hjrnunes at student.dei.uc.pt>wrote:
>>
>>> Hi everyone!I'm building a parser that should generate python code. The
>>> problem is that the variables that I define in the @members section are not
>>> recognized in the rules themselves.
>>> For instance, I have this:
>>>
>>> grammar cfdg_py;
>>>
>>> options{
>>> language=Python;
>>> }
>>>
>>> ...
>>>
>>> @members {
>>> names=[]
>>> rule_name = []
>>> }
>>>
>>> ...
>>>
>>> rule
>>> @init{
>>> rule_name=[]
>>> print "NEW RULE"
>>> }
>>> @after{
>>> names.append(rule_name)
>>> print "END RULE: " + str(names)
>>> }
>>> : RULE USER_STRING (USER_RATIONAL)? LEFTBRACKET buncha_replacements?
>>> RIGHTBRACKET {rule_name.append($USER_STRING.text);}
>>>  ;
>>>
>>> I get a NameError: global name 'names' is not defined in the first line
>>> of the @after section.
>>> How do I make sure the variables are defined?
>>> I've tried inserting the semicolons as advised in the wiki but it didn't
>>> work. The antlr python documentation is really scarce so I can't figure it
>>> out.
>>> I'm attaching the whole grammar with this. Using ANTLR 3.1 on Windows.
>>> Thanks in advance.
>>>
>>> Henrique Nunes
>>>
>>>
>>>
>>> List: http://www.antlr.org/mailman/listinfo/antlr-interest
>>> Unsubscribe:
>>> http://www.antlr.org/mailman/options/antlr-interest/your-email-address
>>>
>>>
>>>
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20080918/f806e41f/attachment.html 


More information about the antlr-interest mailing list