Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Xtext grammar(Xtext grammar)
Xtext grammar [message #1731328] Wed, 04 May 2016 10:43 Go to next message
RamaRao Nandamuri is currently offline RamaRao NandamuriFriend
Messages: 120
Registered: April 2016
Senior Member
Hi,

I have syntax like

def a, a[10](10), a(10)[10], a{10}[10](10), a(10)[10){10}, a[10]{10}(10)

No guarantee of order of declaration for [10] or {10} or (10).

I have written grammar like this

Decl : 'def' name+=ID
(isArray?='[' (maxOccurs+=NUMBER) ']')?
(isRange?='[' (range+=RANGE) ']')?
(isChar?='[' (Id+=ID) ']')?
(hasSize ?= '(' varsize=NUMBER ')')?
('=' Id+=ID ('(' Id+=ID (','? Id+=ID)* ')')?)?
('=' const=Constant)?
('=' string=STRING)? ','?
;

I know here my grammar follows the order of definition. How can I handle this which allows me all the orders?
Re: Xtext grammar [message #1731329 is a reply to message #1731328] Wed, 04 May 2016 10:49 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
have a look at the grammar concept of unordered groups

Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Xtext grammar [message #1731331 is a reply to message #1731329] Wed, 04 May 2016 11:19 Go to previous messageGo to next message
RamaRao Nandamuri is currently offline RamaRao NandamuriFriend
Messages: 120
Registered: April 2016
Senior Member
Thank you. I got the solution. The main problem for me is questioning google about my problem
Re: Xtext grammar [message #1731444 is a reply to message #1731331] Thu, 05 May 2016 10:33 Go to previous messageGo to next message
RamaRao Nandamuri is currently offline RamaRao NandamuriFriend
Messages: 120
Registered: April 2016
Senior Member
Hi Christian,

My below grammar is failing because keyword 'math' is optional

Assignment returns Statement : (name=('math')? ((exp+=Expression) (','? exp+=Expression)*)|((oneDecl+=OneDecl) (','? oneDecl+=OneDecl)*)) ','?;

But if I remove 'math' optional it is working:

Assignment returns Statement : (name=('math') ((exp+=Expression) (','? exp+=Expression)*)|((oneDecl+=OneDecl) (','? oneDecl+=OneDecl)*)) ','?;

But I want case 1. Could you please tell me how can I achieve that?
Re: Xtext grammar [message #1731446 is a reply to message #1731444] Thu, 05 May 2016 10:48 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
Please share the complete grammar

Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Xtext grammar [message #1731522 is a reply to message #1731446] Fri, 06 May 2016 12:47 Go to previous messageGo to next message
RamaRao Nandamuri is currently offline RamaRao NandamuriFriend
Messages: 120
Registered: April 2016
Senior Member

Model:
(variables+=Variable)*
;

Variable:
name=ID '=' expression=Expression
;

Expression:
BooleanExpression;

BooleanExpression returns Expression:
Comparison
(({AndOrExpression.left=current} op=("||"|"&&")) right=Comparison)*;

Comparison returns Expression:
Equals
(({Comparison.left=current} op=("<") ) right=Equals)*;

Equals returns Expression:
Addition
(({Equals.left=current} op=("==") ) right=Addition)*;

Addition returns Expression:
Multiplication
(({Plus.left=current} '+' | {Minus.left=current} '-')
right=Multiplication)*;

Multiplication returns Expression:
Prefixed (({MultiOrDiv.left=current} op=("*"|"/")) right=Prefixed)*;

Prefixed returns Expression:
{BooleanNegation} =>"!" expression=Atomic | /* right associativity */
{ArithmeticSigned} =>"-" expression=Atomic | /* right associativity */
Atomic;

Atomic returns Expression:
'(' Expression ')' |
{NumberLiteral} value=INT |
{StringLiteral} value=STRING |
{BooleanLiteral} value=('true'|'false') |
{VariableReference} ref=[Variable]
;

// the types will be used only internally by the type system
Type:
{IntType} 'int' |
{StringType} 'string' |
{BooleanType} 'boolean'
;

[Updated on: Fri, 06 May 2016 13:38]

Report message to a moderator

Re: Xtext grammar [message #1731523 is a reply to message #1731522] Fri, 06 May 2016 12:49 Go to previous messageGo to next message
RamaRao Nandamuri is currently offline RamaRao NandamuriFriend
Messages: 120
Registered: April 2016
Senior Member
No Message Body

[Updated on: Sat, 07 May 2016 11:36]

Report message to a moderator

Re: Xtext grammar [message #1731526 is a reply to message #1731523] Fri, 06 May 2016 13:29 Go to previous message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
well i had hoped for a minimal reproducable example not 200 lines of grammar.

the there is a question you have to answer: a assignment can start with a assignment?

Assignment: ('math')? (expression+=Expression '=' expression+=Expression) (',' expression+=Expression'=' expression+=Expression)*;

Expression:
Primary
;

Primary returns Expression: value+=NUMBER |assignment+=Assignment
;


i dont know if that is intended
thats the point of the left recusion.
what should happen here ?!?

and it makes no sense to me since this is already handled on assignmentlevel ?!?


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Previous Topic:[Delete]
Next Topic:no content assist proposal for XExpression
Goto Forum:
  


Current Time: Thu Apr 25 03:36:49 GMT 2024

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

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

Back to the top