Skip to main content



      Home
Home » Modeling » TMF (Xtext) » Grammar help(How to write rule, which can be used anywhere in the model)
Grammar help [message #1784897] Thu, 05 April 2018 06:42 Go to next message
Eclipse UserFriend
Hi,

I have a strange requirement, where I need to write grammar.
And one of the rule, is independent of place.
I mean that can be written anywhere and at any level in the model.



Some rule
       test
      Another rule
                test
     .
     .
     .                     test
     nth  rule

In the above sample example, rule 'test' can appear any where in the level.
If needed I can provide more details.
How can I define such rules?

Thanks in advance.

Thanks and regards,
Virag Purnam
Re: Grammar help [message #1784931 is a reply to message #1784897] Fri, 06 April 2018 00:21 Go to previous messageGo to next message
Eclipse UserFriend
Hi,

Can I define terminal for the above requirement?
So that I can use than terminal rule any where in the model.
Re: Grammar help [message #1784937 is a reply to message #1784931] Fri, 06 April 2018 01:59 Go to previous messageGo to next message
Eclipse UserFriend
Your requirements are too vague

So the answer is anything between do some hidden rule like comments up to I don't know
Re: Grammar help [message #1784943 is a reply to message #1784937] Fri, 06 April 2018 04:10 Go to previous messageGo to next message
Eclipse UserFriend
Sorry Mr. Christian Dietrich for a vague requirements.
Here I have tried to explain in better way.
Could you please help me with this.

Sample grammar:
grammar com.hpe.nsdee.lexer.LexerDsl with org.eclipse.xtext.common.Terminals hidden ( COPY_STMT, WS )

generate lexerDsl "http://www.hpe.com/nsdee/lexer/LexerDsl"

Model:
	greetings+=Greeting*;
	
Greeting:
	'Hello' name=ID '!';


terminal COPY_STMT	: 'Copy' -> ';';


Sample model:
Hello Virag!
Copy test.txt;
Hello Purnam!
Copy file.txt;


Parser ignore Copy rule as it is hidden and I do not want to specify this Copy in Model grammar.
Requirement is to read Copy lines and take contents from the specified text file.
How can I read these lines from parser?

Thanks in advance.

Best regards,
Virag Purnam

Re: Grammar help [message #1784968 is a reply to message #1784943] Fri, 06 April 2018 07:19 Go to previous messageGo to next message
Eclipse UserFriend
sorry i have no idea on that.

what do you mean be "read"
Re: Grammar help [message #1785016 is a reply to message #1784968] Sat, 07 April 2018 03:39 Go to previous messageGo to next message
Eclipse UserFriend
Hi Virag,

I understand that you want to have a special construct NOTE in your grammar like "STARTNOTE sometext and so on ENDNOTE"
which allows you (similar as the comments) to place information anywhere inside your DSL independen of the defined rules.

What you want to avoid is adding this NOTE construct in any possible places of your grammar as an optional attribute.

And you want in the generator the possibility to get hold of all these NOTE lines so you can dump the contents to a file (or anything else).

Perhaps this will help Christian (TIA) what we are looking for,
Uli
Re: Grammar help [message #1785018 is a reply to message #1785016] Sat, 07 April 2018 04:05 Go to previous messageGo to next message
Eclipse UserFriend
Hi

I caution against attempting to solve this kind of problem in the grammar. Grammars are great for well-structured source text. If your source text is messy, so will the grammar be and endless debugging excitements lie in store. Look for a preprocessing that makes the grammar clean.

Given that your insert allows absolutely anything up to a simple terminator, you might consider using a lexer/TokenStream solution rather than parser solution. If all characters within 'Copy' -> ';' are lexable, an override of XXXParser.createTokenStream can intercept 'Copy' -> ';' and return it as a single Copy token that is treated as whitespace. If not all characters within 'Copy' -> ';' are lexable, you definitely need a customized lexer/character source.

Regards

Ed Willink
Re: Grammar help [message #1785050 is a reply to message #1785018] Sun, 08 April 2018 13:54 Go to previous messageGo to next message
Eclipse UserFriend
perhaps one can use a special comment like /*@ ... */ ???

how to obtain comments may be shown in https://www.eclipse.org/forums/index.php?t=msg&th=947734&goto=1567778&#msg_1567778
it's a pretty old one, so I do not know is actual xtext version still support the node model as shown there
Re: Grammar help [message #1785065 is a reply to message #1785050] Mon, 09 April 2018 01:07 Go to previous messageGo to next message
Eclipse UserFriend
Thanks Mr. Christian Dietrich, Mr. Uli Merkel, Mr. Ed Willink for the replies.
As explained by Mr. Uli Merkel, that is what I want to achieve.
I will give a try with the option proposed by Mr. Ed Willink and update here.

Iterating over INode, also provides "Copy_STMT", I am trying to use this as well.

Thanks and regards,
Virag Purnam
Re: Grammar help [message #1785069 is a reply to message #1785065] Mon, 09 April 2018 01:57 Go to previous message
Eclipse UserFriend
Hi

http://git.eclipse.org/c/ocl/org.eclipse.ocl.git/tree/plugins/org.eclipse.ocl.xtext.base/src/org/eclipse/ocl/xtext/base/services/RetokenizingTokenSource.java

and its usage at

http://git.eclipse.org/c/ocl/org.eclipse.ocl.git/tree/plugins/org.eclipse.ocl.xtext.completeocl/src/org/eclipse/ocl/xtext/completeocl/CompleteOCLRuntimeModule.java

may provide some inspiration.

Regards

Ed Willink
Previous Topic:Removing Left Recursion
Next Topic:Open remote file (on server) in Xtext DSL editor is read only
Goto Forum:
  


Current Time: Sat Jun 14 01:28:12 EDT 2025

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

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

Back to the top