Question concerning "IF", "ELSE IF", "ELSE" structures [message #1765892] |
Wed, 14 June 2017 11:51  |
Eclipse User |
|
|
|
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 133 times)
|
|
|
|
Re: Question concerning "IF", "ELSE IF", "ELSE" structures [message #1765899 is a reply to message #1765897] |
Wed, 14 June 2017 12:35   |
Eclipse User |
|
|
|
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 #1765905 is a reply to message #1765900] |
Wed, 14 June 2017 12:52   |
Eclipse User |
|
|
|
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 #1765915 is a reply to message #1765911] |
Wed, 14 June 2017 13:14   |
Eclipse User |
|
|
|
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';
|
|
|
Re: Question concerning "IF", "ELSE IF", "ELSE" structures [message #1765918 is a reply to message #1765915] |
Wed, 14 June 2017 13:22   |
Eclipse User |
|
|
|
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 #1765940 is a reply to message #1765937] |
Wed, 14 June 2017 14:15   |
Eclipse User |
|
|
|
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   |
Eclipse User |
|
|
|
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?
|
|
|
|
Powered by
FUDForum. Page generated in 0.05510 seconds