[antlr-interest] Problem with dynamic attribute scopes after upgrade to 3.1

Grzegorz Cieslewski cieslewski at hcs.ufl.edu
Fri Aug 15 10:08:59 PDT 2008


Hey All,

I have encountered a little problem with the dynamic attribute scopes
when upgraded to 3.1 (from 3.1b1 probably).  In my grammar I was using
dynamic scopes communicate with distant rules.  In particular to allow
the lower level rule (id) to identify  which rule has invoked it
(type1 or type2).  In previous versions this grammar used to work:

expr
	: (type1 | type2)*
	;	
type1
scope{
	boolean isType1;
}
@init{
	$type1::isType1 = true;
}
	: INT id
	;
type2
scope{
	boolean isType2;
}
@init{
	$type2::isType2 = true;
}
	: id
	;	
id
	:	ID
	{
		if($type1::isType1==true)
		{
			System.out.println("TYPE1");
		}
		if($type2::isType2==true)
		{
			System.out.println("TYPE2");
		}
	}
	;

Unfortunately in the 3.1 it gives me run-time errors:
Exception in thread "main" java.util.EmptyStackException
	at java.util.Stack.peek(Stack.java:85)
	at G5Parser.id(G5Parser.java:292)
	at G5Parser.type1(G5Parser.java:186)
	at G5Parser.expr(G5Parser.java:107)
	at __Test__.main(__Test__.java:14)

I compared code generated in the two versions and it seems that the
following changed:
FROM:
if(((type1_stack.size()>0)?((type1_scope)type1_stack.peek()).isType1:false)==true)
{
     System.out.println("TYPE1");
}
TO:
if(((type1_scope)type1_stack.peek()).isType1==true)
{
     System.out.println("TYPE1");
}
The new code does not protect against checking the Stack when empty.

I am curious what is the reason for this change?
Is there a better way to accomplish something similar?

Thanks

Greg

-- 
=====================================================
Grzegorz Cieslewski
Research Assistant
High-performance Computing & Simulation (HCS) Research Laboratory
University of Florida, Dept. of Electrical and Computer Engineering
330 Benton Hall, Gainesville, FL, 32611-6200
Phone: (352) 392-9041
Email: cieslewski at hcs.ufl.edu
Web: www.hcs.ufl.edu
=====================================================


More information about the antlr-interest mailing list