All Downloads are FREE. Search and download functionalities are using the official Maven repository.

cz.cuni.mff.d3s.spl.formula.FormulaParser.jj Maven / Gradle / Ivy

Go to download

Stochastice Performance Logic is a formalism for capturing performance assumptions. It is, for example, possible to capture assumption that newer version of a function bar is faster than the previous version or that library foobar is faster than library barfoo when rendering antialiased text. The purpose of this framework is to allow evaluation of SPL formulas inside Java applications.

There is a newer version: 1.0.4
Show newest version
/*
 * Copyright 2015 Charles University in Prague
 * Copyright 2015 Vojtech Horky
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
options {
	STATIC = false;
	SUPPORT_CLASS_VISIBILITY_PUBLIC = false;
}

PARSER_BEGIN(FormulaParser)
package cz.cuni.mff.d3s.spl.formula;

import java.util.*;

import cz.cuni.mff.d3s.spl.formula.Formula;
import cz.cuni.mff.d3s.spl.interpretation.KindergartenInterpretation;
import cz.cuni.mff.d3s.spl.Result;

class FormulaParser {
	public static Formula parse(String formula) throws FormulaParsingException {
		try {
			FormulaParser parser = new FormulaParser(formula);
			Formula parsedFormula = parser.splFormula();
			parsedFormula.setInterpretation(Defaults.getInterpretation());
			return parsedFormula;
		} catch (TokenMgrError e) {
			throw new FormulaParsingException(e, formula);
		} catch (ParseException e) {
			throw new FormulaParsingException(e, formula);
		}
	}
	
	public FormulaParser(String formula) {
		this(new java.io.BufferedReader(new java.io.StringReader(formula)));
	}
}

PARSER_END(FormulaParser)

SKIP :
{
	< " " >
	| < "\t" >
	| < "\n" >
	| < "\r" >
}

TOKEN : {
	
	| 
	| 
	| 
	| ">
	| 
	| 
	| 
	| ">
	| 
	| 
}

Formula splFormula():
{
	Formula tmp;
}
{
	tmp = logicFormulaImply() {
		return tmp;
	}
}

Formula logicFormulaImply():
{
	List parts = new LinkedList();
	Formula tmp;
}
{
	tmp = logicFormulaOr() { parts.add(tmp); } (  tmp = logicFormulaOr() { parts.add(tmp); } )* {
		Formula result = parts.get(0); 
		for (int i = 1; i < parts.size(); i++) {
			result = new LogicImply(result, parts.get(i));
		}
		return result;
	}
}

Formula logicFormulaOr():
{
	List parts = new LinkedList();
	Formula tmp = null;
}
{
	tmp = logicFormulaAnd() { parts.add(tmp); } (  tmp = logicFormulaAnd() { parts.add(tmp); } )* {
		Formula result = parts.get(0); 
		for (int i = 1; i < parts.size(); i++) {
			result = new LogicOr(result, parts.get(i));
		}
		return result;
	}
}

Formula logicFormulaAnd():
{
	List parts = new LinkedList();
	Formula tmp;
}
{
	tmp = logicFormulaAtomic() { parts.add(tmp); } (  tmp = logicFormulaAtomic() { parts.add(tmp); } )* {
		Formula result = parts.get(0); 
		for (int i = 1; i < parts.size(); i++) {
			result = new LogicAnd(result, parts.get(i));
		}
		return result;
	}
}

Formula logicFormulaAtomic():
{
	Formula atomic;
}
{
	atomic = plainComparison() {
		return atomic;
	}
	|  atomic = splFormula()  {
		return atomic;
	}
}

Formula plainComparison():
{
	String leftName;
	double leftConst;
	String rightName;
	double rightConst;
	Comparison.Operator op;
}
{
	LOOKAHEAD(3)
	leftName = sourceName() op = comparisonOperator() rightName = sourceName() {
		return new Comparison(leftName, rightName, op);
	}
	| leftConst = constantSource() op = inverseComparisonOperator() rightName = sourceName() {
		return new ComparisonWithConst(rightName, leftConst, op);
	}
	| leftName = sourceName() op = comparisonOperator() rightConst = constantSource() {
		return new ComparisonWithConst(leftName, rightConst, op);
	}
}

String sourceName():
{
	Token name;
}
{
	name =  {
		return name.image;
	}
}

double constantSource():
{
	Token t;
}
{
	t =  {
		return Integer.valueOf(t.image);
	}
	| t =  {
		return Double.valueOf(t.image);
	}
	| t =  {
		return Double.valueOf("0" + t.image);
	}
}

Comparison.Operator comparisonOperator():
{}
{
	 {
		return Comparison.Operator.GT;
	}
	|  {
		return Comparison.Operator.LT;
	}
}

Comparison.Operator inverseComparisonOperator():
{}
{
	 {
		return Comparison.Operator.GT;
	}
	|  {
		return Comparison.Operator.LT;
	}
}






© 2015 - 2024 Weber Informatics LLC | Privacy Policy