Skip to main content



      Home
Home » Modeling » TMF (Xtext) » Boolean expressions grammar(working with parse tree)
Boolean expressions grammar [message #653739] Thu, 10 February 2011 17:26 Go to next message
Eclipse UserFriend
Hello!

I have a problem with defining boolean expressions grammar.
Here it is (was made based on 'Simple arithmetics' example):

Model:
boolexprs+=BoolExpr*;

BoolExpr:
OrExpr;

OrExpr returns BoolExpr:
AndExpr ({Or.left=current} '||' right=AndExpr)*;

AndExpr returns BoolExpr:
PrimaryExpr ({And.left=current} '&&' right=PrimaryExpr)*;

PrimaryExpr returns BoolExpr:
'(' BoolExpr ')' | (name=ID);

Say, I have a code example like this:
a && (b || c)
When I work with BoolExprs during generation I can get their names (a, b, and c) but I don't have access to the types of logical relationships between them.

Please help me with modifying the grammar or Xpand hints so I can recover the information of boolean operations between the operands at the generation stage.
Re: Boolean expressions grammar [message #653744 is a reply to message #653739] Thu, 10 February 2011 17:51 Go to previous messageGo to next message
Eclipse UserFriend
Do you mean that you want to keep the operator? If so, just do:
OrExpr returns BoolExpr:
AndExpr ({Or.left=current} op = '||' right=AndExpr)*;

etc

- henrik
On 2/10/11 11:26 PM, Igor wrote:
> Hello!
>
> I have a problem with defining boolean expressions grammar.
> Here it is (was made based on 'Simple arithmetics' example):
>
> Model:
> boolexprs+=BoolExpr*;
>
> BoolExpr:
> OrExpr;
>
> OrExpr returns BoolExpr:
> AndExpr ({Or.left=current} '||' right=AndExpr)*;
>
> AndExpr returns BoolExpr:
> PrimaryExpr ({And.left=current} '&&' right=PrimaryExpr)*;
>
> PrimaryExpr returns BoolExpr:
> '(' BoolExpr ')' | (name=ID);
>
> Say, I have a code example like this:
> a && (b || c)
> When I work with BoolExprs during generation I can get their names (a,
> b, and c) but I don't have access to the types of logical relationships
> between them.
>
> Please help me with modifying the grammar or Xpand hints so I can
> recover the information of boolean operations between the operands at
> the generation stage.
Re: Boolean expressions grammar [message #653746 is a reply to message #653739] Thu, 10 February 2011 17:55 Go to previous messageGo to next message
Eclipse UserFriend
Hi,

what about using xpand polymorphism support

«IMPORT org::xtext::example::mydsl::myDsl»

«DEFINE main FOR Model-»
«FILE "test.txt"-»
«EXPAND expression FOREACH boolexprs»
«ENDFILE-»
«ENDDEFINE»

«DEFINE expression FOR BoolExpr»«name»«ENDDEFINE»

«DEFINE expression FOR And»And(«EXPAND expression FOR left»,«EXPAND expression FOR right»)«ENDDEFINE»

«DEFINE expression FOR Or»Or(«EXPAND expression FOR left»,«EXPAND expression FOR right»))«ENDDEFINE»


another possibility is to ask an expression for its metaType and use isAssignableFrom or you use the isInstance check

«DEFINE main FOR Model-»
«FILE "test.txt"-»
«EXPAND expression FOREACH boolexprs»
«ENDFILE-»
«ENDDEFINE»

«DEFINE expression FOR BoolExpr-»
«IF Or.isAssignableFrom(this.metaType)»
Or(«EXPAND expression FOR left»,«EXPAND expression FOR right»)
«ELSEIF And.isInstance(this)»
And(«EXPAND expression FOR left»,«EXPAND expression FOR right»)
«ELSE»
«name»
«ENDIF»
«ENDDEFINE»


~Christian
Re: Boolean expressions grammar [message #653828 is a reply to message #653746] Fri, 11 February 2011 08:12 Go to previous messageGo to next message
Eclipse UserFriend
Hi Christian!

Unfortunately the polymorphic way didn't work for me. It just kept on expanding BoolExpr's, not And's or Or's. And I still can't figure out the solution.

By the way I want to understand what the following syntactic construction
{Or.left=current}
does.

Is it new parse tree node being created and stored somewhere? And what 'Or' actually stands for - node type or its name?
Re: Boolean expressions grammar [message #653837 is a reply to message #653828] Fri, 11 February 2011 08:50 Go to previous messageGo to next message
Eclipse UserFriend
Hi,

can you be more specific what you want to do (pseudo template)?

~Christian

Re: Boolean expressions grammar [message #653840 is a reply to message #653828] Fri, 11 February 2011 08:43 Go to previous messageGo to next message
Eclipse UserFriend
That is described in the documentation.
It creates an instance of Or and assigns what was already parsed to it's
left feature. The newly created Or is now current, and is returned by the
rule.


- henrik
Igor <ishl@mail.ru> wrote:
> Hi Christian!
>
> Unfortunately the polymorphic way didn't work for me. It just kept on
> expanding BoolExpr's, not And's or Or's. And I still can't figure out the solution.
> By the way I want to understand what the following syntactic construction {Or.left=current}
> does.
>
> Is it new parse tree node being created and stored somewhere? And what
> 'Or' actually stands for - node type or its name?


--
- henrik
Re: Boolean expressions grammar [message #653931 is a reply to message #653837] Fri, 11 February 2011 17:36 Go to previous messageGo to next message
Eclipse UserFriend
Christian,

I've made some changes to your Xpand template just to see how it would work:
«DEFINE expression FOR BoolExpr»
«FILE name-»
«ENDFILE-»
«ENDDEFINE»

«DEFINE expression FOR And»
«FILE "And"-»
«ENDFILE-» «EXPAND expression FOR left»,«EXPAND expression FOR right»
«ENDDEFINE»

«DEFINE expression FOR Or»
«FILE "Or"-»
«ENDFILE-»
«EXPAND expression FOR left»,«EXPAND expression FOR right»
«ENDDEFINE»


The grammar itself stays unchanged.

There is the following query in the mwe2 file:
expand = "templates::Template::expression	FOREACH boolexprs"


So, after the generation of the same example a && (b || c) Xtext produces 3 files: a, b and c, and nothing for Or and And.

My goal is to reflect initial expression structure in the generated code. But at the current step I just want to transfer input expression to an output as it is.
Re: Boolean expressions grammar [message #653973 is a reply to message #653931] Sat, 12 February 2011 03:25 Go to previous messageGo to next message
Eclipse UserFriend
Hi,

it seems to me that you stumbled into a common pitfall of Xtexts mwe reader Componentent. I guess your workflow looks like this.

module workflow.MyDslGenerator

import org.eclipse.emf.mwe.utils.*

var targetDir = "src-gen"
var fileEncoding = "Cp1252"
var modelPath = "src/model"

Workflow {

	component = org.eclipse.xtext.mwe.Reader {
		// lookup all resources on the classpath
		// useJavaClassPath = true

		// or define search scope explicitly
		path = modelPath

		// this class will be generated by the xtext generator 
		register = org.xtext.example.mydsl.MyDslStandaloneSetup {}
		load = {
			slot = "boolexprs"
			type = "BoolExpr"
		}
	}

	component = org.eclipse.xpand2.Generator {
		expand = "templates::Template::main FOREACH boolexprs"
		outlet = {
			path = targetDir
		}
		fileEncoding = fileEncoding
	}
}



That that does is basically: Take all BoolExpr (really all, not only the root ones) that have a (qualified)name != null (which are a b and c) and call the root main definition in the Template foreach of them.

your strategy makes no sense to me: why do you want to have a file foreach expression no matter where it is in the tree? more bad: if the reader adds all the stuff to inital for loop, why doing recursive calls in the template.


I simpley Introduced a IQualifiedNameProvider to give the model a name and used a workflow like follows

package org.xtext.example.mydsl;

import org.eclipse.xtext.naming.DefaultDeclarativeQualifiedNameProvider;
import org.xtext.example.mydsl.myDsl.Model;

public class MyQNP extends DefaultDeclarativeQualifiedNameProvider {
	
	public String qualifiedName(Model model) {
		return model.eResource().getURI().toString();
	}

}


public class MyDslRuntimeModule extends org.xtext.example.mydsl.AbstractMyDslRuntimeModule {

	@Override
	public Class<? extends IQualifiedNameProvider> bindIQualifiedNameProvider() {
		return MyQNP.class;
	}
	
}


module workflow.MyDslGenerator

import org.eclipse.emf.mwe.utils.*

var targetDir = "src-gen"
var fileEncoding = "Cp1252"
var modelPath = "src/model"

Workflow {

	component = org.eclipse.xtext.mwe.Reader {
		// lookup all resources on the classpath
		// useJavaClassPath = true

		// or define search scope explicitly
		path = modelPath

		// this class will be generated by the xtext generator 
		register = org.xtext.example.mydsl.MyDslStandaloneSetup {}
		load = {
			slot = "model"
			type = "Model"
		}
	}

	component = org.eclipse.xpand2.Generator {
		expand = "templates::Template::main FOREACH model"
		outlet = {
			path = targetDir
		}
		fileEncoding = fileEncoding
	}
}



I hope this gives you an idea how i solved the problem.

~Christian

[Updated on: Sat, 12 February 2011 03:32] by Moderator

Re: Boolean expressions grammar [message #654042 is a reply to message #653973] Sun, 13 February 2011 10:02 Go to previous message
Eclipse UserFriend
Christian,

I think now I understand this. Thank you for the explanation!
Previous Topic:#include-like feature?
Next Topic:Duplicate Recognition
Goto Forum:
  


Current Time: Mon Jul 07 10:07:59 EDT 2025

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

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

Back to the top