GRMRGRPH.Cpp
资源名称:ictprop.rar [点击查看]
上传用户:tenhai
上传日期:2021-02-19
资源大小:492k
文件大小:5k
源码类别:
组合框控件
开发平台:
Visual C++
- #include "stdafx.h"
- #include "error.h"
- #include "symbol.h"
- #include "symbtbl.h"
- #include "grmrgrph.h"
- symbol_node_ref_tbl_t t_tbl, nt_tbl;
- symbol_node_t *ssym, *fsym;
- ruleindex_t *ruleindex;
- symbol_node_t **symserial;
- unsigned int oldrulcount, rulcount, rulupbound;
- unsigned int tntcount;
- int arccount;
- grmrindxinit(void){
- ruleindex=(ruleindex_t *)calloc(MAXRULES, sizeof(ruleindex_t));
- symserial=(symbol_node_t **)calloc(MAXROLES, sizeof(symbol_node_t *));
- ruleindex[0].length=1;
- ruleindex[0].fstidx=0;
- ruleindex[0].probability=1.0;
- return 0;
- }
- grmrindxfini(void){
- unsigned int i;
- ruleindex=(ruleindex_t *)realloc(ruleindex, sizeof(ruleindex_t)*(rulcount+1));
- symserial=(symbol_node_t **)realloc(symserial, sizeof(symbol_node_t *)*(arccount+2));
- symserial[0]=NULL;
- symserial[1]=ssym;
- for (i=1;i<rulcount;i++){
- ruleindex[i].length=(unsigned int)(ruleindex[i+1].fstidx-ruleindex[i].fstidx-1);
- ruleindex[i].leftcate=symserial[ruleindex[i].fstidx]->sid;
- }
- ruleindex[rulcount].length=(unsigned int)(arccount-ruleindex[rulcount].fstidx+1);
- ruleindex[rulcount].leftcate=symserial[ruleindex[rulcount].fstidx]->sid;
- return 0;
- }
- serialize(symbol_node_t *psym){
- symserial[2+arccount++]=psym;
- if ((arccount+2)%MAXROLES==0)
- symserial=(symbol_node_t **)
- realloc(symserial, sizeof(symbol_node_t *)*(2+arccount+MAXROLES));
- if (rulcount>oldrulcount){
- ruleindex[rulcount].fstidx=arccount+1;
- if ((rulcount+1)%MAXRULES==0)
- ruleindex=(ruleindex_t *)
- realloc(ruleindex, sizeof(ruleindex_t)*(rulcount+1+MAXRULES));
- }
- oldrulcount=rulcount;
- return 0;
- }
- unsigned int token2tcode(char *token){
- if (tblsearch(&t_tbl, token))
- return tblcurrent(t_tbl)->sid;
- else
- return tcount;
- }
- releasenode(symbol_node_t *psym){
- symbol_arc_t *parc, *qarc;
- for (parc=psym->fstlst;parc!=NULL;parc=qarc){
- qarc=parc->nxt;
- free(parc);
- }
- for (parc=psym->lstlst;parc!=NULL;parc=qarc){
- qarc=parc->nxt;
- free(parc);
- }
- for (parc=psym->adjlst;parc!=NULL;parc=qarc){
- qarc=parc->nxt;
- free(parc);
- }
- free(psym->literal);
- free(psym);
- return 0;
- }
- symbol_node_t *instnode(char *token, int tokensize, char mode){
- symbol_node_t *pnews;
- char *str;
- if (tblsearch(&nt_tbl, token))
- /*the token is a known NT, exit*/
- return tblcurrent(nt_tbl);
- if (tblsearch(&t_tbl, token)){
- if (mode=='R')
- /*the token is a known T, exit*/
- return tblcurrent(t_tbl);
- /*since the token appears on the left, it is an NT from now on*/
- EGEN(tblfull(nt_tbl), TBLFUL, return NULL;)
- tblremove(&t_tbl, &pnews);
- tblinsert(&nt_tbl, pnews);
- }
- else{
- EGEN(mode=='L' && tblfull(nt_tbl)||
- mode=='R' && tblfull(t_tbl),
- TBLFUL, return NULL;)
- /*make a new symbol node*/
- EGEN((str=(char*)malloc(tokensize))==NULL, MEMFUL, return NULL;)
- EGEN((pnews=(symbol_node_t *)malloc(sizeof (symbol_node_t)))==NULL,
- MEMFUL, return NULL;)
- pnews->sid=0;
- pnews->fstlst=pnews->lstlst=pnews->adjlst=NULL;
- strcpy(str, token);
- pnews->literal=str;
- if (mode=='R')
- tblinsert(&t_tbl, pnews);
- else
- tblinsert(&nt_tbl, pnews);
- /*debug use*/
- /*printf("%sn", token);*/
- }
- return pnews;
- }
- symbol_node_t *instfinode(void){
- /*make the finishing node*/
- EGEN((fsym=(symbol_node_t *)malloc(sizeof (symbol_node_t)))==NULL,
- MEMFUL, return NULL;)
- EGEN((fsym->literal=(char *)malloc(sizeof(char)))==NULL, MEMFUL, free(fsym);return NULL;)
- *fsym->literal=' ';
- fsym->sid=0;
- fsym->fstlst=fsym->lstlst=fsym->adjlst=NULL;
- tblappend(&t_tbl, fsym);
- return fsym;
- }
- numnodes(void){
- unsigned int idx;
- tntcount=(unsigned int)(tcount+ntcount);
- for (idx=0;idx<tcount;idx++)
- tblinfo(t_tbl, idx)->sid=idx;
- for (;idx<tntcount;idx++)
- tblinfo(nt_tbl, idx-tcount)->sid=idx;
- return 0;
- }
- symbol_arc_t *reltnodes(symbol_node_t *from,
- symbol_node_t *to,
- unsigned int rule, unsigned int role,
- char mode){
- symbol_arc_t *pnewa;
- EGEN((pnewa=(symbol_arc_t *)malloc(sizeof (symbol_arc_t)))==NULL,
- MEMFUL, return NULL;)
- pnewa->aim=to;
- pnewa->rule=rule;
- pnewa->role=role;
- switch (mode){
- case 'f':pnewa->nxt=from->fstlst;from->fstlst=pnewa;break;
- case 'a':pnewa->nxt=from->adjlst;from->adjlst=pnewa;break;
- case 'l':pnewa->nxt=from->lstlst;from->lstlst=pnewa;break;
- }
- /*debug use*/
- /*printf("from %s to %d.%d %sn", from->literal, rule, role, to->literal);*/
- return pnewa;
- }
- releasenodes(void){
- unsigned int idx;
- for (idx=0;idx<tcount;idx++)
- releasenode(tblinfo(t_tbl, idx));
- for (idx=0;idx<ntcount;idx++)
- releasenode(tblinfo(nt_tbl, idx));
- tblclear(&t_tbl);
- tblclear(&nt_tbl);
- tntcount=0;
- ssym=fsym=NULL;
- return 0;
- }
- releasegrmr(void){
- releasenodes();
- free(ruleindex);
- free(symserial);
- ruleindex=NULL;
- symserial=NULL;
- rulcount=0;
- oldrulcount=0;
- arccount=0;
- return 0;
- }