Ejemplo n.º 1
0
// IfThenElse -> Condition (? Condition : Condition)?
Expression::Node *Expression::parseIfThenElse() {
    //printf("parsing if then else\n");
    Node *result = parseCondition();

    if (consume("?")) {
        Node *then = parseCondition();
        assert(consume(":"), "If Then Else missing else case\n");
        result = new IfThenElse(result, then, parseCondition());
    }

    return result;
}
Ejemplo n.º 2
0
void Condition::parseCondition()
{
  curToken=t.getToken();
  if(curToken == 20) {
    altNum=1;
    comp.parseCompare();
  }
  else if(curToken == 15) {
    altNum=2;
    t.skipToken();
    parseCondition();
  }
  else if(curToken == 16) {
    t.skipToken();
    parseCondition();
    curToken=t.getToken();
    if(curToken == 18){
      altNum=3;
    }
    else if(curToken == 19){
      altNum=4;
    }
    else{
      cout<<"Error-expecting && or ||"<<endl;
      exit(0);
    }
    t.skipToken();
    parseCondition();
    curToken=t.getToken();
    if(curToken == 17) {
      t.skipToken();
    }
    else {
      cout<<"Error-expecting ]"<<endl;
      exit(0);
    }
  } 
  else {
    cout<<"Error. Exiting"<<endl;
    exit(0);
  }
}
Ejemplo n.º 3
0
void KConfigPropagator::readKcfgFile()
{
  QString filename = locate( "kcfg", mKcfgFile );
  if ( filename.isEmpty() ) {
    kdError() << "Unable to find kcfg file '" << mKcfgFile << "'" << endl;
    return;
  }

  QFile input( filename );
  QDomDocument doc;
  QString errorMsg;
  int errorRow;
  int errorCol;
  if ( !doc.setContent( &input, &errorMsg, &errorRow, &errorCol ) ) {
    kdError() << "Parse error in " << mKcfgFile << ", line " << errorRow << ", col " << errorCol << ": " << errorMsg << endl;
    return;
  }

  QDomElement cfgElement = doc.documentElement();

  if ( cfgElement.isNull() ) {
    kdError() << "No document in kcfg file" << endl;
    return;
  }

  mRules.clear();

  QDomNode n;
  for ( n = cfgElement.firstChild(); !n.isNull(); n = n.nextSibling() ) {
    QDomElement e = n.toElement();

    QString tag = e.tagName();

    if ( tag == "propagation" ) {
      Rule rule = parsePropagation( e );
      mRules.append( rule );
    } else if ( tag == "condition" ) {
      Condition condition = parseCondition( e );
      QDomNode n2;
      for( n2 = e.firstChild(); !n2.isNull(); n2 = n2.nextSibling() ) {
        QDomElement e2 = n2.toElement();
        if ( e2.tagName() == "propagation" ) {
          Rule rule = parsePropagation( e2 );
          rule.condition = condition;
          mRules.append( rule );
        } else {
          kdError() << "Unknow tag: " << e2.tagName() << endl;
        }
      }
    }
  }
}
Ejemplo n.º 4
0
Liquid::Condition Liquid::IfTag::parseLogicalCondition(Parser& parser)
{
    Condition cond = parseCondition(parser);
    Condition::LogicalOperator logicalOp = Condition::LogicalOperator::None;
    if (parser.consumeId("and")) {
        logicalOp = Condition::LogicalOperator::And;
    } else if (parser.consumeId("or")) {
        logicalOp = Condition::LogicalOperator::Or;
    }
    if (logicalOp != Condition::LogicalOperator::None) {
        cond.setLogicalCondition(logicalOp, std::make_shared<Condition>(parseLogicalCondition(parser)));
    }
    return cond;
}
Ejemplo n.º 5
0
PARSENODE_PTR SQLParser::parseDelete() {
    if (!startsDelete(nowReading)) {
        syntaxError(nowReading, "expect delete!");
        return nullptr;
    }
    PARSENODE_PTR deleteNode = PARSENODE_PTR(new ParseNode(DELETE));
    readToken();
    expect(FROM);
    deleteNode->children.push_back(parseIdentifier());
    if (nowReading == WHERE) {
        readToken();
        deleteNode->children.push_back(parseCondition());
    }
    expect(TERMINATOR);
    return deleteNode;
}
Ejemplo n.º 6
0
PARSENODE_PTR SQLParser::parseSelect() {
    if (!startsSelect(nowReading)) {
        syntaxError(nowReading, "expect select token!");
        return nullptr;
    }
    PARSENODE_PTR selectNode = PARSENODE_PTR(new ParseNode(SELECT));
    readToken();
    expect(STAR);
    expect(FROM);
    selectNode->children.push_back(parseIdentifier());
    if (nowReading == WHERE) {
        readToken();
        selectNode->children.push_back(parseCondition());
    }
    expect(TERMINATOR);
    return selectNode;
}
Ejemplo n.º 7
0
void SVGSMILElement::parseBeginOrEnd(const String& parseString, BeginOrEnd beginOrEnd)
{
    Vector<SMILTimeWithOrigin>& timeList = beginOrEnd == Begin ? m_beginTimes : m_endTimes;
    if (beginOrEnd == End)
        m_hasEndEventConditions = false;
    HashSet<double> existing;
    for (unsigned n = 0; n < timeList.size(); ++n)
        existing.add(timeList[n].time().value());
    Vector<String> splitString;
    parseString.split(';', splitString);
    for (unsigned n = 0; n < splitString.size(); ++n) {
        SMILTime value = parseClockValue(splitString[n]);
        if (value.isUnresolved())
            parseCondition(splitString[n], beginOrEnd);
        else if (!existing.contains(value.value()))
            timeList.append(SMILTimeWithOrigin(value, SMILTimeWithOrigin::ParserOrigin));
    }
    sortTimeList(timeList);
}
Ejemplo n.º 8
0
static CondResult parseConditionString (const Key * meta, const Key * suffixList, Key * parentKey, Key * key, KeySet * ks, Operation op)
{
	const char * conditionString = keyString (meta);
	const char * regexString1 = "(\\(((.*)?)\\))[[:space:]]*\\?";
	const char * regexString2 = "\\?[[:space:]]*(\\(((.*)?)\\))";
	const char * regexString3 = "[[:space:]]*:[[:space:]]*(\\(((.*)?)\\))";
	regex_t regex1, regex2, regex3;
	CondResult ret;
	if ((ret = regcomp (&regex1, regexString1, REGEX_FLAGS_CONDITION)))
	{
		ELEKTRA_SET_ERROR (87, parentKey, "Couldn't compile regex: most likely out of memory"); // the regex compiles so the only
		// possible error would be out of
		// memory
		ksDel (ks);
		return ERROR;
	}
	if ((ret = regcomp (&regex2, regexString2, REGEX_FLAGS_CONDITION)))
	{
		ELEKTRA_SET_ERROR (87, parentKey, "Couldn't compile regex: most likely out of memory"); // the regex compiles so the only
		// possible error would be out of
		// memory
		regfree (&regex1);
		ksDel (ks);
		return ERROR;
	}
	if ((ret = regcomp (&regex3, regexString3, REGEX_FLAGS_CONDITION)))
	{
		ELEKTRA_SET_ERROR (87, parentKey, "Couldn't compile regex: most likely out of memory"); // the regex compiles so the only
		// possible error would be out of
		// memory
		regfree (&regex1);
		regfree (&regex2);
		ksDel (ks);
		return ERROR;
	}
	int subMatches = 6;
	regmatch_t m[subMatches];
	int nomatch = regexec (&regex1, conditionString, subMatches, m, 0);
	if (nomatch)
	{
		ELEKTRA_SET_ERRORF (134, parentKey, "Invalid syntax: \"%s\". Check kdb info conditionals for additional information",
				    conditionString);
		regfree (&regex1);
		regfree (&regex2);
		regfree (&regex3);
		ksDel (ks);
		return ERROR;
	}
	if (m[1].rm_so == -1)
	{
		ELEKTRA_SET_ERRORF (134, parentKey, "Invalid syntax: \"%s\". Check kdb info conditionals for additional information",
				    conditionString);
		regfree (&regex1);
		regfree (&regex2);
		regfree (&regex3);
		ksDel (ks);
		return ERROR;
	}
	int startPos = m[1].rm_so;
	int endPos = m[1].rm_eo;
	char * condition = elektraMalloc (endPos - startPos + 1);
	char * thenexpr = NULL;
	char * elseexpr = NULL;
	strncpy (condition, conditionString + startPos, endPos - startPos);
	condition[endPos - startPos] = '\0';
	nomatch = regexec (&regex2, conditionString, subMatches, m, 0);
	if (nomatch)
	{
		ELEKTRA_SET_ERRORF (134, parentKey, "Invalid syntax: \"%s\". Check kdb info conditionals for additional information",
				    conditionString);
		regfree (&regex1);
		regfree (&regex2);
		regfree (&regex3);
		ksDel (ks);
		return ERROR;
	}
	if (m[1].rm_so == -1)
	{
		ELEKTRA_SET_ERRORF (134, parentKey, "Invalid syntax: \"%s\". Check kdb info conditionals for additional information",
				    conditionString);
		regfree (&regex1);
		regfree (&regex2);
		regfree (&regex3);
		ksDel (ks);
		return ERROR;
	}

	startPos = m[1].rm_so;
	endPos = m[1].rm_eo;
	thenexpr = elektraMalloc (endPos - startPos + 1);
	strncpy (thenexpr, conditionString + startPos, endPos - startPos);
	thenexpr[endPos - startPos] = '\0';

	nomatch = regexec (&regex3, conditionString, subMatches, m, 0);
	if (!nomatch)
	{
		if (m[1].rm_so == -1)
		{
			ELEKTRA_SET_ERRORF (134, parentKey,
					    "Invalid syntax: \"%s\". Check kdb info conditionals for additional information",
					    conditionString);
			regfree (&regex1);
			regfree (&regex2);
			regfree (&regex3);
			ksDel (ks);
			return ERROR;
		}
		thenexpr[strlen (thenexpr) - ((m[0].rm_eo - m[0].rm_so))] = '\0';
		startPos = m[1].rm_so;
		endPos = m[1].rm_eo;
		elseexpr = elektraMalloc (endPos - startPos + 1);
		strncpy (elseexpr, conditionString + startPos, endPos - startPos);
		elseexpr[endPos - startPos] = '\0';
	}

	ret = parseCondition (key, condition, suffixList, ks, parentKey);
	if (ret == TRUE)
	{
		if (op == ASSIGN)
		{
			const char * assign = isAssign (key, thenexpr, parentKey, ks);
			if (assign != NULL)
			{
				keySetString (key, assign);
				ret = TRUE;
				goto CleanUp;
			}
			else
			{
				ret = ERROR;
				goto CleanUp;
			}
		}
		else
		{
			ret = parseCondition (key, thenexpr, suffixList, ks, parentKey);
			if (ret == FALSE)
			{
				ELEKTRA_SET_ERRORF (135, parentKey, "Validation of Key %s: %s failed. (%s failed)",
						    keyName (key) + strlen (keyName (parentKey)) + 1, conditionString, thenexpr);
			}
			else if (ret == ERROR)
			{
				ELEKTRA_SET_ERRORF (134, parentKey,
						    "Invalid syntax: \"%s\". Check kdb info conditionals for additional information",
						    thenexpr);
			}
		}
	}
	else if (ret == FALSE)
	{
		if (elseexpr)
		{
			if (op == ASSIGN)
			{
				const char * assign = isAssign (key, elseexpr, parentKey, ks);
				if (assign != NULL)
				{
					keySetString (key, assign);
					ret = TRUE;
					goto CleanUp;
				}
				else
				{
					ret = ERROR;
					goto CleanUp;
				}
			}
			else
			{
				ret = parseCondition (key, elseexpr, suffixList, ks, parentKey);

				if (ret == FALSE)
				{
					ELEKTRA_SET_ERRORF (135, parentKey, "Validation of Key %s: %s failed. (%s failed)",
							    keyName (key) + strlen (keyName (parentKey)) + 1, conditionString, elseexpr);
				}
				else if (ret == ERROR)
				{
					ELEKTRA_SET_ERRORF (
						134, parentKey,
						"Invalid syntax: \"%s\". Check kdb info conditionals for additional information", elseexpr);
				}
			}
		}
		else
		{
			ret = NOEXPR;
		}
	}
	else if (ret == ERROR)
	{
		ELEKTRA_SET_ERRORF (134, parentKey, "Invalid syntax: \"%s\". Check kdb info conditionals for additional information",
				    condition);
	}

CleanUp:
	elektraFree (condition);
	elektraFree (thenexpr);
	if (elseexpr) elektraFree (elseexpr);
	regfree (&regex1);
	regfree (&regex2);
	regfree (&regex3);
	ksDel (ks);
	return ret;
}
Ejemplo n.º 9
0
	/**
	 * parse a condition recursively.
	 */
	PDDLGoalDescription DomainParser::parseCondition(const VAL::goal* goal) {
		
		// simple proposition (base case 1)
		const VAL::simple_goal* sg = dynamic_cast<const VAL::simple_goal*>(goal);
		if (sg) {
			const VAL::proposition* valprop = sg->getProp();
			PDDLAtomicFormula prop;
			prop.name = valprop->head->symbol::getName();
	        for (VAL::parameter_symbol_list::const_iterator ci = valprop->args->begin(); ci != valprop->args->end(); ci++) {
				const VAL::parameter_symbol* param = *ci;
				PDDLTypedSymbol sym;
				sym.type = param->type->getName();
				sym.name = param->pddl_typed_symbol::getName();
				prop.vars.push_back(sym);
			}
			PDDLGDAtomic pgda(prop);
			return pgda;
		}

		// function inequality (base case 2)
		const VAL::comparison* co = dynamic_cast<const VAL::comparison*>(goal);
		if (co) {
			PDDLFunctionInequality bin_expression = EQ;
			switch(co->getOp()) {
				case VAL::E_GREATER: bin_expression = GT; break;
				case VAL::E_GREATEQ: bin_expression = EQGT; break;
				case VAL::E_LESS: bin_expression = LT; break;
				case VAL::E_LESSEQ: bin_expression = EQLT; break;
			};
			PDDLFunction lhs = parseExpression(co->getLHS());
			PDDLFunction rhs = parseExpression(co->getRHS());
			PDDLGDFunction pgdf(lhs, rhs, bin_expression);
			return pgdf;
		}

		// negative condition
		const VAL::neg_goal* ng = dynamic_cast<const VAL::neg_goal*>(goal);
		if (ng) {
			PDDLGDConjunction pgda(NOT);
			PDDLGoalDescription inner = parseCondition(ng->getGoal());
			pgda.goal_conditions.push_back(inner);
			return pgda;
		}

		// conjunctive condition
		const VAL::conj_goal* cg = dynamic_cast<const VAL::conj_goal*>(goal);
		if (cg) {
			PDDLGDConjunction pgda(AND);
	        const VAL::goal_list* goals = cg->getGoals();
	        for (VAL::goal_list::const_iterator ci = goals->begin(); ci != goals->end(); ci++) {
				PDDLGoalDescription inner = parseCondition((*ci));
				pgda.goal_conditions.push_back(inner);
			}
			return pgda;
		}

		// disjunctive condition
		const VAL::disj_goal* dg = dynamic_cast<const VAL::disj_goal*>(goal);
		if (dg) {
			PDDLGDConjunction pgda(OR);
	        const VAL::goal_list* goals = dg->getGoals();
	        for (VAL::goal_list::const_iterator ci = goals->begin(); ci != goals->end(); ci++) {
				PDDLGoalDescription inner = parseCondition((*ci));
				pgda.goal_conditions.push_back(inner);
			}
			return pgda;
		}

		// timed condition
		const VAL::timed_goal* tg = dynamic_cast<const VAL::timed_goal*>(goal);
		if (tg) {
			PDDLTimeSpecifier time_spec = START;
			switch(tg->getTime()) {
				case VAL::E_AT_END: time_spec = END; break;
				case VAL::E_OVER_ALL: time_spec = ALL; break;
			};
			PDDLGDTimed pgdt(time_spec);
			PDDLGoalDescription inner = parseCondition(tg->getGoal());
			pgdt.goal_conditions.push_back(inner);
			return pgdt;
		}
	}
Ejemplo n.º 10
0
	/**
	 * parse the domain file
	 */
	void DomainParser::parseDomain(const std::string domainPath) {
		
		// only parse domain once
		if(domain_parsed) return;
		domain_parsed = true;

		std::string domainFileName = (domainPath);
		ROS_INFO("KCL: (KB) Parsing domain: %s.", domainFileName.c_str());

		// save filename for VAL
		std::vector<char> writable(domainFileName.begin(), domainFileName.end());
		writable.push_back('\0');
		current_filename = &writable[0];

		// parse domain
		VAL::current_analysis = &VAL::an_analysis;
		std::ifstream domainFile;
		domainFile.open(domainFileName.c_str());
		yydebug = 0;

		VAL::yfl = new yyFlexLexer;

		if (domainFile.bad()) {
			ROS_ERROR("KCL: (KB) Failed to open domain file.");
			line_no = 0;
			VAL::log_error(VAL::E_FATAL,"Failed to open file");
		} else {
			line_no = 1;
			VAL::yfl->switch_streams(&domainFile, &std::cout);
			yyparse();

			// domain name
			VAL::domain* domain = VAL::current_analysis->the_domain;
			domain_name = domain->name;

			// types
			VAL::pddl_type_list* types = domain->types;
			for (VAL::pddl_type_list::const_iterator ci = types->begin(); ci != types->end(); ci++) {
				const VAL::pddl_type* type = *ci;
				domain_types.push_back(type->getName());
			}

			// predicates
			VAL::pred_decl_list* predicates = domain->predicates;
			if(predicates) {
				for (VAL::pred_decl_list::const_iterator ci = predicates->begin(); ci != predicates->end(); ci++) {
					const VAL::pred_decl* predicate = *ci;

					// predicate name
					PDDLAtomicFormula pred;
					pred.name = predicate->getPred()->symbol::getName();
					domain_predicates[pred.name] = pred;

					// predicate variables
					for (VAL::var_symbol_list::const_iterator vi = predicate->getArgs()->begin(); vi != predicate->getArgs()->end(); vi++) {

						const VAL::var_symbol* var = *vi;
						PDDLTypedSymbol sym;
						sym.type = var->type->getName();
						sym.name = var->pddl_typed_symbol::getName();
						domain_predicates[pred.name].vars.push_back(sym);
					}
				}
			}

			// functions
			VAL::func_decl_list* functions = domain->functions;
			if(functions) {
				for (VAL::func_decl_list::const_iterator ci = functions->begin(); ci != functions->end(); ci++) {
					const VAL::func_decl* function = *ci;
					// function name
					PDDLAtomicFormula func;
					func.name = function->getFunction()->symbol::getName();
					domain_functions[func.name] = func;

					// parameters
					for (VAL::var_symbol_list::const_iterator vi = function->getArgs()->begin(); vi != function->getArgs()->end(); vi++) {
						const VAL::var_symbol* var = *vi;
						PDDLTypedSymbol sym;
						sym.type = var->type->getName();
						sym.name = var->pddl_typed_symbol::getName();
						domain_functions[func.name].vars.push_back(sym);
					}
				}
			}

			// operators
			VAL::operator_list* operators = domain->ops;
			for (VAL::operator_list::const_iterator ci = operators->begin(); ci != operators->end(); ci++) {			
				const VAL::operator_* op = *ci;

				// name
				std::string name = op->name->symbol::getName();

				// condition
				const VAL::goal* precondition = op->precondition;
				PDDLGoalDescription c = parseCondition(precondition);

				// duration
				PDDLFunctionNumber n(0);
				PDDLDuration d(n, EQ);
				const VAL::durative_action* da = dynamic_cast<const VAL::durative_action*>(op);
				if (da) {
					// TODO get the real duration from VAL
					PDDLFunctionNumber n(1);
					d = PDDLDuration(n, EQ);
				}

				// operator
				PDDLOperator oper(d, c);
				oper.name = name;

				// parameters
				for (VAL::var_symbol_list::const_iterator vi = op->parameters->begin(); vi != op->parameters->end(); vi++) {
					const VAL::var_symbol* var = *vi;
					PDDLTypedSymbol sym;
					sym.type = var->type->getName();
					sym.name = var->pddl_typed_symbol::getName();
					oper.parameters.push_back(sym);
				}

				domain_operators.insert( std::pair<std::string,PDDLOperator>(name, oper) );
			}
		}
		domainFile.close();
		delete VAL::yfl;
	}