Index: src/main.cc
===================================================================
--- src/main.cc	(revision 8b7ee0974cc08327cc6c696da347e0f8d3d1f50c)
+++ src/main.cc	(revision 80722d07c38e70225f74e0448996a46b64c82364)
@@ -10,11 +10,17 @@
 // Created On       : Fri May 15 23:12:02 2015
 // Last Modified By : Peter A. Buhr
-// Last Modified On : Fri Aug 19 08:31:22 2016
-// Update Count     : 350
+// Last Modified On : Sat Aug 20 12:52:22 2016
+// Update Count     : 403
 //
 
 #include <iostream>
 #include <fstream>
-#include <getopt.h>
+#include <signal.h>										// signal
+#include <getopt.h>										// getopt
+#include <execinfo.h>									// backtrace, backtrace_symbols_fd
+#include <cxxabi.h>										// __cxa_demangle
+
+using namespace std;
+
 #include "Parser/lex.h"
 #include "Parser/parser.h"
@@ -35,10 +41,9 @@
 #include "InitTweak/FixInit.h"
 #include "Common/UnimplementedError.h"
-
 #include "../config.h"
 
 using namespace std;
 
-#define OPTPRINT(x) if ( errorp ) std::cerr << x << std::endl;
+#define OPTPRINT(x) if ( errorp ) cerr << x << endl;
 
 
@@ -68,11 +73,64 @@
 static void parse_cmdline( int argc, char *argv[], const char *& filename );
 static void parse( FILE * input, LinkageSpec::Spec linkage, bool shouldExit = false );
-static void dump( std::list< Declaration * > & translationUnit, std::ostream & out = std::cout );
+static void dump( list< Declaration * > & translationUnit, ostream & out = cout );
+
+void sigSegvBusHandler( int sig_num ) {
+	enum { Frames = 50 };
+	void * array[Frames];
+	int size = backtrace( array, Frames );
+
+	cerr << "*CFA runtime error* program cfa-cpp terminated with "
+		 <<	(sig_num == SIGSEGV ? "segment fault" : "bus error")
+		 << " backtrace:" << endl;
+
+	char ** messages = backtrace_symbols( array, size );    
+
+	// skip first stack frame (points here)
+	for ( int i = 2; i < size - 2 && messages != nullptr; i += 1 ) {
+		char * mangled_name = nullptr, * offset_begin = nullptr, * offset_end = nullptr;
+		for ( char *p = messages[i]; *p; ++p ) {        // find parantheses and +offset
+			if (*p == '(') {
+				mangled_name = p; 
+			} else if (*p == '+') {
+				offset_begin = p;
+			} else if (*p == ')') {
+				offset_end = p;
+				break;
+			} // if
+		} // for
+
+		// if line contains symbol, attempt to demangle
+		if ( mangled_name && offset_begin && offset_end && mangled_name < offset_begin ) {
+			*mangled_name++ = '\0';
+			*offset_begin++ = '\0';
+			*offset_end++ = '\0';
+
+			int status;
+			char * real_name = __cxxabiv1::__cxa_demangle( mangled_name, 0, 0, &status );
+			if ( status == 0 ) {						// demangling successful ?
+				cerr << "(" << i - 2 << ") " << messages[i] << " : " 
+					 << real_name << "+" << offset_begin << offset_end << endl;
+
+			} else {									// otherwise, output mangled name
+				cerr << "(" << i - 2 << ") " << messages[i] << " : " 
+					 << mangled_name << "+" << offset_begin << offset_end << endl;
+			} // if
+			free( real_name );
+		} else {										// otherwise, print the whole line
+			cerr << "(" << i - 2 << ") " << messages[i] << endl;
+		} // if
+	} // for
+	free( messages );
+	exit( EXIT_FAILURE );
+} // sigSegvBusHandler
 
 int main( int argc, char * argv[] ) {
 	FILE * input;										// use FILE rather than istream because yyin is FILE
-	std::ostream *output = & std::cout;
-	std::list< Declaration * > translationUnit;
+	ostream *output = & cout;
 	const char *filename = nullptr;
+	list< Declaration * > translationUnit;
+
+	signal( SIGSEGV, sigSegvBusHandler );
+	signal( SIGBUS, sigSegvBusHandler );
 
 	parse_cmdline( argc, argv, filename );				// process command-line arguments
@@ -122,5 +180,5 @@
 
 		if ( parsep ) {
-			parseTree->printList( std::cout );
+			parseTree->printList( cout );
 			delete parseTree;
 			return 0;
@@ -144,5 +202,5 @@
 
 		if ( expraltp ) {
-			ResolvExpr::AlternativePrinter printer( std::cout );
+			ResolvExpr::AlternativePrinter printer( cout );
 			acceptAll( translationUnit, printer );
 			return 0;
@@ -210,28 +268,28 @@
 		CodeGen::generate( translationUnit, *output, ! noprotop );
 
-		if ( output != &std::cout ) {
+		if ( output != &cout ) {
 			delete output;
 		} // if
 	} catch ( SemanticError &e ) {
 		if ( errorp ) {
-			std::cerr << "---AST at error:---" << std::endl;
-			dump( translationUnit, std::cerr );
-			std::cerr << std::endl << "---End of AST, begin error message:---\n" << std::endl;
-		} // if
-		e.print( std::cerr );
-		if ( output != &std::cout ) {
+			cerr << "---AST at error:---" << endl;
+			dump( translationUnit, cerr );
+			cerr << endl << "---End of AST, begin error message:---\n" << endl;
+		} // if
+		e.print( cerr );
+		if ( output != &cout ) {
 			delete output;
 		} // if
 		return 1;
 	} catch ( UnimplementedError &e ) {
-		std::cout << "Sorry, " << e.get_what() << " is not currently implemented" << std::endl;
-		if ( output != &std::cout ) {
+		cout << "Sorry, " << e.get_what() << " is not currently implemented" << endl;
+		if ( output != &cout ) {
 			delete output;
 		} // if
 		return 1;
 	} catch ( CompilerError &e ) {
-		std::cerr << "Compiler Error: " << e.get_what() << std::endl;
-		std::cerr << "(please report bugs to " << std::endl;
-		if ( output != &std::cout ) {
+		cerr << "Compiler Error: " << e.get_what() << endl;
+		cerr << "(please report bugs to " << endl;
+		if ( output != &cout ) {
 			delete output;
 		} // if
@@ -369,9 +427,9 @@
 } // notPrelude
 
-static void dump( std::list< Declaration * > & translationUnit, std::ostream & out ) {
-	std::list< Declaration * > decls;
+static void dump( list< Declaration * > & translationUnit, ostream & out ) {
+	list< Declaration * > decls;
 
 	if ( noprotop ) {
-		filter( translationUnit.begin(), translationUnit.end(), std::back_inserter( decls ), notPrelude );
+		filter( translationUnit.begin(), translationUnit.end(), back_inserter( decls ), notPrelude );
 	} else {
 		decls = translationUnit;
