Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Xtext optional keyword grammar is not working(Xtext optional keyword grammar is not working)
Xtext optional keyword grammar is not working [message #1733621] Mon, 30 May 2016 13:29 Go to next message
Sachin Samaram is currently offline Sachin SamaramFriend
Messages: 271
Registered: April 2016
Senior Member
Hi,

I have OpenAssignment in my edit like a = b or math a = b or math %2.0a = b

I have written grammar for OpenAssignment as shown in below grammar. Parser not parsing the below grammar

OpenAssignment: {OpenAssignment} ('math')? (precision=Precision)? ((exp+=Expression) (','? (exp+=Expression))*);

but if I remove '?' after 'math' keyword it is working.

OpenAssignment: {OpenAssignment} ('math') (precision=Precision)? ((exp+=Expression) (','? (exp+=Expression))*);

How can I handle this case?

Model: {Model} NL* statements+=Statement*;

Statement: (OpenAssignment);

OpenAssignment: {OpenAssignment} ('math')? (precision=Precision)? ((exp+=Expression) (','? (exp+=Expression))*);

Expression:
LogicalOr
;
LogicalOr returns Expression:
BitwiseOr ({LogicalOr.left=current} '||' right=BitwiseOr)*
;
BitwiseOr returns Expression:
LogicalAnd ({BitwiseOr.left=current} '|' right=LogicalAnd)*
;
LogicalAnd returns Expression:
BitwiseAnd ({LogicalAnd.left=current} '&&' right=BitwiseAnd)*
;
BitwiseAnd returns Expression:
Equivalency ({BitwiseAnd.left=current} '&' right=Equivalency)*
;
Equivalency returns Expression:
Relational ({Equivalency.left=current} ('=='|'!='|'=') right=Relational)*
;
Relational returns Expression:
PlusMinus ({Relational.left=current} ('>='|'<='|'>'|'<') right=PlusMinus)*
;
PlusMinus returns Expression:
DivMult ({PlusMinus.left=current} ('+'|'-') right=DivMult)*
;
DivMult returns Expression:
LogicalNot ({DivMult.left=current} ('/'|'*') right=LogicalNot)*
;
LogicalNot returns Expression:
OnesComplement ({LogicalNot.left=current} '!' right=OnesComplement)*
;
OnesComplement returns Expression:
Exponentiation ({OnesComplement.left=current} ('~') right=Exponentiation)*
;
Exponentiation returns Expression:
Concatenation ({Exponentialtion.left=current} '^' right=Concatenation)*
;
Concatenation returns Expression:
Primary ({Concatenation.left=current} '##' right=Primary)*
;
Primary returns Expression: {Expression}
( => jplId=JPLID | number=NUMBER | str=STRING | ('[' arrSize=(ID|NUMBER) ']') | ('(' ((ID|NUMBER|STRING) (','? (ID|NUMBER|STRING))*)? ')') | ('{' (id+=ID|num+=NUMBER) (','? (id+=ID|num+=NUMBER))*) '}' )
;
GSD returns Expression:
JPLID {GSD.left=current} ('[' occurence=(NUMBER|STRING) ']')?('(' m=NUMBER (',' (n=NUMBER))? ')')? '->' right=JPLID
;

terminal DBMS: ('DBMS'|'Dbms'|'dbms') (('\\'(' '* '\r'? '\n')) | !('\n' | '\r'))*;

terminal ID : (':'('+')?)? ('a'..'z'|'A'..'Z'|'_'|'$'|'.'|'@')('a'..'z'|'A'..'Z'|'_'|'-'|'+'|'$'|'.'|'0'..'9'|'@')*;

terminal STRING :
'"' ( '\\'('"'|('\r'? '\n')) | !('\n'|'"') )* '"' |
"'" ( '\\'("'"|('\r'? '\n')) | !('\n'|"'") )* "'";

terminal SL_COMMENT: '//' !('\n' | '\r')*;

terminal HASH_COMMENT : ('#'('\r'? '\n')?) | ('#' !('#'|'\n'|'\r')+ ('\r'? '\n')?) | (('#''#''#') !('\n'|'\r')* ('\r'? '\n')?);

terminal WS : (' '|'\t'| ('\\'(' '* '\r'? '\n')))+;

terminal NL : ('\r'? '\n')+;

terminal NUMBER: ('-'|'+')? (OCTAL|FLOAT|INT|BINARY|HEX);

terminal OCTAL returns ecore::EInt: ('0') ('0'..'7')+;

terminal FLOAT returns ecore::EFloat: ('0'..'9' ('0'..'9')*) '.' ('0'..'9')*;

terminal INT returns ecore::EInt: '0'..'9' ('0'..'9')*;

terminal RANGE: '0'..'9' '..' ('0'..'9')*;

terminal BINARY returns ecore::EInt: ('0b'|'0B') ('0'|'1')+;

terminal HEX returns ecore::EInt:('0x'|'0X')('0'..'9'|'a'..'f'|'A'..'F')+;

[Updated on: Mon, 30 May 2016 13:30]

Report message to a moderator

Re: Xtext optional keyword grammar is not working [message #1733651 is a reply to message #1733621] Mon, 30 May 2016 17:22 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14735
Registered: July 2009
Senior Member
what about doing what i recommended before.
this would have shown you that a minimal grammar like

Model: {Model} statements+=OpenAssignment*;

OpenAssignment: {OpenAssignment} ('math')? ((exp+=Expression) (','? (exp+=Expression))*);
Expression:
Primary
;

Primary returns Expression: {Expression}
number=NUMBER
;

terminal SL_COMMENT: '//' !('\n' | '\r')*;

terminal NUMBER: ('-'|'+')? INT;

terminal INT returns ecore::EInt: '0'..'9' ('0'..'9')*;



reproduces the problem.

now have a look at this example model:

1 2 3


should that give
3 statements with 1 expr each
2 statements, 1 with 1 expr, 2 with 2 expr
or 1 statement with 3 expr

???
your language seems to be amibigous like hell

you could try something like

OpenAssignment:

	->'math'? exp+=Expression(->','? ->exp+=Expression*);


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Day Job: https://www.everest-systems.com
Re: Xtext optional keyword grammar is not working [message #1733708 is a reply to message #1733651] Tue, 31 May 2016 07:48 Go to previous messageGo to next message
Sachin Samaram is currently offline Sachin SamaramFriend
Messages: 271
Registered: April 2016
Senior Member
Hi,

I am building entire grammar step by step as you suggested.

Up to the OpenAssignment everything was fine.

I tried the above already. But no luck. I was trying it from last two days but it is not solved

Can you please tell me why 'math' is working but 'math'? not?

[Updated on: Tue, 31 May 2016 07:49]

Report message to a moderator

Re: Xtext optional keyword grammar is not working [message #1733709 is a reply to message #1733708] Tue, 31 May 2016 08:03 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14735
Registered: July 2009
Senior Member
Yes but the grammar is for a analyzing way to big and complicated

Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Day Job: https://www.everest-systems.com
Re: Xtext optional keyword grammar is not working [message #1733711 is a reply to message #1733708] Tue, 31 May 2016 08:06 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14735
Registered: July 2009
Senior Member
p.s.

if you have math required you know that a new OpenAssignment is started if the word math occurrs

=>

math 1 2 3 => 1 OpenAssignment
math 1 math 2 math 3 => 3 OpenAssignments
math 1 2 math 3 => 2 OpenAssignments


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Day Job: https://www.everest-systems.com
Re: Xtext optional keyword grammar is not working [message #1733719 is a reply to message #1733711] Tue, 31 May 2016 08:36 Go to previous messageGo to next message
Sachin Samaram is currently offline Sachin SamaramFriend
Messages: 271
Registered: April 2016
Senior Member
Ok I will try to fix it. No more problem I am facing with concatenation operator '##'.

In my language '##' is concatenation operator and also have hash comment which should not preceded or followed by anything except spaces.

But when a hash comment starts with two hashes ## it is taking it as concatenation operator specified in

Concatenation returns Expression:
Primary ({Concatenation.left=current} '##' right=Primary)*
;
and throwing error not showing as comment.
I want something like this

##This is a comment

a = "String1" ## "String2" -------------> this should be concatenation

I have tried something like this :

terminal CONCAT: (('\\'('\r'? '\n')) | !('\n'|'\r')) '##'
terminal HASH_COMMENT : ('#' !('\n'|'\r')* ('\r'? '\n')?) ;

Concatenation returns Expression:
Primary ({Concatenation.left=current} CONCAT right=Primary)*
;

Re: Xtext optional keyword grammar is not working [message #1733720 is a reply to message #1733719] Tue, 31 May 2016 08:37 Go to previous message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14735
Registered: July 2009
Senior Member
sry dont have the time for this.
please open a new topic and add a MINIMAL! example reproducing the problem


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Day Job: https://www.everest-systems.com
Previous Topic:Create Rodin project from Xtend for my DSL
Next Topic:Xtext hash comment problem
Goto Forum:
  


Current Time: Sun Dec 08 09:18:38 GMT 2024

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

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

Back to the top