Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Xtext grammar related query(Xtext grammar related query)
Xtext grammar related query [message #1741934] Tue, 30 August 2016 16:38 Go to next message
Sachin Samaram is currently offline Sachin SamaramFriend
Messages: 271
Registered: April 2016
Senior Member
Hi,

Can anyone please tell me how to use parser rule as part of expression?

Below grammar showing error at Primary Expression because I added Annotation inside Expression. But I need it because my language contains this type of coding.

@app()->test=123 ----------- this works fine

if @app()->test=123 -------------- causes error
{
///////
}

My grammar is:

Model: {Model} statements+=Statement*;

Statement: (Annotation)

Annotation: '@app()->' exp+=Expression ;

ArrayDecl : {ArrayDecl} ('[' ']') | ('[' size=exp+=Expression']');

Parenthesis : {Parenthesis} ('(' ')') | ('(' size=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} '!' expr=OnesComplement)
;
OnesComplement returns Expression:
Exponentiation ({OnesComplement.left=current} ('~') right=Exponentiation)*
;
Exponentiation returns Expression:
Concatenation ({Exponentialtion.left=current} '^' right=Concatenation)*
;
Concatenation returns Expression:
ArrowExpression ({Concatenation.left=current} '##' right=ArrowExpression)*
;
ArrowExpression returns Expression:
ColonOperator ({ArrowExpression.left=current} '->' right=ColonOperator)*
;
ColonOperator returns Expression:
Primary | ({ColonOperator} (':'('+')?) expr=Primary)
;
Primary returns Expression: {Expression}
name=NUMBER | name=STRING | name=ID (ArrayDecl=ArrayDecl? & Parenthesis=Parenthesis?) | ArrayDecl=ArrayDecl | Parenthesis=Parenthesis|Annotation
;
Re: Xtext grammar related query [message #1741938 is a reply to message #1741934] Tue, 30 August 2016 16:52 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14661
Registered: July 2009
Senior Member
can you please post

(a) a mininal and complete grammar reproducing the problem
(b) if you have customizations to the parserGenerator in the workflow post these as well


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Xtext grammar related query [message #1741939 is a reply to message #1741938] Tue, 30 August 2016 17:01 Go to previous messageGo to next message
Sachin Samaram is currently offline Sachin SamaramFriend
Messages: 271
Registered: April 2016
Senior Member
Hi,

I have shared you the minimal grammar that reproduces the problem.
I didn't add any customizations to the parserGenerator
Re: Xtext grammar related query [message #1741941 is a reply to message #1741939] Tue, 30 August 2016 17:08 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14661
Registered: July 2009
Senior Member
The grammar is not complete
No definition of if number etc
It is not minimal since the expressions contains way too much stuff

Generating it give a lot of warnings so I assume you use backtracking which is a customization of the parser generator


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Xtext grammar related query [message #1741942 is a reply to message #1741941] Tue, 30 August 2016 17:11 Go to previous messageGo to next message
Sachin Samaram is currently offline Sachin SamaramFriend
Messages: 271
Registered: April 2016
Senior Member
No I am not using any customizations even backtracking also.

Could you please look into the grammar I attached

[Updated on: Wed, 31 August 2016 08:22]

Report message to a moderator

Re: Xtext grammar related query [message #1741946 is a reply to message #1741942] Tue, 30 August 2016 17:44 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14661
Registered: July 2009
Senior Member
i do not want to have a look at such a big grammar to solve your problem

i am sure you use backtracking (have a look at your workflow)

the grammar gives me errors and

warning(200): ../org.xtext.example.mydsl1/src-gen/org/xtext/example/mydsl1/parser/antlr/internal/InternalMyDsl.g:719:3: Decision can match input such as "'->' RULE_NUMBER" using multiple alternatives: 1, 2
As a result, alternative(s) 2 were disabled for that input
warning(200): ../org.xtext.example.mydsl1/src-gen/org/xtext/example/mydsl1/parser/antlr/internal/InternalMyDsl.g:719:3: Decision can match input such as "'->' RULE_ID" using multiple alternatives: 1, 2
As a result, alternative(s) 2 were disabled for that input
warning(200): ../org.xtext.example.mydsl1/src-gen/org/xtext/example/mydsl1/parser/antlr/internal/InternalMyDsl.g:719:3: Decision can match input such as "'->' RULE_STRING" using multiple alternatives: 1, 2
As a result, alternative(s) 2 were disabled for that input

and many others


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Xtext grammar related query [message #1741947 is a reply to message #1741946] Tue, 30 August 2016 17:45 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14661
Registered: July 2009
Senior Member
btw what is the error you get.
please use this grammar as starting point

grammar org.xtext.example.mydsl1.MyDsl with org.eclipse.xtext.common.Terminals hidden(WS, SL_COMMENT, ML_COMMENT)

generate myDsl "http://www.xtext.org/example/mydsl1/MyDsl"

import "http://www.eclipse.org/emf/2002/Ecore" as ecore

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

Statement: (NonRecursingStatement|ConditionalElseBlock) ';'? NL+ | HASH_COMMENT NL*;

NonRecursingStatement returns Statement: 
	(	OpenAssignment
	)
;
//Annotation: {Annotation} '@app' '(' (appExp+=Expression)* ')' ('->'appExp+=Expression);
Annotation: '@app' '(' (appExp+=Expression)* ')' ('->'appExp+=Expression);

Block : (statements+=StartBlock) statementlist+=Statement* (statements+=EndBlock);
StartBlock returns Statement : {StartBlock}'{' NL+;
EndBlock returns Statement : {EndBlock}'}';

IfStatement returns Statement: {Statement} 'if'  (ifExp+=Expression)*;

ElseStatement returns Statement: {Else} (HASH_COMMENT NL*)* 'else';

ElseIfStatement returns Statement: (HASH_COMMENT NL*)* ('else' NL* 'if') (exp+=Expression (','? exp+=Expression)*);

ConditionalIfBlock returns Statement:
	IfStatement {ConditionalIfBlock.left=current} NL+ (=>block+=Block|right+=(NonRecursingStatement)|conditionalIfBlock+=ConditionalIfBlock)
;
ConditionalElseIfBlock returns Statement:
	ConditionalIfBlock {ConditionalElseBlock.left=current} (NL+ elseif+=ElseIfStatement NL+ (=>block+=Block|right+=(NonRecursingStatement)))*
;
ConditionalElseBlock returns Statement:
	ConditionalElseIfBlock {ConditionalElseBlock.left=current} (NL+ else+=ElseStatement NL+ (=>block+=Block|right+=(NonRecursingStatement)))?
;

OpenAssignment: args+=Assignment (',' args+=Assignment)* ','?;

Assignment: name=('math')? (precision=Precision)? assignExp+=Expression;

Precision: (width=NUMBER)|((width=NUMBER)?'.'(decimal_places=NUMBER));


Expression:
	Equivalency
;

Equivalency returns Expression:
	Relational ({Equivalency.left=current} ('=') right=Relational)*
;

Relational returns Expression:
	PlusMinus ({Relational.left=current} ('>') right=PlusMinus)*
;
PlusMinus returns Expression:
	ArrowExpression ({PlusMinus.left=current} ('-') right=ArrowExpression)*
;

ArrowExpression returns Expression:
	Primary ({ArrowExpression.left=current} '->' right=Primary)*
;

Primary returns Expression: {Expression}
	name=NUMBER | Annotation=Annotation | name=ID
;

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'| ('\\'((' '|'\t')* '\r'? '\n')))+;

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

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

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



Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Xtext grammar related query [message #1742107 is a reply to message #1741947] Wed, 31 August 2016 13:48 Go to previous message
Sachin Samaram is currently offline Sachin SamaramFriend
Messages: 271
Registered: April 2016
Senior Member
Thank you sir.
Previous Topic:What is the best JavaFX diagram builder for Xtext language?
Next Topic:Unexpected name generation of the Activator of the UI project in Xtext
Goto Forum:
  


Current Time: Thu Mar 28 09:29:27 GMT 2024

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

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

Back to the top