GRMRPARS.Cpp
资源名称:ictprop.rar [点击查看]
上传用户:tenhai
上传日期:2021-02-19
资源大小:492k
文件大小:4k
源码类别:
组合框控件
开发平台:
Visual C++
- #include "stdafx.h"
- #include "error.h"
- #include "symbol.h"
- #include "grmrgrph.h"
- #include "grmrpars.h"
- #include "sntncelex.h"
- FILE *pfg;
- FILE *pfd;
- FILE *pft;
- int verbose;
- int ntrees=0;
- char *cuttoken(char **lhd){
- char c, *token;
- /*skip all blanks*/
- while ((c=**lhd)!=' ' && (c==' ' || c=='t' || c=='n'))
- (*lhd)++;
- token=*lhd;
- /*forward*/
- while ((c=**lhd)!=' ' && !(c==' ' || c=='t' || c=='n'))
- (*lhd)++;
- /*terminate and proceed*/
- if (**lhd!=' '){
- **lhd=' ';
- (*lhd)++;
- }
- return token;
- }
- char *uncuttoken(char **lhd, char *token){
- char *tmplhd;
- tmplhd=(*lhd);
- *lhd=token;
- *(--tmplhd)=' ';
- return *lhd;
- }
- parseg(){
- char line[MAXLINELEN], *linehead=line, *token;
- symbol_node_t *left, *prer, *curr;
- unsigned int rolcount;
- double pval;
- grmrindxinit();
- /*install the finishing symbol*/
- ECHK(instfinode();, return -1;)
- /*repeatedly reading rules*/
- for (;fgets(line, MAXLINELEN, pfg)!=NULL;linehead=line){
- /*reading left part*/
- token=cuttoken(&linehead);
- if (*token==' ')
- /*skip blank lines*/
- continue;
- if (*token=='%')
- break;
- /*probability of the rule*/
- pval=atof(token);
- token=cuttoken(&linehead);
- /*left part read, build it into graph*/
- ECHK(left=instnode(token, linehead-token, 'L');, return -1;)
- /*memorize start symbol*/
- if (strcmp(token, START)==0)
- ssym=left;
- /*reading mid part*/
- token=cuttoken(&linehead);
- /*mismatch*/
- if (strcmp(token, GENERATOR)!=0){
- errcode=NOGENERATOR;
- return -1;
- }
- //EGEN(strcmp(token, GENERATOR)!=0, NOGENERATOR, return -1;)
- do{
- /*mid part read, reading first component of right part*/
- rulcount++;
- rolcount=0;
- ruleindex[rulcount].probability=pval;
- token=cuttoken(&linehead);
- /*missing right part*/
- EGEN(*token==' '||strcmp(token, SEPARATOR)==0,
- NORIGHTPART, return -1;)
- /*first right component read, build it into graph*/
- ECHK(curr=prer=instnode(token, linehead-token, 'R');,
- return -1;)
- serialize(left);
- /*draw an arc of "first" type, update role counter*/
- ECHK(reltnodes(curr, left, rulcount, rolcount++, 'f');,
- return -1;)
- serialize(curr);
- /*repeatedly read the following right conponents*/
- while (*(token=cuttoken(&linehead))!=' ' &&
- strcmp(token, SEPARATOR)!=0){
- ECHK(curr=instnode(token, linehead-token, 'R');,
- return -1;)
- /*draw an arc of "adjacent" type*/
- ECHK(reltnodes(curr, prer, rulcount, rolcount++, 'a');,
- return -1;)
- serialize(curr);
- prer=curr;
- }
- /*draw an arc of "last" type, from left to the most right*/
- ECHK(reltnodes(left, curr, rulcount, rolcount, 'l');,
- return -1;)
- }while(*token!=' ');
- }
- /*missing starting nonterminal*/
- EGEN(ssym==NULL, NOSTART, return -1;)
- /*draw an arc of "adjacent" type, from fininshing symbol to starting symbol*/
- ECHK(reltnodes(fsym, ssym, 0, 1, 'a');, return -1;)
- /*number all the symbols*/
- numnodes();
- grmrindxfini();
- rulupbound=(unsigned int)(rulcount+1);
- return 0;
- }
- // extern int maxlink, fufiled;
- installdict(){
- char line[MAXLINELEN], *linehead=line, *token;
- wentry_t *wentry;
- unsigned int term;
- for (;fgets(line, MAXLINELEN, pfd)!=NULL;linehead=line){
- token=cuttoken(&linehead);
- if (*token==' ')
- continue;
- wentry=dictinsert(token);
- while (*(token=cuttoken(&linehead))!=' '){
- // if ((term=token2tcode(token))==-1)
- // printf("%s", token);
- EGEN((term=token2tcode(token))==-1, UNKNOWNTERM, {return -1;})
- adddef(wentry, term);
- }
- }
- wentry=dictinsert("");
- adddef(wentry, fsym->sid);
- // printf("%dn", maxlink);
- // printf("%dn", fufiled);
- return 0;
- }