From: "Tim Halloran" <hallor...@gmail.com>
Date: Thu, 30 Oct 2008 13:44:35 -0400
Local: Thurs, Oct 30 2008 1:44 pm
Subject: Re: [antlr-interest] Precedence problem
Well, one fix is to separate, taking "sin" as an example, sin SPACE from sin(
I just made the SPACE significant -- It seems to work on all your grammar Expression; } expr : addExpr EOF! ; addExpr multExpr unaryExpr sinExpr cosExpr lnExpr expExpr atom parenExpr NUMBER : '-'? '0'..'9'+ ('.' '0'..'9'*)? ; On Thu, Oct 30, 2008 at 1:05 PM, Todd O'Bryan <toddobr...@gmail.com> wrote: List: http://www.antlr.org:8080/mailman/listinfo/antlr-interest > I've assigned my high school programming students a symbolic algebra > project and provided them an ANTLR parser so they could translate > Strings to values easily. I used an AST, so the translation is pretty > easy. The basic idea is a mapping like this: > "sin(x ^ 2)" --> new Sin(new Exp(new Var(), new Number(2.0)) > Everything was working great, until... I wanted exponentiation to have > "~x^3" ---> new Neg(new Exp(new Var(), new Number(2.0))) > But a student tried this: > "sin(x) ^ 2" > Clearly this SHOULD be new Exp(new Sin(new Var()), new Number(2.0)), > I can't figure out how to fix it, however, because sin(...) should > grammar Expression; > expr : addExpr EOF! > addExpr : multExpr (('+'^|'-'^) multExpr)* > multExpr : unaryExpr (('*'^|'/'^) unaryExpr)* > unaryExpr : ('sin'^|'cos'^|'ln'^|'~'^) expExpr > expExpr : atom ('^'^ atom)* > parenExpr : ('sin'^|'cos'^|'ln'^|'~'^)? '('! addExpr ')'! > atom : parenExpr > NUMBER : '-'? '0'..'9'+ ('.' '0'..'9'*)? ; > List: http://www.antlr.org:8080/mailman/listinfo/antlr-interest Unsubscribe: http://www.antlr.org:8080/mailman/options/antlr-interest/your-email-a... You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
| ||||||||||||||