[antlr-interest] modifying expression grammar ; unexpected token null

Joan Pujol joanpujol at gmail.com
Wed Aug 4 10:22:20 PDT 2004


Your grammar works well.
I test it. You have problems in your main program.

This main program is command line and works well:

import java.io.DataInputStream;
import antlr.collections.AST;
import antlr.debug.misc.ASTFrame;

public class Main {
    public static void main(String[] args) {
	try {
	
      ExpressionLexer lexer = new ExpressionLexer(new
DataInputStream(System.in));
      ExpressionParser parser = new ExpressionParser(lexer);
      ExpressionTreeWalker treeparser= new ExpressionTreeWalker();
 
	while(true) {
	parser.expr();
	AST ast=parser.getAST();
	if(ast!=null) {
	  ASTFrame frame = new ASTFrame("calc", ast);
	  frame.setVisible(true);
	  double resultat = treeparser.expr(ast);
	  System.out.println("resultat:" + resultat);
	}
      }
	} catch(Exception e) {
	    System.err.println("exception: "+e);
	}
    }
}

Cheers,

On Wed, 04 Aug 2004 04:17:49 -0000, tjs <tjs_tjs4 at yahoo.com> wrote:
> I am trying to modify the Expression grammar given in the
> JS Mill's tutorial (as of this time down;
> http://supportweb.cs.bham.ac.uk/documentation/tuto
> rials/docsystem/build/tutorials/antlr/antlrhome.html)
> 
> I have 1 main goal here:
> To make it so you don't need parantheses around the top expression
> I did this by recrusively defining an expression as the same as in the
> tutorial (fairly straightforward), and instead of
> 
> | expr
> 
> on the atom rule, I used
> 
> | (LPAREN^ expr RPAREN!)
> 
> Which is the way the Java grammars I've look at handle expressions.
> All this grammar does is parse math expressions and enforce
> parantheses (for example, 2+2 outputs 4.0, and (1-3)*4 outputs -8, or
> it SHOULD anyway). Again, I modified the ORIGINAL grammar.
> 
> My PROBLEM is that when I feed it input (in the form of a text file
> that is only 3 characters in length, it gives me the following error:
> 
> line 1:4 unexpected token: null
>  ( + 2 )
> (AST): unexpected end of subtree
> 
> My interpretation of this is that it is trying to read the 4th
> character of the input (which doesn't exist, hence null) and then the
> AST doesn't understand why it can't find the end of the subtree, it
> apparently also thinks it needs another token when it shouldn't. I
> can't figure out why it wants a 4th character or any other problem.
> 
> The following are 1. the input text 2. the grammar 3. the ORIGINAL
> grammar ( which does work properly, but you need to do (2+2) instead
> of 2+2, because it makes you use parantheses around everything ), 4.
> the way in which I load the text file into the lexer
> 
> finally, before you think i am a "Newbie" who doesn't deserve any
> help, i have read 50+ pages of ANTLR documentation, searched
> everything in /docs and this forum, experimented with this grammar for
> over 4 hours, looked at Java grammars to see how they handled
> expressions, and wrote a very crude expression parser that was very
> messy before to graph functions (its messiness is part of the reason i
> am using ANTLR to redo it). i just can't figure out my dumb problem,
> that's all. also, if you help me i'll give you credit in my program
> for correcting my dumb mistakes!
> 
> ------- 1. input text
> 2+2
> 
> (this is only 3 characters, no newlines, or anything else)
> 
> ------- 2. my expression grammar
> 
> class ExpressionParser extends Parser;
> options { buildAST=true; }
> 
> expr     :  sumExpr ;
> sumExpr  : prodExpr ((PLUS^|MINUS^) prodExpr)* ;
> prodExpr : powExpr ((MUL^|DIV^|MOD^) powExpr)* ;
> powExpr  : atom (POW^ atom)? ;
> atom     : INT | (LPAREN^ expr RPAREN!) ;
> 
> class ExpressionLexer extends Lexer;
> 
> LPAREN : '(';
> FUNCSIN : 's';
> RPAREN : ')';
> PLUS  : '+' ;
> MINUS : '-' ;
> MUL   : '*' ;
> DIV   : '/' ;
> MOD   : '%' ;
> POW   : '^' ;
> SEMI  : ';' ;
> protected DIGIT : '0'..'9' ;
> INT   : (DIGIT)+ ;
> 
> {import java.lang.Math;}
> class ExpressionTreeWalker extends TreeParser;
> 
> expr returns [double r]
>   { double a,b; r=0; }
> 
>   : #(PLUS  a=expr b=expr)  { r=a+b; }
>   | #(MINUS a=expr b=expr)  { r=a-b; }
>   | #(MUL   a=expr b=expr)  { r=a*b; }
>   | #(DIV   a=expr b=expr)  { r=a/b; }
>   | #(MOD   a=expr b=expr)  { r=a%b; }
>   | #(POW   a=expr b=expr)  { r=Math.pow(a,b); }
>   | #(LPAREN a=expr) { r=a;}
>   | i:INT { r=(double)Integer.parseInt(i.getText()); }
>   ;
> 
> ------ 3. the original grammar from Mill's tutorial (working, but noly
> when the input is (2+2)  not 2+2 )
> 
> class ExpressionParser extends Parser;
> options { buildAST=true; }
> 
> expr     : (LPAREN^ sumExpr RPAREN!) ;
> sumExpr  : prodExpr ((PLUS^|MINUS^) prodExpr)* ;
> prodExpr : powExpr ((MUL^|DIV^|MOD^) powExpr)* ;
> powExpr  : atom (POW^ atom)? ;
> atom     : INT | expr ;
> 
> class ExpressionLexer extends Lexer;
> 
> LPAREN : '(';
> RPAREN : ')';
> PLUS  : '+' ;
> MINUS : '-' ;
> MUL   : '*' ;
> DIV   : '/' ;
> MOD   : '%' ;
> POW   : '^' ;
> SEMI  : ';' ;
> protected DIGIT : '0'..'9' ;
> INT   : (DIGIT)+ ;
> 
> {import java.lang.Math;}
> class ExpressionTreeWalker extends TreeParser;
> 
> expr returns [double r]
>   { double a,b; r=0; }
> 
>   : #(PLUS  a=expr b=expr)  { r=a+b; }
>   | #(MINUS a=expr b=expr)  { r=a-b; }
>   | #(MUL   a=expr b=expr)  { r=a*b; }
>   | #(DIV   a=expr b=expr)  { r=a/b; }
>   | #(MOD   a=expr b=expr)  { r=a%b; }
>   | #(POW   a=expr b=expr)  { r=Math.pow(a,b); }
>   | #(LPAREN a=expr) { r=a;}
>   | i:INT { r=(double)Integer.parseInt(i.getText()); }
>   ;
> 
> ---- 4. excerpt from my test program
> 
> ExpressionLexer lexer = new ExpressionLexer(new DataInputStream(
> new FileInputStream("C:\\antlr\\EXPRINPUT.txt")));
> ExpressionParser parser = new ExpressionParser(lexer);
> 
> 
> Yahoo! Groups Links
> 
> 
> 
> 
> 


-- 
Joan Jesús Pujol Espinar


 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http://groups.yahoo.com/group/antlr-interest/

<*> To unsubscribe from this group, send an email to:
    antlr-interest-unsubscribe at yahoogroups.com

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.com/info/terms/
 



More information about the antlr-interest mailing list