Skip to content
Snippets Groups Projects
Commit 75de5262 authored by Paweł Kędzia's avatar Paweł Kędzia
Browse files

A part of logical predicates have been implemented.

parent 276222e6
No related branches found
No related tags found
No related merge requests found
...@@ -16,7 +16,6 @@ Parser::~Parser() ...@@ -16,7 +16,6 @@ Parser::~Parser()
} }
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
/** /**
* @desc Parse string operator writed in std::string. Converts writed operator * @desc Parse string operator writed in std::string. Converts writed operator
* to istream and calling parseStringOperator with istream * to istream and calling parseStringOperator with istream
...@@ -47,6 +46,36 @@ boost::shared_ptr<Wccl::Function<Wccl::StrSet> > Parser::parseStringOperator( ...@@ -47,6 +46,36 @@ boost::shared_ptr<Wccl::Function<Wccl::StrSet> > Parser::parseStringOperator(
return parser.parse_string_operator(); 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();
}
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
/** /**
......
...@@ -30,6 +30,11 @@ public: ...@@ -30,6 +30,11 @@ public:
parseStringOperator(const std::string&) const; parseStringOperator(const std::string&) const;
boost::shared_ptr<Wccl::Function<Wccl::StrSet> > boost::shared_ptr<Wccl::Function<Wccl::StrSet> >
parseStringOperator(std::istream& ) const; 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: private:
const Corpus2::Tagset &tagset; const Corpus2::Tagset &tagset;
......
...@@ -17,6 +17,8 @@ header { ...@@ -17,6 +17,8 @@ header {
#include <libwccl/sentencecontext.h> #include <libwccl/sentencecontext.h>
// operators // operators
#include <libwccl/ops/or.h>
#include <libwccl/ops/nor.h>
#include <libwccl/ops/and.h> #include <libwccl/ops/and.h>
#include <libwccl/ops/affix.h> #include <libwccl/ops/affix.h>
#include <libwccl/ops/toupper.h> #include <libwccl/ops/toupper.h>
...@@ -114,9 +116,8 @@ parse_predicates ...@@ -114,9 +116,8 @@ parse_predicates
returns [boost::shared_ptr<Wccl::Function<Wccl::Bool> > res] returns [boost::shared_ptr<Wccl::Function<Wccl::Bool> > res]
{ {
Wccl::Variables vars; Wccl::Variables vars;
boost::shared_ptr<Wccl::Bool> mret;
} }
: mret = predicates [vars, res] : res = predicates [vars]
; ;
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
// Rules for parsing values in scope (variables). // Rules for parsing values in scope (variables).
...@@ -220,7 +221,6 @@ sym_set ...@@ -220,7 +221,6 @@ sym_set
; ;
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
// Bool: $?name // Bool: $?name
/*
boolean boolean
[Wccl::Variables& vars] [Wccl::Variables& vars]
returns [boost::shared_ptr<Wccl::Constant<Wccl::Bool> > op] returns [boost::shared_ptr<Wccl::Constant<Wccl::Bool> > op]
...@@ -232,7 +232,6 @@ boolean ...@@ -232,7 +232,6 @@ boolean
op.reset(new Wccl::Constant<Wccl::Bool>(*val.get())); op.reset(new Wccl::Constant<Wccl::Bool>(*val.get()));
} }
; ;
*/
// Boolean $!name // Boolean $!name
/* /*
boolean_ref [std::string& name]: boolean_ref [std::string& name]:
...@@ -367,15 +366,13 @@ sym_set_v [std::string& value] ...@@ -367,15 +366,13 @@ sym_set_v [std::string& value]
*/ */
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
// boolean: // boolean:
/*
boolean_v boolean_v
[Wccl::Variables& vars] [Wccl::Variables& vars]
returns [boost::shared_ptr<Wccl::Bool> val] returns [boost::shared_ptr<Wccl::Constant<Wccl::Bool> > val]
: "True" { val.reset(new Wccl::Bool(true )); } : "True" { val.reset(new Wccl::Constant<Wccl::Bool>(Wccl::Bool(true ))); }
| "False" { val.reset(new Wccl::Bool(false)); } | "False" { val.reset(new Wccl::Constant<Wccl::Bool>(Wccl::Bool(false))); }
| val = boolean [vars] | val = boolean [vars]
; ;
*/
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
// position value: // position value:
/* /*
...@@ -583,44 +580,72 @@ op_affix ...@@ -583,44 +580,72 @@ op_affix
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
predicates predicates
[Wccl::Variables& vars, boost::shared_ptr<Wccl::Function<Wccl::Bool> >& pr] [Wccl::Variables& vars]
returns [boost::shared_ptr<Wccl::Bool> ret] returns [boost::shared_ptr<Wccl::Function<Wccl::Bool> > ret]
: ret = logical_predicates [vars, pr] : ret = logical_predicates [vars]
; ;
// Implementations of predicates: // Implementations of predicates:
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
logical_predicates logical_predicates
[Wccl::Variables& vars, boost::shared_ptr<Wccl::Function<Wccl::Bool> >& pr] [Wccl::Variables& vars]
returns [boost::shared_ptr<Wccl::Bool> ret] 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; boost::shared_ptr<Wccl::Function<Wccl::Bool> > pred;
} ret_v.reset(new std::vector<boost::shared_ptr<Wccl::Function<Wccl::Bool> > >);
: ret = lpred_and [vars] {
// pr.reset(new Wccl::And(v));
} }
: pred = logical_predicates [vars] {
ret_v->push_back(pred);
} (
COMMA pred = logical_predicates [vars] {
ret_v->push_back(pred);
})*
; ;
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
lpred_and lpred_and
[Wccl::Variables& vars] [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_v = logical_predicates_comma_sep [vars] RPAREN {
op.reset(new Wccl::And(ret_v));
} }
: "and" LPAREN ret = logical_predicates [vars, tmpPr] (COMMA ret = logical_predicates [vars, tmpPr])* RPAREN
; ;
/*
lpred_not
lpred_or lpred_or
*/ [Wccl::Variables& vars]
/* returns [boost::shared_ptr<Wccl::Function<Wccl::Bool> > op]
boolean_op [std::string& name] {
: "and" LPAREN seq_et [name] RPAREN boost::shared_ptr<
| "not" LPAREN seq_et [name] RPAREN std::vector<boost::shared_ptr<Wccl::Function<Wccl::Bool> > >
| "or" LPAREN seq_et [name] RPAREN > 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 // ANTLR LEXER
......
...@@ -6,7 +6,7 @@ ...@@ -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() int main()
...@@ -14,10 +14,14 @@ int main() ...@@ -14,10 +14,14 @@ int main()
std::string str_in; std::string str_in;
Corpus2::Tagset tagset; Corpus2::Tagset tagset;
Parser parser(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) { while (1) {
std::cerr << "Enter a string operator expression: "; std::cerr << "Enter a string predicates expression: ";
getline(std::cin, str_in); getline(std::cin, str_in);
...@@ -31,25 +35,26 @@ int main() ...@@ -31,25 +35,26 @@ int main()
} }
else { else {
try { try {
// valRet = parser.parseValue(str_in); retOp = parser.parsePredicate(str_in);
/*
if (retOp.get()) { if (retOp.get()) {
if ((retStr = retOp->apply(sc)).get()) { std::cerr
std::cerr << "Parsed expression: " << retStr->to_raw_string() << std::endl; << "Parsed expression: "
<< (retValue = retOp->apply(sc))->to_raw_string()
<< std::endl;
} }
else { else {
std::cerr << "Problem while parsing -- haven't 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;
} }
} }
else {
std::cerr << "Problem while parsing -- haven't Function<Wccl::StrSet> object in boost::shared_ptr!" << std::endl;
}
*/
}
catch (antlr::MismatchedTokenException &e) { catch (antlr::MismatchedTokenException &e) {
std::cerr << "Mismatch token exception!" << std::endl; std::cerr << "Mismatch token exception!" << std::endl;
} }
/*
catch (antlr::TokenStreamRecognitionException &e) {
std::cerr << "[2] Syntax error!" << std::endl;
}
*/
catch (...) { catch (...) {
std::cerr << "[N] Syntax error!" << std::endl; std::cerr << "[N] Syntax error!" << std::endl;
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment