Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Error: Duplicate VarDecl 'i' (scoping problem)
Error: Duplicate VarDecl 'i' (scoping problem) [message #884609] Mon, 11 June 2012 13:23 Go to next message
Cash Ko is currently offline Cash KoFriend
Messages: 21
Registered: May 2012
Junior Member
Hi, i need some support to solve my problem (don't understand how to get it working)

I get an error message for the 'FOR' statements in following simple example:

PROCEDURE test_1
  DIM a : BYTE
  LET a = 1
  FOR i = 1 TO 5              // <-------- Error: Duplicate VarDecl 'i'
    LET a = a + i
  NEXT
  FOR i = 5 TO 1 STEP -1      // <-------- Error: Duplicate VarDecl 'i'
    LET a = a * (i * i)
  NEXT
END



Corresponded xText definition:



Programm: procs+=Procedure*;
	
enum Type: BYTE | SINGLE | INTEGER;

VarDecl: name=ID (':' type=Type)?;

// Expressions
Primary returns Expression:
  {SubExpression} '(' expr=Expression ')' 
  | {Literal} val=INT
  | {Reference} varRef=[VarDecl]
;
Term returns Expression: sign=('+'|'-')?	expr=Primary;
Multiplication returns Expression: left=Term (op=('*' | '/') right=Multiplication)?;
Expression returns Expression: left=Multiplication (op=('+' | '-') right=Expression)?;


// Statements
Statement returns Statement:
  {DimStmt} 'DIM' var=VarDecl
  | {LetStmt} 'LET' ref=[VarDecl] '=' expr=Expression
  | {ForStmt} 'FOR' var=VarDecl '=' from=Expression 'TO' to=Expression ('STEP' step=Expression)? stmts+=Statement* 'NEXT' 
;

Procedure:
  'PROCEDURE' name=ID ('(' parameters+=VarDecl* ')')?
  stmts+=Statement*
  'END'
;


MWE2:

fragment = validation.JavaValidatorFragment {
  //    composedCheck = "org.eclipse.xtext.validation.ImportUriValidator"
        composedCheck = "org.eclipse.xtext.validation.NamesAreUniqueValidator"
}


[Updated on: Mon, 11 June 2012 13:43]

Report message to a moderator

Re: Error: Duplicate VarDecl 'i' (scoping problem) [message #884623 is a reply to message #884609] Mon, 11 June 2012 13:54 Go to previous messageGo to next message
Holger Schill is currently offline Holger SchillFriend
Messages: 71
Registered: July 2009
Member
Hi,

the QualifiedName is used to reference elements. The variable i is
contained in ForStmt and then in Procedure. ForStmt has no name so the
QualifiedName for i is test_1.i for both variables.
You need to specify a custom QualifiedNameProviderImplementation where
you could give the ForStmt a syntectic name so that i is unique in the
meaning of the QualifiedName. Something like test_1.statement0.i

Cheers,

Holger


Am 11.06.12 15:23, schrieb Cash Ko:
> Hi, i need some support to solve my problem (don't understand how to get
> it working)
>
> I get an error message for the 'FOR' statements in the following simple
> example:
>
>
> PROCEDURE test_1
> DIM a : BYTE
> LET a = 1
> FOR i = 1 TO 5 // <-------- Error: Duplicate VarDecl 'i'
> LET a = a + i
> NEXT
> FOR i = 5 TO 1 STEP -1 // <-------- Error: Duplicate VarDecl 'i'
> LET a = a * (i * i)
> NEXT
> END
>
>
>
> Corresponded xText definition:
>
>
>
>
> Programm: procs+=Procedure*;
>
> enum Type: BYTE | SINGLE | INTEGER;
>
> VarDecl: name=ID (':' type=Type)?;
>
> // Expressions
> Primary returns Expression:
> {SubExpression} '(' expr=Expression ')' | {Literal} val=INT
> | {Reference} varRef=[VarDecl]
> ;
> Term returns Expression: (sign='+'|'-')? expr=Primary;
> Multiplication returns Expression: left=Term (op=('*' | '/')
> right=Multiplication)?;
> Expression returns Expression: left=Multiplication (op=('+' | '-')
> right=Expression)?;
>
>
> // Statements
> Statement returns Statement:
> {DimStmt} 'DIM' var=VarDecl
> | {LetStmt} 'LET' ref=[VarDecl] '=' expr=Expression
> | {ForStmt} 'FOR' var=VarDecl '=' from=Expression 'TO' to=Expression
> ('STEP' step=Expression)? stmts+=Statement* 'NEXT' ;
>
> Procedure:
> 'PROCEDURE' name=ID ('(' parameters+=VarDecl ')')?
> stmts+=Statement*
> 'END'
> ;
>
>
> MWE2:
>
>
> fragment = validation.JavaValidatorFragment {
> // composedCheck = "org.eclipse.xtext.validation.ImportUriValidator"
> composedCheck = "org.eclipse.xtext.validation.NamesAreUniqueValidator"
> }
>
>
>


--
Need professional support for Eclipse Modeling?
Go visit: http://xtext.itemis.com


--
Need professional support for Eclipse Modeling?
Go visit: http://xtext.itemis.com
Re: Error: Duplicate VarDecl 'i' (scoping problem) [message #884739 is a reply to message #884623] Mon, 11 June 2012 17:57 Go to previous messageGo to next message
Cash Ko is currently offline Cash KoFriend
Messages: 21
Registered: May 2012
Junior Member
Thank you Holger for quick response to my question.

Would someone please check if the following implementation can be an acceptable solution to this problem (if it does not have any hidden side effects) -- Thank You.
package org.xtext.example.mydsl;

import org.eclipse.xtext.naming.DefaultDeclarativeQualifiedNameProvider;
import org.eclipse.xtext.naming.QualifiedName;
import org.xtext.example.mydsl.myDsl.ForStmt;

public class MyDslQNP extends DefaultDeclarativeQualifiedNameProvider {

	QualifiedName qualifiedName(ForStmt s) {
		String stmtName = "STMT:" + Integer.toString(s.hashCode());
		return QualifiedName.create(stmtName);
	}
}

[Updated on: Tue, 12 June 2012 06:23]

Report message to a moderator

Re: Error: Duplicate VarDecl 'i' (scoping problem) [message #884965 is a reply to message #884739] Tue, 12 June 2012 07:25 Go to previous message
Holger Schill is currently offline Holger SchillFriend
Messages: 71
Registered: July 2009
Member
Does it work for you? You should include the name of the father of the
statement to make it unique. I would not use the hashCode. I would use
the index of the Statement in the list stmts of the procedure.

Am 11.06.12 19:57, schrieb Cash Ko:
> Thank you Holger for quick respond to my question.
>
> Would someone check please if the following implementation can be an
> acceptable solution to this problem (if it does not have any hidden side
> effects) -- Thank You.
>
> package org.xtext.example.mydsl;
>
> import org.eclipse.xtext.naming.DefaultDeclarativeQualifiedNameProvider;
> import org.eclipse.xtext.naming.QualifiedName;
> import org.xtext.example.mydsl.myDsl.ForStmt;
>
> public class MyDslQNP extends DefaultDeclarativeQualifiedNameProvider {
>
> QualifiedName qualifiedName(ForStmt s) {
> String stmtName = "STMT:" + Integer.toString(s.hashCode());
> return QualifiedName.create(stmtName);
> }
> }
>


--
Need professional support for Eclipse Modeling?
Go visit: http://xtext.itemis.com


--
Need professional support for Eclipse Modeling?
Go visit: http://xtext.itemis.com
Previous Topic:Problems with referencing ecore's
Next Topic:[Xcore] derived feature and notification
Goto Forum:
  


Current Time: Sun Dec 21 16:50:03 GMT 2014

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

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