Skip to main content



      Home
Home » Modeling » TMF (Xtext) » Mismatched character
Mismatched character [message #1838634] Tue, 02 March 2021 12:49 Go to next message
Eclipse UserFriend
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 12:51] by Moderator

Re: Mismatched character [message #1838640 is a reply to message #1838634] Tue, 02 March 2021 13:37 Go to previous messageGo to next message
Eclipse UserFriend
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')'"'
;
Re: Mismatched character [message #1838645 is a reply to message #1838640] Tue, 02 March 2021 15:18 Go to previous messageGo to next message
Eclipse UserFriend
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] Tue, 02 March 2021 23:26 Go to previous messageGo to next message
Eclipse UserFriend
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
Re: Mismatched character [message #1838670 is a reply to message #1838649] Wed, 03 March 2021 06:03 Go to previous messageGo to next message
Eclipse UserFriend
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 12:11] by Moderator

Re: Mismatched character [message #1838800 is a reply to message #1838670] Fri, 05 March 2021 03:47 Go to previous messageGo to next message
Eclipse UserFriend
?
Re: Mismatched character [message #1838801 is a reply to message #1838800] Fri, 05 March 2021 03:59 Go to previous message
Eclipse UserFriend
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];
Previous Topic:Referenced DSL-Files are not recognized on clean
Next Topic:Begginer on Xtext Validation
Goto Forum:
  


Current Time: Wed Jul 23 00:27:36 EDT 2025

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

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

Back to the top