Created
March 29, 2013 21:20
-
-
Save artsobolev/5273716 to your computer and use it in GitHub Desktop.
Crazy expressions parser
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <string> | |
#include <vector> | |
class Token { | |
typedef std::vector<std::string> TokenList; | |
TokenList _tokens; | |
public: | |
Token(std::string t) { | |
t = std::string("Token[") + t + "]"; | |
_tokens.push_back(t); | |
} | |
Token(const TokenList &tl) : _tokens(tl) { | |
} | |
const TokenList& tokens() const { return _tokens; } | |
}; | |
Token op(std::string operation, const Token &a) { | |
std::vector<std::string> tokens; | |
tokens.push_back(operation); | |
tokens.insert(tokens.end(), a.tokens().begin(), a.tokens().end()); | |
return Token(tokens); | |
} | |
Token op(std::string operation, const Token &a, const Token &b) { | |
std::vector<std::string> tokens; | |
tokens.push_back(operation); | |
tokens.insert(tokens.end(), a.tokens().begin(), a.tokens().end()); | |
tokens.insert(tokens.end(), b.tokens().begin(), b.tokens().end()); | |
return Token(tokens); | |
} | |
Token brackets(const Token &a) { | |
std::vector<std::string> tokens; | |
// tokens.push_back("BRACKET_OPEN"); | |
tokens.insert(tokens.end(), a.tokens().begin(), a.tokens().end()); | |
// tokens.push_back("BRACKET_CLOSE"); | |
return Token(tokens); | |
} | |
Token operator+(const Token &a, const Token &b) { | |
return op("ADD", a, b); | |
} | |
Token operator*(const Token &a, const Token &b) { | |
return op("MUL", a, b); | |
} | |
Token operator/(const Token &a, const Token &b) { | |
return op("DIV", a, b); | |
} | |
Token operator-(const Token &a, const Token &b) { | |
return op("SUB", a, b); | |
} | |
Token operator-(const Token &a) { | |
return op("MINUS", a); | |
} | |
Token operator^(const Token &a, const Token &b) { | |
return op("XOR", a, b); | |
} | |
Token operator|(const Token &a, const Token &b) { | |
return op("BIT_OR", a, b); | |
} | |
Token operator&(const Token &a, const Token &b) { | |
return op("BIT_AND", a, b); | |
} | |
Token operator&&(const Token &a, const Token &b) { | |
return op("AND", a, b); | |
} | |
Token operator||(const Token &a, const Token &b) { | |
return op("OR", a, b); | |
} | |
std::ostream& operator<<(std::ostream &out, Token &token) { | |
const std::vector<std::string> &tl = token.tokens(); | |
for (size_t i = 0, length = tl.size(); i < length; ++i) { | |
out << tl[i] << " "; | |
} | |
return out; | |
} | |
int main() { | |
#include "expr" | |
std::cout << val << std::endl; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import subprocess | |
import re | |
import os | |
print "Input expression and we'll parse it!" | |
expr = raw_input('--> ') | |
tokens = re.findall("(\\w+)", expr) | |
expr2 = re.sub("(\\w+)", "t\\1", expr) | |
f = open("expr", "w") | |
content = "" | |
for i in set(tokens): | |
content += 'Token t' + i + '("' + i + '");' | |
content += "Token val = (" + expr2 + ");" | |
f.write(content) | |
f.close() | |
proc = subprocess.Popen("g++ main.cpp -o prog", shell=True, stderr=subprocess.PIPE) | |
if proc.stderr.readline() == "": | |
proc = subprocess.Popen("./prog", shell=True, stdout=subprocess.PIPE) | |
ans = proc.stdout.readline() | |
os.remove("prog") | |
print ans | |
else: | |
print "Error, check your expression" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment