Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parsergenerator #22

Open
wants to merge 23 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
b9b50bd
adding lexer, parser, and driver for ruleparser
larry-gonzalez May 7, 2019
f484340
adding bison and flex dependencies
larry-gonzalez May 7, 2019
4552556
adding code to generate cpp objects from AST
larry-gonzalez May 7, 2019
77c355d
calling parseRuleFile (ruleparser) instead of readFromFile function m…
larry-gonzalez May 7, 2019
032f2fc
adding datatypes to constant when parsing rules (string only)
larry-gonzalez May 7, 2019
7392012
adding comment
larry-gonzalez May 7, 2019
6297da4
typo and simpler characters for quotation marks
larry-gonzalez May 8, 2019
eee8258
deletion of a comment and spacing
larry-gonzalez May 8, 2019
703225e
ignore inline comments
larry-gonzalez May 8, 2019
dbfe969
add required version for bison
larry-gonzalez May 29, 2019
5016579
update flex version
larry-gonzalez May 31, 2019
daabeb6
Merge branch 'master' into parsergenerator
larry-gonzalez May 31, 2019
b2cbde0
add flex and bison as dependencies in readme. Improve example
larry-gonzalez May 31, 2019
54027a3
simple improve
larry-gonzalez May 31, 2019
2ffe9e3
simplification
larry-gonzalez May 31, 2019
2017754
add DEBUG message
larry-gonzalez May 31, 2019
862dc69
add option useParserGenerator.
larry-gonzalez Jun 11, 2019
c0816f5
Merge branch 'master' into parsergenerator
larry-gonzalez Jun 12, 2019
2fdfeb8
use rewriteMultihead parameter in parseRuleFile
larry-gonzalez Jun 12, 2019
f13adfd
use new metod to create rules
larry-gonzalez Jun 12, 2019
d21f649
fix silly name error
larry-gonzalez Jun 12, 2019
a140ab4
fix merge conflict
larry-gonzalez Oct 17, 2019
8da549c
Merge branch 'master' into parsergenerator
larry-gonzalez Nov 1, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,29 @@ file(GLOB vlog_SRC
"src/launcher/vlogscan.cpp"
)

#PARSER GENERATOR
find_package(BISON 3.0 REQUIRED)
find_package(FLEX 2.6 REQUIRED)

add_custom_target(parserdirectory ALL COMMAND ${CMAKE_COMMAND} -E make_directory ${CMAKE_CURRENT_BINARY_DIR}/parser)

BISON_TARGET(ruleparser
src/parser/ruleparser.yy
${CMAKE_CURRENT_BINARY_DIR}/parser/ruleparser.tab.cc)
FLEX_TARGET(rulelexer
src/parser/rulelexer.l
${CMAKE_CURRENT_BINARY_DIR}/parser/rulelexer.yy.cc)
ADD_FLEX_BISON_DEPENDENCY(rulelexer ruleparser)

add_library(parsergenerator SHARED
src/parser/ruledriver.cpp
${FLEX_rulelexer_OUTPUTS}
${BISON_ruleparser_OUTPUTS}
)
add_dependencies(parsergenerator parserdirectory)

include_directories(${CMAKE_CURRENT_BINARY_DIR}/parser)

#ZLIB
find_package(ZLIB REQUIRED)

Expand Down Expand Up @@ -198,6 +221,7 @@ ENDIF()
#Create both a library and the executable program
add_library(vlog-core SHARED ${vlog_SRC})
add_executable(vlog src/launcher/main.cpp)
add_executable(localruleparser src/parser/rulemain.cpp)

IF(SPARQL)
target_link_libraries(vlog-core ${CURL_LIBRARIES})
Expand All @@ -212,6 +236,9 @@ if (CMAKE_THREAD_LIBS_INIT)
target_link_libraries(vlog-core "${CMAKE_THREAD_LIBS_INIT}")
endif()

target_link_libraries(vlog-core parsergenerator)
target_link_libraries(localruleparser parsergenerator)

#Set compiler options
if((${CMAKE_CXX_COMPILER_ID} STREQUAL "Clang") OR (${CMAKE_CXX_COMPILER_ID} STREQUAL "GNU"))
set(CMAKE_CXX_FLAGS_RELEASE "-DNDEBUG -O3")
Expand Down
29 changes: 22 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,34 @@ cmake ..
make
```

External libraries should be automatically downloaded and installed in the same directory. The only library that should be already installed is zlib, which is necessary to read gzip files. This library is usually already present by default.
### Dependencies
* zlib
* bison (3.0 or higher)
* flex (2.6 or higher)
* Other external libraries should be automatically downloaded and installed in the same directory.

To enable the web-interface, you need to use the -DWEBINTERFACE=1 option to cmake.
### Options for cmake
* `-DWEBINTERFACE=1` to enable the web interface
* `-DCMAKE_BUILD_TYPE=Debug` to build the DEBUG version of the program
* `-DSPARQL=1` to support SPARQL queries to external data sources
* `-DJAVA=1` to build the java modules
* For other options, please check the file `CMakeLists.txt`

If you want to build the DEBUG version of the program, including the web interface: proceed as follows:
#### Example

In this example we show how to install VLog in debug mode with support for the web interface from the command line.

```
mkdir build_debug
cd build_debug
cmake -DWEBINTERFACE=1 -DCMAKE_BUILD_TYPE=Debug ..
make
$ git clone https://github.com/karmaresearch/vlog.git
$ cd vlog
$ mkdir build_debug
$ cd build_debug
$ cmake -DWEBINTERFACE=1 -DCMAKE_BUILD_TYPE=Debug ..
$ make -j
```

Please note that the option `-j` for make will build VLog in parallel

## Docker

In case you do not want to compile the program, you can use a Docker image that
Expand Down
67 changes: 67 additions & 0 deletions include/parser/ruledriver.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#ifndef __MCDRIVER_HPP__
#define __MCDRIVER_HPP__ 1

#include <string>
#include <cstddef>
#include <istream>
#include <sstream>

#include "rulescanner.h"
#include "ruleparser.tab.hh"

namespace MC{

class RuleAST{
public:
RuleAST(std::string type="",
std::string value="",
RuleAST *first=NULL,
RuleAST *second=NULL):
type(type),
value(value),
first(first),
second(second){}
void print(int sep=0);
~RuleAST();
std::string getType();
std::string getValue();
MC::RuleAST *getFirst();
MC::RuleAST *getSecond();
private:
std::string type;
std::string value;
RuleAST *first;
RuleAST *second;
void print_indented(std::string to_indent, int spaces);
};


class RuleDriver{
private:
MC::RuleAST *root = nullptr;
MC::RuleParser *parser = nullptr;
MC::RuleScanner *scanner = nullptr;
void parse_helper( std::istream &stream );
public:
RuleDriver() = default;
~RuleDriver(){
delete(scanner);
scanner = nullptr;
delete(parser);
parser = nullptr;
delete(root);
root = nullptr;
};

//! parse - parse from a file
void parse( std::string filename );
//! parse - parse from a file
void parse( const char *filename );
//! parse - parse from a c++ input stream
void parse( std::istream &iss );
void set_root(RuleAST *newroot){ root = newroot;}
RuleAST *get_root(){ return root;}
};
} /* end namespace MC */
#endif /* END __MCDRIVER_HPP__ */

38 changes: 38 additions & 0 deletions include/parser/rulescanner.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#ifndef __MCSCANNER_HPP__
#define __MCSCANNER_HPP__ 1

#if ! defined(yyFlexLexerOnce)
#include <FlexLexer.h>
#endif

#include "ruleparser.tab.hh" //automatically generated
#include "location.hh" //automatically generated

namespace MC{

class RuleScanner : public yyFlexLexer{
public:

RuleScanner(std::istream *in) : yyFlexLexer(in) {};

virtual ~RuleScanner() {};

//get rid of override virtual function warning
using FlexLexer::yylex;

virtual
int yylex( MC::RuleParser::semantic_type * const lval,
MC::RuleParser::location_type *location );
// YY_DECL defined in rulelexer.l
// Method body created by flex in rulelexer.yy.cc


private:
/* yyval ptr */
MC::RuleParser::semantic_type *yylval = nullptr;
};

} /* end namespace MC */

#endif /* END __MCSCANNER_HPP__ */

7 changes: 7 additions & 0 deletions include/vlog/concepts.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
#include <map>
#include <unordered_map>

#include <parser/ruledriver.h>
#include <parser/rulescanner.h>

/*** PREDICATES ***/
#define EDB 0
#define IDB 1
Expand Down Expand Up @@ -515,6 +518,10 @@ class Program {

VLIBEXP std::string readFromString(std::string rules, bool rewriteMultihead = false);

VLIBEXP void parseRuleFile(std::string pathFile, bool rewriteMultihead = false);
VLIBEXP void parseAST(MC::RuleAST *root, bool rewriteMultihead, Dictionary *dictVariables, std::vector<Literal> *listOfLiterals, std::vector<VTerm> *terms);
VLIBEXP void parseVariableAST(MC::RuleAST *root, Dictionary *dictVariables, std::vector<VTerm> *terms);
VLIBEXP void parseConstantAST(MC::RuleAST *root, std::vector<VTerm> *terms);
PredId_t getPredicateID(std::string &p, const uint8_t card);

VLIBEXP std::string getPredicateName(const PredId_t id);
Expand Down
17 changes: 13 additions & 4 deletions src/launcher/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,7 @@ bool initParams(int argc, const char** argv, ProgramArgs &vm) {
query_options.add<bool>("","no-filtering", false, "Disable filter optimization.",false);
query_options.add<bool>("","no-intersect", false, "Disable intersection optimization.",false);
query_options.add<string>("","graphfile", "", "Path to store the rule dependency graph",false);
query_options.add<bool>("","useParserGenerator", false, "Use flex + bison as parser generator for rule, and (in the future) data", false);

ProgramArgs::GroupArgs& load_options = *vm.newGroup("Options for <load>");
load_options.add<string>("i","input", "",
Expand Down Expand Up @@ -537,10 +538,18 @@ void launchFullMat(int argc,
std::string pathRules) {
//Load a program with all the rules
Program p(&db);
std::string s = p.readFromFile(pathRules,vm["rewriteMultihead"].as<bool>());
if (!s.empty()) {
LOG(ERRORL) << s;
return;

if (vm["useParserGenerator"].as<bool>()){
//use parser generator
p.parseRuleFile(pathRules, vm["useParserGenerator"].as<bool>());
}
else {
//use previous method to parse rules
std::string s = p.readFromFile(pathRules,vm["rewriteMultihead"].as<bool>());
if (!s.empty()) {
LOG(ERRORL) << s;
return;
}
}

//Existential check
Expand Down
106 changes: 106 additions & 0 deletions src/parser/ruledriver.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
#include <cctype>
#include <fstream>
#include <cassert>

#include <parser/ruledriver.h>


void MC::RuleDriver::parse( std::string filename ) {
assert( filename != "" );
std::ifstream in_file( filename );
parse_helper(in_file);
return;
}

void MC::RuleDriver::parse( const char * filename ) {
assert( filename != nullptr );
std::ifstream in_file( filename );
parse_helper(in_file);
return;
}


void MC::RuleDriver::parse( std::istream &stream ) {
if( ! stream.good() && stream.eof() ) {
return;
}
//else
parse_helper( stream );
return;
}


void MC::RuleDriver::parse_helper( std::istream &stream ) {
delete(scanner);
try {
scanner = new MC::RuleScanner( &stream );
}
catch( std::bad_alloc &ba ) {
std::cerr << "Failed to allocate scanner: (" <<
ba.what() << "), exiting!!\n";
exit( EXIT_FAILURE );
}
delete(parser);
try {
parser = new MC::RuleParser( (*scanner) /* scanner */,
(*this) /* driver */ );
}
catch( std::bad_alloc &ba ) {
std::cerr << "Failed to allocate parser: (" <<
ba.what() << "), exiting!!\n";
exit( EXIT_FAILURE );
}
const int accept( 0 );
if( parser->parse() != accept ) {
std::cerr << "Parse failed!!\n";
}
return;
}


void MC::RuleAST::print(int sep/*=0*/){
std::cout << "starting print" << std::endl;
print_indented("type: " + type, sep);
print_indented("value: " + value, sep);
print_indented("first: ", sep);
if (first != NULL)
first->print(sep+4);
else
std::cout << std::endl;
print_indented("second: ", sep);
if (second != NULL)
second->print(sep+4);
else
std::cout << std::endl;
std::cout << "ending print" << std::endl;
}


void MC::RuleAST::print_indented(std::string to_indent, int spaces) {
std::string ret = "";
for (int i = 0; i < spaces; i++ )
ret += " ";
ret += to_indent;
std::cout << ret << std::endl;
}


MC::RuleAST::~RuleAST() {
if (first !=NULL)
delete(first);
if (second !=NULL)
delete(second);
}

std::string MC::RuleAST::getType(){
return type;
}
std::string MC::RuleAST::getValue(){
return value;
}
MC::RuleAST *MC::RuleAST::getFirst(){
return first;
}
MC::RuleAST *MC::RuleAST::getSecond(){
return second;
}
Loading