Java Concatenation, BigDecimal math.

422 views
Skip to first unread message

Hunter Blakely

unread,
Jun 30, 2014, 9:26:17 PM6/30/14
to antlr-di...@googlegroups.com
Based on the context of the leftmost part of an expression I would like to change what occurs in the rest of the expression.


left + middle + right
|                   \
left         middle + right
               |         |       |
           middle    +     right

If left is a string literal, the '+' equates to exactly a '+'.

If left is a number, the + is used as BigDecimal.add(BigDecimal).



All numbers picked up by the parser are being converted into BigDecimal numbers. Big Decimal does not allow the following:
 (new BigDecimal(1))+(new BigDecimal(1)) 


Instead, BigDecimal requires the use of .add():
 (new BigDecimal(1)).add((new BigDecimal(1)))



This becomes a problem when I am concatenating strings and numbers. If I just make every '+' between two numbers a number1.add(number2) then concatenating them wouldn't work as intended.

My grammar cyntax:
 write "hello " + 1 + 2 

Desired output:
 System.out.println("hello "+(new BigDecimal(1))+(new BigDecimal(2)));


This will print:
hello 12


I also want to be able to do math with my #'s. When I see 1+2 without a string in the rest of the expression I would like to do BigDecimal addition:

(new BigDecimal(1)).add((new BigDecimal(2)))
Message has been deleted

Jonathan Coveney

unread,
Jun 30, 2014, 11:02:27 PM6/30/14
to antlr-di...@googlegroups.com
I think this is something you'd want to deal with in your intermediate representation and not at the parser level. That said it is possible with parser predicates, or with rules in your walker.

El lunes, 30 de junio de 2014, Hunter Blakely <hunter.j...@gmail.com> escribió:
The expressions are almost exactly like java expressions.

expression
 : primary
 | expression (+|-) expression
 ;

primary
 : StringLiteral
 | NumberLiteral
 ;


That's a basic representation.


--
You received this message because you are subscribed to the Google Groups "antlr-discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to antlr-discussi...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages