Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Disable terminal within rule
Disable terminal within rule [message #1097346] Thu, 29 August 2013 15:28 Go to next message
georges.chaudyskyscanner.net Georges is currently offline georges.chaudyskyscanner.net GeorgesFriend
Messages: 5
Registered: August 2013
Junior Member
Hi guy's

Is there a way to have contextual terminals? By this I mean having a certain set of terminal disabled in a sections of the grammar ?

I am trying to build a DSL that need to ba a valid JSON document:

Json : '{' pairs+=Pair ( ',' pairs+=Pair)* );
Pair : key=STRING ':' value= Value;
Value : '"' type=Type ':' name=ID '"';
Type : 'Sport'|'TV' ;

terminal ID : ('a'..'z'|'A'..'Z')+;
terminal STRING : '"' (!'"')* '"';

Here is an example of what output
{
"monday" : "SPORT:hiking",
"tuesday" : "TV:dexter"
}

The issue is that "SPORT:hiking" is considered as a STRING and I am getting an error.

Any idea?


Re: Disable terminal within rule [message #1097365 is a reply to message #1097346] Thu, 29 August 2013 15:52 Go to previous messageGo to next message
Henrik Lindberg is currently offline Henrik LindbergFriend
Messages: 2509
Registered: July 2009
Senior Member
On 2013-29-08 17:28, georges.chaudyskyscanner.net Georges wrote:
> Hi guy's
>
> Is there a way to have contextual terminals? By this I mean having a
> certain set of terminal disabled in a sections of the grammar ?

No.
Lexing happens before parsing.

You can use a data type with a value converter but that may require you
to switch to an external model (instead of having one generate from your
grammar).

Alternatively, just add validation that the form of the string is
correct and then have functions that return type, value.

Those would be far simpler than fixing this in the lexer if this is the
only such construct you have.

You could also do a m2m transformation after parsing.
Depends on your use-cases.

- henrik

> I am trying to build a DSL that need to ba a valid JSON document:
> Json : '{' pairs+=Pair ( ',' pairs+=Pair)* );
> Pair : key=STRING ':' value= Value;
> Value : '"' type=Type ':' name=ID '"';
> Type : 'Sport'|'TV' ;
> terminal ID : ('a'..'z'|'A'..'Z')+;
> terminal STRING : '"' (!'"')* '"';
>
> Here is an example of what output
> {
> "monday" : "SPORT:hiking",
> "tuesday" : "TV:dexter"
> }
>
> The issue is that "SPORT:hiking" is considered as a STRING and I am
> getting an error.
>
> Any idea?
>
>
>
Re: Disable terminal within rule [message #1097936 is a reply to message #1097365] Fri, 30 August 2013 11:14 Go to previous message
Jens Kuenzer is currently offline Jens KuenzerFriend
Messages: 29
Registered: October 2009
Junior Member
It is a perfect example for use of semantic predicates in the lexer (for antlr but not in Xtext).
On the other side it is a good example why not to use it. It would need to change together with every grammar change and only works for certain grammars.

So maybe I can add more practical proposals: Just not support strings at all, and use '"' ID '"' for the key (if it is possible for your case).
If only the result needs to be a valid JSON document then you are free to write in a different more domain specific language and generate the JSON document at the end.
Previous Topic:Lexer problem
Next Topic:Duplicate references in Xtext Editor
Goto Forum:
  


Current Time: Fri Apr 26 06:06:43 GMT 2024

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

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

Back to the top