[antlr-interest] "finally" blocks

Paul J. Lucas pauljlucas at mac.com
Mon Jan 17 22:13:41 PST 2005


On Tue, 18 Jan 2005, Alan Gutierrez wrote:

> Paul J Lucas wrote:
> > 	I don't have Bjarne's book in front of me, but he talks about
> > 	the general concept is about resource aquisition and release
> > 	(which is mostly what "finally" in Java is used for, e.g., open
> > 	a file, make sure it's closed, etc.).
> 
>     RAII. Resource Allocation is Initialization. Modern C++ relies heavily on
>     the use of destructors to restore state when the stack is unwound.

	Yes, I know.

>     The question as to whether it is general, I'm not sure I understand. If
>     you're wondering whether it is a generally accepted practice in C++, the
>     answer is yes.

	I wasn't asking.  I think you're misquoting.  Terence was the
	one who was asking.

> > 	In ANTLR for C++, you don't need "finally" at all because if
> > 	the user wants the functionality, s/he can simply do what I
> > 	did, i.e., create a variable having a destructor.
> 
>     And a decent C++ type is built around RAII and a destrcutor that
>     does the right thing.

	Yes, I agree, but there's no such standard type wrappers for
	built-in types which is exactly what my example was for.

	The lone standard such wrapper is auto_ptr<T>.

>     You could still have that finally block for C++ ...

	How, exactly?  if I have:

		myRule
		{
		    bool flag = true;
		}
		    : SOME TOKEN
		        {
			    if ( disaster )
			       throw outta_here;
			}
		    ;
		    exception
		    finally {
		        flag = false;
		    }

	How is ANTLR going to "know" to do the right thing?  It would
	have to actually parse the C++ code looking for "throw" or
	"return" and copy the finally code like:

		myRule() {
		    bool flag = true;
		    // ...
		    if ( disaster ) {
		        flag = false;
			throw outta_here;
		    }
		    // ...
		    flag = false;
		}

>     Finally exists in C++ ...

	No it doesn't.

	- Paul



More information about the antlr-interest mailing list