[antlr-interest] anybody using dynamic scopes?

Oliver Zeigermann oliver.zeigermann at gmail.com
Wed Feb 10 10:03:46 PST 2010


I use them a lot and they are useful to me. Of course they are not
indispensable, but really convenient.

2010/2/10 Terence Parr <parrt at cs.usfca.edu>:
> 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
>
> List: http://www.antlr.org/mailman/listinfo/antlr-interest
> Unsubscribe: http://www.antlr.org/mailman/options/antlr-interest/your-email-address
>


More information about the antlr-interest mailing list