[antlr-interest] anybody using dynamic scopes?
Terence Parr
parrt at cs.usfca.edu
Wed Feb 10 10:16:53 PST 2010
I plan on allowing initializers this time. :)
T
On Feb 10, 2010, at 10:07 AM, Iztok Kavkler wrote:
> I use it for error reporting from a compiler: during tree parsing phase
> I call several functions that may throw CompileError, which needs
> information about current node's line and column. Since I don't want to
> pass the current node as a parameter to all of these functions I have
> created a scope
>
> scope InfoScope {
> CommonTree node;
> }
>
> There is some extra work involved, since I have to say
>
> scope InfoScope;
> $InfoScope::node = $rule.start;
>
> at the beginning of every rule.
>
> Then again, perhaps there is an easier way to do this?
>
> Iztok
>
>
> On 10.2.2010 2:30, Terence Parr wrote:
>> 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
>
> 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