Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » How to properly translate this antlr grammer into Xtext?
How to properly translate this antlr grammer into Xtext? [message #753949] Mon, 31 October 2011 06:55 Go to next message
iokerica is currently offline iokericaFriend
Messages: 14
Registered: September 2011
Junior Member
Hello all,

I am new to DSL grammer. And I have a simple antlr grammer below. I am interested that what is the proper way to translate it into Xtext grammer?

What I am interested the java block nested in the grammer to help to eval the value.

grammar Exp;

/* This will be the entry point of our parser. */
eval returns [double value]

: exp=additionExp{$value = $exp.value;}


;

/* Addition and subtraction have the lowest precedence. */
additionExp returns [double value]

: m1=multiplyExp {$value = $m1.value;}
( '+' m2=multiplyExp {$value += $m2.value;}
| '-' m2=multiplyExp {$value -= $m2.value;}
)*
;

/* Multiplication and division have a higher precedence. */
multiplyExp returns [double value]

: a1=atomExp {$value = $a1.value;}
( '*' a2=atomExp {$value *= $a2.value;}
| '/' a2=atomExp {$value /= $a2.value;}
)*
;

/* An expression atom is the smallest part of an expression: a number. Or
when we encounter parenthesis, we're making a recursive call back to the
rule 'additionExp'. As you can see, an 'atomExp' has the highest precedence. */
atomExp returns [double value]
: n=Number {$value = Double.parseDouble($n.text);}
| '(' exp=additionExp ')' {$value = $exp.value;}
;

/* A number: can be an integer value, or a decimal value */
Number
: ('0'..'9')+ ('.' ('0'..'9')+)?
;

/* We're going to ignore all white space characters */
WS
: (' ' | '\t' | '\r'| '\n') {$channel=HIDDEN;}
;
Re: How to properly translate this antlr grammer into Xtext? [message #753953 is a reply to message #753949] Mon, 31 October 2011 07:22 Go to previous message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14716
Registered: July 2009
Senior Member
http://blog.efftinge.de/2010/08/parsing-expressions-with-xtext.html

Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Previous Topic:Ocl grammar use
Next Topic:ConcurrentModificationException
Goto Forum:
  


Current Time: Tue Sep 24 03:35:20 GMT 2024

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

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

Back to the top