Skip to main content



      Home
Home » Modeling » TMF (Xtext) » Grammar parsing problem(Is it possible to fix grammar and not to allow using of the variable before declaration)
Grammar parsing problem [message #1798695] Thu, 22 November 2018 18:51 Go to next message
Eclipse UserFriend
Hi all, I have the following grammar(please, see it below) and it's not allow the code
class test {
	testKey : text;
	testIndex : text;
	key testKey:text;
	index testkey;
}


With the error message
mismatched input ';' expecting ':'


I know that it happens because ANTLR tries to recognize the string "index testkey;" as variable and ask for the variable declaration. But is it possible to change the grammar to fix this issue, not allow of using a variable before the declaration, or I need to add the precedence checking into validator?

grammar org.blockchain.rell.Rell with org.eclipse.xtext.common.Terminals

generate rell "http://www.blockchain.org/rell/Rell"

Model:
	entities+=ClassDefinition*
	operations+=Operation*;

ClassDefinition:
	'class' name=ID ('extends' superType=[ClassDefinition])? '{'
	attributeListField+=AttributeListField*
	specificationList+=Specification*
	'}';

Specification:
	((key=Key | index=Index | ((key=Key index=Index)) name=[VariableDeclaration])) ';';

AttributeListField:
	key=Key? index=Index? attributeList+=RelAttrubutesList ';';

Operation:
	"operation" name=ID "(" parameters=RelAttrubutesList? ")" "{" statements+=Statement* "}";

Statement:
	(relation=Relational | variable=Variable | varInit=VariableInit) ';';

Variable:
	declaration=VariableDeclaration (('=' expression=Expression)?);

Relational:
	Update | Delete | Create;

Update:
	'update' entity=[ClassDefinition] '(' conditions=Conditions? ')' '{' variableList+=VariableInit
	(',' variableList+=VariableInit*)? '}';

Delete:
	'delete' entity=[ClassDefinition] '(' conditions=Conditions? ')';

Create:
	'create' entity=[ClassDefinition] '(' conditions=Conditions? ')';

Conditions:
	elements+=ConditionElement (',' elements+=ConditionElement*)?;

ConditionElement:
	name=ID ('==' | '!=' | '>' | '<' | '>=' | '<=') expr=Expression;

VariableInit:
	name=[VariableDeclaration] '=' expression=Expression;

Expression:
	or=Or;

Or returns Expression:
	And ({Or.left=current} "or" right=And)*;

And returns Expression:
	Equality ({And.left=current} "and" right=Equality)*;

Equality returns Expression:
	Comparison ({Equality.left=current} op=("==" | "!=")
	right=Comparison)*;

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

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

MulOrDiv returns Expression:
	Primary ({MulOrDiv.left=current} op=('*' | '/')
	right=Primary)*;

Primary returns Expression:
	'(' Expression ')' |
	{Not} "not" expression=Primary |
	Atomic;

Atomic returns Expression:
	{IntConstant} value=INT |
	{StringConstant} value=STRING |
	{BoolConstant} value=('true' | 'false') |
	{VariableRef} value=[VariableDeclaration];

RelAttrubutesList:
	value+=Variable (',' value+=Variable)*;

VariableDeclaration:
	name=Name | name=Pubkey | name=Tuid | (name=ID ':' type=TypeReference);

TypeReference:
	primitive=PrimitiveType | entityType=ClassType;

PrimitiveType:
	primitiveType=(Text | Tuid | Pubkey|Name | Timestamp | Integer | Json | ByteArray | Boolean);

ClassType:
	entityRef=[ClassDefinition];

Name:
	'name';

Pubkey:
	'pubkey';

Timestamp:
	'timestamp';

Tuid:
	'tuid';

Boolean:
	'bool';

Json:
	'json';

Integer:
	'integer';

Text:
	'text';

ByteArray:
	'byte_array';

Key:
	'key';

Index:
	'index';


Re: Grammar parsing problem [message #1798706 is a reply to message #1798695] Fri, 23 November 2018 00:20 Go to previous messageGo to next message
Eclipse UserFriend
Hi am not sure if I understand your problem

Mismatched input means there is something in the model that is not allowed according to the grammar
So you would need to adapt the grammar

Doing a usage before a declaration is a thing you ususally do in the validator
Re: Grammar parsing problem [message #1798738 is a reply to message #1798706] Fri, 23 November 2018 07:29 Go to previous messageGo to next message
Eclipse UserFriend
I just want to change my grammar to allow the mentioned text.
Re: Grammar parsing problem [message #1798740 is a reply to message #1798738] Fri, 23 November 2018 07:51 Go to previous messageGo to next message
Eclipse UserFriend
but which rule shall parse
index testkey;
?
Re: Grammar parsing problem [message #1798742 is a reply to message #1798740] Fri, 23 November 2018 07:57 Go to previous messageGo to next message
Eclipse UserFriend
so maybe you got yur specification rule wrong

ClassDefinition:
'class' name=ID ('extends' superType=[ClassDefinition])? '{'
(attributeListField+=AttributeListField|
specificationList+=Specification)*
'}';

Specification:
((key='key' | index='index' | ((key='key' index='index'))) name=ID) ';';
Re: Grammar parsing problem [message #1798745 is a reply to message #1798742] Fri, 23 November 2018 08:15 Go to previous messageGo to next message
Eclipse UserFriend
Specification:
	((key=Key | index=Index | ((key=Key index=Index)) name=[VariableDeclaration])) ';';
Re: Grammar parsing problem [message #1798747 is a reply to message #1798745] Fri, 23 November 2018 08:24 Go to previous messageGo to next message
Eclipse UserFriend
But, maybe I can set the priority of the rule.
I.e. we parse the sentence as Specification and if it not recognized try to parse as Variable
Re: Grammar parsing problem [message #1798751 is a reply to message #1798747] Fri, 23 November 2018 09:14 Go to previous messageGo to next message
Eclipse UserFriend
no i mean your parsens look wired.
i changed it from [vardef] to ID to get rid of 60% of grammar unrelated to your actual problem
Re: Grammar parsing problem [message #1798754 is a reply to message #1798751] Fri, 23 November 2018 10:28 Go to previous messageGo to next message
Eclipse UserFriend
Problem is I need the link on the VariableDeclaration. Like in the MySQL or other I declare the variable first then create the index or key with it.
Re: Grammar parsing problem [message #1798764 is a reply to message #1798754] Fri, 23 November 2018 12:08 Go to previous message
Eclipse UserFriend
no i mean your parens are WRONG

(key=Key | index=Index | (key=Key index=Index)) name=[VariableDeclaration] ';';
Previous Topic:Xpect Test case issue
Next Topic:Switch case problem with JvmTypeReference
Goto Forum:
  


Current Time: Sat Jun 21 14:21:50 EDT 2025

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

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

Back to the top