Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Question concerning "IF", "ELSE IF", "ELSE" structures
Question concerning "IF", "ELSE IF", "ELSE" structures [message #1765892] Wed, 14 June 2017 11:51 Go to next message
Christoph Steinforth is currently offline Christoph SteinforthFriend
Messages: 11
Registered: June 2017
Junior Member
Hi there,

currently I'm working on a grammar for a simple scripting language. Now I'm stuck at a structure like

IF (A == B) THEN 
  (...)
ELSE IF (A == C) THEN 
  (...)
ELSE
  IF (C == E) THEN 
    (...)
  ELSE IF (C == F) THEN 
    (...)
  ELSE
    (...)
  ENDIF  
  (...)
ENDIF


I end up with the message

error(211): ../org.xtext.example.iftest/src-gen/org/xtext/example/iftest/parser/antlr/internal/InternalIfTest.g:1009:3: [fatal] rule ruleCondition has non-LL(*) decision due to recursive rule invocations reachable from alts 1,2.  Resolve by left-factoring or using syntactic predicates or using backtrack=true option.


but in fact I don't have an idea how to rewrite this in order to work. I guess the main problem is the fact there's no way to distinguish "ELSE IF" from "ELSE" and "IF".

My grammar is appended.

Any help is appreciated.

Thanks in advance,

Christoph
  • Attachment: IfTest.xtext
    (Size: 0.98KB, Downloaded 125 times)
Re: Question concerning "IF", "ELSE IF", "ELSE" structures [message #1765897 is a reply to message #1765892] Wed, 14 June 2017 12:13 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14722
Registered: July 2009
Senior Member
did you read: https://zarnekow.blogspot.de/2012/10/xtext-corner-5-backtracking-vs.html and https://dslmeinte.wordpress.com/2011/12/05/using-syntactic-predicates-in-xtext-part-1/

Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Day Job: https://www.everest-systems.com
Re: Question concerning "IF", "ELSE IF", "ELSE" structures [message #1765899 is a reply to message #1765897] Wed, 14 June 2017 12:35 Go to previous messageGo to next message
Christoph Steinforth is currently offline Christoph SteinforthFriend
Messages: 11
Registered: June 2017
Junior Member
Hi,

if understand those blog entries I can "guide" the parser by using the syntactic predicate. In fact the message does not appear if I write

Condition hidden(WS):
	{Condition}
	'IF' '('if=Expression')' 'THEN'
		(cond+=Condition)*
	(('ELSE' 'IF') '('elseif=Expression')' 'THEN'
		(cond+=Condition)*)*	
	(=> 'ELSE'
		(cond+=Condition)*)?	
	'ENDIF'	
;



but this results in an error when testing the intended structure like

IF (A == B) THEN 
  (...)
ELSE IF (A == C) THEN 
  (...)
ELSE
  IF (C == E) THEN 
    (...)
  ELSE IF (C == F) THEN 
    (...)
  ELSE
    (...)
  ENDIF  
  (...)
ENDIF


BTW the problem does not appear if the "ELSE IF" could be written "ELSEIF" (not possible, unfortunately). Do I have to structure the "ELSE IF"/"ELSE" part in a different way?

Thanks in advance for your patience,

Christoph
Re: Question concerning "IF", "ELSE IF", "ELSE" structures [message #1765900 is a reply to message #1765899] Wed, 14 June 2017 12:39 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14722
Registered: July 2009
Senior Member
no more like a

Condition hidden(WS):
{Condition}
'IF' '('if=Expression')' 'THEN'
(cond+=Condition)*
(=> 'ELSE'
(cond+=Condition)*)?
'ENDIF'
;


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Day Job: https://www.everest-systems.com
Re: Question concerning "IF", "ELSE IF", "ELSE" structures [message #1765902 is a reply to message #1765900] Wed, 14 June 2017 12:46 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14722
Registered: July 2009
Senior Member
p.s.

what are your requirements to parse else if


is it a if inside a else
or is it a elseif


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Day Job: https://www.everest-systems.com
Re: Question concerning "IF", "ELSE IF", "ELSE" structures [message #1765905 is a reply to message #1765900] Wed, 14 June 2017 12:52 Go to previous messageGo to next message
Ed Willink is currently offline Ed WillinkFriend
Messages: 7676
Registered: July 2009
Senior Member
Hi

This is an infamous problem for which the C language had an excuse; it predated YACC-like grammar tools. https://en.wikipedia.org/wiki/Dangling_else

Nowadays LALR tools should always help you redesign your grammar. Xtext is not LALR but I understand that ANTLR has some related facilities behind the scenes. IMHO syntactic predicates are a sticking plaster to avoid competent design.

You have an ENDIF that should avoid the usual problem but ELSE IF reintroduces it. I suspect that ELSE THEN, or ELSEIF should cure it.

Regards

Ed Willink
Re: Question concerning "IF", "ELSE IF", "ELSE" structures [message #1765910 is a reply to message #1765902] Wed, 14 June 2017 12:53 Go to previous messageGo to next message
Christoph Steinforth is currently offline Christoph SteinforthFriend
Messages: 11
Registered: June 2017
Junior Member
Hi,

the "ELSE IF" is an "ELSEIF", so there's a difference between

IF (....) THEN
ELSE
  IF (....) THEN
  ENDIF
ENDIF


and

IF (....) THEN
ELSE IF (....) THEN
ENDIF


Christoph
Re: Question concerning "IF", "ELSE IF", "ELSE" structures [message #1765911 is a reply to message #1765910] Wed, 14 June 2017 12:54 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14722
Registered: July 2009
Senior Member
is this an indentation based language?

Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Day Job: https://www.everest-systems.com
Re: Question concerning "IF", "ELSE IF", "ELSE" structures [message #1765915 is a reply to message #1765911] Wed, 14 June 2017 13:14 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14722
Registered: July 2009
Senior Member
if yes maybe then you should use the whitespace feature of xtext instead of mimic it

Model:
	cs+=IfStatement*;
	
	
Literal:
	value=INT
;

IfStatement:
	{Condition}
	'IF' '('if+=Literal')' 'THEN' BEGIN
		expr+=Expression+
	END
	('ELSE' 'IF'  '('if+=Literal')' 'THEN' BEGIN expr+=Expression+ END)*
	( 'ELSE' BEGIN expr+=Expression+ END)?
	'ENDIF'	
;

Expression:
    IfStatement | Literal;

terminal BEGIN: 'synthetic:BEGIN';
terminal END: 'synthetic:END';


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Day Job: https://www.everest-systems.com
Re: Question concerning "IF", "ELSE IF", "ELSE" structures [message #1765918 is a reply to message #1765915] Wed, 14 June 2017 13:22 Go to previous messageGo to next message
Ed Willink is currently offline Ed WillinkFriend
Messages: 7676
Registered: July 2009
Senior Member
Hi

It may be your intention that an ELSE IF is an ELSEIF but that is the ambiguity. It is also an ELSE (IF ...). I strongly recommend that you use yacc / bison / LPG to check your grammar; intuitive intentions do not work. For a small example manual conversion will do. For larger examples, there is an Xtext2LPG transformation to assist in OCL grammar checking as part of the OCL project releng tooling.

Regards

Ed Willink
Re: Question concerning "IF", "ELSE IF", "ELSE" structures [message #1765932 is a reply to message #1765915] Wed, 14 June 2017 13:52 Go to previous messageGo to next message
Christoph Steinforth is currently offline Christoph SteinforthFriend
Messages: 11
Registered: June 2017
Junior Member
Hi,

unfortunately it is not identation aware.

Christoph
Re: Question concerning "IF", "ELSE IF", "ELSE" structures [message #1765935 is a reply to message #1765932] Wed, 14 June 2017 13:55 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14722
Registered: July 2009
Senior Member
well for if else it is?!?

Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Day Job: https://www.everest-systems.com
Re: Question concerning "IF", "ELSE IF", "ELSE" structures [message #1765936 is a reply to message #1765918] Wed, 14 June 2017 13:55 Go to previous messageGo to next message
Christoph Steinforth is currently offline Christoph SteinforthFriend
Messages: 11
Registered: June 2017
Junior Member
Hi,

could this

Quote:

It may be your intention that an ELSE IF is an ELSEIF but that is the ambiguity. It is also an ELSE (IF ...).


lead to the fact that there will be no solution in Xtext apart from changing "ELSE IF" to "ELSEIF"?

Christoph
Re: Question concerning "IF", "ELSE IF", "ELSE" structures [message #1765937 is a reply to message #1765936] Wed, 14 June 2017 13:58 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14722
Registered: July 2009
Senior Member
well i dont know what happens if you partially apply the indentation thing and hide the synthethic tokens elsewhere

Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Day Job: https://www.everest-systems.com
Re: Question concerning "IF", "ELSE IF", "ELSE" structures [message #1765940 is a reply to message #1765937] Wed, 14 June 2017 14:15 Go to previous messageGo to next message
Ed Willink is currently offline Ed WillinkFriend
Messages: 7676
Registered: July 2009
Senior Member
Hi

This is not an Xtext problem. When you design a bad grammar it means that your users are going to encounter surprises. (C programmers have to experience pain to learn about the bad else problem. Or they mindlessly follow a corporate style guide that mandates gratuitous {}.) You need to rethink the grammar. I suggested ELSEIF or ELSE THEN.

Regards

Ed Willink
Re: Question concerning "IF", "ELSE IF", "ELSE" structures [message #1765967 is a reply to message #1765940] Wed, 14 June 2017 15:53 Go to previous messageGo to next message
Christoph Steinforth is currently offline Christoph SteinforthFriend
Messages: 11
Registered: June 2017
Junior Member
Hi,

thanks for your advice. The indention approach seems to solve the problem, in addition this would greatly increase the readability. The change of "ELSE IF" to "ELSEIF" has to be discussed with the development team. As stated already this would also lead to the desired structure.

Thanks to all of you,

Christoph

p.s.: Is there am way to mark this as solved?
Re: Question concerning "IF", "ELSE IF", "ELSE" structures [message #1765980 is a reply to message #1765892] Wed, 14 June 2017 16:45 Go to previous message
Karsten Thoms is currently offline Karsten ThomsFriend
Messages: 762
Registered: July 2009
Location: Dortmund, Germany
Senior Member

>p.s.: Is there am way to mark this as solved?
nope, not in this forum
Previous Topic:Couldn't find resource on classpath. URI was 'classpath:/de.mdsd.xtext.support.utils/GenModel.npsm#|
Next Topic:Getting object type/name from an unresolved eProxy
Goto Forum:
  


Current Time: Tue Oct 08 15:27:32 GMT 2024

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

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

Back to the top