Skip to main content



      Home
Home » Modeling » TMF (Xtext) » Xtext grammar(Xtext grammar)
Xtext grammar [message #1731328] Wed, 04 May 2016 06:43 Go to next message
Eclipse UserFriend
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 06:49 Go to previous messageGo to next message
Eclipse UserFriend
have a look at the grammar concept of unordered groups
Re: Xtext grammar [message #1731331 is a reply to message #1731329] Wed, 04 May 2016 07:19 Go to previous messageGo to next message
Eclipse UserFriend
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 06:33 Go to previous messageGo to next message
Eclipse UserFriend
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 06:48 Go to previous messageGo to next message
Eclipse UserFriend
Please share the complete grammar
Re: Xtext grammar [message #1731522 is a reply to message #1731446] Fri, 06 May 2016 08:47 Go to previous messageGo to next message
Eclipse UserFriend

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 09:38] by Moderator

Re: Xtext grammar [message #1731523 is a reply to message #1731522] Fri, 06 May 2016 08:49 Go to previous messageGo to next message
Eclipse UserFriend
No Message Body

[Updated on: Sat, 07 May 2016 07:36] by Moderator

Re: Xtext grammar [message #1731526 is a reply to message #1731523] Fri, 06 May 2016 09:29 Go to previous message
Eclipse UserFriend
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 ?!?
Previous Topic:[Delete]
Next Topic:no content assist proposal for XExpression
Goto Forum:
  


Current Time: Wed Jul 02 16:31:30 EDT 2025

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

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

Back to the top