[antlr-interest] ANTLR based Python parser

John B. Brodie jbb at acm.org
Sat Sep 14 14:54:42 PDT 2002


Shayam asked about transforming the following grammar fragment from
the definition of Python for use in Antlr:

/* Start of Grammar */

primary : 
             atom | attributeref
              | subscription | slicing | call;

attributeref : 
             primary "." IDENTIFIER;

subscription : 
             primary "[" expression_list "]";

slicing : 
             simple_slicing | extended_slicing;
  
simple_slicing : 
             primary "[" short_slice "]";
  
extended_slicing : 
             primary "[" slice_list "]" ;
             
call : 
             primary "(" (argument_list)? ")";  

/* End of Grammar */

I do not know Python at all, but I'd try:

    1) factor the primary out of the atributeref, subscription,
       simple_slicing, extended_slicing and call rules;

    2) factor the "[" out of the resultant new forms of the
       subscription, simple_slicing, and extended_slicing rules;

    3) and then remove the recursion in primary

these transformations yield (i think):

primary :
    atom ( a | s | c )*
  ;

a :
    "." IDENTIFIER
  ;

s :
    "[" ( expression_list | short_slice | slice_list ) "]"
  ;

c :
    "(" ( argument_list )? ")"
  ;

Hope this helps....

 

Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/ 



More information about the antlr-interest mailing list