Skip to content

Commit

Permalink
Fixed stuff.
Browse files Browse the repository at this point in the history
  • Loading branch information
bl0b committed Sep 9, 2012
1 parent c40f183 commit 3aad9d7
Show file tree
Hide file tree
Showing 10 changed files with 156 additions and 59 deletions.
4 changes: 3 additions & 1 deletion gen_pc.pl
Expand Up @@ -19,7 +19,9 @@ ()
my $pkgconfigdir = &check_dir(@env_pkgconfig, "/usr/local/lib/pkgconfig", "/usr/share/pkgconfig", "/usr/lib/pkgconfig") or die("Can't locate pkg-config install directory");

open my $fd, "<$input";
my $pcfile = "$pkgconfigdir/$input";
my $basename = $input;
$basename =~ s/.*\///;
my $pcfile = "$pkgconfigdir/$basename";
$pcfile =~ s/\.in$//;
open my $out, ">$pcfile" or die("Couldn't open file $pcfile for writing.\n");

Expand Down
23 changes: 17 additions & 6 deletions src/Makefile
@@ -1,3 +1,11 @@
INSTALL_PREFIX=/usr/local


INSTALL_BIN=$(INSTALL_PREFIX)/bin
INSTALL_INCLUDE=$(INSTALL_PREFIX)/include/tinyap
INSTALL_LIB=$(INSTALL_PREFIX)/lib


CSRC=ape_prettyprint.c bootstrap.c parse_context.c pilot_manager.c serialize.c stack.c token_utils.c walkableast.c walker.c
CPPSRC=lr.cc ast.cc tinyap.cc unparser.cc trie.cc tinyap_alloc.cc string_registry.cc

Expand Down Expand Up @@ -93,9 +101,12 @@ flavours: $(FLAVOURS)
FORCE:

install:
mkdir -p /usr/local/lib/tinyap
mkdir -p /usr/local/include/tinyap
cp -v opt/libtinyap.so /usr/local/lib/tinyap
rm -f /usr/local/include/tinyap/*.h
cp -v *.h /usr/local/include/tinyap
cp opt/tinyap /usr/local/bin
mkdir -p $(INSTALL_LIB)
mkdir -p $(INSTALL_BIN)
mkdir -p $(INSTALL_INCLUDE)
cp -v opt/libtinyap.so $(INSTALL_LIB)
rm -f $(INSTALL_INCLUDE)/*.h
cp -v *.h $(INSTALL_INCLUDE)
cp opt/tinyap $(INSTALL_BIN)
../gen_pc.pl ../tinyap.pc.in $(INSTALL_PREFIX) 2.0

15 changes: 14 additions & 1 deletion src/lr.cc
Expand Up @@ -35,7 +35,6 @@ namespace item {
static ext::hash_map<const ast_node_t, base*, lr::hash_an, lr::ptr_eq<_ast_node_t> >& registry = _static_init.grammar_registry;

ext::hash_map<const char*, token::Nt*>& token::Nt::registry = _static_init.nt_registry;
ext::hash_map<const char*, trie_t>& token::Bow::all = _static_init.trie_registry;

void clean_registry_at_exit() {
typedef ext::hash_map<const ast_node_t, base*, lr::hash_an, lr::ptr_eq<_ast_node_t> >::iterator mapiter_t;
Expand Down Expand Up @@ -374,6 +373,20 @@ namespace combination {
cts = alt;
}

void Prefix::contents(Grammar* g, item::base* x) {
_ = item::token::Nt::instance(rule::base::auto_tag<Prefix>());
cts = item::gc(new Seq())
->add(visitors::item_rewriter(g).process(x))
->add(item::token::Nt::instance(tag()));
}

void Postfix::contents(Grammar* g, item::base* x) {
_ = item::token::Nt::instance(rule::base::auto_tag<Postfix>());
cts = item::gc(new Seq())
->add(visitors::item_rewriter(g).process(x))
->add(item::token::Nt::instance(tag()));
}

} /* namespace combination */
} /* namespace item */

Expand Down
99 changes: 74 additions & 25 deletions src/lr.h
Expand Up @@ -207,9 +207,11 @@ namespace lr {
int id;
item_set items;
struct transitions_ {
follow_set_text from_text;
follow_set_text from_text_t;
follow_set_text from_text_bow;
follow_set_text from_text_re;
follow_set_stack from_stack;
transitions_() : from_text(), from_stack() {}
transitions_() : from_text_t(), from_text_bow(), from_text_re(), from_stack() {}
} transitions;
item_set reductions;
state(item_set& s) : id(0), items(s), transitions(), reductions() {}
Expand All @@ -235,26 +237,33 @@ namespace lr {
o << "===(" << std::setw(4) << S->id << ")======================================================" << std::endl;
o << S->items;
follow_set_text::const_iterator
fti = S->transitions.from_text.begin(),
ftj = S->transitions.from_text.end();
if(fti!=ftj) {
o << "-- Token transitions ------------------------------------------" << std::endl;
for(;fti!=ftj;++fti) {
if((*fti).second) { o << (*fti); }
}
fti = S->transitions.from_text_t.begin(),
ftj = S->transitions.from_text_t.end();
o << "-- Token transitions ------------------------------------------" << std::endl;
for(;fti!=ftj;++fti) {
if((*fti).second) { o << (*fti); }
}
fti = S->transitions.from_text_bow.begin();
ftj = S->transitions.from_text_bow.end();
for(;fti!=ftj;++fti) {
if((*fti).second) { o << (*fti); }
}
fti = S->transitions.from_text_re.begin();
ftj = S->transitions.from_text_re.end();
for(;fti!=ftj;++fti) {
if((*fti).second) { o << (*fti); }
}

follow_set_stack::const_iterator
fsi = S->transitions.from_stack.begin(),
fsj = S->transitions.from_stack.end();
o << "-- Non-terminal transitions -----------------------------------" << std::endl;
if(fsi!=fsj) {
o << "-- Non-terminal transitions -----------------------------------" << std::endl;
for(;fsi!=fsj;++fsi) {
if((*fsi).second) { o << (*fsi); }
}
}
if(S->reductions.size()) {
o << "-- Reductions -------------------------------------------------" << std::endl << S->reductions;
}
o << "-- Reductions -------------------------------------------------" << std::endl << S->reductions;
return o;
}

Expand Down Expand Up @@ -321,10 +330,18 @@ push u onto stack
item_set K;
A->kernel(S->items, K);
buffer << K;
if(S->transitions.from_text.size()) {
if(S->transitions.from_text_t.size()
+ S->transitions.from_text_bow.size()
+ S->transitions.from_text_re.size()) {
buffer << "expected one of ";
grammar::visitors::debugger d(buffer);
for(ti=S->transitions.from_text.begin(), tj=S->transitions.from_text.end();ti!=tj;++ti) {
for(ti=S->transitions.from_text_t.begin(), tj=S->transitions.from_text_t.end();ti!=tj;++ti) {
((grammar::item::base*)(*ti).first)->accept(&d); buffer << ' ';
}
for(ti=S->transitions.from_text_bow.begin(), tj=S->transitions.from_text_bow.end();ti!=tj;++ti) {
((grammar::item::base*)(*ti).first)->accept(&d); buffer << ' ';
}
for(ti=S->transitions.from_text_re.begin(), tj=S->transitions.from_text_re.end();ti!=tj;++ti) {
((grammar::item::base*)(*ti).first)->accept(&d); buffer << ' ';
}
} else {
Expand Down Expand Up @@ -496,17 +513,17 @@ push u onto stack
GOTO = GOTO_;
tr->accept(this);
}
virtual void visit(grammar::item::token::Str* x) { S->transitions.from_text[x] = GOTO; }
virtual void visit(grammar::item::token::Re* x) { S->transitions.from_text[x] = GOTO; }
virtual void visit(grammar::item::token::Str* x) { S->transitions.from_text_t[x] = GOTO; }
virtual void visit(grammar::item::token::Re* x) { S->transitions.from_text_re[x] = GOTO; }
virtual void visit(grammar::item::token::Epsilon* x) {}
virtual void visit(grammar::item::token::Eof* x) { S->transitions.from_text[x] = GOTO; }
virtual void visit(grammar::item::token::Comment* x) { S->transitions.from_text[x] = GOTO; }
virtual void visit(grammar::item::token::T* x) { S->transitions.from_text[x] = GOTO; }
virtual void visit(grammar::item::token::Eof* x) { S->transitions.from_text_t[x] = GOTO; }
virtual void visit(grammar::item::token::Comment* x) { S->transitions.from_text_re[x] = GOTO; }
virtual void visit(grammar::item::token::T* x) { S->transitions.from_text_t[x] = GOTO; }
virtual void visit(grammar::item::token::Nt* x) { S->transitions.from_stack[x->tag()] = GOTO; }
virtual void visit(grammar::item::token::Bow* x) { S->transitions.from_text[x] = GOTO; }
virtual void visit(grammar::item::token::AddToBag* x) { S->transitions.from_text[x] = GOTO; }
virtual void visit(grammar::item::token::Bow* x) { S->transitions.from_text_bow[x] = GOTO; }
virtual void visit(grammar::item::token::AddToBag* x) { S->transitions.from_text_bow[x] = GOTO; }

virtual void visit(grammar::item::combination::RawSeq* x) { S->transitions.from_text[x] = GOTO; }
virtual void visit(grammar::item::combination::RawSeq* x) { S->transitions.from_text_re[x] = GOTO; }
};

void compute_transitions(item_set& items, follow_set_builder& transitions) {
Expand Down Expand Up @@ -605,7 +622,7 @@ push u onto stack
#define _tinyap_min(a, b) (a<b?a:b)
item_set ker;
kernel(S->items, ker);
char* aststr = (char*)ast_serialize_to_string(n->get_state_ast());
char* aststr = (char*)ast_serialize_to_string(n->ast, 0);
std::clog << " === ACTIVE STATE ===(" << S->id << ") @" << n->id.O << ':'
<< std::string(buffer+n->id.O, _tinyap_min(buffer+n->id.O+20, buffer+size)) << std::endl
<< ker << "ast : " << aststr << std::endl; free(aststr);
Expand All @@ -623,7 +640,7 @@ push u onto stack
* PHASE 2 : SHIFT
*/

for(ti=S->transitions.from_text.begin(), tj=S->transitions.from_text.end();ti!=tj;++ti) {
for(ti=S->transitions.from_text_t.begin(), tj=S->transitions.from_text_t.end();ti!=tj && didnt_shift;++ti) {
if(!(*ti).second) {
std::cerr << "null entry in transition table !" << std::endl;
continue;
Expand All @@ -639,6 +656,38 @@ push u onto stack
}
}

for(ti=S->transitions.from_text_bow.begin(), tj=S->transitions.from_text_bow.end();ti!=tj && didnt_shift;++ti) {
if(!(*ti).second) {
std::cerr << "null entry in transition table !" << std::endl;
continue;
}
const grammar::item::base* token = (*ti).first;
std::pair<ast_node_t, unsigned int> ret = token->recognize(buffer, ofs, size);
/*std::clog << "follow by "; ((grammar::item::base*)token)->accept(&debug); std::clog << " => " << ((int)(ret.first?ret.second:-1)) << std::endl;*/
if(ret.first) {
didnt_shift=false;
/*ret.first->raw.ref++;*/
stack->shift(n, (grammar::item::base*)(*ti).first, (*ti).second, ret.first, ret.second, NULL);
/*delete_node(ret.first);*/
}
}

for(ti=S->transitions.from_text_re.begin(), tj=S->transitions.from_text_re.end();ti!=tj && didnt_shift;++ti) {
if(!(*ti).second) {
std::cerr << "null entry in transition table !" << std::endl;
continue;
}
const grammar::item::base* token = (*ti).first;
std::pair<ast_node_t, unsigned int> ret = token->recognize(buffer, ofs, size);
/*std::clog << "follow by "; ((grammar::item::base*)token)->accept(&debug); std::clog << " => " << ((int)(ret.first?ret.second:-1)) << std::endl;*/
if(ret.first) {
didnt_shift=false;
/*ret.first->raw.ref++;*/
stack->shift(n, (grammar::item::base*)(*ti).first, (*ti).second, ret.first, ret.second, NULL);
/*delete_node(ret.first);*/
}
}

/*
* PHASE 1 : REDUCE
*/
Expand Down
37 changes: 25 additions & 12 deletions src/lr_grammar.h
Expand Up @@ -468,7 +468,7 @@ namespace grammar {
const char* tag_;
bool keep_;
trie_t mybow;
static ext::hash_map<const char*, trie_t>& all;
/*static ext::hash_map<const char*, trie_t>& all;*/
public:
Bow(const char*_, bool k) : tag_(_), keep_(k) {
mybow = find(_);
Expand Down Expand Up @@ -500,6 +500,11 @@ namespace grammar {
return std::pair<ast_node_t, unsigned int>(NULL, offset);
}

static ext::hash_map<const char*, trie_t>& _registry() {
static ext::hash_map<const char*, trie_t> all;
return all;
}

static trie_t find(const char*tag) {
/*static struct tries : ext::hash_map<const char*, trie_t> {
~tries() {
Expand All @@ -509,9 +514,9 @@ namespace grammar {
}
}
} all;*/
trie_t ret = all[tag];
trie_t ret = _registry()[tag];
if(!ret) {
all[tag] = ret = trie_new();
_registry()[tag] = ret = trie_new();
}
return ret;
}
Expand All @@ -531,16 +536,22 @@ namespace grammar {
}
}
public:
AddToBag(const char* p, const char* b, bool k) : Re_base<AddToBag>(p), keep_(k), bag(Bow::find(b)), tag_(b) {
/*std::cout << "NEW AddToBag(" << pattern() << ", " << tag() << ", " << keep() << std::endl;*/
AddToBag(const char* p, const char* b, bool k)
: Re_base<AddToBag>(p), keep_(k),
bag(Bow::find(b)),
tag_(b) {
/*std::cout << "NEW AddToBag(" << pattern() << ", " << tag()*/
/*<< ", " << (keep() ? "keep" : "discard") << " "*/
/*<< ((void*)bag) << std::endl;*/
}
bool keep() const { return keep_; }
const char* tag() const { return tag_; }
virtual std::pair<ast_node_t, unsigned int> recognize(const char* source, unsigned int offset, unsigned int size) const {
std::pair<ast_node_t, unsigned int> ret = Re_base<AddToBag>::recognize(source, offset, size);
/*std::cout << "AddToBag => " << ret.first << " +" << ret.second << std::endl;*/
if(ret.second>offset) {
/*std::cout << "AddToBag(" << ((void*)bag) << ") => " << Value(Car(ret.first)) << " +" << ret.second << std::endl;*/
trie_insert(bag, Value(Car(ret.first)));
/*trie_dump(bag, 0);*/
}
if(keep_) {
return ret;
Expand Down Expand Up @@ -1050,27 +1061,29 @@ namespace grammar {

class Prefix : public tagged_single<Prefix> {
protected:
virtual void contents(Grammar*g, item::base*x) {}
virtual void contents(Grammar*g, item::base*x); // {}
public:
typedef rule::Prefix expansion_rule_type;
using single<Prefix>::contents;
using single<Prefix>::commit;
Prefix(Grammar*g, item::base* prefix, const char* nt) : tagged_single<Prefix>(nt) {
_ = item::token::Nt::instance(rule::base::auto_tag<Prefix>());
cts = gc(new Seq())->add(prefix)->add(item::token::Nt::instance(nt));
/*_ = item::token::Nt::instance(rule::base::auto_tag<Prefix>());*/
/*cts = gc(new Seq())->add(prefix)->add(item::token::Nt::instance(nt));*/
contents(g, prefix);
}
};

class Postfix : public tagged_single<Postfix> {
protected:
virtual void contents(Grammar*g, item::base*x) {}
virtual void contents(Grammar*g, item::base*x);// {}
public:
typedef rule::Postfix expansion_rule_type;
using single<Postfix>::contents;
using single<Postfix>::commit;
Postfix(Grammar*g, item::base* postfix, const char* nt) : tagged_single<Postfix>(nt) {
_ = item::token::Nt::instance(rule::base::auto_tag<Postfix>());
cts = gc(new Seq())->add(postfix)->add(token::Nt::instance(nt));
/*_ = item::token::Nt::instance(rule::base::auto_tag<Postfix>());*/
/*cts = gc(new Seq())->add(postfix)->add(token::Nt::instance(nt));*/
contents(g, postfix);
}
};
}
Expand Down
4 changes: 2 additions & 2 deletions src/static_init.h
Expand Up @@ -53,8 +53,8 @@ struct tinyap_static_init {
pair_registry_t pair_registry;
ext::hash_map<const ast_node_t, grammar::item::base*, lr::hash_an, lr::ptr_eq<_ast_node_t> > grammar_registry;
ext::hash_map<const char*, grammar::item::token::Nt*> nt_registry;
ext::hash_map<const char*, trie_t> trie_registry;
tinyap_static_init() { tinyap_init(); }
ext::hash_map<const char*, trie_t>& trie_registry;
tinyap_static_init() : trie_registry(grammar::item::token::Bow::_registry()) { tinyap_init(); }
~tinyap_static_init() { tinyap_terminate(); }
};

Expand Down
8 changes: 8 additions & 0 deletions src/test.cc
Expand Up @@ -529,6 +529,14 @@ int test_automaton(int n=-1) {
{ "(OperatorRule X (RawSeq (T ~) (RE [^~,]?) (T ,) (RE [^~,]?) (T ~)))", " ~\",\"~ ", "((X:7 \":2 \":4))" },
{ "(OperatorRule X (BOW _test !))", "pouet", "((X:5 pouet:0))" },
{ "(OperatorRule X (BOW _test ))", "pouet", "((X:5))" },
{ "(OperatorRule X (Prefix (Prefix (NT a) (NT b)) (NT b)))"
"(OperatorRule a (RE toto))"
"(OperatorRule b (RE pouet))"
, "toto pouet pouet", "((X:16 (b:16 (b:11 (a:5 toto:0) pouet:5) pouet:11)))"
},
{ "(OperatorRule X (Seq (AddToBag (RE toto) bag !) (BOW bag !)))", "toto toto", "((X:9 toto:0 toto:5))" },
{ "(OperatorRule X (Seq (AddToBag (RE toto) bag !) (BOW bag )))", "toto toto", "((X:9 toto:0))" },
/*{ "(OperatorRule X (Seq (AddToBag (RE toto) bag ) (BOW bag !)))", "toto toto", "((X:9 toto:5))" },*/
#endif
{ NULL, NULL, NULL }
};
Expand Down

0 comments on commit 3aad9d7

Please sign in to comment.