Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Precedence problem
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  4 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Todd O'Bryan  
View profile  
 More options Oct 30 2008, 1:05 pm
From: "Todd O'Bryan" <toddobr...@gmail.com>
Date: Thu, 30 Oct 2008 13:05:44 -0400
Local: Thurs, Oct 30 2008 1:05 pm
Subject: [antlr-interest] Precedence problem
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
higher precedence than unary operators, so

"~x^3" ---> new Neg(new Exp(new Var(), new Number(2.0)))
"sin x ^ 2" ---> new Sin(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)),
but since ^ has higher precedence than sin, it doesn't work.

I can't figure out how to fix it, however, because sin(...) should
have the same precedence as a parenthesized expression (higher than
^), but sin ... should have lower precedence. I tried a syntactic
predicate, but since the sin rule is in two rules, I can't get rid of
the ambiguity. Here's my grammar that doesn't work. Can anybody help?

grammar Expression;
options {
  output = AST;
  ASTLabelType=CommonTree;

}

expr : addExpr EOF!
  ;

addExpr : multExpr (('+'^|'-'^) multExpr)*
  ;

multExpr : unaryExpr (('*'^|'/'^) unaryExpr)*
  ;

unaryExpr : ('sin'^|'cos'^|'ln'^|'~'^) expExpr
  | expExpr
  ;

expExpr : atom ('^'^ atom)*
  ;

parenExpr : ('sin'^|'cos'^|'ln'^|'~'^)? '('! addExpr ')'!
  ;

atom : parenExpr
  | NUMBER
  | VAR
  ;

NUMBER : '-'? '0'..'9'+ ('.' '0'..'9'*)? ;
VAR : 'x' ;
WHITESPACE : (' '|'\t'|'\n'|'\r')+ { skip(); } ;

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.
Tim Halloran  
View profile  
 More options Oct 30 2008, 1:44 pm
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
examples.  Good luck

grammar Expression;
options {
 output = AST;
 ASTLabelType=CommonTree;

}

expr
        : addExpr EOF!
        ;

addExpr
        : multExpr (('+'^|'-'^) multExpr)*
        ;

multExpr
        : unaryExpr (('*'^|'/'^) unaryExpr)*
        ;

unaryExpr
        : '~'^ expExpr
        | sinExpr
        | cosExpr
        | lnExpr
        | expExpr
 ;

sinExpr
        : 'sin ' expExpr
        ;

cosExpr
        : 'cos ' WHITESPACE expExpr
        ;

lnExpr
        : 'ln ' WHITESPACE expExpr
        ;

expExpr
        : atom ('^'^ atom)*
        ;

atom
        : parenExpr
        | NUMBER
        | VAR
        ;

 parenExpr
        : 'sin(' addExpr ')'
        | 'cos(' addExpr ')'
        | 'ln(' addExpr ')'
        | '('! addExpr ')'!
        ;

NUMBER : '-'? '0'..'9'+ ('.' '0'..'9'*)? ;
VAR : 'x' ;
WHITESPACE : (' '|'\t'|'\n'|'\r')+ { skip(); } ;

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.
Tim Halloran  
View profile  
 More options Oct 30 2008, 1:45 pm
From: "Tim Halloran" <hallor...@gmail.com>
Date: Thu, 30 Oct 2008 13:45:34 -0400
Local: Thurs, Oct 30 2008 1:45 pm
Subject: Re: [antlr-interest] Precedence problem
Upps, delete the WHITESPACE from cosExpr and lnExpr (harmless but bad)

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.
Todd O'Bryan  
View profile  
 More options Oct 30 2008, 7:30 pm
From: "Todd O'Bryan" <toddobr...@gmail.com>
Date: Thu, 30 Oct 2008 19:30:57 -0400
Local: Thurs, Oct 30 2008 7:30 pm
Subject: Re: [antlr-interest] Precedence problem
That makes perfect sense! I was too busy thinking that spaces were
insignificant to realize that they're significant. Thanks!

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.
End of messages
« Back to Discussions « Newer topic     Older topic »