[antlr-interest] Making a distinction between float and int calculation

William B. Clodius wclodius at los-alamos.net
Sun Jan 24 19:51:54 PST 2010


Steven:

I should have originally posted my answer to antlr-interest not directly to you. So far I have only been using ANTLR to test the lexing and parsing of a language I am developing as a hobby. I have read the tree parsing material in the ANTLR reference but have not used it. Roughly what I would do is have each of INT and float have two attributes associated with it: a type (INT and FLOAT respectively) and a value, to be determined by the string that represents the value. Similarly an ID and an expression would also have a type and a value associated with them. Since you include boolean expressions you will have to decide if you want to explicitly have a boolean type or just have it be an integer type. For these other entities the types and values will have to be determined as you traverse the tree. For example for numericexpression you should examine the types of the two  atoms, if both have type INT, then the type of multexpression is INT and the value is the sum of the two values, if both are FLOAT the corresponding logic is used, if one is INT and the other is FLOAT then you should include an intermediate step that converts the INT to a FLOAT, both in type and value, then perform the numeric operation. Read Chapter 6 of the reference if you have it.

On Jan 23, 2010, at 3:03 PM, Steven Raemaekers wrote:

> Hi William,
> 
> How should i do this exactly in ANTLR? Should I test for this in my Tree walker? I do not have a clue where to start, when I make my numericexpression like this:
> 
> numericexpression returns [int value]
> 	: ^(PLUS mult1 = mult mult2 = mult) { $value = 20; }
> 	| ^(MINUS mult1 = mult mult2 = mult) { $value = 20; }
> 	| ^(MULTIPLY mult1 = mult mult2 = mult) { $value = 20; }
> 	| ^(DIVIDE mult1 = mult mult2 = mult) { $value = 20; }
> 	;
> 
> What value should it return, if all mults can be either floats or integers? 
> 
> Thanks,
> 
> Steven
> 
> On Sat, Jan 23, 2010 at 8:19 PM, William B. Clodius <wclodius at los-alamos.net> wrote:
> THis is normally done as part of the semantic evaluation not as parsing. If and when you start including named entities you will normally be unable to make this distinction using syntax (unless you require integers and floats to have special categories of names). Putting it off until the semantics analysis also allows better error reporting, if you should say make assignment and comparison equalities both valid expressions.
> 
> On Jan 23, 2010, at 9:36 AM, Steven Raemaekers wrote:
> 
> > Hello,
> >
> > In my grammar there should be an evaluator for numeric expressions. These
> > numeric expressions should return an integer, or a float, depending on the
> > contents of the expression.
> > For example:
> >
> > 3 + 2.0: should return float
> > 3 + 2: should return integer
> > 2.0 + 3.0: should return float
> > 1 / 3: should return float
> > 4 / 2: should return int
> >
> > In my grammar there is only one rule for a numeric expression. I do not know
> > whether I should duplicate the entire operator precedence rules for the
> > distinction between float and int.
> > The following statements are part of my grammar:
> >
> > expression
> > : list
> > | quotedword
> > | booleanexpression
> > ;
> >
> > booleanexpression
> > : numericexpression (BOOL^ numericexpression)*
> > ;
> >
> > numericexpression
> > : mult ((PLUS^ | MINUS^) mult)*
> > ;
> >
> > mult
> > : atom ((MULTIPLY^ | DIVIDE^) atom)*
> > ;
> >
> > atom
> > : INT
> > | FLOAT
> > | ID
> > | LEFTPAREN expression RIGHTPAREN
> > -> ^(EXPRESSION expression)
> > ;
> >
> > Does anybody have a idea how I should take care of this distinction between
> > float and int? Or is this distinction even necessary?
> >
> > --
> > Regards,
> >
> > Steven
> >
> > List: http://www.antlr.org/mailman/listinfo/antlr-interest
> > Unsubscribe: http://www.antlr.org/mailman/options/antlr-interest/your-email-address
> >
> 



More information about the antlr-interest mailing list