|
|
Re: Boolean-Rule not used but causes Error in ANTlr [message #787884 is a reply to message #787677] |
Wed, 01 February 2012 04:33 |
Henrik Lindberg Messages: 2509 Registered: July 2009 |
Senior Member |
|
|
There really is no such thing as "unused terminals" - the lexer will
deliver the input as tokens - one per found terminal. If your grammar
does not use a particular terminal just means that there will be a
syntax error when the lexer matches and delivers this token.
BTW, handling keywords as terminals is typically a really bad idea. By
default, your keywords are treated a special way - if the lexer matches
input that is a keyword it will return the token for the keyword instead
of what it otherwise matched. For booleans, you probably want a datatype
that also transforms from the string form "true"/"false" and a boolean
value.
BooleanValue returns ecore::EBoolean: 'true' | 'false';
And you associate a transformer with the "BooleanValue" rule. Perhaps
something like this:
@ValueConverter(rule = "BooleanValue")
public IValueConverter<Boolean> BooleanValue() {
return new IValueConverter<Boolean>() {
public String toString(Boolean value) {
return value.toString();
}
public Boolean toValue(String string, INode node) {
if(Strings.isEmpty(string))
throw new ValueConverterException("Could not convert empty string
to boolean", node, null);
return new Boolean(string).equals(Boolean.TRUE)
? Boolean.TRUE
: Boolean.FALSE;
}
};
}
Regards
- henrik
On 2012-31-01 22:31, Hendrik Renken wrote:
> Hello,
>
> i'm having some troubles with a small grammar i've created. I'm using
> xtext 2.2.1
>
>
> grammar de.lang.common
>
> import "http://www.eclipse.org/emf/2002/Ecore" as ecore
>
> generate common "http://www.lang.de/Common"
>
> Model: name=QNAME;
>
> QNAME:
> ID ('.' ID)*;
>
> terminal ID:
> '^'?('a'..'z'|'A'..'Z'|'_') ('a'..'z'|'A'..'Z'|'_'|'0'..'9')*;
> terminal BOOLEAN:
> 'true'|'false';
>
>
> When i try to compile this grammar, antlr says:
> 'The following token definitions can never be matched because prior
> tokens match the same input: RULE_BOOLEAN'
>
> I don't know how to fix this. The Boolean-Rule isn't even used. This
> grammar will become a set of rules that i want to use throughout my
> other grammars and now i'm stuck, since it won't compile correctly.
>
> If someone could point me into the right direction, maybe i do not
> understand the error message correctly...
> That would be awesome.
>
> Kind regards
> Hendrik
|
|
|
|
Powered by
FUDForum. Page generated in 0.04162 seconds