Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Decision can match input such as "RULE_ID '=' RULE_ID"
Decision can match input such as "RULE_ID '=' RULE_ID" [message #903916] Mon, 27 August 2012 09:05 Go to next message
Tu Do is currently offline Tu DoFriend
Messages: 21
Registered: August 2012
Junior Member
This is my sample grammar rules:
grammar org.xtext.example.mydsl1.MyDsl with org.eclipse.xtext.common.Terminals

generate myDsl ""

START:	
	{START}
	features += (NUM_SYMBOL | STR_SYMBOL | VARIABLE | STRING_VARIABLE | SET)*';'
;

//Declaration
VARIABLE:
	'VARIABLE' name=ID ('[' INT ']' ('[' INT ']')* )? '=' content = ID ';'
;
        
STRING_VARIABLE:
       'STRING' name=ID '=' STRING ';'
;

NUM_SYMBOL:
	'NUM SYMBOL' ID '=' content = ID ';'
;

STR_SYMBOL:
	'SYMBOL' name = ID '=' content = ID ';'
;

//Definition (or assignments, can either be string or number)
SET: 
	'SET' (VAR_ASSIGNMENT | STRING_ASSIGNMENT) ';'
;

//array assignment works too
VAR_ASSIGNMENT:
	name = [VARIABLE] ('[' INT ']' ('[' INT ']')* )? '=' ASSIGNED_VALUE
;

ASSIGNED_VALUE:
	{ASSIGNED_VALUE}
	INT | name = [NUM_SYMBOL]
	;

STRING_ASSIGNMENT:
	name = [STRING_VARIABLE] '=' (STRING | ref = STR_SYMB_REF)
;

STR_SYMB_REF:
	name = [STR_SYMBOL]
;


It generates warning: warning(200): ../au.com.dektech.plex.ide.ui/src-gen/au/com/dektech/plex/ide/ui/contentassist/antlr/internal/InternalPLEX.g:1159:1: 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


It seems that Xtext can't resolve same rule with different references. However, assignment usually uses the same rule. For example:
/* Declare */
int var1;
String var2;

/* Define */
//This assignment should refer to declared var1 (integer declaration only)
var1 = 10;

//This assignment should refer to declared var2 (string declaration only)
var2 = "Test";


I don't want to merge different types of assignment into one generic assignment rule. How can I fix this?

[Updated on: Mon, 27 August 2012 10:45]

Report message to a moderator

Re: Decision can match input such as "RULE_ID '=' RULE_ID" [message #903923 is a reply to message #903916] Mon, 27 August 2012 09:48 Go to previous messageGo to next message
Tobias Mayer is currently offline Tobias MayerFriend
Messages: 8
Registered: August 2012
Junior Member
I try to copy your grammar in a new project. The editor shows some mistakes. The SYMBOL Rule is missing, too. Can you upload a new version of your grammar?

In general it is good Idea to open the generated antlr grammar (the ".g" file in your project) with antlr works. By pressing ctrl-r it checks your grammar and visualizes the alternatives with a call graph. A syntactic predicate (=>) at the right point will maybe solve your problem.

Best Regards,

Tobias
Re: Decision can match input such as "RULE_ID '=' RULE_ID" [message #903927 is a reply to message #903923] Mon, 27 August 2012 10:46 Go to previous messageGo to next message
Tu Do is currently offline Tu DoFriend
Messages: 21
Registered: August 2012
Junior Member
Tobias Mayer wrote on Mon, 27 August 2012 05:48
I try to copy your grammar in a new project. The editor shows some mistakes. The SYMBOL Rule is missing, too. Can you upload a new version of your grammar?

In general it is good Idea to open the generated antlr grammar (the ".g" file in your project) with antlr works. By pressing ctrl-r it checks your grammar and visualizes the alternatives with a call graph. A syntactic predicate (=>) at the right point will maybe solve your problem.

Best Regards,

Tobias

Hi Tobias,

Sorry about that. I fixed the grammar and it works fine now.
Re: Decision can match input such as "RULE_ID '=' RULE_ID" [message #903942 is a reply to message #903916] Mon, 27 August 2012 11:36 Go to previous messageGo to next message
Tobias Mayer is currently offline Tobias MayerFriend
Messages: 8
Registered: August 2012
Junior Member
When I generated the language infrastructure, a further error message appears:

constraint is INVALID for context SET and type VAR_ASSIGNMENT
constraint is INVALID for context VAR_ASSIGNMENT and type VAR_ASSIGNMENT

When I modify your SET rule to:

SET:
'SET' ( => VAR_ASSIGNMENT | STRING_ASSIGNMENT) ';'
;

the error message mentioned by you disappears.

Best Regards,

Tobias

[Updated on: Mon, 27 August 2012 11:39]

Report message to a moderator

Re: Decision can match input such as "RULE_ID '=' RULE_ID" [message #903966 is a reply to message #903942] Mon, 27 August 2012 13:04 Go to previous messageGo to next message
Tu Do is currently offline Tu DoFriend
Messages: 21
Registered: August 2012
Junior Member
Tobias Mayer wrote on Mon, 27 August 2012 07:36
When I generated the language infrastructure, a further error message appears:

constraint is INVALID for context SET and type VAR_ASSIGNMENT
constraint is INVALID for context VAR_ASSIGNMENT and type VAR_ASSIGNMENT

When I modify your SET rule to:

SET:
'SET' ( => VAR_ASSIGNMENT | STRING_ASSIGNMENT) ';'
;

the error message mentioned by you disappears.

Best Regards,

Tobias

Hi Tobias,

are you sure there's no additional warnings? When I copied back the grammar above and generated the language artifacts, it has the following warnings:

constraint is INVALID for context SET and type VAR_ASSIGNMENT
constraint is INVALID for context VAR_ASSIGNMENT and type VAR_ASSIGNMENT

warning(200): ../org.xtext.example.mydsl/src-gen/org/xtext/example/mydsl/parser/antlr/internal/InternalMyDsl.g:468:1: 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

warning(200): ../org.xtext.example.mydsl.ui/src-gen/org/xtext/example/mydsl/ui/contentassist/antlr/internal/InternalMyDsl.g:442:1: 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


However, when I changed the rule according to your suggestion, the "warning(200)" message vanished, but the constraint warnings remained. What exactly does this operator "=>" do? And what is the constraint warnings suggesting?

Thanks.

[Updated on: Mon, 27 August 2012 13:05]

Report message to a moderator

Re: Decision can match input such as "RULE_ID '=' RULE_ID" [message #903982 is a reply to message #903916] Mon, 27 August 2012 13:45 Go to previous messageGo to next message
Tobias Mayer is currently offline Tobias MayerFriend
Messages: 8
Registered: August 2012
Junior Member
Hi,

the "constraint" warning is still there, too. Up to now, I have never seen this error message. Hopefully one of the xtext pro's in this forum sees this topic and gives us an answer Smile.

To understand Syntactic Predicates, I recommend to read the Section "Syntactic Predicates" in the Xtext documentation.

There are also very helpfull informations about Syntactic Predicates

on

www.antlr.org/wiki/display/ANTLR3/How+to+remove+global+backtracking+from+your+grammar

.

Best Regards,

Tobias

[Updated on: Mon, 27 August 2012 13:46]

Report message to a moderator

Re: Decision can match input such as "RULE_ID '=' RULE_ID" [message #904162 is a reply to message #903916] Mon, 27 August 2012 23:04 Go to previous messageGo to next message
Henrik Lindberg is currently offline Henrik LindbergFriend
Messages: 2509
Registered: July 2009
Senior Member
On 2012-27-08 11:05, Tu Do wrote:
> I don't want to merge different types of assignment into one generic
> assignment rule. How can I fix this?

The grammar can not differentiate what the reference may point to - it
sees an ID that may be a reference to one of several things.

You can not do it that way.

You basically have to accept this, and have one rule that assigns and
use validation to check semantics (type is correct etc.)...

You will discover that this is far better than trying to encode all of
your language semantics in the grammar. (i.e. better to give user "can
not assign a string to an int variable" than "syntax error, no viable
alternative at line 3").

Just my 25c :)

(The alternative requires semantic predicates (which are not available
in Xtext) and close to parsing the input twice; once to establish "what
is what" so you can lookup if the ID "x" is a reference to an INT or a
STRING. This quickly gets very messy.

You could try implementing it in the lexer and return different tokens
for the different IDs, but the problem is then to build the table of int
and string variables - which is difficult for two reasons; partial
lexing, and referencing things in other files (in both cases, the
instance of the lexer that is running will not have seen what it needs
to see). In all cases, you have the additional problem of scoping to
take into account; can "x" be a reference to different types in
different scopes? It may be possible to make it work depending on the
semantics of your language - but it is not a general solution.).

Regards
- henrik
Re: Decision can match input such as "RULE_ID '=' RULE_ID" [message #904244 is a reply to message #904162] Tue, 28 August 2012 04:26 Go to previous message
Tu Do is currently offline Tu DoFriend
Messages: 21
Registered: August 2012
Junior Member
Hi Henrik,

I think you are right. What I did is a reduce-reduce conflict (both has the form "RULE_ID '=' RULE_ID" and there's no way for the parser to take care of this ambiguity if I don't make change.

Thanks for you suggestion.

Best Regards,

Tu.
Previous Topic:Runtime instance error after upgrading to Juno + xtext 2.3
Next Topic:testing xbase language referring to another xbase resource
Goto Forum:
  


Current Time: Thu Apr 25 10:15:26 GMT 2024

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

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

Back to the top