[antlr-interest] ANTLRWorks destroys its AST

Stephan Opfer stephan.opfer at gmx.net
Fri May 11 01:20:15 PDT 2012


Dear ANTLR-Interest Group,

I have two grammars, which are almost the same and an example input: A &
B | C & !A | (D | E) & F

The first grammar works quite nice, the other grammar generates NIL as
AST. However, next to the last step in debugging both grammars produced
exactly the same ParseTree and exactly the same AST.

Just after the consumption of <EOF> the other grammar throws the
computed AST away.

Is it just a ANTLRWorks problem or does anybody know, what I am doing
wrong. The only difference between the two grammars is the additional
abbreviation rule.

Please find the two grammars in the attachment.

Best Regards,
  Stephan
-------------- next part --------------
grammar Works;

options{
	language=Java;
	output=AST;
	ASTLabelType = CommonTree;
}

tokens{
	PREDICATE;
	FUNCTION;
}

/*------------------------------------------------------------------
 * PARSER RULES
 *------------------------------------------------------------------*/

condition: formula EOF! ;

formula	
	:	((FORALL^ | EXISTS^) VARIABLE)? disjunction ;

disjunction
	:	conjunction (OR^ conjunction)* ;

conjunction
	:	negation (AND^ negation)* ;

negation 
	:	NOT^? (predicate | LPAREN! formula RPAREN!) ;

predicate 
	:	PROPOSITION predicateTuple -> ^(PREDICATE PROPOSITION predicateTuple)
	| 	PROPOSITION ;

predicateTuple
	:	LPAREN! term (','! term)* RPAREN! ;

term	:	function | VARIABLE ;

function:	CONSTANT functionTuple -> ^(FUNCTION CONSTANT functionTuple)
	|	CONSTANT;

functionTuple
	:	LPAREN! (CONSTANT | VARIABLE) (','! (CONSTANT | VARIABLE) )* RPAREN!;

/*------------------------------------------------------------------
 * LEXER RULES
 *------------------------------------------------------------------*/

LPAREN : '(' ;
RPAREN :  ')' ;
AND :  '&' ;
OR :  '|' ;
NOT :  '!' ;
FORALL :  'Forall' ;
EXISTS :  'Exists' ;
IMPLY : '->';
BIIMPLY	: '<->';

VARIABLE: '?' (('a'..'z') | ('0'..'9')) CHARACTER* ;

CONSTANT: (('a'..'z') | ('0'..'9')) CHARACTER* ;

PROPOSITION: ('A'..'Z') CHARACTER* ;

fragment CHARACTER: ('0'..'9' | 'a'..'z' | 'A'..'Z' | '_') ;

WS : (' ' | '\t' | '\r' | '\n')+ {$channel = HIDDEN ;} ;
-------------- next part --------------
grammar WorksNot;

options{
	language=Java;
	output=AST;
	ASTLabelType = CommonTree;
}

tokens{
	PREDICATE;
	FUNCTION;
}

/*------------------------------------------------------------------
 * PARSER RULES
 *------------------------------------------------------------------*/

condition: abbreviation EOF!;

abbreviation
	:	f1=formula ((IMPLY f2=formula -> ^(OR  ^(NOT $f1) $f2)) | (BIIMPLY f2=formula -> ^(AND ^(OR  ^(NOT $f1) $f2) ^(OR  ^(NOT $f2) $f1))))?;

formula	
	:	((FORALL^ | EXISTS^) VARIABLE)? disjunction ;

disjunction
	:	conjunction (OR^ conjunction)* ;

conjunction
	:	negation (AND^ negation)* ;

negation 
	:	NOT^? (predicate | LPAREN! formula RPAREN!) ;

predicate 
	:	PROPOSITION predicateTuple -> ^(PREDICATE PROPOSITION predicateTuple)
	| 	PROPOSITION ;

predicateTuple
	:	LPAREN! term (','! term)* RPAREN! ;

term	:	function | VARIABLE ;

function:	CONSTANT functionTuple -> ^(FUNCTION CONSTANT functionTuple)
	|	CONSTANT;

functionTuple
	:	LPAREN! (CONSTANT | VARIABLE) (','! (CONSTANT | VARIABLE) )* RPAREN!;

/*------------------------------------------------------------------
 * LEXER RULES
 *------------------------------------------------------------------*/

LPAREN : '(' ;
RPAREN :  ')' ;
AND :  '&' ;
OR :  '|' ;
NOT :  '!' ;
FORALL :  'Forall' ;
EXISTS :  'Exists' ;
IMPLY : '->';
BIIMPLY	: '<->';

VARIABLE: '?' (('a'..'z') | ('0'..'9')) CHARACTER* ;

CONSTANT: (('a'..'z') | ('0'..'9')) CHARACTER* ;

PROPOSITION: ('A'..'Z') CHARACTER* ;

fragment CHARACTER: ('0'..'9' | 'a'..'z' | 'A'..'Z' | '_') ;

WS : (' ' | '\t' | '\r' | '\n')+ {$channel = HIDDEN ;} ;


More information about the antlr-interest mailing list