[antlr-interest] [C Target] Getting segmentation fault in isNilNode() from libatnlr3c.so

L. Rahyen research at science.su
Thu Dec 2 10:22:07 PST 2010


	I'm trying to get working (for C target) very simple example from "The 
Definitive ANTLR Reference" book, page 73, "3.3 Evaluating Expressions Using an 
AST Intermediate Form". Of course, for Java target everything is fine. I'm 
trying to write the same thing using C target but it looks like I did something 
wrong because I get segmentation fault at this line in Test.cpp:

  walker->prog(walker); (in isNilNode() from libatnlr3c.so)

	I'm just getting started with ANTLR. I would really appreciate if somebody 
suggest something that could help me to find where is the problem. To reproduce 
the problem, please use attached files:

	0) Save all files in the same directory, cd to it and run "chmod +x 
compile"
	1) Open Expr.g in ANTLRWorks, generate code, then do the same with Eval.g
	2) Run (I'm using GNU C++ compiler): ./compile
	3) Run: ./Test input
-------------- next part --------------
tree grammar Eval;
options
{
  language=C;
  tokenVocab = Expr; // Read token type from Expr tokens file
  ASTLabelType = pANTLR3_BASE_TREE;
}

@members
{
  #include <string>
  #include <iostream>
  #include <tr1/unordered_map>  
  #include <antlr3commontree.h>
  namespace std { using namespace tr1; }
  using namespace std;
  
  unordered_map<string, int> memory;
}

prog	:	stat+;

stat	:	expr { cout << $expr.value << endl; }
	|	^('=' ID expr) { memory.insert( { (char*)$ID.text->chars, $expr.value } ); };

expr returns [int value]
	:	^('-' a=expr b=expr) {$value = a-b; }
	|	^('+' a=expr b=expr) {$value = a+b; }
	|	^('*' a=expr b=expr) {$value = a*b; }
	|	ID
		{
		  auto key = (char*)$ID.text->chars;
		  if(memory.count(key) ) $value = memory[key]; // If found, set return value
		  else cout << "Undefined variable " << $ID.text->chars << endl;
		}
	|	INT {$value = atoi( (char*)$INT.text->chars); };
-------------- next part --------------
(3+4)*5
-------------- next part --------------
A non-text attachment was scrubbed...
Name: compile
Type: application/x-shellscript
Size: 97 bytes
Desc: not available
Url : http://www.antlr.org/pipermail/antlr-interest/attachments/20101202/252d35f3/attachment.bin 
-------------- next part --------------
grammar Expr;
options
{
  language=C;
  output = AST;
  ASTLabelType = pANTLR3_BASE_TREE; // Type of $stat.tree ref etc...
}

@members
{
  #include <iostream>
  using namespace std;
}

// START:stat
/** Match a series of stat rules and, for each one, print out the
 *  tree stat returns, $stat.tree.  toStringTree() prints the tree
 *  out in form: (root child1 ... childN).  ANTLR's default tree 
 *  construction mechanism will build a list (flat tree) of the stat
 *  result trees.  This tree will be the input to the tree parser.
 */
prog	:	(stat { /* cout << $stat.tree==NULL?"NULL":$stat.tree->toStringTree() << endl; */ } )+;
stat	:	expr NEWLINE        -> expr
	|	ID '=' expr NEWLINE -> ^('=' ID expr)
	|	NEWLINE             ->;
// END:stat

// START:expr
expr	:	multExpr (('+'^|'-'^) multExpr)*;
multExpr:	atom ('*'^ atom)*;
atom	:	INT
	|	ID
	|	'('! expr ')'!;
// END:expr

// START:tokens
ID	:	('a'..'z'|'A'..'Z')+;
INT	:	'0'..'9'+;
NEWLINE	:	'\r'? '\n';
WS	:	(' '|'\t'|'\n'|'\r')+ { SKIP(); };
// END:tokens


More information about the antlr-interest mailing list