Home » Modeling » TMF (Xtext) » Xtext Grammer Issue(facing some issues in creating Xtext grammer of NRL)
Xtext Grammer Issue [message #655367] |
Sun, 20 February 2011 00:59 |
romeh Messages: 35 Registered: February 2011 |
Member |
|
|
Hi all,
i am trying to convert NRL(Natural Rule language)http:// nrl.sourceforge.net/spec/spec-nrl-20100407.html#concrete-syn tax ,to Xtext grammer, and and when run MW2 file, the generated Ecore model , somhow is to small and not complete, can anybody of the experts give me a hint , here is the my Xtext grammar,
grammar org.xtext.example.mydsl.MyNrl with org.eclipse.xtext.common.Terminals
import 'platform:/resource/NRLModel/model/edmmodel.ecore' as mymodel
generate myNrl "http://www.xtext.org/example/mydsl/MyNrl"
DomainModel:
(elements+=Rule)*;
Rule:ValidationRule | ActionRule |{GlobalVariableDeclaration};
ValidationRule: Context 'Validation Rule' name = QuotedID (ValidationRuleVariableDeclaration)* Constraint ('report:' CompoundReport)?;
ValidationRuleVariableDeclaration : SimpleVariableDeclaration ','?;
ValidationFragment : MultipleParameterContext 'Validation Fragment' name = QuotedID Constraint;
RuleSet : 'Rule set' name = QuotedID ('applies to' ModelReference 'where' Constraint)?;
GlobalVariableDeclaration : SimpleVariableDeclaration; /* (Additional constraints apply! */
Context : 'Context:' ModelReference;
MultipleParameterContext : 'Context:' MultipleContextParameter;
MultipleContextParameter : ModelReference '(' QuotedName ')' (',' MultipleContextParameter)*;
//Constraints
Constraint : IfThenStatement;
IfThenStatement : "if" IffStatement "then" IfThenStatement "else" IfThenStatement | IffStatement;
IffStatement : ImpliesStatement ("only if" IffStatement)?;
ImpliesStatement : OrStatement ("implies" ImpliesStatement)?;
OrStatement : AndStatement ("or" OrStatement)?;
AndStatement : LogicalStatement ("and" AndStatement)?;
LogicalStatement : ExistsStatement | NotExistsStatement | GlobalExistsStatement | ForallStatement
| Predicate;
// Quantifiers
ExistsStatement : (Enumerator ("of the")?)? ModelReference ("has" | "have" | "is" | "are") ("present" | SimpleOrComplexConstraint)
| Enumerator ("has" | "have" | "is" | "are") SimpleOrComplexConstraint;
Enumerator : ("at least" | "at most" | "exactly")? ("one" | "two" | "three" | "four" | "no" | "none" | IntegerNumber);
NotExistsStatement : ModelReference ("is not present" | "are not present");
GlobalExistsStatement : ("there is" | "there are") ("no")? ModelReference ("(" QuotedVar ")")? ("where" SimpleOrComplexConstraint)?;
ForallStatement : ("each" | "in each" | "all" | "every") ("of the")? ModelReference ("has" | "have" | "is" | "are")? SimpleOrComplexConstraint
| "for each" QuotedVar "in the collection of" ModelReference ("has" | "have" | "is" | "are" | ",")? SimpleOrComplexConstraint;
SimpleOrComplexConstraint : "(" Constraint ")"
| Predicate;
// Variable Declaration
SimpleVariableDeclaration : QuotedVar ("is" | "are" | "represent" | "represents") Expression;
// Predicates
Predicate : Expression "is one of" ListDefinition | Expression "is not one of" ListDefinition
| ModelReference "is a kind of" ModelElementName | MultipleExistsStatement | MultipleNotExistsStatement
| Expression (BinaryPredicate Expression)?;
BinaryPredicate : "=" | ("is")? "equal to" | "<>" | ("is")? "not equal to" | "<" | ("is")? "less than" | ("is")? "before"
| ">" | ("is")? "greater than" | ("is")? "after" | "<=" | ("is")? "less than or equal to" | ">=" | ("is")? "greater than or equal to";
ListDefinition : Identifier ("," Identifier)*;
MultipleExistsStatement : "following" ("is present" | "are present") ":" ModelReferenceList;
MultipleNotExistsStatement : "following" ("is not present" | "are not present") ":" ModelReferenceList;
// Expressions
Expression : MultiplyExpression;
MultiplyExpression : AdditiveExpression ( ("*" | "/" | "mod") MultiplyExpression)?;
AdditiveExpression : InfixOperatorExpression ( ("+" | "-") AdditiveExpression)?;
InfixOperatorExpression : InfixValidationFragmentApplication (OperatorName InfixOperatorExpression)?;
InfixValidationFragmentApplication : Term (FragmentName InfixValidationFragmentApplication)?;
// Terms
Term : Identifier | FunctionalExpression | OperatorInvocation | ValidationFragmentApplication
| CastExpression | SelectionExpression | "(" Constraint ")";
Identifier : ModelReference | LiteralString | Number | IntegerNumber | BooleanLiteral | CollectionIndex;
FunctionalExpression : "sum of" ModelReference
| "number of" ModelReference
| "number of" "unique" ModelReference "(" "by" ModelReference ")";
OperatorInvocation : OperatorName (OperatorParameterList)?;
OperatorParameterList : Expression ( ("and" | "from" | "to" | "with" | "using") Expression )*;
ValidationFragmentApplication : FragmentName OperatorParameterList;
CollectionIndex : OrdinalNumber ("of")? ModelReference;
CastExpression : ModelReference ("as a" | "as an") ModelElementName;
SelectionExpression : ("first" "of"?)? ModelReference "where" SimpleOrComplexConstraint;
ModelReference : (PackageReference)? ModelElementName ((("." | "of" ) ModelElementName)*)*;
ModelReferenceList : (ModelReference)+;
PackageReference : ModelElementName ("::" ModelElementName)*;
// Reporting
CompoundReport : SimpleReport ( (",")? SimpleReport )*;
SimpleReport : ConcatenatedReport | ConditionalReport;
ConcatenatedReport : ("report")? SimpleTerm ( ("+")? SimpleTerm )*;
ConditionalReport : "if" Constraint "then" CompoundReport ("else" CompoundReport)? ";";
SimpleTerm : Identifier | FunctionalExpression | OperatorInvocation;
// Terminals
terminal BooleanLiteral : 'true' | 'false';
terminal LiteralString : "'" (!'\'''\n''\r')* "'";
terminal ModelElementName : ('a'..'z'|'A'..'Z') ('a'..'z'|'A'..'Z'|'_'|'-'|'0'..'9')*;
terminal Digit : ('0'..'9');
terminal Number : ('-')? (Digit)+ '.' (Digit)*;
terminal OrdinalNumber : 'first' | 'second' | 'third' | (Digit+ ('th' | 'st' | 'nd' | 'rd'));
terminal IntegerNumber : ('-')? (Digit)+;
terminal FragmentName : '{' (!'{''\n''\r')* '}';
terminal OperatorName : '[' (!']''[''\n''\r')* "]";
terminal QuotedID : '"' ID '"';
terminal QuotedVar : '"' ID '"';
terminal QuotedName : '"' ID '"';
// Action section
ActionRule : Context "Action Rule" name = QuotedID Action;
ActionFragment : MultipleParameterContext "Action Fragment" name = QuotedID Action;
// Complex Actions
Action : CompoundAction;
CompoundAction : SimpleAction ((",")? ("then")? CompoundAction)?;
// Simple Actions
SimpleAction : ConditionalAction | CreateAction | RemoveAction | AddAction | RemoveFromCollectionAction | SetAction | ForEachAction
| ActionFragmentApplicationAction | OperatorAction | VariableDeclarationAction;
ConditionalAction : "if" Constraint "then" Action ("else" Action)? ";";
CreateAction : "create" ("new")? ModelReference "(" QuotedVar ")";
RemoveAction : ("Remove" | "Clear") (("each" | "every" | "all" | "any") ("of the")?)? ModelReference ("(" QuotedVar ")" "where" Constraint)?;
AddAction : "add" ModelReference "to" ModelReference;
RemoveFromCollectionAction : "remove" ModelReference "from" ModelReference;
SetAction : "set" ModelReference "to" Expression;
ForEachAction : "for each" ("of the")? ModelReference (",")? Action ";" | "for each" QuotedVar "in the collection of" ModelReference (",")? Action ";";
ActionFragmentApplicationAction : FragmentName ActionFragmentApplicationParameters;
ActionFragmentApplicationParameters : Expression ( ("using" | "with" | "from" | "to" | "and") ActionFragmentApplicationParameters )?;
OperatorAction : OperatorName (OperatorActionParameters)?;
OperatorActionParameters : Expression (("using" | "with" | "from" | "to" | "and") Expression)*;
VariableDeclarationAction : SimpleVariableDeclaration;
Thanks a lot
|
|
| |
Re: Xtext Grammer Issue [message #655389 is a reply to message #655376] |
Sun, 20 February 2011 12:58 |
romeh Messages: 35 Registered: February 2011 |
Member |
|
|
Hi Christian,
thanks a lot for your support, i have a question here, how i can check my Xtext grammar is from syntax and semantic part is valid and correct, is the generation of a complete Ecore model of the Xtext grammar is a an evidence that the grammar is correctly written, or there are any other ways, as the grammar, i have shown above, from semantic part according to NRL specs, is correct, but how i can test it?
Thanks a lot
|
|
|
Re: Xtext Grammer Issue [message #655404 is a reply to message #655389] |
Sun, 20 February 2011 16:31 |
Henrik Lindberg Messages: 2509 Registered: July 2009 |
Senior Member |
|
|
You can use org.eclipse.xtext.junit.AbstractXtextTests to write JUnit
tests - parse strings or files (input streams), and write assertions
that you get the correct/expected model elements. Also check that your
validations/errors are detected.
- henrik
On 2/20/11 1:58 PM, romeh wrote:
> Hi Christian,
>
> thanks a lot for your support, i have a question here, how i can check
> my Xtext grammar is from syntax and semantic part is valid and correct,
> is the generation of a complete Ecore model of the Xtext grammar is a an
> evidence that the grammar is correctly written, or there are any other
> ways, as the grammar, i have shown above, from semantic part according
> to NRL specs, is correct, but how i can test it?
>
> Thanks a lot
|
|
| |
Re: Xtext Grammer Issue [message #655427 is a reply to message #655417] |
Sun, 20 February 2011 19:44 |
Alexander Nittka Messages: 1193 Registered: July 2009 |
Senior Member |
|
|
Hi,
I may not understand your question correctly. Are you saying you don't see all types you expect in the generated ecore file?
This may be because you did not consider Christians notes to carefully. As long as you have
Rule: Stuff1 Stuff2 Stuff3;
without any feature assignment (Rule: stuff1=Stuff stuff2=Stuff2 ...), Rule will (speaking in a very simplyfied way) be a datatype rule, i.e. not a model element with a type but a simple string.
Please read the documentation (grammar reference part) three times and thoroughly.
Note also that your terminal rules are highly conflicting (would fire for the same input). Use datatype rules where possible and change terminal rules only where necessary. (Terminal rules are used without context, i.e. before the document is processed semantically, the lexer chops it up into tokens based on the terminal rule definitions).
my initial sugestions for your terminals:
terminal BooleanLiteral : 'true' | 'false';
datatype rule (if at all, because you may want to put semantics into "true" and "false")
BooleanLiteral: 'true'|'false';
terminal LiteralString : "'" (!'\'''\n''\r')* "'";
override terminal STRING instead
terminal ModelElementName : ('a'..'z'|'A'..'Z') ('a'..'z'|'A'..'Z'|'_'|'-'|'0'..'9')*;
override terminal ID instead
terminal Digit : ('0'..'9');
drop this rule
terminal Number : ('-')? (Digit)+ '.' (Digit)*;
datatype rule
Numder hidden(): ('-')? INT '.' INT?
terminal OrdinalNumber : 'first' | 'second' | 'third' | (Digit+ ('th' | 'st' | 'nd' | 'rd'));
datatype rule
OrdinalNumer hidden(): 'first' | 'second' | 'third' | (INT+ ('th' | 'st' | 'nd' | 'rd'));
terminal IntegerNumber : ('-')? (Digit)+;
datatype rule:
Integer Number: ('-')? INT;
terminal FragmentName : '{' (!'{''\n''\r')* '}';
terminal OperatorName : '[' (!']''[''\n''\r')* "]";
could be used of brackets and braces are used at absolutely no other point in the language
terminal QuotedID : '"' ID '"';
terminal QuotedVar : '"' ID '"';
terminal QuotedName : '"' ID '"';
drop all of them use STRING directly and do a semantic validation on whether the used characters are allowed.
Further, many many keywords in your grammar contain white spaces. This will cause you endless pains, so split them up into several succeeding keywords, that is "only" "if" insead of "only if".
And again, read the grammar reference several times and play around with smaller languages before you come back to this project in earnest. So far you are not up to it, yet.
Alex
|
|
|
Re: Xtext Grammer Issue [message #655436 is a reply to message #655427] |
Mon, 21 February 2011 00:13 |
romeh Messages: 35 Registered: February 2011 |
Member |
|
|
Dear Alexander,
your advices was a huge help and support to me,
now,i have read the grammar reference part many times, and finally , i got a valid Ecore model and complete one of my Xtext Grammar, due to ur huge support of course, my only remaining questions are as following:
1- when i run MW2 file, i got many warnings, but when i searched for these types of warnings, i figured out due to Antlr parser, so be enabling (backtrack = true), all warnings have gone , what is the effect of enabling backtrack? , and is it good to enabled it or not?
2- you have mentioned, to not include white spaces into Key words. what are the issues may be raised from that?
again, thanks a lot for your huge support and response ,
i really appreciated it
|
|
|
Re: Xtext Grammer Issue [message #655574 is a reply to message #655436] |
Mon, 21 February 2011 19:27 |
Alexander Nittka Messages: 1193 Registered: July 2009 |
Senior Member |
|
|
Hi,
as to 1, I am not the right one to answer, but here is the short version. Backtracking is a potential performance bottleneck, but if it is not enabled (when actually necessary) models cannot be correctly instantiated (if th parser makes wrong choices and can't take them back).
As to 2, one reason is that if you specify exactly one space ("greater than"), you cannot have two in the model ("greater than"), but more problematic is the fact that the (context insensitive) lexer works greedily. It tries to make tokens as long as possible. Consider the following grammar and sample model
Model:
stuff+=Stuff*;
Stuff:
("The" subject=ID blue?="is blue"|
subject=ID "is" adjective=ID)
'.';
The sky is blue.
Helping is cool.
You'd think the model is valid, right? But it is not (syntax error
mismatched character 'c' expecting 'b'). There is a white space after "is", there is a token that starts with "is ", so the lexer wants to use it (making the token as long as possible).
And this is the type of problem you will run into all the time when allowing white spaces in your keywords.
Alex
|
|
|
Goto Forum:
Current Time: Fri Apr 26 16:26:08 GMT 2024
Powered by FUDForum. Page generated in 0.04157 seconds
|