[antlr-interest] Older Grammar in ANTLR 3

John B. Brodie jbb at acm.org
Sat Oct 3 16:08:07 PDT 2009


Greetings!
On Sat, 2009-10-03 at 18:24 -0400, Pennington, Elliot wrote:
> Howdy All,
> 
> I've got an old grammar that used to work in ANTLR 2 but that now  
> fails in ANTLR 3. I'd like to make the switch, but I'm at a loss as to  
> how to fix this. For the following example code:
> 
> --------------
> 
> void input_a() {
>     integer a, bb, xyz, b3, c, p, q;
>     real b;
>     a = b3;
>     b = -2.5;
>     xyz = 2 + a + bb + c - p / q;
>     a = xyz * ( p + q );
>     p = a - xyz - p;
> }
> 
> -------------
> 
> I get this tree result:
> 
> -------------
> 
> def
> 	function_declaration
> 		type
>   			void
> 		input_a
> 		(
> 		)
> 		code_block
> 			FailedPredicateException(code_block,{can't deal with predicates  
> yet}?)
> 			FailedPredicateException(code_block,{synpred2_javalet}?)
> 
> ------------
> 
> Here's the grammar I'm using. Has anyone seen anything like this  
> before for a grammar that worked in ANTLR 2? I'm not sure how to get  
> it to parse properly so I don't get the failed predicate exception.
> 

I don't get any failed predicate exceptions!

I replaced your two @header thingys with the following test driver:

// begin ----- cut here ----- cut here -----
@members {
    private static final String [] x = new String[]{
       " void input_a() { \n"
       + "     integer a, bb, xyz, b3, c, p, q; \n"
       + "     real b; \n"
       + "     a = b3; \n"
       + "     b = -2.5; \n"
       + "     xyz = 2 + a + bb + c - p / q; \n"
       + "     a = xyz * ( p + q ); \n"
       + "     p = a - xyz - p; \n"
       + " } \n"
    };

    public static void main(String [] args) {
        for( int i = 0; i < x.length; ++i ) {
            try {
                System.out.println("about to parse:`"+x[i]+"`");
                JavaletLexer lexer = new JavaletLexer(new
ANTLRStringStream(x[i]));
                CommonTokenStream tokens = new CommonTokenStream(lexer);

                JavaletParser parser = new JavaletParser(tokens);
                JavaletParser.start_return p_result = parser.start();

                CommonTree ast = p_result.tree;
                if( ast == null ) {
                   System.out.println("resultant tree: is NULL");
                } else {
                   System.out.println("resultant tree: " +
ast.toStringTree());
                }
                System.out.println();
            } catch(Exception e) {
                e.printStackTrace();
            }
        }
    }
}

//@header {package javalet.compiler;}
//@lexer::header{package javalet.compiler;}

start : def /*EOF!*/;

// end ----- cut here ----- cut here -----

Note that I do not use ANTLRWorks. Are you, by any chance, trying to
exercise your grammar in AW's interpreter? If so, try using the debugger
instead... or even better, use the command-line tools.

> Thanks,

You are welcome, hope this helps....
   -jbb

> ------------
> 
> grammar Javalet;
> options {
> 	backtrack = true;
> 	output=AST;
> 	ASTLabelType=CommonTree;
> 	k=2;
> 	}
> tokens	{
> 	FUNCTION = 'function';
> }
> 
> @header {package javalet.compiler;}
> @lexer::header{package javalet.compiler;}
> 
> def 	:	function_declaration
> 	;
> 
> 
> code_block
> 	:	'{' statement* '}' -> (statement)*
> 	|	statement
> 	;
> 
> statement
> 	:	block_statement
> 	|	variable_declaration ';' -> variable_declaration
> 	|	assignment ';' -> assignment
> 	;
> 
> block_statement
> 	:	IF '(' value_0 ')' code_block
> 		(else_block)? -> ^(IF value_0 code_block (else_block)?)
> 	|	WHILE '(' value_0 ')' code_block -> ^(WHILE value_0 code_block)
> 	;
> 
> else_block
> 	:	ELSE code_block -> ^(ELSE code_block)
> 	;
> 
> assignment
> 	:	ID EQ value_0 -> ^(EQ ID value_0)
> 	;
> 
> variable_declaration
> 	:	type ID (',' ID)* -> ^(type ID*)
> 	;
> 
> function_declaration
> 	:	type ID '(' ')' code_block -> ^('function' type ID code_block)
> 	;
> 
> operator_0
> 	:	LOGICAL_OR
> 	;
> 	
> LOGICAL_OR
> 	:	'||'
> 	;
> 
> operator_1
> 	:	LOGICAL_AND
> 	;
> 
> LOGICAL_AND
> 	:	'&&'
> 	;
> 
> operator_2
> 	:	LOGICAL_EQUALITY
> 	|	LOGICAL_INEQUALITY
> 	|	LOGICAL_GREATER_THAN
> 	|	LOGICAL_LESS_THAN
> 	|	LOGICAL_GREATER_THAN_OR_EQUAL
> 	|	LOGICAL_LESS_THAN_OR_EQUAL
> 	;
> 
> LOGICAL_EQUALITY
> 	:	'=='
> 	;
> 
> LOGICAL_INEQUALITY
> 	:	'!='
> 	;
> 
> LOGICAL_GREATER_THAN
> 	:	'>'
> 	;
> 
> LOGICAL_LESS_THAN
> 	:	'<'
> 	;
> 
> LOGICAL_GREATER_THAN_OR_EQUAL
> 	:	'>='
> 	;
> 
> LOGICAL_LESS_THAN_OR_EQUAL
> 	:	'<='
> 	;
> 
> operator_3
> 	:	ADD
> 	|	SUBTRACT
> 	;
> 
> ADD	:	'+';
> SUBTRACT:	'-';
> 
> operator_4
> 	:	MULTIPLY
> 	|	DIVIDE
> 	;
> 
> MULTIPLY:	'*';
> DIVIDE	:	'/';
> 
> value_0
> 	:	value_1 operator_0 value_0 -> ^(operator_0 value_1 value_0)
> 	|	value_1
> 	;
> 
> value_1
> 	:	value_2 operator_1 value_1 -> ^(operator_1 value_2 value_1)
> 	|	value_2
> 	;
> 
> value_2
> 	:	value_3 operator_2 value_2 -> ^(operator_2 value_3 value_2)
> 	|	value_3
> 	;
> 
> value_3
> 	:	value_4 operator_3 value_3 -> ^(operator_3 value_4 value_3)
> 	|	value_4
> 	;
> 
> value_4
> 	:	value_5 operator_4 value_4 -> ^(operator_4 value_5 value_4)
> 	|	value_5
> 	;
> 
> value_5
> 	:	'(' value_0 ')' -> ^(value_0)
> 	|	num
> 	;
> 
> 
> num
> 	:	(REAL|INTEGER|ID)
> 	;
> 
> WS	:	(' '|'\r'|'\t'|'\u000C'|'\n') {$channel=HIDDEN;}
> 	;
> 
> COMMENT	:	'/*' .* '*/' {$channel=HIDDEN;}
>   	;
> 
> LINE_COMMENT
> 	:	'//' ~('\n'|'\r')* '\r'? '\n' {$channel=HIDDEN;} ;
> 
> //fragment
> IF	:	'if' ;
> 
> //fragment
> ELSE	:	'else' ;
> 
> //fragment
> WHILE	:	'while' ;
> 
> 
> //fragment
> DO	:	'do' ;
> 
> //fragment
> FOR	:	'for' ;
> 
> //fragment
> EQ	:	'='
> 	;
> 
> fragment
> DIGIT	:	'0'..'9' ;
> 
> fragment
> LETTER	:	('a'..'z'|'A'..'Z') ;
> 
> type	:	TYPE_VOID
> 	|	TYPE_INTEGER
> 	|	TYPE_REAL
> 	;
> 	
> TYPE_VOID
> 	:	'void'
> 	;
> TYPE_INTEGER
> 	:	'integer'
> 	;
> TYPE_REAL
> 	:	'real'
> 	;
> 
> INTEGER	:	(DIGIT)+ ;
> 
> REAL	:	('+'|'-') (DIGIT)+ '.' (DIGIT)+ ;
> 
> ID	:	LETTER ('_' | DIGIT | LETTER)* ;
> 
> ____________
> 
> elliot at wpi.edu
> Worcester Polytechnic Institute '09
> Computer Science
> 
> 
> List: http://www.antlr.org/mailman/listinfo/antlr-interest
> Unsubscribe: http://www.antlr.org/mailman/options/antlr-interest/your-email-address




More information about the antlr-interest mailing list