Skip to main content



      Home
Home » Modeling » TMF (Xtext) » Why my grammar don't validate the program and how to fix it
Why my grammar don't validate the program and how to fix it [message #1799110] Thu, 29 November 2018 15:47 Go to next message
Eclipse UserFriend
Hi all. I have create the grammar(please, see it bellow and not sure why following text generate the errors.

My program
class user {
       key pubkey;
       index username: text;
       firstName: text;
       lastName: text;
       email: text;
}

operation add_user (admin_pubkey: integer, pubkey, username: text, firstName: text, lastName: text, email: text) {
    create user (pubkey, username, firstName, lastName, email); // Error on this line
}


Errors
Quote:

Multiple markers at this line - no viable alternative at input 'pubkey' - missing ';' at 'username' - no viable alternative at input
'email' - no viable alternative at input 'firstName' - no viable alternative at input 'lastName'


My grammar

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*
	'}';

AttributeListField:
	key=Key? index=Index? mutable=Mutable? 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] '(' (attributeList+=CreateAttrMember (',' attributeList+=CreateAttrMember)*) ')';

CreateAttrMember:
	{VarRefDecl} name=[VariableDeclaration] |
	{Default} name=ID '=' expr=Expression;

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+=VariableOrRef (',' value+=VariableOrRef)*;

VariableOrRef:
	{Var} value=Variable |
	{VarDeclRef} value=[VariableDeclaration];

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';
	
Mutable:
	'mutable'
;


Re: Why my grammar don't validate the program and how to fix it [message #1799111 is a reply to message #1799110] Thu, 29 November 2018 15:48 Go to previous messageGo to next message
Eclipse UserFriend
Why does it not work and how to fix it?
Regards,
Vladimir
Re: Why my grammar don't validate the program and how to fix it [message #1799114 is a reply to message #1799111] Thu, 29 November 2018 17:37 Go to previous messageGo to next message
Eclipse UserFriend
Probably error is in the using of default name of the variable in the expression

In this code
operation add_user (pubkey) {
	i:pubkey=pubkey;// error in this place
    
}


I have the following error message
java.lang.AssertionError: Unexpected errors: XtextSyntaxDiagnostic: null:3 no viable alternative at input 'pubkey', XtextSyntaxDiagnostic: null:4 missing ';' at '}'

But how to fix it? It's not clear
Re: Why my grammar don't validate the program and how to fix it [message #1799123 is a reply to message #1799114] Fri, 30 November 2018 01:17 Go to previous messageGo to next message
Eclipse UserFriend
First of all: simplify.
You don't need all the expression stuff to reproduce
Then think which rules should parse the stuff that does not parse
Third. Since Xtext separates Lexing and parsing and Lexing is done context free you cannot have a keyword at places
Where an ID is expected. Depending on the situation )this is resolvable
https://blogs.itemis.com/en/xtext-hint-identifiers-conflicting-with-keywords

Additionally you can tell Xtext to create a debug grammar (parserGenerator={denugGramar=true} if I remember correct
Then you can use antlr works to analyze (no directly related butsimilar https://blogs.itemis.com/en/debugging-xtext-grammars-what-to-do-when-your-language-is-ambiguous

[Updated on: Fri, 30 November 2018 01:18] by Moderator

Re: Why my grammar don't validate the program and how to fix it [message #1799279 is a reply to message #1799123] Mon, 03 December 2018 16:55 Go to previous messageGo to next message
Eclipse UserFriend
Thank you very much for the reply. But, please consider the following grammar.

grammar org.xtext.example.mydsl1.MyDsl with org.eclipse.xtext.common.Terminals

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

Model:
	variables+=Variable*;

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

VariableDeclaration:
	name=ValuableID  ((':' type=PrimitiveType)?);

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=[Variable];

ValuableID:
	PrimitiveType | ID;

PrimitiveType:
	Text | Tuid | Pubkey | Name | Timestamp | Integer | Json | ByteArray | Boolean;
Name:
	'name';

Pubkey:
	'pubkey';

Timestamp:
	'timestamp';

Tuid:
	'tuid';

Boolean:
	'bool';

Json:
	'json';

Integer:
	'integer';

Text:
	'text';

ByteArray:
	'byte_array';

Key:
	'key';

Index:
	'index';


And this code
integer=5;
i:integer=integer;


On the first string, everything is fine but on the second string, I have following error message
Quote:
no viable alternative at input 'integer'

I don't understand why and how to fix it. May I ask you to help?
Re: Why my grammar don't validate the program and how to fix it [message #1799286 is a reply to message #1799279] Tue, 04 December 2018 00:51 Go to previous messageGo to next message
Eclipse UserFriend
{VariableRef} value=[Variable];

Is short for {VariableRef} value=[Variable|ID];

And means parse an ID to reference a Variable

=>

{VariableRef} value=[Variable|ValueableID];
Re: Why my grammar don't validate the program and how to fix it [message #1799302 is a reply to message #1799286] Tue, 04 December 2018 08:06 Go to previous message
Eclipse UserFriend
Yes, it works. Thanks
Previous Topic:Problem saving model instances with cross-references
Next Topic:Quickfix Unit Test
Goto Forum:
  


Current Time: Thu Jun 19 23:41:49 EDT 2025

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

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

Back to the top