[antlr-interest] Translation of Parethesized / Unary Negative Expressions

Jones, Larry Larry.Jones at aspentech.com
Thu Sep 4 05:32:53 PDT 2008


I am translating from one languague to another.
 
My grammar is basically working; however, I just discovered an error in
translating a unary negative expression: "- b * 2 - c / 20". My
incorrect translation is "- (b * 2 - c / 20)."
 
The relavant parts of the combined grammar are:

	factor
	    : ...
	    | '(' expr ')' -> ^(PAREN expr)
	    | '+' expr -> expr          // unary '+'
	    | '-' expr -> ^(NEG expr)   // unary '-'
	    ;
	

The relevant parts of the tree grammar are:

	expr
	    : ...
	    | ^(NEG neg_expr=expr) -> negate(expr={$neg_expr.st})
// emits "- (expr)"
	    | ^(PAREN paren_expr=expr) -> paren(expr={$paren_expr.st})
// emits "(expr)"
	    ;
	

In addition, in the combined grammar, I used the "arithmetic expression
pattern" of encoding precedence in an expression, term, factor "tree"
and collapsing that tree into a single expression in the tree grammar.
 
I have two questions that I think are related. First, when tranlating
parenthesized expressions, I had to encode the parentheses (see the
PAREN imaginary token in my combined grammar) in order to emit the
correct parenthesized expression in the translated grammar. I'm
wondering if another technique exists to emit the correct translated
expression.
 
Second, as I mentioned at the beginning of the note, I translate unary
negative expressions incorrectly. Any examples on how to perform these
translations correctly?
 
Thanks.
 
--

Lawrence Allan Jones
Senior Developer / Technical Advisor
Aspen Technology, Inc.
2500 CityWest Blvd. Suite 1500
Houston, Texas 77042
USA
281-504-3324 (work)
281-504-3395 (fax)
larry.jones at aspentech.com
www.aspentech.com <http://www.aspentech.com/> 

 
 	
 

This e-mail and any attachments are intended only for use by the addressee(s) named herein and may contain legally privileged and/or confidential information. If you are not the intended recipient of this e-mail, you are hereby notified any dissemination, distribution or copying of this email, and any attachments thereto, is strictly prohibited. If you receive this email in error please immediately notify the sender and permanently delete the original copy and any copy of any e-mail, and any printout thereof.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20080904/416b7641/attachment.html 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: image/gif
Size: 2078 bytes
Desc: email_2_03.gif
Url : http://www.antlr.org/pipermail/antlr-interest/attachments/20080904/416b7641/attachment.gif 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: image/gif
Size: 200 bytes
Desc: email_2_04.gif
Url : http://www.antlr.org/pipermail/antlr-interest/attachments/20080904/416b7641/attachment-0001.gif 


More information about the antlr-interest mailing list