-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
finish the 1st stage of parsing ExtendedDirac
- Loading branch information
1 parent
95d53d6
commit 1cc6679
Showing
21 changed files
with
2,685 additions
and
41 deletions.
There are no files selected for viewing
109 changes: 109 additions & 0 deletions
109
include/autoq/parsing/ExtendedDirac/EvaluationVisitor.h
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
|
||
#include "ExtendedDiracBaseVisitor.h" | ||
#include "ExtendedDiracParser.h" | ||
|
||
#include "autoq/aut_description.hh" | ||
#include "autoq/symbol/concrete.hh" | ||
#include "autoq/complex/complex.hh" | ||
|
||
// Thanks to https://github.com/boostorg/multiprecision/issues/297 | ||
boost::multiprecision::cpp_int bin_2_dec2(const std::string_view& num) | ||
{ | ||
boost::multiprecision::cpp_int dec_value = 0; | ||
auto cptr = num.data(); | ||
long long len = num.size(); | ||
// check if big enough to have 0b postfix | ||
if (num.size() > 2) { | ||
// check if 2nd character is the 'b' binary postfix | ||
// skip over it & adjust length accordingly if it is | ||
if (num[1] == 'b' || num[1] == 'B') { | ||
cptr += 2; | ||
len -= 2; | ||
} | ||
} | ||
// change i's type to cpp_int if the number of digits is greater | ||
// than std::numeric_limits<size_t>::max() | ||
for (long long i = len - 1; 0 <= i; ++cptr, --i) { | ||
if (*cptr == '1') { | ||
boost::multiprecision::bit_set(/*.val = */ dec_value, /*.pos = */ i); | ||
} | ||
} | ||
return dec_value; | ||
} | ||
|
||
template <typename Symbol> | ||
class EvaluationVisitor : public ExtendedDiracBaseVisitor { | ||
public: | ||
std::map<std::string, AUTOQ::Complex::Complex> constants; | ||
std::map<std::string, std::string> predicates; | ||
EvaluationVisitor(const std::map<std::string, AUTOQ::Complex::Complex> &constants, const std::map<std::string, std::string> &predicates) : constants(constants), predicates(predicates) {} | ||
|
||
std::any visitExtendedDirac(ExtendedDiracParser::ExtendedDiracContext *ctx) override { | ||
if (ctx->set().size() == 1) { // RULE: set | ||
return visit(ctx->set(0)); | ||
} else { // RULE: set '\\' set | ||
// TODO: | ||
/* | ||
auto left = visit(ctx->set(0)); // Left operand | ||
auto right = visit(ctx->set(1)); // Right operand | ||
return visit(ctx->set(0)); //std::make_pair(left, right); | ||
*/ | ||
return {}; | ||
} | ||
} | ||
|
||
std::any visitSet(ExtendedDiracParser::SetContext *ctx) override { | ||
if (ctx->n != nullptr) { // RULE: set '^' n | ||
int times = std::stoi(ctx->n->getText()); | ||
if (times == 1) { | ||
return visit(ctx->set(0)); | ||
} else { // if (times > 1) { | ||
auto lsta = std::any_cast<AUTOQ::Automata<Symbol>>(visit(ctx->set(0))); | ||
auto result = lsta; | ||
while ((--times) > 0) { | ||
result = result * lsta; // TODO: implement *= | ||
} | ||
return result; | ||
} | ||
} else if (ctx->op == nullptr) { | ||
if (!ctx->set().empty()) { // RULE: '(' set ')' | ||
return visit(ctx->set(0)); | ||
} else if (ctx->diracs() != nullptr) { // RULE: '{' diracs '}' | ||
return visit(ctx->diracs()); | ||
} else { // if (ctx->dirac() != nullptr && ctx->ijklens() != nullptr) { // RULE: '{' dirac '|' ijklens '}' | ||
// TODO | ||
/**/ | ||
return {}; | ||
} | ||
} else if (ctx->op->getType() == ExtendedDiracParser::PROD) { // RULE: set op=PROD set | ||
return std::any_cast<AUTOQ::Automata<Symbol>>(visit(ctx->set(0))) * std::any_cast<AUTOQ::Automata<Symbol>>(visit(ctx->set(1))); | ||
} else if (ctx->op->getType() == ExtendedDiracParser::UNION) { // RULE: set op=UNION set | ||
return std::any_cast<AUTOQ::Automata<Symbol>>(visit(ctx->set(0))) || std::any_cast<AUTOQ::Automata<Symbol>>(visit(ctx->set(1))); | ||
} else { // if (ctx->op->getType() == ExtendedDiracParser::INTERSECTION) { // RULE: set op=INTERSECTION set | ||
// TODO: implement && | ||
/**/ | ||
return {}; | ||
} | ||
} | ||
|
||
std::any visitDiracs(ExtendedDiracParser::DiracsContext *ctx) override { | ||
if (ctx->diracs() == nullptr) { // RULE: dirac | ||
return visit(ctx->dirac()); | ||
} else { // RULE: diracs ',' dirac | ||
return std::any_cast<AUTOQ::Automata<Symbol>>(visit(ctx->diracs())) || std::any_cast<AUTOQ::Automata<Symbol>>(visit(ctx->dirac())); | ||
} | ||
} | ||
|
||
std::any visitDirac(ExtendedDiracParser::DiracContext *ctx) override { | ||
bool dummy; | ||
return from_tree_to_automaton<Symbol>(ctx->getText(), constants, predicates, dummy); | ||
} | ||
|
||
// std::any visitIjklens(ExtendedDiracParser::IjklensContext *ctx) override { | ||
// return visitChildren(ctx); | ||
// } | ||
|
||
// std::any visitIjklen(ExtendedDiracParser::IjklenContext *ctx) override { | ||
// return visitChildren(ctx); | ||
// } | ||
}; |
53 changes: 53 additions & 0 deletions
53
include/autoq/parsing/ExtendedDirac/ExtendedDiracBaseListener.h
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
|
||
// Generated from src/ExtendedDirac/ExtendedDirac.g4 by ANTLR 4.13.2 | ||
|
||
#pragma once | ||
|
||
|
||
#include "antlr4-runtime.h" | ||
#include "ExtendedDiracListener.h" | ||
|
||
|
||
/** | ||
* This class provides an empty implementation of ExtendedDiracListener, | ||
* which can be extended to create a listener which only needs to handle a subset | ||
* of the available methods. | ||
*/ | ||
class ExtendedDiracBaseListener : public ExtendedDiracListener { | ||
public: | ||
|
||
virtual void enterExtendedDirac(ExtendedDiracParser::ExtendedDiracContext * /*ctx*/) override { } | ||
virtual void exitExtendedDirac(ExtendedDiracParser::ExtendedDiracContext * /*ctx*/) override { } | ||
|
||
virtual void enterSet(ExtendedDiracParser::SetContext * /*ctx*/) override { } | ||
virtual void exitSet(ExtendedDiracParser::SetContext * /*ctx*/) override { } | ||
|
||
virtual void enterDiracs(ExtendedDiracParser::DiracsContext * /*ctx*/) override { } | ||
virtual void exitDiracs(ExtendedDiracParser::DiracsContext * /*ctx*/) override { } | ||
|
||
virtual void enterDirac(ExtendedDiracParser::DiracContext * /*ctx*/) override { } | ||
virtual void exitDirac(ExtendedDiracParser::DiracContext * /*ctx*/) override { } | ||
|
||
virtual void enterCket(ExtendedDiracParser::CketContext * /*ctx*/) override { } | ||
virtual void exitCket(ExtendedDiracParser::CketContext * /*ctx*/) override { } | ||
|
||
virtual void enterComplex(ExtendedDiracParser::ComplexContext * /*ctx*/) override { } | ||
virtual void exitComplex(ExtendedDiracParser::ComplexContext * /*ctx*/) override { } | ||
|
||
virtual void enterAngle(ExtendedDiracParser::AngleContext * /*ctx*/) override { } | ||
virtual void exitAngle(ExtendedDiracParser::AngleContext * /*ctx*/) override { } | ||
|
||
virtual void enterIjklens(ExtendedDiracParser::IjklensContext * /*ctx*/) override { } | ||
virtual void exitIjklens(ExtendedDiracParser::IjklensContext * /*ctx*/) override { } | ||
|
||
virtual void enterIjklen(ExtendedDiracParser::IjklenContext * /*ctx*/) override { } | ||
virtual void exitIjklen(ExtendedDiracParser::IjklenContext * /*ctx*/) override { } | ||
|
||
|
||
virtual void enterEveryRule(antlr4::ParserRuleContext * /*ctx*/) override { } | ||
virtual void exitEveryRule(antlr4::ParserRuleContext * /*ctx*/) override { } | ||
virtual void visitTerminal(antlr4::tree::TerminalNode * /*node*/) override { } | ||
virtual void visitErrorNode(antlr4::tree::ErrorNode * /*node*/) override { } | ||
|
||
}; | ||
|
56 changes: 56 additions & 0 deletions
56
include/autoq/parsing/ExtendedDirac/ExtendedDiracBaseVisitor.h
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
|
||
// Generated from src/ExtendedDirac/ExtendedDirac.g4 by ANTLR 4.13.2 | ||
|
||
#pragma once | ||
|
||
|
||
#include "antlr4-runtime.h" | ||
#include "ExtendedDiracVisitor.h" | ||
|
||
|
||
/** | ||
* This class provides an empty implementation of ExtendedDiracVisitor, which can be | ||
* extended to create a visitor which only needs to handle a subset of the available methods. | ||
*/ | ||
class ExtendedDiracBaseVisitor : public ExtendedDiracVisitor { | ||
public: | ||
|
||
virtual std::any visitExtendedDirac(ExtendedDiracParser::ExtendedDiracContext *ctx) override { | ||
return visitChildren(ctx); | ||
} | ||
|
||
virtual std::any visitSet(ExtendedDiracParser::SetContext *ctx) override { | ||
return visitChildren(ctx); | ||
} | ||
|
||
virtual std::any visitDiracs(ExtendedDiracParser::DiracsContext *ctx) override { | ||
return visitChildren(ctx); | ||
} | ||
|
||
virtual std::any visitDirac(ExtendedDiracParser::DiracContext *ctx) override { | ||
return visitChildren(ctx); | ||
} | ||
|
||
virtual std::any visitCket(ExtendedDiracParser::CketContext *ctx) override { | ||
return visitChildren(ctx); | ||
} | ||
|
||
virtual std::any visitComplex(ExtendedDiracParser::ComplexContext *ctx) override { | ||
return visitChildren(ctx); | ||
} | ||
|
||
virtual std::any visitAngle(ExtendedDiracParser::AngleContext *ctx) override { | ||
return visitChildren(ctx); | ||
} | ||
|
||
virtual std::any visitIjklens(ExtendedDiracParser::IjklensContext *ctx) override { | ||
return visitChildren(ctx); | ||
} | ||
|
||
virtual std::any visitIjklen(ExtendedDiracParser::IjklenContext *ctx) override { | ||
return visitChildren(ctx); | ||
} | ||
|
||
|
||
}; | ||
|
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
|
||
// Generated from src/ExtendedDirac/ExtendedDirac.g4 by ANTLR 4.13.2 | ||
|
||
#pragma once | ||
|
||
|
||
#include "antlr4-runtime.h" | ||
|
||
|
||
|
||
|
||
class ExtendedDiracLexer : public antlr4::Lexer { | ||
public: | ||
enum { | ||
T__0 = 1, T__1 = 2, T__2 = 3, T__3 = 4, T__4 = 5, T__5 = 6, T__6 = 7, | ||
T__7 = 8, T__8 = 9, T__9 = 10, T__10 = 11, T__11 = 12, ADD = 13, DIV = 14, | ||
DIGITS = 15, INTERSECTION = 16, KET = 17, MUL = 18, NAME = 19, PROD = 20, | ||
SUB = 21, UNION = 22, WS = 23 | ||
}; | ||
|
||
explicit ExtendedDiracLexer(antlr4::CharStream *input); | ||
|
||
~ExtendedDiracLexer() override; | ||
|
||
|
||
std::string getGrammarFileName() const override; | ||
|
||
const std::vector<std::string>& getRuleNames() const override; | ||
|
||
const std::vector<std::string>& getChannelNames() const override; | ||
|
||
const std::vector<std::string>& getModeNames() const override; | ||
|
||
const antlr4::dfa::Vocabulary& getVocabulary() const override; | ||
|
||
antlr4::atn::SerializedATNView getSerializedATN() const override; | ||
|
||
const antlr4::atn::ATN& getATN() const override; | ||
|
||
// By default the static state used to implement the lexer is lazily initialized during the first | ||
// call to the constructor. You can call this function if you wish to initialize the static state | ||
// ahead of time. | ||
static void initialize(); | ||
|
||
private: | ||
|
||
// Individual action functions triggered by action() above. | ||
|
||
// Individual semantic predicate functions triggered by sempred() above. | ||
|
||
}; | ||
|
46 changes: 46 additions & 0 deletions
46
include/autoq/parsing/ExtendedDirac/ExtendedDiracListener.h
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
|
||
// Generated from src/ExtendedDirac/ExtendedDirac.g4 by ANTLR 4.13.2 | ||
|
||
#pragma once | ||
|
||
|
||
#include "antlr4-runtime.h" | ||
#include "ExtendedDiracParser.h" | ||
|
||
|
||
/** | ||
* This interface defines an abstract listener for a parse tree produced by ExtendedDiracParser. | ||
*/ | ||
class ExtendedDiracListener : public antlr4::tree::ParseTreeListener { | ||
public: | ||
|
||
virtual void enterExtendedDirac(ExtendedDiracParser::ExtendedDiracContext *ctx) = 0; | ||
virtual void exitExtendedDirac(ExtendedDiracParser::ExtendedDiracContext *ctx) = 0; | ||
|
||
virtual void enterSet(ExtendedDiracParser::SetContext *ctx) = 0; | ||
virtual void exitSet(ExtendedDiracParser::SetContext *ctx) = 0; | ||
|
||
virtual void enterDiracs(ExtendedDiracParser::DiracsContext *ctx) = 0; | ||
virtual void exitDiracs(ExtendedDiracParser::DiracsContext *ctx) = 0; | ||
|
||
virtual void enterDirac(ExtendedDiracParser::DiracContext *ctx) = 0; | ||
virtual void exitDirac(ExtendedDiracParser::DiracContext *ctx) = 0; | ||
|
||
virtual void enterCket(ExtendedDiracParser::CketContext *ctx) = 0; | ||
virtual void exitCket(ExtendedDiracParser::CketContext *ctx) = 0; | ||
|
||
virtual void enterComplex(ExtendedDiracParser::ComplexContext *ctx) = 0; | ||
virtual void exitComplex(ExtendedDiracParser::ComplexContext *ctx) = 0; | ||
|
||
virtual void enterAngle(ExtendedDiracParser::AngleContext *ctx) = 0; | ||
virtual void exitAngle(ExtendedDiracParser::AngleContext *ctx) = 0; | ||
|
||
virtual void enterIjklens(ExtendedDiracParser::IjklensContext *ctx) = 0; | ||
virtual void exitIjklens(ExtendedDiracParser::IjklensContext *ctx) = 0; | ||
|
||
virtual void enterIjklen(ExtendedDiracParser::IjklenContext *ctx) = 0; | ||
virtual void exitIjklen(ExtendedDiracParser::IjklenContext *ctx) = 0; | ||
|
||
|
||
}; | ||
|
Oops, something went wrong.
1cc6679
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
BV - O
BVBugSym - O
BVSym - O
GHZall - ijklen
GHZzero - O
Grover - redefine multiplication
GroverSym - redefine multiplication
H2 - ijklen
H2BugSym - O
H2Sym - O
HXH - ijklen
MCToffoli - ijklen, set difference
MOBV_reorder - ijklen
MOGrover - ijklen, redefine multiplication
MOGroverSym - ijklen, redefine multiplication