[Syntactic predicates] Choosing alternative path 2 [message #989871] |
Sat, 08 December 2012 21:49 |
Boris Brodski 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
get parsed like
(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 |
Henrik Lindberg 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
|
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.05037 seconds