sntncelex.cpp
上传用户:tenhai
上传日期:2021-02-19
资源大小:492k
文件大小:4k
源码类别:

组合框控件

开发平台:

Visual C++

  1. #include "stdafx.h"
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include "sntncelex.h"
  6. #include "grmrgrph.h"
  7. #define MAXTOKENSIZE 100
  8. //#define MAXLINESIZE 5000
  9. char token[MAXTOKENSIZE];
  10. char sentbuf[MAXSENTLEN];
  11. int symbmode;
  12. int endofsntnce;
  13. int endposofsntnce;
  14. unsigned int nextcate, currcate;
  15. unsigned int curchartnode, prechartnode, accchartnode;
  16. wentry_t *nextwentry, *currwentry;
  17. wentry_t *wentries[MAXCNODE-1];
  18. unsigned int (*nextdefs)[MAXDEFS], (*currdefs)[MAXDEFS];
  19. unsigned int deflettice[MAXCNODE-1][MAXDEFS];
  20. FILE *pfs;
  21. int bufptr;
  22. //int maxlink;
  23. //int fufiled;
  24. wentry_t *dictionary[MAXWENTRY];
  25. int hash(char *s){
  26.   unsigned hashval;
  27.   for (hashval=0;*s!='';s++)
  28.       hashval=*s+31*hashval;
  29.   return hashval % MAXWENTRY;
  30. }
  31. wentry_t *listsearch(int hashval, char *word){
  32.   wentry_t *curwentry;
  33.   for (curwentry=dictionary[hashval];curwentry!=NULL;curwentry=curwentry->nxt){
  34. //   link++;
  35. //     if (link>maxlink)
  36.  //maxlink=link;
  37.       if (strcmp(curwentry->word, word)==0)
  38.          break;
  39.   }
  40.   return curwentry;
  41. }
  42. int listrelease(int hashval){
  43.   wentry_t *pwentry, *qwentry;
  44.   def_t *pdef, *qdef;
  45.   for (pwentry=dictionary[hashval];pwentry!=NULL;pwentry=qwentry){
  46.       qwentry=pwentry->nxt;
  47.       for (pdef=pwentry->def;pdef!=NULL;pdef=qdef){
  48.           qdef=pdef->nxt;
  49.           free(pdef);
  50.       }
  51.       free(pwentry->word);
  52.       free(pwentry);
  53.   }
  54.   return 0;
  55. }
  56. int dictrelease(){
  57.   int i;
  58.   for (i=0;i<MAXWENTRY;i++){
  59.       listrelease(i);
  60.       dictionary[i]=NULL;
  61.   } 
  62.   return 0;
  63. }
  64. wentry_t *dictsearch(char *word){
  65.   return listsearch(hash(word), word);
  66. }
  67. wentry_t *dictinsert(char *word){
  68.   int hashval;
  69.   wentry_t *newwentry;
  70.   hashval=hash(word);
  71.   newwentry=listsearch(hashval, word);
  72.   if (newwentry==NULL){
  73.      newwentry=(wentry_t *)malloc(sizeof(wentry_t));
  74.      newwentry->def=NULL;
  75.      newwentry->word=(char *)malloc(strlen(word)+1);
  76.      strcpy(newwentry->word, word);
  77.      newwentry->nxt=dictionary[hashval];
  78. //  if (newwentry->nxt==NULL)
  79. //  fufiled++;
  80.      dictionary[hashval]=newwentry;
  81.  
  82.   } 
  83.   return newwentry;
  84. }
  85. adddef(wentry_t *wentry, unsigned int cate){
  86.   def_t *curdef, *newdef;
  87.   for (curdef=wentry->def;curdef!=NULL;curdef=curdef->nxt)
  88.       if (curdef->cate==cate)
  89.          break;
  90.   if (curdef==NULL){
  91.      newdef=(def_t *)malloc(sizeof(def_t));
  92.      newdef->cate=cate;
  93.      newdef->nxt=wentry->def;
  94.      wentry->def=newdef;
  95.   }
  96.   return 0;
  97. }
  98. /*
  99. char *gettoken(){
  100.   int c, pos=0;
  101.   while ((c=fgetc(pfs))==' '||c=='t')
  102.     ;
  103.   if (c==EOF || c=='n')
  104.   endofsntnce=1;
  105.   else{
  106.      do{
  107.        token[pos++]=(char)c;
  108.      }while ((c=getc(pfs))!=EOF && c!=' ' && c!='t' && c!='n') ;
  109.  if (c=='n')
  110.     ungetc(c, pfs);
  111.   }
  112.   token[pos]='';
  113.   
  114.   return token;
  115. }
  116. */
  117. filllattice(){
  118. int i,j,c,pos;
  119. char *stoken;
  120. def_t *pdef;
  121. endposofsntnce=-1;
  122. for (i=0; i<(MAXCNODE-1); i++){
  123. if (endposofsntnce>=0)
  124. break;
  125. pos=0;
  126. while (((c=sentbuf[bufptr++])==' ')||c=='t')
  127. ;
  128. if (c=='n' || c=='')
  129. endposofsntnce=i;
  130. else{
  131. do{
  132. token[pos++]=(char)c;
  133. }while ((c=sentbuf[bufptr++])!='' && c!=' ' && c!='t' && c!='n') ;
  134. if (c=='n'||c=='')
  135. bufptr--;
  136. }
  137. token[pos]='';
  138. if (*token=='')
  139. endposofsntnce=i;
  140. if (*token!='')
  141. stoken=strtok(token, "/");
  142. else
  143. stoken=token;
  144. wentries[i]=dictinsert(stoken);
  145. if (*token!='')
  146. stoken=strtok(NULL, "/");
  147. else
  148. stoken=NULL;
  149. if (stoken!=NULL){
  150. deflettice[i][0]=token2tcode(stoken);
  151. deflettice[i][1]=tcount;
  152. }
  153. else{
  154. for (j=0, pdef=wentries[i]->def;pdef!=NULL;pdef=pdef->nxt){
  155. deflettice[i][j++]=pdef->cate;
  156. }
  157. deflettice[i][j]=tcount;
  158. }
  159. }
  160. return 0;
  161. }
  162. lookahead(){
  163. nextwentry=wentries[curchartnode];
  164. nextdefs=&deflettice[curchartnode];
  165. // for (i=0; i<MAXDEFS; i++)
  166. // nextdefs[i]=deflettice[curchartnode][i];
  167. endofsntnce=(endposofsntnce==curchartnode);
  168.   return 0;
  169. }