grammar - Xtext: Recursive Rule Inovcations -
i attempting build language can declare methods , fields, intrinsic support generics. able use primitive types string
, declare own classes.
this should valid syntax:
string someprimitive class myclass { } myclass someobject; class list { } list<string> stringlist; list<myclass> objectlist; list<string> getnames() { }
i have grammar supports these operations:
model: (members+=modelmembers)*; modelmembers: class | field | methoddeclaration ; class: 'class' name=id '{' '}' ; field: type=type name=id ; enum primitivetype: string="string" | number="number"; type: ( {typeobject} clazz=[class] ("<" a+=type ("," a+=type)* ">")? | {typeprimitive} type=primitivetype ) ; methoddeclaration: returntype=type name=id "(" ")" "{" "}" ;
but contains error:
[fatal] rule rule__modelmembers__alternatives has non-ll(*) decision due recursive rule invocations reachable alts 2,3. resolve left-factoring or using syntactic predicates or using backtrack=true option.
the problem seems stem fact type
rule recursive, , can matches either beginning of methoddeclaration
or field
.
however, possible figure out rule 1 building, method have () { }
after name.
what really confuses me, if replace recursive rule [class]
, e.g. field: type=[class] name=id
(and same methoddeclaration
) grammar valid.
i there ambiguity when 1 sees instance of type
, lead onto method or field.... that's same when replace [class]
. instances of class can lead onto method or field.
how can ambiguous using type
, not ambiguous using [class]
?
this not direct answer question, did considered using xbase instead of plain xtext? xbase can simple use predefined rules, match need:
- java types generics
- expressions
- annotations
and many more.
here couple of useful links:
- xbase: https://wiki.eclipse.org/xbase
- extending xbase blog: http://koehnlein.blogspot.de/2011/07/extending-xbase.html
- 7 languages jvm (7 examples): http://www.eclipse.org/xtext/7languages.html
- screencasts: http://xtextcasts.org/?tag_id=10
if xbase doesn't suite you, can learn it's xbase-xtext-grammar.
Comments
Post a Comment