sntncepars.cpp
资源名称:ictprop.rar [点击查看]
上传用户:tenhai
上传日期:2021-02-19
资源大小:492k
文件大小:18k
源码类别:
组合框控件
开发平台:
Visual C++
- #include "stdafx.h"
- #include "symbol.h"
- #include "rrtbl.h"
- #include "grmrgrph.h"
- #include "sntncelex.h"
- #include "sntncepars.h"
- #include "math.h"
- #define pfat(cate, from) catetbl[from*tntcount+cate]
- //#define DELTA 1e-10
- #define MAXSORTSTACK 50
- char *mymark;
- extern FILE *pfo;
- FILE *pfedgs;
- int interactive;
- chartnode_t chart[MAXCNODE];
- //unsigned int glbmaxsub=0;
- typedef struct range_t{
- int l;
- int r;
- }range_t;
- range_t rangestack[MAXSORTSTACK];
- rrset_struct *rrset=NULL;
- typedef struct rrrqentry_t{
- edge_t ed;
- unsigned int cate;
- int nxt;
- }rrqentry_t;
- typedef struct rrq_t{
- rrqentry_t queue[MAXSQUE];
- int topptr;
- int botptr;
- }rrq_t;
- unsigned int *lstack;
- int lsptr;
- rrq_t rq;
- pf_t *pfroot=NULL, *pfhead=NULL;
- pf_t **catetbl;
- edge_t *edgepool;
- pf_t *trnodepool;
- int curavle=0;
- int loope=0;
- int npfs;
- pf_t *tokenpf;
- int cirflag;
- edge_t *pooledg;
- rrqentry_t *nre;
- int unacc[MAXCNODE];
- pf_t *addpf(unsigned int cate, int from){
- int index;
- pf_t *ppf;
- index=from*tntcount+cate;
- if ((ppf=catetbl[index])==NULL){
- ppf=&trnodepool[npfs++];
- //ppf=(pf_t *)malloc(sizeof(pf_t));
- ppf->cate=cate;
- ppf->litoramb.literal=NULL;
- ppf->subpfs=NULL;
- // ppf->nxt=pfhead;
- ppf->prb=0;
- // ppf->refs=0;
- pfhead=ppf;
- catetbl[index]=ppf;
- }
- return ppf;
- }
- int sumsubpf(pf_t *pf, edge_t *compedge){
- double subprb=1.0;
- int solid=0;
- unsigned int cursub, pos, rlen;
- edge_t *pe;
- if (compedge->rule<=rulcount){
- rlen=rightlenof(compedge->rule);
- for (pe=compedge->prevedge; (pe!=NULL) ;pe=pe->prevedge)
- subprb*=pe->ppf->prb;
- subprb*=probof(compedge->rule);
- if (compedge->ppf->prb>0){
- subprb*=compedge->ppf->prb;
- solid=1;
- }
- if (-pf->prb<=subprb){
- if (pf->subpfs){
- if (!solid)
- if (((pf->litoramb.totalamb+1)%MAXAMB)==0){
- pf->subpfs=(subpf_t *)realloc(pf->subpfs,
- (pf->litoramb.totalamb+1+MAXAMB)*sizeof(subpf_t));
- }
- }
- else{
- pf->subpfs=(subpf_t *)calloc(MAXAMB, sizeof(subpf_t));
- pf->subpfs[0].sublst=NULL;
- }
- if (solid){
- cursub=0;
- pf->subpfs[cursub].rulno=compedge->rule;
- if (!pf->subpfs[0].sublst){
- pf->subpfs[cursub].sublst=
- (pf_t **)calloc(MAXRLEN, sizeof(pf_t *));
- }
- }
- else{
- cursub=++pf->litoramb.totalamb;
- pf->subpfs[cursub].rulno=compedge->rule;
- pf->subpfs[cursub].sublst=
- (pf_t **)calloc(rlen, sizeof(pf_t *));
- }
- for (pos=rlen-1,pe=compedge;pe!=NULL;pos--,pe=pe->prevedge){
- pf->subpfs[cursub].sublst[pos]=pe->ppf;
- }
- if (!solid)
- subprb*=-compedge->ppf->prb;
- if (-pf->prb<subprb){
- pf->prb=-subprb;
- }
- }
- }
- else{
- pf->litoramb.literal=currwentry->word;
- pf->prb=1.0;
- }
- return 0;
- }
- releasepf(pf_t *ppf){
- unsigned int c;
- if (ppf->subpfs){
- for (c=0;c<=ppf->litoramb.totalamb;c++)
- if (ppf->subpfs[c].sublst)
- free(ppf->subpfs[c].sublst);
- free(ppf->subpfs);
- }
- //free(ppf);
- return 0;
- }
- releasewholepf(){
- int i;
- for (i=0; i<npfs; i++){
- releasepf(&trnodepool[i]);
- }
- /*
- int nonz=0;
- pf_t *ppf, *qpf;
- for (qpf=ppf=pfhead;ppf!=NULL;){
- if (ppf==pfhead){
- pfhead=qpf=ppf->nxt;
- if (ppf->refs==0)
- nonz++;
- releasepf(ppf);
- ppf=pfhead;
- }
- else{
- qpf->nxt=ppf->nxt;
- if (ppf->refs==0)
- nonz++;
- releasepf(ppf);
- ppf=qpf->nxt;
- }
- // npfs--;
- }
- pfhead=NULL;
- printf("n%fn", (double)nonz/(double)npfs);
- */
- npfs=0;
- return 0;
- }
- addchartnode(){
- int i;
- unsigned int j;
- for (i=0; i<MAXCNODE; i++){
- chart[i].numb=0;
- chart[i].pled=-1;
- chart[i].plst=-1;
- chart[i].chd=-1;
- chart[i].ctl=-1;
- chart[i].expeclist=(char *)malloc(sizeof(char)*tntcount);
- for (j=0; j<tntcount; j++){
- chart[i].expeclist[j]=0;
- }
- }
- return 0;
- }
- quicksrt(int l, int r){
- int i,j;
- edge_t ed;
- int rstptr;
- rangestack[rstptr=0].l=l;
- rangestack[rstptr].r=r;
- while (rstptr>=0){
- l=rangestack[rstptr].l;
- r=rangestack[rstptr].r;
- rstptr--;
- st:
- if (l>=r)
- continue;
- i=l;j=r;
- ed=edgepool[i];
- while (!(i==j)){
- while ((j>i) && ( EXP(edgepool[j].rule, edgepool[j].role)>EXP(ed.rule, ed.role)))
- j--;
- if (i<j)
- edgepool[i++]=edgepool[j];
- while ((j>i) && ( EXP(edgepool[i].rule, edgepool[i].role)<EXP(ed.rule, ed.role)))
- i++;
- if (i<j)
- edgepool[j--]=edgepool[i];
- }
- edgepool[i]=ed;
- i++;j--;
- if ((r-i)>(j-l)){
- rstptr++;
- rangestack[rstptr].l=i;
- rangestack[rstptr].r=r;
- r=j;
- goto st;
- }
- else{
- rstptr++;
- rangestack[rstptr].l=l;
- rangestack[rstptr].r=j;
- l=i;
- goto st;
- }
- }
- return 0;
- }
- qsrt(int l, int r){
- int qi,qj;
- edge_t ed;
- edge_t *edg;
- int flagO, flagL;
- int rstptr;
- rangestack[rstptr=0].l=l;
- rangestack[rstptr].r=r;
- while (rstptr>=0){
- l=rangestack[rstptr].l;
- r=rangestack[rstptr].r;
- rstptr--;
- start:
- if (l>=r)
- continue;
- qi=l;qj=r;
- ed=edgepool[qi];
- while (!(qi==qj)){
- edg=&edgepool[qj];
- while ( (qj>qi) &&
- ( ((flagO=edg->origin-ed.origin)>0) ||
- ( !flagO &&
- ( ((flagL=leftof(edg->rule)-leftof(ed.rule))>0) ||
- ( !flagL &&
- (PST(edg->rule, edg->role)>PST(ed.rule, ed.role))
- // RGT(edg->rule, edg->role, ed.rule, ed.role)
- )
- )
- )
- )
- )
- edg=&edgepool[--qj];
- if (qi<qj)
- edgepool[qi++]=edgepool[qj];
- edg=&edgepool[qi];
- while ( (qj>qi) &&
- ( ((flagO=ed.origin-edg->origin)>0) ||
- ( !flagO &&
- ( ((flagL=leftof(ed.rule)-leftof(edg->rule))>0) ||
- ( !flagL &&
- (PST(ed.rule, ed.role)>PST(edg->rule, edg->role))
- // RGT(ed.rule, ed.role, edg->rule, edg->role)
- )
- )
- )
- )
- )
- edg=&edgepool[++qi];
- if (qi<qj)
- edgepool[qj--]=edgepool[qi];
- }
- edgepool[qi]=ed;
- qi++;qj--;
- if ((r-qi)>(qj-l)){
- rstptr++;
- rangestack[rstptr].l=qi;
- rangestack[rstptr].r=r;
- r=qj;
- goto start;
- }
- else{
- rstptr++;
- rangestack[rstptr].l=l;
- rangestack[rstptr].r=qj;
- l=qi;
- goto start;
- }
- }
- return 0;
- }
- releasechart(){
- int p;
- for (p=0;p<MAXCNODE;p++){
- chart[p].pled=chart[p].plst=-1;
- chart[p].chd=chart[p].ctl=-1;
- free(chart[p].expeclist);
- chart[p].expeclist=NULL;
- }
- return 0;
- }
- releaseedges(){
- int p;
- unsigned int q;
- for (p=0;(chart[p].numb!=0); p++){
- chart[p].numb=0;
- for (q=0; q<tntcount; q++){
- chart[p].expeclist[q]=0;
- }
- }
- }
- searchcompe(unsigned int cate, int *pos){
- int bottom, middle, top;
- bottom=chart[accchartnode].plst+1;
- top=chart[accchartnode].pled;
- if (top<bottom){
- *pos=top;
- return 0;
- }
- while (top>bottom){
- middle=(top+bottom)/2;
- if (cate>EXP(edgepool[middle].rule, edgepool[middle].role))
- bottom=middle+1;
- else
- top=middle;
- }
- /*
- if (top==bottom){
- if (cate>EXP(edgepool[top].rule, edgepool[top].role))
- bottom++;
- }
- *pos=bottom;
- */
- *pos=top;
- if (
- !(cate==EXP(edgepool[*pos].rule, edgepool[*pos].role)))
- return 0;
- else
- return 1;
- }
- #define addexpec(expec){
- chart[curchartnode].expeclist[expec]=1;
- }
- #define addedge(dumrule, dumrole, dumorigin, dumprevedge, dumppf){
- pooledg=&edgepool[curavle++];
- pooledg->rule=dumrule;
- pooledg->role=dumrole;
- pooledg->origin=dumorigin;
- pooledg->prevedge=dumprevedge;
- pooledg->ppf=dumppf;
- if (dumrole==0)
- loope++;
- chart[curchartnode].numb++;
- chart[curchartnode].pled++;
- }
- #define enqedge(dumrule, dumrole, dumorigin, dumprevedge, dumppf, dumcate){
- nre=&rq.queue[++rq.topptr];
- nre->nxt=-1;
- nre->cate=dumcate;
- nre->ed.rule=dumrule;
- nre->ed.role=dumrole;
- nre->ed.origin=dumorigin;
- nre->ed.prevedge=dumprevedge;
- nre->ed.ppf=dumppf;
- (chart[dumorigin].chd==-1)?(chart[dumorigin].chd=rq.topptr):
- (rq.queue[chart[dumorigin].ctl].nxt=rq.topptr);
- chart[dumorigin].ctl=rq.topptr;
- }
- #define rqinit {rq.topptr=rq.botptr=-1;}
- #define lsempty (lsptr==-1)
- #define lsinit lsptr=-1;
- #define lspush(catee) lstack[++lsptr]=catee;
- #define lspop(pcate) pcate=lstack[lsptr--];
- double prbbranch(pf_t *ppf){
- subpf_t subpf;
- int sublen, curpos;
- unsigned int amb, vp=0;
- double subprb, maxprb;
- if (ppf->subpfs==NULL){
- ppf->prb=1.0;
- return 1.0;
- }
- maxprb=-1.0;
- ppf->prb=0;
- for (amb=0; amb<=ppf->litoramb.totalamb; amb++){
- subprb=1.0;
- subpf=ppf->subpfs[amb];
- if (subpf.sublst==NULL)
- continue;
- sublen=rightlenof(subpf.rulno);
- for (curpos=0;curpos<sublen;curpos++){
- double x=subpf.sublst[curpos]->prb;
- if ((x==0)&&(tokenpf)){
- cirflag=1;
- subprb=0;
- continue;
- }
- if ((sublen==1)&&(x<0)&&(!tokenpf))
- tokenpf=ppf;
- subprb*=(x>=0)?x:prbbranch(subpf.sublst[curpos]);
- if (ppf==tokenpf){
- tokenpf=NULL;
- cirflag=0;
- }
- }
- subprb*=probof(subpf.rulno);
- if (subprb>=maxprb){
- maxprb=subprb;
- vp=amb;
- }
- }
- if (!cirflag){
- ppf->prb=maxprb;
- for (amb=0; amb<=ppf->litoramb.totalamb; amb++){
- if (amb==vp)
- continue;
- if (ppf->subpfs[amb].sublst){
- free(ppf->subpfs[amb].sublst);
- ppf->subpfs[amb].sublst=NULL;
- }
- }
- subpf=ppf->subpfs[vp];
- free(ppf->subpfs);
- ppf->subpfs=(subpf_t *)malloc(sizeof(subpf_t));
- ppf->subpfs[0]=subpf;
- ppf->litoramb.totalamb=0;
- return ppf->prb;
- }
- else{
- ppf->prb=-1.0;
- return maxprb;
- }
- }
- prbmaxprefix(){
- int bottom, top, curbot, curtop, curedp;
- double curmaxprb, curprb;
- edge_t *pe, *ptop, *pcur;
- double x;
- bottom=chart[curchartnode].plst+1;
- top=chart[curchartnode].pled;
- for (curtop=curbot=bottom; curtop<=top;){
- curmaxprb=-1;
- curedp=curtop;
- do{
- for (x=1.0, pe=&edgepool[curtop]; pe!=NULL; pe=pe->prevedge)
- x*=pe->ppf->prb;
- curprb=x*=probof(edgepool[curtop].rule);
- if (curprb>curmaxprb){
- /*
- if (curedp!=curtop)
- if (edgepool[curedp].ppf)
- edgepool[curedp].ppf->refs--;
- */
- curedp=curtop;
- curmaxprb=curprb;
- }
- /*
- else
- if (edgepool[curedp].ppf)
- edgepool[curtop].ppf->refs--;
- */
- curtop++;
- }while ((curtop<=top)&&
- ((ptop=&edgepool[curtop])->origin==(pcur=&edgepool[curedp])->origin)&&
- (leftof(ptop->rule)==leftof(pcur->rule))&&
- (PST(ptop->rule, ptop->role)==PST(pcur->rule, pcur->role))
- //REQ(ptop->rule, ptop->role, pcur->rule, pcur->role)
- );
- edgepool[curbot++]=edgepool[curedp];
- }
- chart[curchartnode].pled=curbot-1;
- curavle=curbot;
- bottom=chart[curchartnode].plst+1;
- top=chart[curchartnode].pled;
- return 0;
- }
- pred(){
- int match=0;
- unsigned int cate, expecate;
- rulrol_struct *qrr;
- int loc, prerr;
- int ptr,lastptr;
- unsigned int rul;
- unsigned int rol;
- int i;
- if ((*nextdefs)[0]==0)
- return 0;
- while (!lsempty){
- lspop(cate);
- if ((cate<tcount)){
- if (!match)
- for (i=0; (*nextdefs)[i]<tcount; i++){
- if (cate==(*nextdefs)[i]){
- match=1;
- break;
- }
- }
- }
- else{
- prerr=0;
- //merge rrlist
- for (i=0; (*nextdefs)[i]<tcount; i++){
- for (qrr=lrr(cate, (*nextdefs)[i]); qrr!=NULL; qrr=qrr->nxt){
- loc=ROLPOS(qrr->rule, qrr->role);
- if (!rrset[loc].prr){
- rrset[loc].prr=qrr;
- rrset[prerr].next=loc;
- prerr=loc;
- }
- }
- }
- lastptr=0;
- if ((ptr=rrset[0].next)!=-1)
- match=1;
- for (;ptr!=-1;ptr=rrset[ptr].next){
- rul=rrset[ptr].prr->rule;
- rol=rrset[ptr].prr->role;
- // addedge(rul, rol, curchartnode, NULL, NULL);
- expecate=EXP(rul, rol);
- addexpec(expecate);
- addexpec(leftof(rul));
- if (expecate>=tcount && !mymark[expecate]){
- lspush(expecate);
- mymark[expecate]=1;
- }
- rrset[ptr].prr=NULL;
- rrset[lastptr].next=-1;
- lastptr=ptr;
- }
- }
- }
- lsinit;
- return match;
- }
- init(){
- extern int bufptr;
- unsigned int nidx;
- for (nidx=0;nidx<tntcount;nidx++)
- mymark[nidx]=0;
- bufptr=0;
- lsinit;
- rqinit;
- curavle=0;
- loope=0;
- curchartnode=prechartnode=0;
- filllattice();
- lookahead();
- chart[curchartnode].plst=chart[curchartnode].pled=-1;
- chart[curchartnode].chd=chart[curchartnode].ctl=-1;
- // addedge(0, 0, 0, NULL, NULL);
- lspush(ssym->sid);
- chart[0].expeclist[ssym->sid]=1;
- catetbl[ssym->sid]=NULL;
- }
- scan(){
- int catedim, i;
- /*choose the most probable prefix parse*/
- if (curchartnode>=1){
- qsrt(chart[prechartnode].plst+1, chart[prechartnode].pled);
- prbmaxprefix();
- }
- quicksrt(chart[prechartnode].plst+1, chart[prechartnode].pled);
- /*step forward*/
- currwentry=nextwentry;
- currdefs=nextdefs;
- accchartnode=prechartnode=curchartnode++;
- lookahead();
- chart[curchartnode].plst=chart[curchartnode].pled=curavle-1;
- for (i=0; (*currdefs)[i]<tcount; i++)
- enqedge(rulupbound, 0, prechartnode, NULL, NULL, (*currdefs)[i]);
- catedim=curchartnode*tntcount;
- for (i=0;i<catedim;i++)
- catetbl[i]=NULL;
- return 0;
- }
- comp(){
- edge_t *compedge, *prevedge;
- rulrol_struct *qrr;
- pf_t *newpf;
- unsigned int cate, expecate;
- int epos=0;
- unsigned int rul;
- unsigned int rol;
- int ptr, lastptr, lastiniptr;
- int loc, prerr;
- int i;
- unsigned int nidx;
- int hd;
- int curcatedim;
- int eend;
- /*initialize the marks for prediction*/
- for (nidx=0;nidx<tntcount;nidx++)
- mymark[nidx]=0;
- while ((accchartnode>0) || (chart[0].chd>=0)){
- hd=chart[accchartnode].chd;
- /*dequeue at accchartnode*/
- cate=rq.queue[hd].cate;
- compedge=&(rq.queue[hd].ed);
- hd=chart[accchartnode].chd=rq.queue[hd].nxt;
- if (hd==-1)
- chart[accchartnode].ctl=-1;
- eend=chart[accchartnode].pled;
- if ((newpf=pfat(cate, accchartnode))!=NULL){
- /*add ambiguity*/
- sumsubpf(newpf, compedge);
- }
- else{
- /*fill in the qualified-roles-list*/
- loc=prerr=0;
- for (i=0; (*nextdefs)[i]<tcount; i++){
- for (qrr=rrr(cate, (*nextdefs)[i]);qrr!=NULL;qrr=qrr->nxt){
- loc=ROLPOS(qrr->rule, qrr->role);
- if (rrset[loc].prr==NULL){
- rrset[loc].prr=qrr;
- rrset[prerr].next=loc;
- prerr=loc;
- }
- }
- }
- /*examine the expected roles*/
- //epos=chart[accchartnode].plst+1;
- if (searchcompe(cate, &epos))
- do{
- prevedge=&edgepool[epos];
- rul=prevedge->rule;
- rol=(unsigned int)(prevedge->role+1);
- loc=ROLPOS(rul, rol);
- if (rrset[loc].prr){
- /*if this role is expected and qualified, add it*/
- /*new node can be added in the forest. only executed at the first time*/
- if (!newpf)
- sumsubpf(newpf=addpf(cate, accchartnode), compedge);
- // newpf->refs++;
- if (rul>0){
- expecate=EXP(rul, rol);
- if (TOSHIFT(rul, rol)){
- /*rightmost not met yet, put into edgepool*/
- addedge(rul, rol, prevedge->origin, prevedge, newpf);
- if (!mymark[expecate]){
- /*mark the expected cates, for prediction*/
- lspush(expecate);
- mymark[expecate]=1;
- }
- }
- else
- /*rightmost of rhs met, enque for completion*/
- enqedge(rul, rol, prevedge->origin, prevedge, newpf, expecate);
- }
- }
- // epos++;
- }while ((++epos<=eend)
- && (EXP(edgepool[epos].rule, edgepool[epos].role)==cate));
- /*clear the qulified-roles-list*/
- lastptr=lastiniptr=0;
- for (ptr=rrset[0].next;ptr!=-1;ptr=rrset[ptr].next){
- rrset[lastptr].next=-1;
- lastptr=ptr;
- if (rrset[ptr].prr->role>1){
- rrset[ptr].prr=NULL;
- }
- else{
- rrset[lastiniptr].next=ptr;
- lastiniptr=ptr;
- }
- }
- //examine x.1 roles
- if (chart[accchartnode].expeclist[cate]){
- lastptr=0;
- for (ptr=rrset[0].next; ptr!=-1; ptr=rrset[ptr].next){
- rul=rrset[ptr].prr->rule;
- rol=rrset[ptr].prr->role;
- if ((rul==0)||(chart[accchartnode].expeclist[leftof(rul)])){
- if (!newpf)
- sumsubpf(newpf=addpf(cate, accchartnode), compedge);
- if (rul>0){
- expecate=EXP(rul, rol);
- if (TOSHIFT(rul, rol)){
- //rightmost not met yet, put into edgepool
- addedge(rul, rol, accchartnode, NULL, newpf);
- if (!mymark[expecate]){
- //mark the expected cates, for prediction
- lspush(expecate);
- mymark[expecate]=1;
- }
- }
- else
- //rightmost of rhs met, enque for completion
- enqedge(rul, rol, accchartnode, NULL, newpf, expecate);
- }
- }
- }
- }
- /*clear the qulified-roles-list*/
- lastptr=0;
- for (ptr=rrset[0].next;ptr!=-1;ptr=rrset[ptr].next){
- rrset[ptr].prr=NULL;
- rrset[lastptr].next=-1;
- lastptr=ptr;
- }
- }
- /*spread from right to left*/
- if (chart[accchartnode].chd<0){
- curcatedim=(accchartnode+1)*tntcount;
- for (i=accchartnode*tntcount; i<curcatedim; i++){
- if (catetbl[i])
- if (catetbl[i]->prb<0)
- prbbranch(catetbl[i]);
- }
- while ((accchartnode>0)&&(chart[accchartnode].chd<0))
- accchartnode--;
- }
- }
- rqinit;
- }
- parses(){
- init();
- for (;!endofsntnce;prechartnode=curchartnode){
- // printf("parsing at %dn", curchartnode);
- if (!pred()){
- endofsntnce=0;
- releaseedges();
- curavle=0;
- pfroot=NULL;
- nextwentry=NULL;
- return -1;
- }
- scan();
- // if (nextwentry==NULL)
- // break;
- comp();
- }
- pfroot=catetbl[ssym->sid];
- endofsntnce=0;
- releaseedges();
- fprintf(pfedgs, "%dt%dt%dt%dn", curavle, loope, npfs, endposofsntnce);
- curavle=0;
- loope=0;
- return 0;
- }