Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Keyword clashing with terminal rule
Keyword clashing with terminal rule [message #1858872] Thu, 27 April 2023 12:58 Go to next message
Simon Cockx is currently offline Simon CockxFriend
Messages: 69
Registered: October 2021
Member
I have a data rule containing the keyword '/':
MultiplicativeOperation returns Expression:
	BinaryOperation ( =>({ArithmeticOperation.left=current} operator=('*'|'/')) right=RosettaCalcBinary)*
;


Now I wanted to add support for JavaScript like regular expressions, e.g., `/[a-b]+/`. I added the following terminal rule for it:
terminal PATTERN returns Pattern:
	'/' ( '\\' . | !('\\'|'/') )* '/'
;

(given `type Pattern wraps Pattern` in Xcore)

However, now the `MultiplicativeOperation` rule doesn't work anymore. Any occurences of a division "a / b" results in a syntax error "mismatched character '<EOF>' expecting '/'".

Is there a way around this?
Re: Keyword clashing with terminal rule [message #1858873 is a reply to message #1858872] Thu, 27 April 2023 13:02 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14735
Registered: July 2009
Senior Member
i dont think this is solvable without introducing a stateful lexer (you migh look into jflex)

Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Day Job: https://www.everest-systems.com
Re: Keyword clashing with terminal rule [message #1858874 is a reply to message #1858873] Thu, 27 April 2023 13:03 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14735
Registered: July 2009
Senior Member
or you check if you can reuse https://github.com/eclipse/n4js/blob/master/plugins/org.eclipse.n4js/src/org/eclipse/n4js/parser/RegExLiteralAwareLexer.java

Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Day Job: https://www.everest-systems.com
Re: Keyword clashing with terminal rule [message #1858875 is a reply to message #1858874] Thu, 27 April 2023 13:17 Go to previous messageGo to next message
Simon Cockx is currently offline Simon CockxFriend
Messages: 69
Registered: October 2021
Member
I feared this was gonna be hard. Thank you for the quick response and the references.
Re: Keyword clashing with terminal rule [message #1858883 is a reply to message #1858875] Fri, 28 April 2023 07:37 Go to previous messageGo to next message
Ed Willink is currently offline Ed WillinkFriend
Messages: 7680
Registered: July 2009
Senior Member
When grammar tooling starts to fail, it is often a forewarning that there are ambiguous syntaxes that will not always be parsed in accordance with your user's expectations.

It is obviously possible to make a regex look like all sorts of regular grammar.

Better to introduce something distinctive such as a 'regex' keyword prefix or surrounding quotes. Just imagine 'improving' the Java grammar to support call of the regex support without quotes.
Re: Keyword clashing with terminal rule [message #1858948 is a reply to message #1858872] Tue, 02 May 2023 19:46 Go to previous messageGo to next message
Luong Tiet is currently offline Luong TietFriend
Messages: 4
Registered: May 2023
Junior Member
This case works for me

grammar org.xtext.example.mydsl.MyDsl hidden(WS)

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

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

Model:Test;
Test: value+=(MultiplicativeOperation | Pattern)+;
BinaryOperation: value=INT;
RosettaCalcBinary: value=INT;

MultiplicativeOperation returns Expression: BinaryOperation ( =>({ArithmeticOperation.left=current} operator='*' | '/') right=RosettaCalcBinary)*;

Pattern:	{Pattern} '/' P_BODY '/';

terminal INT returns ecore::EInt: ('0'..'9')+;
terminal P_BODY: ( '\\' | '\\*' | '\\/' | !('\\'|'/'|'*'|' '|'\t'|'\r'|'\n') )+ ;
terminal WS: (' '|'\t'|'\r'|'\n')+;
Re: Keyword clashing with terminal rule [message #1858949 is a reply to message #1858948] Tue, 02 May 2023 19:56 Go to previous message
Luong Tiet is currently offline Luong TietFriend
Messages: 4
Registered: May 2023
Junior Member
to modify in previous message:
MultiplicativeOperation returns Expression: BinaryOperation ( =>({ArithmeticOperation.left=current} operator=('*'|'/')) right=RosettaCalcBinary)*
Previous Topic:Syntactic predicates and proposal provider execution time
Next Topic:Xtext 2.31.0.M2 is out
Goto Forum:
  


Current Time: Mon Dec 02 16:10:50 GMT 2024

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

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

Back to the top