Expression *parseTerm(FILE *source, Expression *lvalue) { Token token = scanner(source); Expression *expr; int i ; switch (token.type){ case PlusOp: case MinusOp: case Alphabet: case PrintOp: for (i = strlen(token.tok) - 1; i >= 0; i--) { ungetc(token.tok[i], source); } return lvalue; case MulOp: expr = (Expression *)malloc( sizeof(Expression) ); (expr->v).type = MulNode; (expr->v).val.op = Mul; expr->leftOperand = lvalue; expr->rightOperand = parseValue(source); return parseTerm(source, expr); case DivOp: expr = (Expression *)malloc( sizeof(Expression) ); (expr->v).type = DivNode; (expr->v).val.op = Div; expr->leftOperand = lvalue; expr->rightOperand = parseValue(source); return parseTerm(source, expr); default: printf("In function parseTerm\n"); printf("Syntax Error: Expect a numeric value or an identifier %s\n", token.tok); exit(1); } }
//------------------------------------------------------------------------------------------------------ PolynomialItem* parseExpression(const QString& a_Eqn, int& a_Pos, QQuickItem* a_Parent) // a_Pos is usually 0, but can be bigger in the case of complex expressions (with nested parentheses) { qInfo() << "Parsing expression"; PolynomialItem* exprItem = new PolynomialItem(a_Parent); exprItem->setExprType(QString("Expression")); exprItem->setZ(-1); exprItem->setBorderColor(QColor("blue")); exprItem->setOp('+'); // the first item has default operation + (any '-' sign should be read by the parseFactor method and would then update the EquationItem accordingly) PolynomialItem* termItem(parseTerm(a_Eqn, a_Pos, exprItem)); termItem->setOp('+'); while(a_Pos < a_Eqn.size() && a_Eqn[a_Pos] != QChar::Null) { QChar op(a_Eqn[a_Pos]); qInfo() << "parseExpression::Operator " << op.toLatin1(); if(op != '+' && op != '-') { return exprItem; } ++a_Pos; termItem = parseTerm(a_Eqn, a_Pos, exprItem); termItem->setOp(op); } return exprItem; }
shared_ptr<Expression> Parser::parseExpression1() { Tag tags[] = { EQ, UNEQ, LT, GT, LE, GE }; auto isRealOp = [&](Tag t){ for (auto i : tags) { if (t == i) { return true; } } return false; }; auto rootExp = parseTerm(); it++; while (it->tag == ADD || it->tag == SUB || isRealOp(it->tag)) { auto opExp = make_shared<Expression>(); opExp->addNode(rootExp); opExp->value = *it; it++; opExp->addNode(parseTerm()); rootExp = opExp; it++; } if (!(it->tag == ADD || it->tag == SUB || isRealOp(it->tag))) { it--; } return rootExp; }
// Factor -> Term ^ Term | Term Expression::Node *Expression::parseFactor() { //printf("parsing factor\n"); Node *result = parseTerm(); if (consume("^")) { result = new Power(result, parseTerm()); } //printf("done parsing factor\n"); return result; }
Expression parseConjunction() { Expression e0; e0 = parseTerm(); if (curchar == '*') { Expression e1; match("*"); e1 = parseTerm(); return makeBinaryExpr(AND, e0, e1); } return e0; }
NodePtr parseRE() { NodePtr ret = parseTerm(); if (reader.peek() == '|') { while (reader.peek() == '|') { reader.next(); ret = std::make_shared<AlternateNode>(ret, parseTerm()); } } return ret; }
void FllImporter::processOutputVariable(const std::string& block, Engine* engine) const { std::istringstream reader(block); std::string line; OutputVariable* outputVariable = new OutputVariable; engine->addOutputVariable(outputVariable); while (std::getline(reader, line)) { std::pair<std::string, std::string> keyValue = parseKeyValue(line, ':'); if ("OutputVariable" == keyValue.first) { outputVariable->setName(keyValue.second); } else if ("enabled" == keyValue.first) { outputVariable->setEnabled(parseBoolean(keyValue.second)); } else if ("range" == keyValue.first) { std::pair<scalar, scalar> range = parseRange(keyValue.second); outputVariable->setRange(range.first, range.second); } else if ("default" == keyValue.first) { outputVariable->setDefaultValue(Op::toScalar(keyValue.second)); } else if ("lock-valid" == keyValue.first) { outputVariable->setLockValidOutput(parseBoolean(keyValue.second)); } else if ("lock-range" == keyValue.first) { outputVariable->setLockOutputRange(parseBoolean(keyValue.second)); } else if ("defuzzifier" == keyValue.first) { outputVariable->setDefuzzifier(parseDefuzzifier(keyValue.second)); } else if ("accumulation" == keyValue.first) { outputVariable->fuzzyOutput()->setAccumulation(parseSNorm(keyValue.second)); } else if ("term" == keyValue.first) { outputVariable->addTerm(parseTerm(keyValue.second, engine)); } else { throw fl::Exception("[import error] key <" + keyValue.first + "> not " "recognized in pair <" + keyValue.first + ":" + keyValue.second + ">", FL_AT); } } }
ExpressionPtr Parser::parseSimpleExpression() { AddOpExpressionPtr expr(new AddOpExpression); Token sign = m_curToken; if (sign.operatorType() != OperatorType::Or && match(TokenType::AddOp)) { if (sign.operatorType() == OperatorType::Subtract) { expr->lhsNegated = true; } else { expr->lhsNegated = false; } } expr->lhs = parseTerm(); if (m_errorCode > ErrorCodes::NoError) { panic(simpleExpressionFollow, simpleExpressionFollowSize); return ExpressionPtr(); } parseSimpleExpression_r(expr); if (m_errorCode > ErrorCodes::NoError) { panic(simpleExpressionFollow, simpleExpressionFollowSize); return ExpressionPtr(); } return expr; }
Statement parseStatement( FILE *source, Token token ) { Token next_token; Expression *value, *term,*expr; switch(token.type){ case Alphabet: next_token = scanner(source); if(next_token.type == AssignmentOp){ value = parseValue(source); term = parseTerm(source, value); expr = parseExpression(source, term); return makeAssignmentNode(token.tok, value, expr); } else{ printf("Syntax Error: Expect an assignment op %s\n", next_token.tok); exit(1); } case PrintOp: next_token = scanner(source); if(next_token.type == Alphabet) return makePrintNode(next_token.tok); else{ printf("Syntax Error: Expect an identifier %s\n", next_token.tok); exit(1); } break; default: printf("Syntax Error: Expect a statement %s\n", token.tok); exit(1); } }
Expression *parsePreTerm(FILE *source) { Expression *v = parseValue(source); Expression *term = parseTerm(source, v); return term; }
Expr* Parser::parseSimpleExpr() { Expr* term = parseTerm(); Expr* node = parseSimpleExprRest( term, term->line, term->column ); return node; }
void Parser::parseTerm_r(TermPtr term) { Token op = m_curToken; if (match(TokenType::MulOp)) { term->mulOp = op.operatorType(); term->rhs = parseTerm(); } }
// As Any keyword in vb6 ?? // TODO2 namenskollision mit klassenname/-modul und variablenname verhindern, klassenname gewinnt // auch [] ohne index als array deklaration erlauben // TODO3 [VB] WithEvents // common // shared, static bool parser::fDIM(pcode *p) /* [QBASIC] - DIM [SHARED] give you access to variable outside of current function. Is implicitly supported, [VB] [*] [*] */ { _BEGIN_ bool bTerm_PRIVATE = false; bool bTerm_PUBLIC = false; bool bTerm_GLOBAL = false; bool bTerm_STATIC = false; bool bTerm_COMMON = false; bool bTerm_SHARED = false; bool bTerm_DIM = false; bool bTerm_REDIM = false; #ifdef KBDEBUG if (stopAtLine(13)){ int i = 9; i++; } #endif // TODO3 blockscope, dim muss aktuelle rekursiontiefe mit ablegen, auf interpreterseite wird dann wie sub ein blockscope gehandelt // aber nur wenn option kbasic { if (!(bTerm_DIM = parseTerm(token__DIM))){ if (isVERYOLDBASIC()){ if ((bTerm_COMMON = parseTerm(token__COMMON))); } if ((isVERYOLDBASIC() || isOLDBASIC()) && !isScopeLocal()){ if ((bTerm_GLOBAL = parseTerm(token__GLOBAL))); } if (isVERYOLDBASIC()){ if ((bTerm_SHARED = parseTerm(token__SHARED))); } if ((bTerm_STATIC = parseTerm(token__STATIC))){ if (isScopeLocal() == false && !isScopeClass()){ semanticError(token__STATIC, "STATIC not allowed outside sub/function/method"); _ERROR_ } } if (bTerm_COMMON || bTerm_SHARED || bTerm_STATIC || bTerm_GLOBAL){ // TODO2 common, shared, global bedeutung berprfen bTerm_DIM = true; } } else { if (isVERYOLDBASIC()){ if ((bTerm_SHARED = parseTerm(token__SHARED))); }
// Expr → Term Expr' bool AnimExpression::parseExpr(const QString& str, QString::const_iterator& iter) { if (!parseTerm(str, iter)) { return false; } if (!parseExprPrime(str, iter)) { return false; } return true; }
bool parser::fENUM(pcode *p, int *nEnumId, int *nElement) { // special keywords must be handled here to prevent using keywords as variables switch(peekToken()){ case token__END: case token__CLASS: case token__MODULE: return false; } _BEGIN_ bool bTerm_DOT = false; bool bTerm_ME = false; bool bTerm_PARENT = false; /* modul global: modulname or direct class static global: classname or direct */ /* interpreter's view: local vars (sub/function/method) global vars (module,class static, class instance) --> selector wird benötigt */ #ifdef KBDEBUG if (stopAtLine(163)){ int i = 9; i++; } #endif if (bTerm_ME = parseTerm(token__ME)){ bTerm_DOT = parseTerm(token__SYM_DOT); } else if (bTerm_PARENT = parseTerm(token__PARENT)){ bTerm_DOT = parseTerm(token__SYM_DOT); } if (bTerm_ME || bTerm_PARENT){ if (!isScopeClassLocalInstance()){ semanticError(token__MSC_IDENTIFIER, "ME or PARENT not allowed outside class instance scope" ); _ERROR_ } }
Token* RegxParser::parseRegx(const bool matchingRParen) { Token* tok = parseTerm(matchingRParen); Token* parentTok = 0; while (fState == REGX_T_OR) { processNext(); if (parentTok == 0) { parentTok = fTokenFactory->createUnion(); parentTok->addChild(tok, fTokenFactory); tok = parentTok; } tok->addChild(parseTerm(matchingRParen), fTokenFactory); } return tok; }
/*! * expression ::= term ( '|' term )* ( '|' | ) */ Node* Parser::parseExpression() { std::vector<Node*> v; v.push_back(parseTerm()); while (token_ == Lexer::Token::bar) { consumeToken(); if ((token_ == Lexer::Token::identifier && peek_token_ != Lexer::Token::coloncolonequal && peek_token_ != Lexer::Token::minusgreater) || token_ == Lexer::Token::text || token_ == Lexer::Token::l_paren) { v.push_back(parseTerm()); } else{ v.push_back(new EmptyNode()); break; } } return new OrNode(v); }
// <sum>::= <weightedterm> | <weightedterm> <sum> // <weightedterm>::= <integer> <oneOrMoreSpace> <term> <oneOrMoreSpace> void OPBParser::parseSum() { active_.lits.clear(); while (!match(*input(), ';', true)) { int coeff = input()->parseInt(INT_MIN + 1, INT_MAX, "Coefficient expected!"); parseTerm(); Literal x = term_.size() == 1 ? term_[0] : builder_->addProduct(term_); active_.lits.push_back(WeightLiteral(x, coeff)); if (**input() == '>' || **input() == '=') break; input()->skipWhite(); } input()->skipWhite(); }
Expr* Parser::parseSimpleExprRest( Expr* e, int lin, int col ) { if ( check( ADDITION_ST ) || check( SUBTRACT_ST ) || check( OR_ST ) ) { Op2 addOp = parseAddOp(); Expr* term = parseTerm(); BinOp* binOp = new BinOp( e, addOp, term, lin, col ); Expr* node = parseSimpleExprRest( binOp, binOp->line, binOp->column ); return node; } else return e; }
void Translator::match(Plasma::RunnerContext &context) { const QString term = context.query(); QString text; QPair<QString, QString> language; if (!parseTerm(term, text, language)) return; if (!context.isValid()) return; if (text.contains(" ")) { if(m_yandexPhrase) { QEventLoop loop; Yandex yandex(this, context, text, language, m_yandexKey); connect(&yandex, SIGNAL(finished()), &loop, SLOT(quit())); loop.exec(); } if(m_glosbePhrase) { if(m_glosbeExamples) { QEventLoop loop; Glosbe glosbe(this, context, text, language, m_glosbeExamples); connect(&glosbe, SIGNAL(finished()), &loop, SLOT(quit())); loop.exec(); } QEventLoop loop; Glosbe glosbe(this, context, text, language); connect(&glosbe, SIGNAL(finished()), &loop, SLOT(quit())); loop.exec(); } } else { if(m_yandexWord) { QEventLoop loop; Yandex yandex(this, context, text, language, m_yandexKey); connect(&yandex, SIGNAL(finished()), &loop, SLOT(quit())); loop.exec(); } if(m_glosbeWord) { if(m_glosbeExamples) { QEventLoop loop; Glosbe glosbe(this, context, text, language, m_glosbeExamples); connect(&glosbe, SIGNAL(finished()), &loop, SLOT(quit())); loop.exec(); } QEventLoop loop; Glosbe glosbe(this, context, text, language); connect(&glosbe, SIGNAL(finished()), &loop, SLOT(quit())); loop.exec(); } } }
Expression *parseExpression( FILE *source, Expression *lvalue ) { Token token = scanner(source); int i; Expression *expr; Expression *value; switch(token.type){ case PlusOp: expr = (Expression *)malloc( sizeof(Expression) ); (expr->v).type = PlusNode; (expr->v).val.op = Plus; expr->leftOperand = lvalue; value = parseValue(source); expr->rightOperand = parseTerm(source,value); return parseExpression(source, expr); case MinusOp: expr = (Expression *)malloc( sizeof(Expression) ); (expr->v).type = MinusNode; (expr->v).val.op = Minus; expr->leftOperand = lvalue; value = parseValue(source); expr->rightOperand = parseTerm(source,value); return parseExpression(source, expr); case Alphabet: case PrintOp: for (i = strlen(token.tok) - 1; i >= 0; i--) { ungetc(token.tok[i], source); } return lvalue; case EOFsymbol: return lvalue; default: printf("Syntax Error: Expect a numeric value or an identifier %s\n", token.tok); exit(1); } }
ExpPtr parseExp (Lexer& lex) { Span spStart, spEnd; ExpList vals; ExpPtr e; Token tok; e = parseTerm(lex); vals.push_back(e); spStart = spEnd = e->span; while (lex.current() == tIdent) if (lex.current().isOperator()) { // <op> tok = lex.advance(); e = Exp::make(eVar, tok.str, bool(true), {}, tok.span); vals.push_back(e); // <term> e = parseTerm(lex); vals.push_back(e); spEnd = e->span; } else lex.expect("operator"); if (vals.size() == 1) return vals[0]; // else if (vals.size() == 3) // return Exp::make(eCall, { vals[1], vals[0], vals[2] }, // spStart + spEnd); else return Exp::make(eInfix, vals, spStart + spEnd); }
Expression parseTerm() { if (curchar == '!') { Expression e; match("!"); e = parseTerm(); return makeNegation(e); } if (curchar == '(') { Expression e; match("("); e = parseEquivalence(); match(")"); return e; } return parseAtom(); }
ExpPtr parseiGet (Lexer& lex) { // ^get <ty> <idx> <term> Span spStart, spEnd, spType; spStart = lex.eat(tiGet).span; auto ty = parseType(lex, spType); auto idx = lex.eat(tNumberInt).valueInt; auto body = parseTerm(lex); spEnd = body->span; auto e = Exp::make(eiGet, idx, { body }, spStart + spEnd); e->setType(ty); return e; }
SymbolTableEntry* Parser::parseSimpleExpression(){ int sign = false; if(isNext(tc_ADDOP)){ if(m_currentToken->getOpType() == op_MINUS){ sign = true; } parseSign(); } SymbolTableEntry* entry = parseTerm(); entry = parseSimpleExpressionPrime(entry); SymbolTableEntry* result = entry; if(sign){ result = newTemp(); m_code->generate(cd_UMINUS, entry, NULL, result); } return result; }
// Expr' → '||' Term Expr' | ε bool AnimExpression::parseExprPrime(const QString& str, QString::const_iterator& iter) { auto token = consumeToken(str, iter); if (token.type == Token::Or) { if (!parseTerm(str, iter)) { unconsumeToken(token); return false; } if (!parseExprPrime(str, iter)) { unconsumeToken(token); return false; } _opCodes.push_back(OpCode {OpCode::Or}); return true; } else { unconsumeToken(token); return true; } }
void Parser::parseSimpleExpression_r(AddOpExpressionPtr expr) { Token op = m_curToken; if (match(TokenType::AddOp)) { expr->addOp = op.operatorType(); AddOpExpressionPtr rhs(new AddOpExpression); rhs->lhs = parseTerm(); if (m_errorCode > ErrorCodes::NoError) { return; } expr->rhs = rhs; parseSimpleExpression_r(rhs); return; } return; }
SymbolTableEntry* Parser::parseSimpleExpressionPrime(SymbolTableEntry* prevEntry){ SymbolTableEntry* temp = prevEntry; if(isNext(tc_ADDOP)){ OpType t = m_currentToken->getOpType(); match(tc_ADDOP); temp = newTemp(); SymbolTableEntry* term = parseTerm(); if(t == op_PLUS){ m_code->generate(cd_ADD, prevEntry, term, temp); } else if(t == op_MINUS){ m_code->generate(cd_SUB, prevEntry, term, temp); } else{ m_code->generate(cd_OR, prevEntry, term, temp); } prevEntry = parseSimpleExpressionPrime(temp); } return temp; }
shared_ptr<Expression> Parser::parseTerm() { auto rootExp = parseFactor(); it++; while (it->tag == MUL || it->tag == DIV ) { auto opExp = make_shared<Expression>(); opExp->value = *it; opExp->addNode(rootExp); it++; opExp->addNode(parseTerm()); rootExp = opExp; it++; } if (!(it->tag == MUL || it->tag == DIV)) { it--; } return rootExp; }
void Translator::match(Plasma::RunnerContext& context) { QString term = context.query(); QString text; QPair<QString, QString> language; if (!parseTerm(term, text, language)) { return; } if (!context.isValid()) { return; } QEventLoop loop; TranslatorJob job(text, language); connect(&job, SIGNAL(finished()), &loop, SLOT(quit())); loop.exec(); parseResult(job.result(), context, text); }