?? parser.c
字號:
pg->obj_inbufcnt = (*pg->readfunc)(obj, pg->obj_inbuf, 128);
}
if(pg->obj_inbufcnt > 0 )
return pg->obj_inbuf[pg->obj_inbufsize - pg->obj_inbufcnt--];
return -1;
}
imethod int
Parse(object self, void *is, char *filename)
{
PG *pg = (PG *)ivPtr(self);
LTABLE *lp = pg->ltab;
PTABLE *pp = pg->ptab;
int debug = pg->debug & 4;
int isobject;
pg->in_file = is;
if(IsObj((object)is))
{
isobject = 1;
pg->readfunc = (void*)imiPointer((object)is, gRead);
}
else
isobject = 0;
for(;;)
{
short lextoken = 0;
int result;
/* Run the lexer */
while(lextoken == 0)
{
if(lp->token < 0)
{
newchr:
if(isobject)
lp->token = read_object(is, pg);
else
lp->token = cfgetc(((cfFILE*)is));
if(lp->token == 0)
goto newchr;
if(lp->token < 0)
{/* EOF */
lp->token = 0;
}
else lp->token &= lp->maxtoken;
if(lp->token == '\n')
{
++pg->line_numb;
pg->line_char = 0;
if(debug)
cfeprintf("\n");
}
else if(lp->token == '\r')
{
goto newchr;
}
else
{
++pg->line_char;
if(debug)
cfeprintf("%c", lp->token);
}
}
lextoken = do_lex(lp);
if(lextoken < 0)
{
return(lextoken);
}
}
/* Run the parser */
pp->token = lextoken;
result = do_parse(pp);
if(result)
{
if(result < 0)
{
return(1);
}
else
{
if(pg->root == 0)
return 2;
return(0);
}
}
}
}
InitMethod()
{
if (CLASS)
return;
CLASS = gNew(Class, cName, ivSize, 0, END);
cMethod(New);
iMethod(New);
iMethod(Parse);
iMethod(Dispose);
iMethodFor(gDeepDispose, Dispose);
iMethodFor(gGCDispose, Dispose);
gDontCollect(CLASS);
}
/* COMMON STUFF */
static void
LoadParserPointers(void *yp, LODTABLE *lp, int notlex)
{
char *xp = (char *) lp;
PTABLE *pp = yp;
#define SPOINTER(a) ((short *)(xp + lp->a))
#define CPOINTER(a) ((char *)(xp + lp->a))
#define LPOINTER(a) ((long *)(xp + lp->a))
#define VALUE(a) ((short)(lp->a))
pp->D_red = SPOINTER(t_D_red);
pp->R_start = SPOINTER(t_R_start);
pp->R_symbol = SPOINTER(t_R_symbol);
pp->R_prod = SPOINTER(t_R_prod);
pp->Head = SPOINTER(t_Head);
pp->rBase = SPOINTER(t_rBase);
pp->MT_beg = SPOINTER(t_MT_beg);
pp->MT_tran = SPOINTER(t_MT_tran);
pp->MN_beg = SPOINTER(t_MN_beg);
pp->MN_tran = SPOINTER(t_MN_tran);
pp->M_bits = SPOINTER(t_M_bits);
pp->LGT = SPOINTER(t_LGT);
pp->G_symbol = LPOINTER(t_G_symbol);
pp->PL = SPOINTER(t_PL);
pp->ACTIONS = LPOINTER(t_ACTIONS);
pp->ACTIONSTRINGS = CPOINTER(t_ACTIONSTRINGS);
pp->bitwords = VALUE(bitwords);
pp->n_terms = VALUE(n_terms);
pp->n_vars = VALUE(n_vars);
pp->n_symbs = VALUE(n_symbs);
pp->n_states = VALUE(n_states);
pp->n_rules = VALUE(n_rules);
pp->n_actions = VALUE(n_actions);
pp->TM = VALUE(TM);
pp->maxtoken = VALUE(maxtoken);
pp->token = '\n';
pp->SS = pp->SStop;
pp->SSmax = pp->SStop + PARSER_STKSIZE - 2;
if(notlex)
{
pp->NS = pp->NStop;
pp->LS = pp->LStop;
pp->Rs = pp->RStop;
}
}
/* ========== Access to the parser file stack (nested includes) ==== */
void
ParserPush(PG *pg, int data)
{
if(pg->fstack_depth < 64)
{
pg->fstack[pg->fstack_depth++] = data;
}
}
int
ParserPop(PG *pg)
{
if(pg->fstack_depth > 0)
{
return pg->fstack[--pg->fstack_depth];
}
return 0;
}
int
ParserStackdepth(PG *pg)
{
return pg->fstack_depth;
}
/* ================ END access to the parser file stack ================= */
static void
hash(void *key, KEY *cat)
{
cat->key = *((unsigned long *)key);
cat->hv = (cat->key * 1103515245UL) + 12345;
}
static int
lookup(PG *pg, char *name, void *result)
{
char *cp = name;
unsigned long myhash = 0;
unsigned int symspot = 0;
PsymP sp;
KEY cat;
PbufP tbl = pg->symhandle;
while(*cp) {
((char*)&myhash)[symspot++ & 3] ^= *cp++;
}
hash(&myhash, &cat);
if((sp = tbl->bins[cat.hv % tbl->size]))
{
do
{
if( sp->cat.hv == cat.hv
&& sp->cat.key == cat.key)
{
/* Do final string check */
if( sp->dat[1] != symspot
|| strcmp(PARSERSYMBOLS[sp->dat[0]].name, name))
{
continue;
}
*((PsymP *)result) = sp;
return 1;
}
} while((sp = sp->next));
}
return 0;
}
int
NewParserSymbol(PG *pg, const char *name)
{
KEY cat;
PsymP *binp;
PsymP sp;
PbufP tbl = pg->symhandle;
int symnum;
if(name != pg->symbase)
{/* From outside, add this symbol to the textchunk */
const char *cp = name;
while(*cp)
add_token(pg, *cp++);
}
hash(&pg->symhash, &cat);
binp = &(tbl->bins[cat.hv % tbl->size]);
if((sp = *binp))
{
do
{
if( sp->cat.hv == cat.hv
&& sp->cat.key == cat.key)
{
/* Do final string check */
if( sp->dat[1] != pg->symspot
|| strcmp(PARSERSYMBOLS[sp->dat[0]].name, name))
{
continue; /* NO MATCH */
}
/* FOUND */
/* Flush the string which was built up in the text chunk */
pg->symend = pg->symbase;
pg->symspot = 0;
pg->symhash = 0;
/* Return data stored in the symbol table */
pg->lastsymnum = sp->dat[0];
pg->lastclass = sp->dat[2];
return sp->dat[0];
}
} while((sp = sp->next));
}
/* NOT FOUND, ENTER A NEW SYMBOL */
symnum = pg->pst.cnt;
if(symnum && ((symnum % SYMBOLCHUNK) == 0))
{/* Allocate more contiguous symbol string pointer space */
PARSERSYMBOLS = reallocC(pg->category, PARSERSYMBOLS,
(symnum*sizeof(PARSER_SYMBOL))+(SYMBOLCHUNK*sizeof(PARSER_SYMBOL)));
}
/* link new symbol to front of bin */
sp = (PsymP)NewAstNode(pg, 0, 0);
if((sp->next = *binp) != 0)
++pg->hashdups;
*binp = sp;
tbl->lastbin = binp; /* used for DelLastParserSymbol */
/* Put data in symbol chunk */
sp->dat[0] = symnum; /* symbol number */
sp->dat[1] = pg->symspot; /* length of symbol */
sp->cat = cat; /* hashing info */
/* Accept the string which was built up in the text chunk */
PARSERSYMBOLS[symnum].name = pg->symbase; /* save pointer to the string */
pg->pst.cnt += 1;
pg->totsymlen += pg->symspot+1;
pg->symbase = ++pg->symend;
pg->symspot = 0;
pg->symhash = 0;
/* Return new data */
pg->lastsymnum = symnum;
pg->lastclass = 0;
return symnum;
}
void
DelLastParserSymbol(PG *pg)
{
if(pg->pst.cnt > 0)
{
void *binp;
if((binp = ((PbufP)pg->symhandle)->lastbin))
{
PsymP p = *((PsymP *)binp);
pg->totsymlen -= p->dat[1]+1;
pg->pst.cnt -= 1;
*((PsymP *)binp) = p->next;
FreeAstNode(pg, (AstP)p);
((PbufP)pg->symhandle)->lastbin = 0;
}
}
}
char *
GetH_symbol(PG *pg, int numb)
{
PTABLE *pp = pg->ptab;
long *symbase = pp->G_symbol;
char *symptr = (char *)symbase;
return symptr + symbase[numb];
}
AstP
NewAstNode(PG *pg, int id, int symb)
{
AstP node;
if(pg->pat.freecnt > 0)
{
node = pg->pat.free;
pg->pat.free = node->down;
node->down = 0;
}
else
{
int i;
AstP fnode;
node = callocC(pg->category, 1, ASTCHUNK*ASTSIZE);
pg->pat.cnt += ASTCHUNK;
pg->pat.freecnt = ASTCHUNK;
pg->pat.free = (AstP)(((char*)node)+ASTSIZE);
fnode = (AstP)(((char*)node)+ASTSIZE);
for(i = 1; i < ASTCHUNK-1; ++i)
{
fnode->down = (AstP)(((char*)fnode)+ASTSIZE);
fnode = fnode->down;
}
}
node->id = id;
node->symb = symb;
--pg->pat.freecnt;
return node;
}
void
FreeAstNode(PG *pg, AstP node)
{
if(node > ASTMINADDR)
{
memclr(node, ASTSIZE);
node->down = pg->pat.free;
pg->pat.free = node;
++pg->pat.freecnt;
}
}
void
PruneAstTree(PG *pg, AstP root, int preserve_root, const AstP top)
{
ASTVARS(256);
int at_top = 1;
if(root > ASTMINADDR)
{
if(top > ASTMINADDR)
at_top = 0;
curnode = root;
MARK(stack);
while(curnode = BOTTOMUP(curnode)) {
if(!at_top)
{
if(curnode == top) {
at_top = 1;
continue;
}
}
if(preserve_root && curnode == root)
break;
if(at_top)
FreeAstNode(pg, curnode);
}
}
}
/*--- Print Abstract Syntax Tree - courtesy of Paul Mann ------------------ */
static void
prt_node (char *indent, AstP node, PG *pg, void *file, int ptrs)
{
char *nodename;
if(node->id == 0)
nodename = "NullNode";
else
nodename = GetH_symbol(pg, node->id>>6);
if(ptrs) {
if(node->right < ASTMINADDR)
(*pg->writefunc) (file, " %6x %6d %6x %s%s_%d",
node, node->lineno, node->down, indent, nodename, node->id & 0x3f);
else
(*pg->writefunc) (file, " %6x %6x %6x %s%s_%d",
node, node->right, node->down, indent, nodename, node->id & 0x3f);
}
else
(*pg->writefunc) (file, "%s%s_%d", indent, nodename, node->id & 0x3f);
if (node->symb > 0)
{
char *symbname = PARSERSYMBOLS [node->symb].name;
if(symbname)
{
if(*symbname != EOF_MARK)
(*pg->writefunc) (file, " %s", symbname);
if(node->Tindx)
{
symbname = PARSERSYMBOLS [node->Tindx].name;
(*pg->writefunc) (file, " %s", symbname);
}
}
else (*pg->writefunc) (file, " [%d]", node->symb);
}
(*pg->writefunc) (file, "\n");
}
static void
traverse (char *indent, AstP node, PG *pg, void *file, int ptrs)
{
while (node->down)
{
strcat (indent, "媚");
prt_node (indent, node, pg, file, ptrs);
indent [strlen(indent)-2] = 0; /* BACK UP */
if (node->right > ASTMINADDR)
{
strcat (indent, "
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -