Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » control values by antlr
control values by antlr [message #638567] Thu, 11 November 2010 16:14 Go to next message
Maria is currently offline MariaFriend
Messages: 49
Registered: October 2010
Member
Hi,
In my Xtext editor i would like to write expressions like this:

3 > 4 // we should compare two integers
string1 != string2 // we should compare two strings

Here I must be sure that I don't write for example string > 4 or 3 != string.

So is it possible to avoid this control by using Xtext and using in its place the parser antlr ?
(the grammar Xtext permit to write (Any type of data 'comparaison operator' Any type of data) and ANTLR verify that "Any type of data is integer or string and prohibit us to write boolean for example ar string compared with an Integer)
If yes, How can I proceed ?
Re: control values by antlr [message #638617 is a reply to message #638567] Thu, 11 November 2010 18:52 Go to previous messageGo to next message
Henrik Lindberg is currently offline Henrik LindbergFriend
Messages: 2509
Registered: July 2009
Senior Member
Xtext does not generate a type checker for your grammar. It does not
know what '+' and '>' etc. means. You have to implement the type
checking. It is not a good idea to have the parser perform the type
checking as the grammar would be complicated and would signal type
problems as syntax errors with no chance of providing quick fixes etc.
(i.e. the input would be seen as invalid as opposed to having the wrong
type).

There are several ways of implementing type checking, ranging from quite
simple to very advanced.

If your operators always operate on literals (like 2 3 "hello" etc) then
it is simply a matter of implementing validation of the corresponding
operator - a validator for 'add' could check that the left and right
sides have the same type for instance.

If you have full expressions in your language, you need to have a
mechanism to associate a type with every expression. It is also a good
idea to cache the type if types are inferred.
A basic implementation can use a polymorphic dispatcher and simply
return the type for different expressions. If you want a cache, one can
be implemented using a model adapter.

As an example - type(LiteralExpression o) would return the getClass()
for the value of the literal expression o. A type(BinaryOpExpression o)
would return the return type of the function that implements the binary
op - which means that it must calculate the left and right parameter
types, then find the function and then get its return type. This gets
tricky if you want to support integer, double, float, and other number
types as you probably want the result to have the more general result
(i.e. int + double => double). To make the example a bit more complete,
a type(VariableExpression o) would look at what the VariableExpression
is referencing (i.e. the cross reference), and get the type from there
(assuming that variables are declared with a type like "Integer a").


Yet more advanced is to infer types - e.g.
var a = "hello"; // variable a is a string
var a = { 1, 2, 3.14 }; // a is list of Number

Once you have a mechanism to return type for any expression, you can
implement validation.

I don't know what is available in the Xbase language - which could be a
starting point. The b3 language I am working on has a quite competent
type inference mechanism (implemented as described above) - perhaps
something to take a look at if you do not find information elsewhere
(look at the class B3BackendTypeProvider as a starting point).

I hope that gives an idea about what is required.
Regards
- henrik



On 11/11/10 5:14 PM, Maria wrote:
> Hi,
> In my Xtext editor i would like to write expressions like this:
>
> 3 > 4 // we should compare two integers
> string1 != string2 // we should compare two strings
> Here I must be sure that I don't write for example string > 4 or 3 !=
> string.
>
> So is it possible to avoid this control by using Xtext and using in its
> place the parser antlr ?
> (the grammar Xtext permit to write (Any type of data 'comparaison
> operator' Any type of data) and ANTLR verify that "Any type of data is
> integer or string and prohibit us to write boolean for example ar string
> compared with an Integer)
> If yes, How can I proceed ?
Re: control values by antlr [message #638754 is a reply to message #638617] Fri, 12 November 2010 13:34 Go to previous messageGo to next message
Maria is currently offline MariaFriend
Messages: 49
Registered: October 2010
Member
So you think that it is better to define the type of expression that we are supposed to put on the left and the right of the operator by using xtext and not by using tha antlr parser?
Re: control values by antlr [message #638756 is a reply to message #638754] Fri, 12 November 2010 13:40 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14716
Registered: July 2009
Senior Member
Hi,

have a look at the xtext typesystem framework: http://code.google.com/a/eclipselabs.org/p/xtext-typesystem/

~Christian


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: control values by antlr [message #638889 is a reply to message #638754] Fri, 12 November 2010 21:53 Go to previous messageGo to next message
Henrik Lindberg is currently offline Henrik LindbergFriend
Messages: 2509
Registered: July 2009
Senior Member
Sorry, but i don't understand your question. You use Xtext to generate
an antlr parser.
I am saying that type checking should be a separate step - imagine the
number of different parser rules you would need for a binary operator
like + ...
- henrik

Maria <lavague@voila.fr> wrote:
> So you think that it is better to define the type of expression that
> we are supposed to put on the left and the right of the operator by
> using xtext and not by using tha antlr parser?


--
- henrik
icon7.gif  Re: control values by antlr [message #639121 is a reply to message #638756] Mon, 15 November 2010 11:28 Go to previous message
Maria is currently offline MariaFriend
Messages: 49
Registered: October 2010
Member
Thank you Mr Dietrich it is exactly what i need Razz
You have saved me
Previous Topic:To export errors...
Next Topic:Multiple DSLs - Single EMF Model?
Goto Forum:
  


Current Time: Tue Sep 24 06:29:17 GMT 2024

Powered by FUDForum. Page generated in 0.03098 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top