Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Mismatched character
Mismatched character [message #1838634] Tue, 02 March 2021 17:49 Go to next message
M D is currently offline M DFriend
Messages: 23
Registered: January 2021
Junior Member
I'm trying to develop a DSL that uses JSON. But I've run into a problem with the following grammar:

grammar org.xtext.example.mydsl.MyDsl hidden(WS, ML_COMMENT, SL_COMMENT)

import "http://www.eclipse.org/emf/2002/Ecore" as ecore

generate myDsl "http://www.xtext.org/example/mydsl/MyDsl"

Model:
'[' objects += Object (',' objects += Object)* ']'
;

Object:
'{' '"name"' ':' '"' name = STRING '"' ',' '"type"' ':' '"object"' ',' '"isOptional"' ':' value = BOOLEAN ',' '"dataType"' ':' '"' dataType = PredefinedType '"' '}'
;

PredefinedType:
type = ('String' | 'int' | 'float' | 'boolean')
;

terminal STRING: "'" ( '\\' . /* 'b'|'t'|'n'|'f'|'r'|'u'|'"'|"'"|'\\' */ | !('\\'|"'") )* "'";

terminal BOOLEAN: 'true' | 'false';

terminal ML_COMMENT : '/*' -> '*/';

terminal SL_COMMENT : '//' !('\n'|'\r')* ('\r'? '\n')?;

terminal WS : (' '|'\t'|'\r'|'\n')+;

terminal ANY_OTHER: .;

The DSL:
[
{
"name": "'Test'",
"type": "object",
"isOptional": false,
"dataType" : "int"
}
]

It gives error "mismatched charachter 'n' expections 's'" at int. It want me to write isOptional instead of int. I've tried changed '"isOptional"' to '"optional"' in the grammar, which fixes the issue. But I'm curious about why I get the error, and why changing the keyword fixes it. Any ideas?

[Updated on: Tue, 02 March 2021 17:51]

Report message to a moderator

Re: Mismatched character [message #1838640 is a reply to message #1838634] Tue, 02 March 2021 18:37 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 13939
Registered: July 2009
Senior Member
the lexer is greedy and thus cannot handle

'"isOptional"' vs '"' 'int' '"'

you can either enable backtracking

parserGenerator = {
options = {
backtrackLexer = true
}
}

or use terminals consistently

e.g.
Object:
'{' '"name"' ':' '"' name = STRING '"' ','
'"type"' ':' '"object"' ','
'"isOptional"' ':' value = BOOLEAN ','
'"dataType"' ':' dataType = PREDEFINED_TYPE '}'
;

terminal PREDEFINED_TYPE:
'"' ('String' | 'int' | 'float' | 'boolean')'"'
;


Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/it-services/methods-and-tools/xtext
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Mismatched character [message #1838645 is a reply to message #1838640] Tue, 02 March 2021 20:18 Go to previous messageGo to next message
M D is currently offline M DFriend
Messages: 23
Registered: January 2021
Junior Member
Thanks, the last solution worked.

Why exactly can it not not differ '"isOptional"' vs '"' 'int' '"'?
Re: Mismatched character [message #1838649 is a reply to message #1838645] Wed, 03 March 2021 04:26 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 13939
Registered: July 2009
Senior Member
The first is a lever rule the second a parser rule

The file is Lexed completely first
Thus when the Lexer see
"i it goes the "isOptional path as it is greedy and won't create two tokens one for " and one for int


Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/it-services/methods-and-tools/xtext
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Mismatched character [message #1838670 is a reply to message #1838649] Wed, 03 March 2021 11:03 Go to previous messageGo to next message
M D is currently offline M DFriend
Messages: 23
Registered: January 2021
Junior Member
Thanks for the explanation!

I've encountered another problem. I try to make it so the user can define their own types. dataType can then either be a predefined type or an already defined type.

If I do the following, I get a "NullPointerException: null" at the beginning of the grammar. You know why?

Model:
'[' objects += Object (',' objects += Object)* ',' '{' '"definedType"' ':' definedType = DefinedType '}' ']'
;

Object:
'{' '"name"' ':' '"' name = STRING '"' ',' '"type"' ':' '"object"' ',' '"isOptional"' ':' value = BOOLEAN ',' '"dataType"' ':' dataType = ([DefinedType|STRING] | PREDEFINED_TYPE) '}'
;

DefinedType:
'"' name = STRING '"'
;

terminal PREDEFINED_TYPE:
'"' ('String' | 'int' | 'float' | 'boolean') '"';

terminal STRING: "'" ( '\\' . /* 'b'|'t'|'n'|'f'|'r'|'u'|'"'|"'"|'\\' */ | !('\\'|"'") )* "'";

[Updated on: Wed, 03 March 2021 17:11]

Report message to a moderator

Re: Mismatched character [message #1838800 is a reply to message #1838670] Fri, 05 March 2021 08:47 Go to previous messageGo to next message
M D is currently offline M DFriend
Messages: 23
Registered: January 2021
Junior Member
?
Re: Mismatched character [message #1838801 is a reply to message #1838800] Fri, 05 March 2021 08:59 Go to previous message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 13939
Registered: July 2009
Senior Member
dataType = ([DefinedType|STRING] | PREDEFINED_TYPE is an antipattern.
without a stacktrace hard to jude

maybe

dataType =AbstractDataType

AbstractDataType: PrimiteDataType | DataTypeRef;

PrimiteDataType: type= PREDEFINED_TYPE;
DataTypeRef: ref=[DefinedType|STRING];


Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/it-services/methods-and-tools/xtext
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Previous Topic:Referenced DSL-Files are not recognized on clean
Next Topic:Begginer on Xtext Validation
Goto Forum:
  


Current Time: Fri May 14 09:38:12 GMT 2021

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

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

Back to the top