From 75de52622f53f0e2545ca5414e42e7487d65faa6 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Pawe=C5=82=20K=C4=99dzia?= <Pawel.Kedzia@pwr.wroc.pl>
Date: Mon, 15 Nov 2010 19:58:54 +0100
Subject: [PATCH] A part of logical predicates have been implemented.

---
 libwccl/parser/Parser.cpp | 31 ++++++++++++-
 libwccl/parser/Parser.h   |  5 +++
 libwccl/parser/grammar.g  | 93 +++++++++++++++++++++++++--------------
 wcclparser/val_main.cpp   | 31 +++++++------
 4 files changed, 112 insertions(+), 48 deletions(-)

diff --git a/libwccl/parser/Parser.cpp b/libwccl/parser/Parser.cpp
index 5ec704b..8f6036e 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 a76b3a9..4b0177b 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 03bb9bb..7818058 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 8a379c5..82d3d05 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;
 			}
-- 
GitLab