[antlr-interest] anybody using dynamic scopes?

Terence Parr parrt at cs.usfca.edu
Tue Feb 9 17:30:12 PST 2010


Hi,

Is anybody using scopes?  E.g., here is the use C grammar:

scope Symbols {
        Set types; // only track types in order to get parser working
}

Every rule that declares its usage of Symbols pushes a new copy on the stack effectively creating a new symbol scope.

translation_unit
scope Symbols; // entire file is a scope
@init {
  $Symbols::types = new HashSet();
}
    : external_declaration+
    ;


Then rule declaration declares a rule scope that lets any invoked rule see isTypedef boolean.  It's much easier than passing that info down as parameters:

declaration
scope {
  boolean isTypedef;
}
@init {
  $declaration::isTypedef = false;
}
    : 'typedef' declaration_specifiers? {$declaration::isTypedef=true;}
      init_declarator_list ';' // special case, looking for typedef
    | declaration_specifiers init_declarator_list? ';'
    ;


Rule direct_declarator can then easily determine whether the IDENTIFIER
should be declared as a type name.

direct_declarator
    :   (   IDENTIFIER
            {
            if ($declaration.size()>0&&$declaration::isTypedef) {
                $Symbols::types.add($IDENTIFIER.text);
                System.out.println("define type "+$IDENTIFIER.text);
            }
            }
        |   '(' declarator ')'
        )
        declarator_suffix*
    ;

Trying to decide if it's worth reimplementing for v4.

Ter


More information about the antlr-interest mailing list