Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Generate matlab script form arithmetic expression(Generate a matlab script for nested arithmetic expressions)
Generate matlab script form arithmetic expression [message #1746369] Thu, 27 October 2016 15:38 Go to next message
Lukas Schaus is currently offline Lukas SchausFriend
Messages: 19
Registered: October 2016
Junior Member
Hello,

I described a Language which is very similar to the arithmetic example provided with xtext. The grammar is defined as follows:
Expression:
    BooleanExpression
;

BooleanExpression returns Expression:
    Comparison 
    (({AndOrExpression.left=current} op=LogicOperator ) right=Comparison)*;
    
Comparison returns Expression:
       Addition
       (({Comparison.left=current} op=EquationOperator ) right=Addition)*;

Addition returns Expression:
	Multiplication (({Plus.left=current} '+' | {Minus.left=current} '-') right=Multiplication)*;

Multiplication returns Expression:
	PrimaryExpression (({Multi.left=current} '*' | {Div.left=current} '/') right=PrimaryExpression)*;

PrimaryExpression returns Expression:
	lBrace='(' expression=Expression rBrace=')' | function=Function lBrace='(' expression=Expression rBrace=')' |
	{NumberLiteral} value=NUMBER | SignalIdentifier | ParameterIdentifier | {BooleanLiteral} bool=('true' | 'false')
;
Function:
	'abs' | 'square' | 'cube'
;

LogicOperator:
	name =('and' | 'or') 
;

EquationOperator:
	name=(Equal | LessOrEqual | LessThan | GreaterOrEqual | GreaterThan | NotEqual)
;
	
	Equal:
		'==' | 'is' 'equal' 'to'
	;
	
	LessOrEqual:
		'<=' | 'is' 'less' 'or' 'equal' 'to'
	;
	
	LessThan:
		'<' | 'is' 'less' 'than'
	;
	
	GreaterOrEqual:
		'>=' | 'is' 'greater' 'or' 'equal' 'to'
	;

	GreaterThan:
		'>' | 'is' 'greater' 'than'
	;
	
	NotEqual:
		'~=' | 'is' 'not' 'equal'
	;

NumberLiteral:
	value=NUMBER
;

SignalIdentifier:
	'signal' sigID=[SignalName]
;

ParameterIdentifier:
	'parameter' paramID=[ParameterName]
;


I would like to generate matlab code from this.

For example the input:
(4+ signal in) / 6 == true and signal in2 is equal to abs(5)
should result in
(4 + in) / 6 == 1 & in2 == abs(5)

I dont know how to traverse the ast correctly to get the job done

i would be grateful for any suggestions
Re: Generate matlab script form arithmetic expression [message #1746391 is a reply to message #1746369] Fri, 28 October 2016 04:02 Go to previous messageGo to next message
Christian Dietrich is currently online Christian DietrichFriend
Messages: 10899
Registered: July 2009
Senior Member
here is a little incomplete startig point

def dispatch CharSequence gen(AndOrExpression exp) {
		'''«exp.left.gen» «exp.op.transform» «exp.right.gen»'''
	}
	
	def dispatch CharSequence transform(LogicOperator exp) {
		if (exp.name == 'and') {
			'&'
		} else if (exp.name == 'or') {
			'|'
		}
	}
	
	def dispatch CharSequence transform(EquationOperator exp) {
                //incomplete, and your grammar kind a sucks regarding whitespace in the sentenses => better to have extra types foreach branch
		if (exp.name == '==') {
			'=='
		} else if (exp.name == 'is equal to') {
			'=='
		} else {
			throw new IllegalArgumentException(exp.toString);
		}
	}
	
	def dispatch CharSequence gen(Comparison exp) {
		'''«exp.left.gen» «exp.op.transform» «exp.right.gen»'''
	}
	def dispatch CharSequence gen(Div exp) {
		'''«exp.left.gen» / «exp.right.gen»'''
	}
	def dispatch CharSequence gen(Multi exp) {
		'''«exp.left.gen» * «exp.right.gen»'''
	}
	
	def dispatch CharSequence gen(Plus exp) {
		'''«exp.left.gen» + «exp.right.gen»'''
	}
	
	def dispatch CharSequence gen(Minus exp) {
		'''«exp.left.gen» + «exp.right.gen»'''
	}
	
	def dispatch CharSequence gen(NumberLiteral exp) {
		'''«exp.value»'''
	}
	def dispatch CharSequence gen(SignalIdentifier exp) {
		'''«exp.sigID»'''
	}
	def dispatch CharSequence gen(ParameterIdentifier exp) {
		'''«exp.paramID»'''
	}
	def dispatch CharSequence gen(BooleanLiteral exp) {
		'''«exp.bool»'''
	}
	
	def dispatch CharSequence gen(Expression exp) {
		if (exp.LBrace != null && exp.RBrace != null && exp.function == null) {
			'''(«exp.expression.gen»)'''
		} else if (exp.LBrace != null && exp.RBrace != null && exp.function != null) {
			'''«exp.function»(«exp.expression.gen»)'''
		} else {
			throw new IllegalArgumentException(exp.toString);
		}
	}


Need professional support for Xtext, Xpand, EMF?
Go to: http://xtext.itemis.com
Twitter : @chrdietrich
Blog : christiandietrich.wordpress.com
Re: Generate matlab script form arithmetic expression [message #1746398 is a reply to message #1746391] Fri, 28 October 2016 07:11 Go to previous messageGo to next message
Lukas Schaus is currently offline Lukas SchausFriend
Messages: 19
Registered: October 2016
Junior Member
Hi thank you so much. That was what i was trying to figure out. But i didnt know about the dispatch mechanism. Ill give it a try!
Re: Generate matlab script form arithmetic expression [message #1746588 is a reply to message #1746398] Tue, 01 November 2016 13:55 Go to previous message
Lukas Schaus is currently offline Lukas SchausFriend
Messages: 19
Registered: October 2016
Junior Member
That was indeed exactly what I needed. Thank you for your help and the good annotations
Previous Topic:Max line length Formatting
Next Topic:Error when running MWE2 file
Goto Forum:
  


Current Time: Mon Jul 24 13:05:38 GMT 2017

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

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