Xtext optional keyword grammar is not working [message #1733621] |
Mon, 30 May 2016 13:29 |
Sachin Samaram 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 |
|
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:
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
|
|
|
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.03931 seconds