Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Ambiguous grammar
Ambiguous grammar [message #989321] Wed, 05 December 2012 11:08 Go to next message
Marco Naddeo is currently offline Marco Naddeo
Messages: 62
Registered: November 2012
Member
Hi Smile

Houston, I have a problem with my Xtext grammar. Confused

I refactored it to eliminate left recursion, but now this fragment:

Expression:
  TerminalExpression ('.' ID '.' ID)? |
  TerminalExpression ('.' ID '.' ID actualParams=ActualParameters)?
;

TerminalExpression returns Expression:
  'this' |
  'null' |
  'false' |
  'true ' |
  STRING_LITERAL |
  NUMBER_LITERAL |
  ID |
  objectCreation=ObjectCreation |
  'super' actualParams=ActualParameters |
  '(' expr=Expression ')'
;


results in these errors and warnings Shocked, when I try to generate the language infrastructure:

error(211): ../magda.xtext.Magda/src-gen/magda/xtext/parser/antlr/internal/InternalMagda.g:801:1: [fatal] rule ruleExpression 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.
warning(200): ../magda.xtext.Magda/src-gen/magda/xtext/parser/antlr/internal/InternalMagda.g:801:1: Decision can match input such as "'this'" using multiple alternatives: 1, 2
As a result, alternative(s) 2 were disabled for that input
warning(200): ../magda.xtext.Magda/src-gen/magda/xtext/parser/antlr/internal/InternalMagda.g:801:1: Decision can match input such as "'null'" using multiple alternatives: 1, 2
As a result, alternative(s) 2 were disabled for that input
warning(200): ../magda.xtext.Magda/src-gen/magda/xtext/parser/antlr/internal/InternalMagda.g:801:1: Decision can match input such as "'false'" using multiple alternatives: 1, 2
As a result, alternative(s) 2 were disabled for that input
warning(200): ../magda.xtext.Magda/src-gen/magda/xtext/parser/antlr/internal/InternalMagda.g:801:1: Decision can match input such as "RULE_ID" using multiple alternatives: 1, 2
As a result, alternative(s) 2 were disabled for that input
warning(200): ../magda.xtext.Magda/src-gen/magda/xtext/parser/antlr/internal/InternalMagda.g:801:1: Decision can match input such as "RULE_NUMBER_LITERAL" using multiple alternatives: 1, 2
As a result, alternative(s) 2 were disabled for that input
warning(200): ../magda.xtext.Magda/src-gen/magda/xtext/parser/antlr/internal/InternalMagda.g:801:1: Decision can match input such as "RULE_STRING_LITERAL" using multiple alternatives: 1, 2
As a result, alternative(s) 2 were disabled for that input
warning(200): ../magda.xtext.Magda/src-gen/magda/xtext/parser/antlr/internal/InternalMagda.g:801:1: Decision can match input such as "'true '" using multiple alternatives: 1, 2
As a result, alternative(s) 2 were disabled for that input


I've tried to enable the backtrack=true option, but nothing change. Rolling Eyes

I suppose syntactic predicates are the solution here, but how I use them? Confused

Could you also give me an example of an ambiguous phrase of this grammar, please?

Thanks in advance. Smile


Marco
Re: Ambiguous grammar [message #989391 is a reply to message #989321] Wed, 05 December 2012 21:49 Go to previous messageGo to next message
Henrik Lindberg is currently offline Henrik Lindberg
Messages: 2499
Registered: July 2009
Senior Member
On 2012-05-12 17:09, Marco Naddeo wrote:
> Expression:
> TerminalExpression ('.' ID '.' ID)? |
> TerminalExpression ('.' ID '.' ID actualParams=ActualParameters)?
> ;
Hint... what happens here if there is just a TerminalExpression? Should
it pick the first part or the second?

Maybe something like this would work for you

Expression:
TerminalExpression ('.' ID '.' ID actualParams=ActualParameters?)?
;

i.e either .ID.ID, .ID.ID ActualParameters, or (nothing)

Don't you want to keep the IDs in the model? They are currently
unassigned (and thus only parsed).

- henrik
Re: Ambiguous grammar [message #989477 is a reply to message #989391] Thu, 06 December 2012 06:54 Go to previous message
Marco Naddeo is currently offline Marco Naddeo
Messages: 62
Registered: November 2012
Member
Henrik Lindberg wrote on Wed, 05 December 2012 21:49
Hint... what happens here if there is just a TerminalExpression? Should
it pick the first part or the second?


Oh, ehm... it was pretty obvious! Embarrassed Very Happy

Henrik Lindberg wrote on Wed, 05 December 2012 21:49
Maybe something like this would work for you

Expression:
TerminalExpression ('.' ID '.' ID actualParams=ActualParameters?)?
;

i.e either .ID.ID, .ID.ID ActualParameters, or (nothing)


Thank you! It seems correct and working. Smile

Henrik Lindberg wrote on Wed, 05 December 2012 21:49
Don't you want to keep the IDs in the model? They are currently
unassigned (and thus only parsed).


Yeah, sure I want. Wink But the grammar is "work in progress" and that wasn't an interesting detail for my question.

Thanks again, bye!


Marco
Previous Topic:Customizable formatting for the Xtext editor
Next Topic:scoping: exclude items
Goto Forum:
  


Current Time: Fri Aug 29 14:17:58 EDT 2014

Powered by FUDForum. Page generated in 0.05126 seconds