Ocl grammar and a temporal extension of it [message #71409] |
Mon, 15 June 2009 09:48  |
Eclipse User |
|
|
|
Hello,
I would like to extend the current OCL grammair in order to add some
temporal operators to it. In order to do that, I intend to write an OCL
grammar that takes into account these operators and for that I use the
XText tool provided by oaw (I really don't know if I should ask this in
the tmf newsgroup) in order to have a parser and a metamodel generated.
After some search and adaptation, I ended up using this grammar:
OclFile
: mm = Model (expressions += OclExpression)+
;
Model
: "MainModel : http://SimplePDL"
;
OclExpression
: contextDecl = ContextDeclaration stereotype = Stereotype ( name = Name
)? ":" exp = Expression ";"
;
ContextDeclaration
: "context" contextName = TypeName
;
Stereotype
: "inv"
;
Expression
: logExp = LogicalExpression
;
IfExpression
: "if" ifExp = Expression
"then" theExp = Expression
"else" elseExp = Expression
"endif"
;
LogicalExpression
: relExp = RelationalExpression (logOp = LogicalOperator sub =
RelationalExpression)*
;
RelationalExpression
: tempExp = TemporalExpression | addExp = AdditiveExpression
( relOp = RelationalOperator sub = AdditiveExpression )?
;
TemporalExpression
: UnaryTemporalExpression
| BinaryTemporalExpression
;
UnaryTemporalExpression
: UnaryTemporalOperator LogicalExpression
;
BinaryTemporalExpression
: LogicalExpression BinaryTemporalOperator LogicalExpression
;
AdditiveExpression
: MultiplicativeExpression ( AddOperator MultiplicativeExpression )*
;
MultiplicativeExpression
: UnaryExpression ( MultiplyOperator UnaryExpression )*
;
UnaryExpression
: ( UnaryOperator PostfixExpression )
| PostfixExpression
;
PostfixExpression
: PrimaryExpression ( ("." | "->") FeatureCall )*
;
PrimaryExpression
: LiteralCollection
| Literal
| PathExp
| "(" Expression ")"
| IfExpression
;
PathExp
: PathName TimeExpression? Qualifiers? FeatureCallParameters?
;
FeatureCallParameters
: "(" ( Declarator )? ( ActualParameterList )? ")"
;
Literal
: STRING | Number | "#" Name | Name
;
EnumerationType
: "enum" "{" "#" Name ( "," "#" Name )* "}"
;
SimpleTypeSpecifier
: PathTypeName
| EnumerationType
;
LiteralCollection
: CollectionKind "{" ExpressionListOrRange? "}"
;
ExpressionListOrRange
: Expression ( ( "," Expression )+ | ( ".." Expression ))?
;
FeatureCall
: PathName TimeExpression? Qualifiers? FeatureCallParameters?
;
Qualifiers
: "[" ActualParameterList "]"
;
Declarator
: Name ( "," Name )* ( ":" SimpleTypeSpecifier )? "|"
;
PathTypeName
: TypeName ( "::" TypeName )*
;
PathName
: ( TypeName | Name ) ( "::" ( TypeName | Name ) )*
;
TimeExpression
: "@" Name
;
ActualParameterList
: Expression ( "," Expression )*
;
Native LogicalOperator
: "'and' | 'or' | 'xor' | 'implies'"
;
Native CollectionKind
: "'Set' | 'Bag' | 'Sequence' | 'Collection'"
;
RelationalOperator
: "=" | ">" | "<" | ">=" | "<=" | "<>"
;
AddOperator
: "+" | "-"
;
Native MultiplyOperator
: "'*' | '/'"
;
Native UnaryOperator
: "'-' | 'not'"
;
UnaryTemporalOperator
: SOMETIME_PAST
| ALWAYS_PAST
| SOMETIME
| ALWAYS
| INITIALLY
;
Native SOMETIME_PAST
: "'sometime_past'"
;
Native ALWAYS_PAST
: "'always_past'"
;
Native SOMETIME
: "'sometime'"
;
Native ALWAYS
: "'always'"
;
Native INITIALLY
: "'initially'"
;
BinaryTemporalOperator
: SOMETIME_SINCE_LAST
| ALWAYS_SINCE_LAST
| UNTIL
| BEFORE
;
Native SOMETIME_SINCE_LAST
: "'sometime_since_last'"
;
Native ALWAYS_SINCE_LAST
: "'always_since_last'"
;
Native UNTIL
: "'until'"
;
Native BEFORE
: "'before'"
;
Native Name
: "'a'..'z' ( 'a'..'z' | '0'..'9' | 'A'..'Z' | '_')*"
;
Native TypeName
: "'A'..'Z' ( 'a'..'z' | '0'..'9' | 'A'..'Z' | '_')*"
;
Native Number
: "'0'..'9' ('0'..'9')*"
;
I thought first to give it a try with just an ocl file, having the form:
MainModel : http://SimplePDL
context Process
inv start_all:
self.processElement->forAll(wd:WorkDefinition|wd.name.size() = 1 implies
wd.name="3");
After running the parser on this file I get the errors:
no viable alternative at input 'self' (line:5, start:61, length:3)
no viable alternative at input 'wd' (line:5, start:134, length:1)
My question to you is whether you see something wrong with the grammar or
it's all about the way XText generates the parser. Thank you!
Regards,
Cosmin
|
|
|
Re: Ocl grammar and a temporal extension of it [message #71448 is a reply to message #71409] |
Wed, 17 June 2009 01:50  |
Eclipse User |
|
|
|
Hi Cosmin
Your grammar does not use OCL and does not use the LPG tooling used by the
MDT-OCL project, so I suggest that you ask for grammar help on a group that has
some relevance.
If you want to re-use the MDT-OCL grammar, you can do that by exploiting
the LPG include capabilities. If you examine the MDT-OCL sources you will
see that it does this internally to promote EssentialOCL into Complete OCL.
The QVT Declarative project does the same to extend EssentialOCL to QVTr
or QVTc.
Regards
Ed Willink
Cosmin Ciuraru wrote:
> Hello,
>
> I would like to extend the current OCL grammair in order to add some
> temporal operators to it. In order to do that, I intend to write an OCL
> grammar that takes into account these operators and for that I use the
> XText tool provided by oaw (I really don't know if I should ask this in
> the tmf newsgroup) in order to have a parser and a metamodel generated.
> After some search and adaptation, I ended up using this grammar:
>
> OclFile : mm = Model (expressions += OclExpression)+ ;
>
> Model
> : "MainModel : http://SimplePDL" ;
>
> OclExpression : contextDecl = ContextDeclaration stereotype =
> Stereotype ( name = Name )? ":" exp = Expression ";"
> ;
>
> ContextDeclaration
> : "context" contextName = TypeName
> ;
>
> Stereotype
> : "inv"
> ;
>
> Expression
> : logExp = LogicalExpression
> ;
>
> IfExpression : "if" ifExp = Expression "then" theExp =
> Expression "else" elseExp = Expression
> "endif"
> ;
> LogicalExpression : relExp = RelationalExpression (logOp =
> LogicalOperator sub = RelationalExpression)*
> ;
>
> RelationalExpression : tempExp = TemporalExpression | addExp =
> AdditiveExpression ( relOp = RelationalOperator sub =
> AdditiveExpression )?
> ;
>
>
> TemporalExpression : UnaryTemporalExpression
> | BinaryTemporalExpression
> ;
>
> UnaryTemporalExpression
> : UnaryTemporalOperator LogicalExpression
> ;
>
> BinaryTemporalExpression
> : LogicalExpression BinaryTemporalOperator LogicalExpression
> ;
> AdditiveExpression : MultiplicativeExpression ( AddOperator
> MultiplicativeExpression )*
> ;
>
> MultiplicativeExpression : UnaryExpression ( MultiplyOperator
> UnaryExpression )*
> ;
>
> UnaryExpression : ( UnaryOperator PostfixExpression )
> | PostfixExpression
> ;
>
> PostfixExpression : PrimaryExpression ( ("." | "->") FeatureCall )*
> ;
>
> PrimaryExpression : LiteralCollection
> | Literal
> | PathExp
> | "(" Expression ")"
> | IfExpression
> ;
>
> PathExp : PathName TimeExpression? Qualifiers? FeatureCallParameters?
> ;
>
> FeatureCallParameters : "(" ( Declarator )? ( ActualParameterList )?
> ")"
> ;
>
> Literal : STRING | Number | "#" Name | Name
> ;
>
> EnumerationType : "enum" "{" "#" Name ( "," "#" Name )* "}"
> ;
>
> SimpleTypeSpecifier : PathTypeName
> | EnumerationType
> ;
>
> LiteralCollection : CollectionKind "{" ExpressionListOrRange? "}"
> ;
>
> ExpressionListOrRange : Expression ( ( "," Expression )+ | ( ".."
> Expression ))?
> ;
>
> FeatureCall : PathName TimeExpression? Qualifiers?
> FeatureCallParameters?
> ;
>
> Qualifiers : "[" ActualParameterList "]"
> ;
>
> Declarator : Name ( "," Name )* ( ":" SimpleTypeSpecifier )? "|"
> ;
>
> PathTypeName : TypeName ( "::" TypeName )*
> ;
>
> PathName : ( TypeName | Name ) ( "::" ( TypeName | Name ) )*
> ;
>
> TimeExpression : "@" Name
> ;
>
> ActualParameterList : Expression ( "," Expression )*
> ;
>
> Native LogicalOperator : "'and' | 'or' | 'xor' | 'implies'"
> ;
>
> Native CollectionKind : "'Set' | 'Bag' | 'Sequence' | 'Collection'"
> ;
>
> RelationalOperator : "=" | ">" | "<" | ">=" | "<=" | "<>"
> ;
>
> AddOperator : "+" | "-"
> ;
>
> Native MultiplyOperator : "'*' | '/'"
> ;
>
> Native UnaryOperator : "'-' | 'not'"
> ;
>
> UnaryTemporalOperator : SOMETIME_PAST
> | ALWAYS_PAST
> | SOMETIME
> | ALWAYS
> | INITIALLY
> ;
>
> Native SOMETIME_PAST
> : "'sometime_past'"
> ;
>
> Native ALWAYS_PAST
> : "'always_past'"
> ;
> Native SOMETIME
> : "'sometime'"
> ;
>
> Native ALWAYS
> : "'always'"
> ;
>
> Native INITIALLY
> : "'initially'"
> ;
>
> BinaryTemporalOperator : SOMETIME_SINCE_LAST | ALWAYS_SINCE_LAST
> | UNTIL | BEFORE
> ;
>
> Native SOMETIME_SINCE_LAST
> : "'sometime_since_last'"
> ;
>
> Native ALWAYS_SINCE_LAST
> : "'always_since_last'"
> ;
>
> Native UNTIL
> : "'until'"
> ;
>
> Native BEFORE
> : "'before'"
> ;
>
>
> Native Name : "'a'..'z' ( 'a'..'z' | '0'..'9' | 'A'..'Z' | '_')*"
> ;
>
> Native TypeName : "'A'..'Z' ( 'a'..'z' | '0'..'9' | 'A'..'Z' | '_')*"
> ;
>
> Native Number : "'0'..'9' ('0'..'9')*"
> ;
>
> I thought first to give it a try with just an ocl file, having the form:
>
>
> MainModel : http://SimplePDL
>
> context Process
> inv start_all:
> self.processElement->forAll(wd:WorkDefinition|wd.name.size() = 1 implies
> wd.name="3");
>
> After running the parser on this file I get the errors:
>
> no viable alternative at input 'self' (line:5, start:61, length:3)
> no viable alternative at input 'wd' (line:5, start:134, length:1)
>
> My question to you is whether you see something wrong with the grammar
> or it's all about the way XText generates the parser. Thank you!
>
> Regards,
> Cosmin
>
|
|
|
Powered by
FUDForum. Page generated in 0.03260 seconds