diff --git a/libwccl/parser/Parser.cpp b/libwccl/parser/Parser.cpp index 5ec704b478fefe52b77cdef573075c349de42d23..8f6036ec7f93bb38e36a5ea35121aa4047e515bd 100644 --- a/libwccl/parser/Parser.cpp +++ b/libwccl/parser/Parser.cpp @@ -16,7 +16,6 @@ Parser::~Parser() } // ---------------------------------------------------------------------------- - /** * @desc Parse string operator writed in std::string. Converts writed operator * to istream and calling parseStringOperator with istream @@ -47,6 +46,36 @@ boost::shared_ptr<Wccl::Function<Wccl::StrSet> > Parser::parseStringOperator( return parser.parse_string_operator(); } +// ---------------------------------------------------------------------------- +/** + * @desc Parse predicates writed in std::string. Converts writed predicates + * to istream and calling parsePredicate with istream + * @arg str writed predicate(s) + * @return boost::shared_ptr<Wccl::Function<Wccl::Bool> > to created predicate + */ +boost::shared_ptr<Wccl::Function<Wccl::Bool> > Parser::parsePredicate( + const std::string& str) const +{ + std::stringstream ss (std::stringstream::in | std::stringstream::out); + ss << str; + + return this->parsePredicate(ss); +} + +/** + * @desc Parse predicate. Runs parse_predicates rule in the parser grammar. + * @arg istr input stream with writed predicate + * @return boost::shared_ptr<Wccl::Function<Wccl::Bool> > to created predicate + */ +boost::shared_ptr<Wccl::Function<Wccl::Bool> > Parser::parsePredicate( + std::istream& istr) const +{ + ANTLRLexer lexer(istr); + ANTLRParser parser(lexer); + + return parser.parse_predicates(); +} + // ---------------------------------------------------------------------------- /** diff --git a/libwccl/parser/Parser.h b/libwccl/parser/Parser.h index a76b3a9c24b94ecd2c6ac04d4bc1c6e530f46eea..4b0177bef21a042003531c2b3824a7672c971af9 100644 --- a/libwccl/parser/Parser.h +++ b/libwccl/parser/Parser.h @@ -30,6 +30,11 @@ public: parseStringOperator(const std::string&) const; boost::shared_ptr<Wccl::Function<Wccl::StrSet> > parseStringOperator(std::istream& ) const; + // methods for parsing predicates (returns bool) + boost::shared_ptr<Wccl::Function<Wccl::Bool> > + parsePredicate(const std::string&) const; + boost::shared_ptr<Wccl::Function<Wccl::Bool> > + parsePredicate(std::istream& ) const; private: const Corpus2::Tagset &tagset; diff --git a/libwccl/parser/grammar.g b/libwccl/parser/grammar.g index 03bb9bbc9591df67e1ecc1ccfc78670adc5fda65..7818058d8e709b046b5ed79b25e816c5a61dedfe 100644 --- a/libwccl/parser/grammar.g +++ b/libwccl/parser/grammar.g @@ -17,6 +17,8 @@ header { #include <libwccl/sentencecontext.h> // operators + #include <libwccl/ops/or.h> + #include <libwccl/ops/nor.h> #include <libwccl/ops/and.h> #include <libwccl/ops/affix.h> #include <libwccl/ops/toupper.h> @@ -114,9 +116,8 @@ parse_predicates returns [boost::shared_ptr<Wccl::Function<Wccl::Bool> > res] { Wccl::Variables vars; - boost::shared_ptr<Wccl::Bool> mret; } - : mret = predicates [vars, res] + : res = predicates [vars] ; // ---------------------------------------------------------------------------- // Rules for parsing values in scope (variables). @@ -220,7 +221,6 @@ sym_set ; // ---------------------------------------------------------------------------- // Bool: $?name -/* boolean [Wccl::Variables& vars] returns [boost::shared_ptr<Wccl::Constant<Wccl::Bool> > op] @@ -232,7 +232,6 @@ boolean op.reset(new Wccl::Constant<Wccl::Bool>(*val.get())); } ; -*/ // Boolean $!name /* boolean_ref [std::string& name]: @@ -367,15 +366,13 @@ sym_set_v [std::string& value] */ // ---------------------------------------------------------------------------- // boolean: -/* boolean_v [Wccl::Variables& vars] - returns [boost::shared_ptr<Wccl::Bool> val] - : "True" { val.reset(new Wccl::Bool(true )); } - | "False" { val.reset(new Wccl::Bool(false)); } + returns [boost::shared_ptr<Wccl::Constant<Wccl::Bool> > val] + : "True" { val.reset(new Wccl::Constant<Wccl::Bool>(Wccl::Bool(true ))); } + | "False" { val.reset(new Wccl::Constant<Wccl::Bool>(Wccl::Bool(false))); } | val = boolean [vars] ; -*/ // ---------------------------------------------------------------------------- // position value: /* @@ -583,44 +580,72 @@ op_affix /////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////// predicates - [Wccl::Variables& vars, boost::shared_ptr<Wccl::Function<Wccl::Bool> >& pr] - returns [boost::shared_ptr<Wccl::Bool> ret] - : ret = logical_predicates [vars, pr] + [Wccl::Variables& vars] + returns [boost::shared_ptr<Wccl::Function<Wccl::Bool> > ret] + : ret = logical_predicates [vars] ; // Implementations of predicates: // ---------------------------------------------------------------------------- logical_predicates - [Wccl::Variables& vars, boost::shared_ptr<Wccl::Function<Wccl::Bool> >& pr] - returns [boost::shared_ptr<Wccl::Bool> ret] + [Wccl::Variables& vars] + returns [boost::shared_ptr<Wccl::Function<Wccl::Bool> > ret] + : ret = lpred_and [vars] + | ret = lpred_or [vars] + | ret = lpred_nor [vars] + | ret = boolean_v [vars] +; +// comma-separated predicates +logical_predicates_comma_sep + [Wccl::Variables& vars] + returns [boost::shared_ptr<std::vector<boost::shared_ptr<Wccl::Function<Wccl::Bool> > > > ret_v] { - boost::shared_ptr<Wccl::LogicalPredicate::BoolFunctionPtr> v; -} - : ret = lpred_and [vars] { - // pr.reset(new Wccl::And(v)); - } + boost::shared_ptr<Wccl::Function<Wccl::Bool> > pred; + ret_v.reset(new std::vector<boost::shared_ptr<Wccl::Function<Wccl::Bool> > >); +} + : pred = logical_predicates [vars] { + ret_v->push_back(pred); + } ( + COMMA pred = logical_predicates [vars] { + ret_v->push_back(pred); + })* ; // ---------------------------------------------------------------------------- lpred_and [Wccl::Variables& vars] - returns [boost::shared_ptr<Wccl::Bool> ret] + returns [boost::shared_ptr<Wccl::Function<Wccl::Bool> > op] { - boost::shared_ptr<Wccl::Function<Wccl::Bool> > tmpPr; + boost::shared_ptr< + std::vector<boost::shared_ptr<Wccl::Function<Wccl::Bool> > > + > ret_v; } - : "and" LPAREN ret = logical_predicates [vars, tmpPr] (COMMA ret = logical_predicates [vars, tmpPr])* RPAREN + : "and" LPAREN ret_v = logical_predicates_comma_sep [vars] RPAREN { + op.reset(new Wccl::And(ret_v)); + } ; - -/* -lpred_not - lpred_or -*/ -/* -boolean_op [std::string& name] - : "and" LPAREN seq_et [name] RPAREN - | "not" LPAREN seq_et [name] RPAREN - | "or" LPAREN seq_et [name] RPAREN - ; -*/ + [Wccl::Variables& vars] + returns [boost::shared_ptr<Wccl::Function<Wccl::Bool> > op] +{ + boost::shared_ptr< + std::vector<boost::shared_ptr<Wccl::Function<Wccl::Bool> > > + > ret_v; +} + : "or" LPAREN ret_v = logical_predicates_comma_sep [vars] RPAREN { + op.reset(new Wccl::Or(ret_v)); + } +; +lpred_nor + [Wccl::Variables& vars] + returns [boost::shared_ptr<Wccl::Function<Wccl::Bool> > op] +{ + boost::shared_ptr< + std::vector<boost::shared_ptr<Wccl::Function<Wccl::Bool> > > + > ret_v; +} + : "nor" LPAREN ret_v = logical_predicates_comma_sep [vars] RPAREN { + op.reset(new Wccl::Nor(ret_v)); + } +; // ---------------------------------------------------------------------------------- // ANTLR LEXER diff --git a/wcclparser/val_main.cpp b/wcclparser/val_main.cpp index 8a379c554a316eb5b531f8791e76272561aae1a8..82d3d058021c67cf6648399d8cdfaa787113f4ef 100644 --- a/wcclparser/val_main.cpp +++ b/wcclparser/val_main.cpp @@ -6,7 +6,7 @@ // ---------------------------------------------------------------------------- /** - * @desc It's simple command line tester for testing string operators + * @desc It's simple command line tester for testing predicates */ int main() @@ -14,10 +14,14 @@ int main() std::string str_in; Corpus2::Tagset tagset; Parser parser(tagset); - boost::shared_ptr<Wccl::Value> valRet; + + boost::shared_ptr<Wccl::Bool> retValue; + boost::shared_ptr<Wccl::Function<Wccl::Bool> > retOp; + boost::shared_ptr<Corpus2::Sentence> sentence; + Wccl::SentenceContext sc(sentence); while (1) { - std::cerr << "Enter a string operator expression: "; + std::cerr << "Enter a string predicates expression: "; getline(std::cin, str_in); @@ -31,25 +35,26 @@ int main() } else { try { - // valRet = parser.parseValue(str_in); + retOp = parser.parsePredicate(str_in); - /* if (retOp.get()) { - if ((retStr = retOp->apply(sc)).get()) { - std::cerr << "Parsed expression: " << retStr->to_raw_string() << std::endl; - } - else { - std::cerr << "Problem while parsing -- haven't StrSet object in boost::shared_ptr!" << std::endl; - } + std::cerr + << "Parsed expression: " + << (retValue = retOp->apply(sc))->to_raw_string() + << std::endl; } else { - std::cerr << "Problem while parsing -- haven't Function<Wccl::StrSet> object in boost::shared_ptr!" << std::endl; + std::cerr << "Problem while parsing -- haven't got Function<Wccl::StrSet> object in boost::shared_ptr!" << std::endl; } - */ } catch (antlr::MismatchedTokenException &e) { std::cerr << "Mismatch token exception!" << std::endl; } + /* + catch (antlr::TokenStreamRecognitionException &e) { + std::cerr << "[2] Syntax error!" << std::endl; + } + */ catch (...) { std::cerr << "[N] Syntax error!" << std::endl; }