Permalink
Browse files

tinyap basically works now.

git-svn-id: https://tinyap.googlecode.com/svn/trunk@127 26adf5cf-fd31-0410-b5e7-cb6bf36da140
  • Loading branch information...
1 parent 88997ec commit bb629aaaefc9e3a57aa11f264318496dcd782bb9 damien.leroux committed Apr 3, 2011
Showing with 163 additions and 266 deletions.
  1. +3 −3 src/lr/Makefile
  2. +30 −18 src/lr/ast.cc
  3. +21 −22 src/lr/ast.h
  4. +16 −11 src/lr/bootstrap.c
  5. +23 −21 src/lr/lr.h
  6. +4 −4 src/lr/lr_grammar.h
  7. +15 −10 src/lr/lr_gss.h
  8. +8 −3 src/lr/main.c
  9. +1 −1 src/lr/pilot_manager.c
  10. +1 −1 src/lr/test.cc
  11. +27 −165 src/lr/test.gram
  12. +12 −5 src/lr/tinyap.cc
  13. +1 −1 src/lr/tinyap.h
  14. +1 −1 src/lr/unparser.cc
View
@@ -7,7 +7,7 @@ COBJ=$(subst .c,.o,$(CSRC))
CPPOBJ=$(subst .cc,.o,$(CPPSRC))
-#PROF=-pg
+PROF=-pg
CFLAGS=-ggdb -Wall $(PROF)
CPPFLAGS=-ggdb -Wno-deprecated -Wall $(PROF)
@@ -33,8 +33,8 @@ $(TEST): $(COBJ) $(CPPOBJ) test.o
$(LD) $(COBJ) $(CPPOBJ) test.o -o $@
.depend: $(CSRC) $(CPPSRC) main.c test.cc Makefile
- $(CC) --depend $(CSRC) > $@
- $(CXX) --depend $(CPPSRC) >> $@
+ $(CC) --depend $(CSRC) main.c > $@
+ $(CXX) --depend $(CPPSRC) test.cc >> $@
main.o $(COBJ):%.o:%.c Makefile
$(CC) -c $< -o $@
View
@@ -42,7 +42,7 @@ static tinyap_stack_t node_stack;
void node_pool_init() {
- node_stack = new_stack();
+ /*node_stack = new_stack();*/
}
@@ -61,25 +61,26 @@ ast_node_t node_alloca() {
if(!ret) {
ret = tinyap_alloc(union _ast_node_t);
_node_alloc_count+=1;
- push(node_stack,ret);
+ /*push(node_stack,ret);*/
} else {
node_pool = node_pool->pool.next;
}
return ret;
}
void node_dealloc(ast_node_t node) {
- /*if(node&&node->type!=ast_Pool) {*/
- /*node->type = ast_Pool;*/
- /*node->pool.next = node_pool;*/
- /*node_pool = node;*/
- /*}*/
- (void) (node&&node->type!=ast_Pool ? node->type = ast_Pool, node->pool.next = node_pool, node_pool = node : 0);
+ if(node&&node->type!=ast_Pool) {
+ node->type = ast_Pool;
+ node->pool.next = node_pool;
+ node_pool = node;
+ }
+ /*(void) (node&&node->type!=ast_Pool ? node->type = ast_Pool, node->pool.next = node_pool, node_pool = node : 0);*/
}
void delete_node(ast_node_t);
void node_pool_flush() {
+#if 0
ast_node_t n;
tinyap_stack_t tmp_stack = new_stack();
@@ -111,14 +112,15 @@ void node_pool_flush() {
free_stack(tmp_stack);
// printf("after node pool flush, %i nodes remain\n",_node_alloc_count);
+#endif
}
void node_pool_term() {
/*printf("node_pool_term\n"); fflush(stdout);*/
- node_pool_flush();
- free_stack(node_stack);
- node_stack = NULL;
+ /*node_pool_flush();*/
+ /*free_stack(node_stack);*/
+ /*node_stack = NULL;*/
}
typedef std::pair<const char*, size_t> atom_key;
@@ -165,13 +167,15 @@ ast_node_t newAtom(const char*data,size_t offset) {
atom_registry[atom_key(ret->atom._str, offset)] = ret;
ret->type=ast_Atom;
ret->raw._p2=NULL; /* useful for regexp cache hack */
- ret->pos.row = offset;
- ret->pos.col=0;
+ ret->pos.offset = offset;
+ /*ret->pos.col=0;*/
ret->node_flags=0;
+ ret->raw.ref = 0;
/*} else {*/
/*std::cout << "reuse alloc'd atom " << ret;*/
}
/*std::cout << " => " << ret->atom._str << std::endl;*/
+ ret->raw.ref++;
return ret;
}
@@ -187,12 +191,16 @@ ast_node_t newPair(const ast_node_t a,const ast_node_t d) {
ret->type=ast_Pair;
ret->pair._car=(ast_node_t )a;
ret->pair._cdr=(ast_node_t )d;
- ret->pos.row=0;
- ret->pos.col=0;
+ if(a) { ((ast_node_t)a)->raw.ref++; }
+ if(d) { ((ast_node_t)d)->raw.ref++; }
+ ret->pos.offset=0;
+ /*ret->pos.col=0;*/
ret->node_flags=0;
+ ret->raw.ref=0;
/*} else {*/
/*std::cout << "reuse alloc'd pair " << ret << std::endl;*/
}
+ ret->raw.ref++;
return ret;
}
@@ -201,14 +209,18 @@ ast_node_t newPair(const ast_node_t a,const ast_node_t d) {
void delete_node(ast_node_t n) {
// static int prout=0;
if(!n) return;
+ n->raw.ref--;
+ if(n->raw.ref) {
+ return;
+ }
switch(n->type) {
case ast_Atom:
/*assert(n->atom._str);*/
/*free(n->atom._str);*/
if(!(n->node_flags&ATOM_IS_NOT_STRING)) {
- unregstr(n->atom._str);
+ /*unregstr(n->atom._str);*/
}
- if(n->raw._p2) {
+ /*if(n->raw._p2) {*/
/* regex cache hack */
// printf("prout %i\n",prout+=1);
/*regfree(n->raw._p2);*/
@@ -217,7 +229,7 @@ void delete_node(ast_node_t n) {
/* FIXME ! */
/*pcre_free(n->raw._p2);*/
- }
+ /*}*/
break;
case ast_Pair:
delete_node(n->pair._car);
View
@@ -50,9 +50,9 @@ union _ast_node_t {
ast_type_t _;
void*_p1;
void*_p2;
- int _row;
- int _col;
+ unsigned int _offset;
int _flags;
+ int ref;
} raw;
void*__align[8];
struct {
@@ -64,8 +64,7 @@ union _ast_node_t {
struct {
ast_type_t _res_at;
void*__res[2];
- int row;
- int col;
+ unsigned int offset;
} pos;
/* direct access to type field */
ast_type_t type;
@@ -107,26 +106,26 @@ union _ast_node_t {
*/
#define getCdr(__n) (ast_type_check((__n),ast_Pair,__FILE__,__LINE__)->pair._cdr)
-static inline int getRow(ast_node_t n) {
+static inline int getOffset(ast_node_t n) {
if(!n) return 0;
- return n->pos.row;
-}
-
-static inline int getCol(ast_node_t n) {
- if(!n) return 0;
- return n->pos.col;
-}
-
-static inline void setRow(ast_node_t n,int r) {
- if(!n) return;
- n->pos.row=r;
-}
-
-static inline void setCol(ast_node_t n,int c) {
- if(!n) return;
- n->pos.col=c;
+ return n->pos.offset;
}
+/*static inline int getCol(ast_node_t n) {*/
+ /*if(!n) return 0;*/
+ /*return n->pos.col;*/
+/*}*/
+/**/
+/*static inline void setRow(ast_node_t n,int r) {*/
+ /*if(!n) return;*/
+ /*n->pos.row=r;*/
+/*}*/
+/**/
+/*static inline void setCol(ast_node_t n,int c) {*/
+ /*if(!n) return;*/
+ /*n->pos.col=c;*/
+/*}*/
+/**/
#define isAtom(__n) ((__n)&&(__n)->type==ast_Atom)
#define isPair(__n) ((__n)&&(__n)->type==ast_Pair)
@@ -186,7 +185,7 @@ static inline ast_node_t ast_type_check(const ast_node_t n,const ast_type_t expe
ast_node_t newAtom(const char*data, size_t offset);
ast_node_t newPair(const ast_node_t a,const ast_node_t d);
-//void delete_node(node_cache_t cache, ast_node_t n);
+void delete_node(ast_node_t n);
void print_pair(ast_node_t n);
View
@@ -58,8 +58,8 @@ const char*short_rules = "((Grammar\n"
"(OperatorRule T (STR \" \"))\n"
"(OperatorRule NT (NT symbol))\n"
"(OperatorRule RE (STR / /))\n"
-"(OperatorRule STR (RawSeq (T ~) (RE [^~,]?) (T ,)"
-" (RE [^~,]?) (T ~)))\n"
+"(OperatorRule STR (RawSeq (T ~) (RE \\\\\\\\?[^~,]?) (T ,)"
+" (RE \\\\\\\\?[^~,]?) (T ~)))\n"
"(OperatorRule BOW (RawSeq (T ~) (RE [_a-zA-Z][_a-zA-Z0-9]*) (RE !?) (T ~)))\n"
"(OperatorRule AddToBag (Seq (NT RE) (T :) (NT symbol) (RE !?)))\n"
"(Comment )"
@@ -73,25 +73,30 @@ const char*short_rules = "((Grammar\n"
"(OperatorRule Rep01 (Seq (NT single_norep) (T ?)))\n"
"(OperatorRule Rep1N (Seq (NT single_norep) (T +)))\n"
"(OperatorRule Rep0N (Seq (NT single_norep) (T *)))\n"
-
+"(TransientRule single (Alt (NT Rep01) (NT Rep0N) (NT Rep1N) (NT single_norep) (NT Space) (NT NewLine) (NT Indent) (NT Dedent)))\n"
+"(TransientRule single_norep (Alt (NT NT) (NT STR) (NT BOW) (NT AddToBag) (NT T) (NT RE) (NT Epsilon) (NT EOF) (NT sub_rmb)))\n"
+"(Comment )"
+"(Comment \\ Top-level)\n"
+"(Comment )"
"(OperatorRule TransientRule (Seq (NT symbol) (Space) (T =) (Space) (NT rmember) (T .) (NewLine)))\n"
"(OperatorRule OperatorRule (Seq (NT symbol) (Space) (T ::=) (Space) (NT rmember) (T .) (NewLine)))\n"
"(TransientRule rmember (Alt (NT Alt) (NT alt_elem)))\n"
-"(TransientRule single (Alt (NT Rep01) (NT Rep0N) (NT Rep1N) (NT single_norep) (NT Space) (NT NewLine) (NT Indent) (NT Dedent)))\n"
-"(TransientRule single_norep (Alt (NT NT) (NT STR) (NT BOW) (NT AddToBag) (NT T) (NT RE) (NT Epsilon) (NT EOF) (NT sub_rmb)))\n"
"(TransientRule sub_rmb (Seq (T \\() (Alt (NT Seq) (NT Alt) (NT RawSeq)) (T \\))))\n"
-"(OperatorRule Comment (Seq (Alt (RawSeq (T #) (RE [^\\r\\n]+)) (T #)) (NewLine)))\n"
+"(OperatorRule Comment (Seq (Alt (RawSeq (T #) (RE [^\\\\r\\\\n]+)) (T #)) (NewLine)))\n"
"(OperatorRule Grammar (Rep1N (Alt (NT TransientRule)"
" (NT OperatorRule)"
" (NT Comment))))"
+"(Comment )"
+"(Comment \\ Miscellaneous)\n"
+"(Comment )"
"(TransientRule _start (NT Grammar))\n"
-"(TransientRule _whitespace (RE [\\ \\r\\n\\t]+))\n"
+"(TransientRule _whitespace (RE [\\ \\\\r\\\\n\\t]+))\n"
"(OperatorRule EOF (T _EOF))\n"
"(OperatorRule Epsilon (T _epsilon))\n"
-"(OperatorRule Space (T Space))\n"
-"(OperatorRule NewLine (T NewLine))\n"
-"(OperatorRule Indent (T Indent))\n"
-"(OperatorRule Dedent (T Dedent))\n"
+"(OperatorRule Space (T .space))\n"
+"(OperatorRule NewLine (T .newline))\n"
+"(OperatorRule Indent (T .indent))\n"
+"(OperatorRule Dedent (T .dedent))\n"
/*"(OperatorRule BKeep (T !))\n"*/
/*"(TransientRule elem_atom (Alt (NT NT) (NT STR) (NT BOW) (NT AddToBag)"*/
/*" (NT T) (NT RE)\n"*/
View
@@ -274,7 +274,7 @@ push u onto stack
void closure(item_set& I, item_set& C) const {
item_set::iterator i, j;
C = I;
- std::cout << "C starts with " << C.size() << " elements" << std::endl;
+ /*std::cout << "C starts with " << C.size() << " elements" << std::endl;*/
std::vector<item> stack(I.begin(), I.end());
while(stack.size()>0) {
item i = stack.back();
@@ -284,11 +284,11 @@ push u onto stack
}
const token::Nt* nt = dynamic_cast<const token::Nt*>(grammar::visitors::item_rewriter(G).process((grammar::item::base*)*i));
if(nt) {
- std::cout << "have NT " << nt->tag() << std::endl;
+ /*std::cout << "have NT " << nt->tag() << std::endl;*/
grammar::Grammar::iterator S = G->find(nt->tag());
rule::base* r = (S==G->end()) ? NULL : S->second;
if(!r) {
- std::cout << "couldn't find rule " << nt->tag() << " !" << std::endl;
+ /*std::cout << "couldn't find rule " << nt->tag() << " !" << std::endl;*/
continue;
}
/* and we add an iterator to each variant of the rule */
@@ -387,10 +387,10 @@ push u onto stack
reduction_candidates(ret.first->items, ret.first->reductions);
stack.push_back(ret.first);
ret.first->id = states.size()-1;
- std::cout << " committed new set :" << std::endl << stack.back()->items;
- } else {
- std::cout << " didn't commit set :" << std::endl << s;
- std::cout << " because of set :" << std::endl << ret.first->items;
+ /*std::cout << " committed new set :" << std::endl << stack.back()->items;*/
+ /*} else {*/
+ /*std::cout << " didn't commit set :" << std::endl << s;*/
+ /*std::cout << " because of set :" << std::endl << ret.first->items;*/
}
return ret.first;
}
@@ -428,21 +428,21 @@ push u onto stack
const item_base* t = f((grammar::item::base*)**i);
if(!t) {
item x = *i;
- std::cout << "COIN " << x << " " << *x
- << " " << typeid(**i).name() << std::endl;
+ /*std::cout << "COIN " << x << " " << *x*/
+ /*<< " " << typeid(**i).name() << std::endl;*/
throw "COIN";
}
item tmp = (*i).next();
- std::cout << " transiting to " << tmp << std::endl;
+ /*std::cout << " transiting to " << tmp << std::endl;*/
std::pair<item_set::iterator, bool> ret = transitions[t].insert(tmp);
if(!ret.second) {
- item x = *ret.first;
- std::cout << "COIN transition pas ajoutée " << x << std::endl;
+ /*item x = *ret.first;*/
+ /*std::cout << "COIN transition pas ajoutée " << x << std::endl;*/
}
- grammar::visitors::debugger d(std::cout);
- std::cout << " => transitions[";
- ((item_base*)t)->accept(&d);
- std::cout << "] = " << transitions[t] << std::endl;
+ /*grammar::visitors::debugger d(std::cout);*/
+ /*std::cout << " => transitions[";*/
+ /*((item_base*)t)->accept(&d);*/
+ /*std::cout << "] = " << transitions[t] << std::endl;*/
}
}
@@ -484,7 +484,7 @@ push u onto stack
void dump_states() const {
state_set_dumper ssd(states.begin(), states.end());
- std::cout << "automaton has " << ssd.size() << " states." << std::endl;
+ /*std::cout << "automaton has " << ssd.size() << " states." << std::endl;*/
state_set::iterator i, j = ssd.end();
for(i=ssd.begin();i!=j;++i) {
/*std::cout << "===============================================================" << std::endl;*/
@@ -522,8 +522,9 @@ push u onto stack
if(!S) {
throw "COIN";
}
-#define min(a, b) (a<b?a:b)
- char* aststr = (char*)ast_serialize_to_string(n->ast); std::cout << " === ACTIVE STATE ===(" << S->id << ") @" << n->id.O << ':' << std::string(buffer+n->id.O, min(buffer+n->id.O+20, buffer+size)) << std::endl << "ast : " << aststr << std::endl; free(aststr);
+#define _tinyap_min(a, b) (a<b?a:b)
+ /*char* aststr = (char*)ast_serialize_to_string(n->ast); std::cout << " === ACTIVE STATE ===(" << S->id << ") @" << n->id.O << ':' << std::string(buffer+n->id.O, _tinyap_min(buffer+n->id.O+20, buffer+size)) << std::endl << "ast : " << aststr << std::endl; free(aststr);*/
+#undef _tinyap_min
item_set::iterator i, j;
follow_set_text::iterator ti, tj;
@@ -537,7 +538,7 @@ push u onto stack
}
const grammar::item::base* token = (*ti).first;
std::pair<ast_node_t, unsigned int> ret = token->recognize(buffer, ofs, size);
- std::cout << "follow by "; ((grammar::item::base*)token)->accept(&debug); std::cout << " => " << ret.first << " (" << ret.second << ')' << std::endl;
+ /*std::cout << "follow by "; ((grammar::item::base*)token)->accept(&debug); std::cout << " => " << ret.first << " (" << ret.second << ')' << std::endl;*/
if(ret.first) {
stack.shift(n, (grammar::item::base*)(*ti).first, (*ti).second, ret.first, ret.second);
}
@@ -546,7 +547,8 @@ push u onto stack
for(i=S->reductions.begin(), j=S->reductions.end();i!=j;++i) {
gss::node*ok = stack.reduce(n, *i, ofs);
item x = *i;
- if(ok) { std::cout << "reduce by " << x << " => " << ok->id.S->id << std::endl; }
+ if(ok) {}
+ /*if(ok) { std::cout << "reduce by " << x << " => " << ok->id.S->id << std::endl; }*/
}
}
farthest = G->skip(buffer, farthest, size);
Oops, something went wrong.

0 comments on commit bb629aa

Please sign in to comment.