Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » [Syntactic predicates] Choosing alternative path 2
[Syntactic predicates] Choosing alternative path 2 [message #989871] Sat, 08 December 2012 21:49 Go to next message
Boris Brodski is currently offline Boris BrodskiFriend
Messages: 112
Registered: July 2009
Senior Member
Hello,


I have a very small grammar (simplified part of Xbase) with a common problem:

warning(200): ../org.xtext.example.mydsl/src-gen/org/xtext/example/mydsl/parser/antlr/internal/InternalMyDsl.g:186:2: Decision can match input such as
"'<' RULE_ID '>' {RULE_ID, '<'}" using multiple alternatives: 1, 2
As a result, alternative(s) 2 were disabled for that input


I know, how to choose alternative 1 hiding the warning, but how to choose alternative 2?

Here are the details:

I want to parse ID, comparation of ID, and IDs with generics '<String>' (just like method calls in Xtend '<String>myMethod(null)')

The grammar without syntactic predicates:

Model: (expressions += Comparation)*;

Comparation returns Expression:
	NumberExpression ({Comparation.left=current} op=('<' | '>') right=NumberExpression)*;

NumberExpression returns Expression:
	({NumberExpression} value=NumberLiteral)
;

NumberLiteral:
		('<' type=ID '>')?
		value=ID
;


This grammar accepts multiple expressions, like

print
<String>print
a > b
<String>getA < <String>getB


Changing the Comparation rule to the

Comparation returns Expression:
	NumberExpression ( =>({Comparation.left=current} op=('<' | '>')) right=NumberExpression)*;


I can choose alternative 1 and get ride of the warning. Now, the input like

print
<String>getA


get parsed like

print < String > getA


(Xtend has the same issue, by the way)

Now I would like to choose the second alternative, so that the input above would get parsed, like a call to "print" and then a call to "<String>getA".

Within the ANTLR it's possible adding following gated semantic predicate:

('<' RULE_INT '>' RULE_INT)=>


after the '*' in the Comparation rule. So mixing Xtext and ANTLR it would be:

Comparation returns Expression:
	NumberExpression {Comparation.left=current} op=('<' | '>') right=NumberExpression)*  ANTLR>>>   ('<' RULE_INT '>' RULE_INT)=>    <<<ANTLR;



Now is it possible to do this in the Xtext?
Is it possible to add such gated semantic predicate at the generation time right to the ANTLR grammar, if no better ways are available?


Thank you and sorry for the long post!


Best regards,
Boris Brodski
Re: [Syntactic predicates] Choosing alternative path 2 [message #989927 is a reply to message #989871] Mon, 10 December 2012 06:27 Go to previous messageGo to next message
Henrik Lindberg is currently offline Henrik LindbergFriend
Messages: 2509
Registered: July 2009
Senior Member
Did you try something like:

NumberLiteral:
=>(('<' type=ID '>')? value=ID)
;

=> is "if you can parse then next, then use that path"

Don't know if that helps in this case. I could not quite follow your
example when RULE_INT started appearing - what is RULE_INT?

Regards
- henrik

On 2012-08-12 22:49, Boris Brodski wrote:
> Hello,
>
>
> I have a very small grammar (simplified part of Xbase) with a common
> problem:
>
>
> warning(200):
> ../org.xtext.example.mydsl/src-gen/org/xtext/example/mydsl/parser/antlr/internal/InternalMyDsl.g:186:2:
> Decision can match input such as
> "'<' RULE_ID '>' {RULE_ID, '<'}" using multiple alternatives: 1, 2
> As a result, alternative(s) 2 were disabled for that input
>
>
> I know, how to choose alternative 1 hiding the warning, but how to
> choose alternative 2?
>
> Here are the details:
>
> I want to parse ID, comparation of ID, and IDs with generics '<String>'
> (just like method calls in Xtend '<String>myMethod(null)')
>
> The grammar without syntactic predicates:
>
>
> Model: (expressions += Comparation)*;
>
> Comparation returns Expression:
> NumberExpression ({Comparation.left=current} op=('<' | '>')
> right=NumberExpression)*;
>
> NumberExpression returns Expression:
> ({NumberExpression} value=NumberLiteral)
> ;
>
> NumberLiteral:
> ('<' type=ID '>')?
> value=ID
> ;
>
>
> This grammar accepts multiple expressions, like
>
>
> print
> <String>print
> a > b
> <String>getA < <String>getB
>
>
> Changing the Comparation rule to the
>
> Comparation returns Expression:
> NumberExpression ( =>({Comparation.left=current} op=('<' | '>'))
> right=NumberExpression)*;
>
>
> I can choose alternative 1 and get ride of the warning. Now, the input like
>
>
> print
> <String>getA
>
>
> get parsed like
>
>
> print < String > getA
>
>
> (Xtend has the same issue, by the way)
>
> Now I would like to choose the second alternative, so that the input
> above would get parsed, like a call to "print" and then a call to
> "<String>getA".
>
> Within the ANTLR it's possible adding following gated semantic predicate:
>
>
> ('<' RULE_INT '>' RULE_INT)=>
>
>
> after the '*' in the Comparation rule. So mixing Xtext and ANTLR it
> would be:
>
>
> Comparation returns Expression:
> NumberExpression {Comparation.left=current} op=('<' | '>')
> right=NumberExpression)* ANTLR>>> ('<' RULE_INT '>' RULE_INT)=>
> <<<ANTLR;
>
>
>
> Now is it possible to do this in the Xtext?
> Is it possible to add such gated semantic predicate at the generation
> time right to the ANTLR grammar, if no better ways are available?
>
>
> Thank you and sorry for the long post!
>
>
> Best regards,
> Boris Brodski
Re: [Syntactic predicates] Choosing alternative path 2 [message #989965 is a reply to message #989927] Mon, 10 December 2012 09:54 Go to previous messageGo to next message
Boris Brodski is currently offline Boris BrodskiFriend
Messages: 112
Registered: July 2009
Senior Member
Hello Henrik,

yes, I tried this one. And also this one:

NumberLiteral:
( =>('<' type=ID '>' value=ID) )
| value=ID
;

But nothing works even, if I take MyGrammar.g, remove all actions, put this predicate and call ANTLR manually. A ANTLR tutorial states, that such predicates should be placed at the most left on the alternative path. The NumberLiteral non-terminal is already to late. The decision will be made at the asterisk in '( ... )*' in the Comparation.

Regards,
Boris
Re: [Syntactic predicates] Choosing alternative path 2 [message #989966 is a reply to message #989965] Mon, 10 December 2012 09:58 Go to previous messageGo to next message
Boris Brodski is currently offline Boris BrodskiFriend
Messages: 112
Registered: July 2009
Senior Member
Hello Henrik,


to address your last question: RULE_INT is the ANTLR name for the INT in the Xtext.
I put it into the ANTLR>>> ... <<<ANTLR to indicate, that the '...' is native ANTLR syntax. This '...' should be
inserted directly into the MyGrammar.g after is was generated from MyGrammar.xtext.


Regards,
Boris
Re: [Syntactic predicates] Choosing alternative path 2 [message #990028 is a reply to message #989966] Mon, 10 December 2012 14:45 Go to previous message
Henrik Lindberg is currently offline Henrik LindbergFriend
Messages: 2509
Registered: July 2009
Senior Member
On 2012-10-12 10:58, Boris Brodski wrote:
> to address your last question: RULE_INT is the ANTLR name for the INT in
> the Xtext.
> I put it into the ANTLR>>> ... <<<ANTLR to indicate, that the '...' is
> native ANTLR syntax. This '...' should be
> inserted directly into the MyGrammar.g after is was generated from
> MyGrammar.xtext.
>

I had similar issues and I solved those with an external lexer i.e. (in
your case) deliver other tokens than < and > when facing < int >.

- henrik
Previous Topic:Extended highlighting breaks embedded editor highlighting
Next Topic:Issue generating a DSL grammar
Goto Forum:
  


Current Time: Wed Apr 24 16:33:57 GMT 2024

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

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

Back to the top