Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Error with optional semicon at the end of line
Error with optional semicon at the end of line [message #1791249] Tue, 26 June 2018 16:43 Go to next message
David Horacek is currently offline David HoracekFriend
Messages: 3
Registered: June 2018
Junior Member
Hello,
I'm building a grammar and run into a weird (for me) error.
When I try to set ending semicon as optional (';')? instead of mandatory ';' It'll throw a warning for two possible alternatives... not just for semicons, but also for assignments like + - / *. I'm really out of idesa why is this happening so I'll be glad for any help.

This is my complete grammar:

Domainmodel: (
	events+=Event |
	consts+=Constant
)*;

// Variables
terminal FLOAT: ('+' | '-')? INT ('.' (INT)?)?;
PrimitiveValue: value=(STRING|FLOAT);
Declared: Constant | LocalVar;
AnyValue: Declared | PrimitiveValue;

// Declarations
Constant:
	'const' name=ID '=' value=PrimitiveValue ';'
;
LocalVar:
	(local='var')? name=ID (assign=AssignList value=Expression)? (';')?
;
Event:
	'event' name=EventList '{'
		(localVars+=LocalVar)*
	'}'
;

// Expression
Expression: ExpressionPart;

ExpressionPart returns ExpressionRes:
	BrackedExpression ({ExpressionPart.left=current} link=ExpressionLink right=BrackedExpression)*
;

BrackedExpression returns ExpressionRes:  
	AnyValue | '(' ExpressionPart ')'
;

// Enums
enum EventList:
	CREATE = 'create' | STEP = 'step'
;
enum AssignList:
	PLUSEQ='+=' | MINUSEQ='-=' | EQ='=' | AND='&=' | OR='|=' | MULTI='*='
;
enum ExpressionLink:
	PLUS='+' | MINUS='-' | MULT='*' | DIV='/'
;


And for the case a short example of language I'm trying to build the grammar for:

const cc = 54;

event create {
	var asd;
	asd += cc + 564 - 45;
}

event step {
	asd = 456;
}


When I try to make a semicon at LocalVar optional it results in:

Decision can match input such as "';'" using multiple alternatives: 1, 2
As a result, alternative(s) 2 were disabled for that input

and the list of warrning goes on with inputs such as: + * / -
Can anyone help me, please?
Re: Error with optional semicon at the end of line [message #1791251 is a reply to message #1791249] Tue, 26 June 2018 17:02 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
here is a minified version of your problem showing the problem

Event:
	{Event}'event' '{'
		(localVars+=LocalVar)*
	'}'
;
LocalVar:
	(local='var')? name=ID (assign="=" value=ExpressionPart)? (';')?
;

ExpressionPart returns ExpressionRes:
	LocalVar ({ExpressionPart.left=current} link="+" right=LocalVar)*
;


you can adapt the workflow to generate a debug grammar

parserGenerator = {
				debugGrammar=true
}


and then use antlrworks to analyze


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de

[Updated on: Tue, 26 June 2018 17:06]

Report message to a moderator

Re: Error with optional semicon at the end of line [message #1791252 is a reply to message #1791251] Tue, 26 June 2018 17:08 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
test model to consider

var x = var x2 + var y + var z

does z belong to y oder to x2?


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Error with optional semicon at the end of line [message #1791253 is a reply to message #1791252] Tue, 26 June 2018 17:12 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
you can use predicates to give hints to the parser e.g.

LocalVar:
	(local='var')? name=ID (assign=AssignList value=Expression)? (->';')?
;

ExpressionPart returns ExpressionRes:
	BrackedExpression ->({ExpressionPart.left=current} link=ExpressionLink right=BrackedExpression)*
;


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Error with optional semicon at the end of line [message #1791255 is a reply to message #1791253] Tue, 26 June 2018 17:37 Go to previous message
David Horacek is currently offline David HoracekFriend
Messages: 3
Registered: June 2018
Junior Member
Ou thats it!
Thanks for both explanation and debug grammar hint.

And the possibility of writing var x = var x2... seems so obvious now... :)
(Actually it was this line that gave me the 'Aha' moment)

Which led me to my mistake with Declared rule and to avoid that a quick fix.

Declared: name=ID;

Constant:
	'const' Declared '=' value=PrimitiveValue (';')?
;
LocalVar: 
	(local='var')? name=Declared (assign=AssignList value=Expression)? (';')?
;


Thank you very much
and have a nice day.
Previous Topic:Guillemets and UTF-8 encoding on Windows vs Mac
Next Topic:Ecore.genmodel: Feature not found?
Goto Forum:
  


Current Time: Thu Apr 25 20:13:24 GMT 2024

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

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

Back to the top