⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 parser.cpp

📁 采用递归下降分析法实现C0语言的语法分析器
💻 CPP
字号:
#include "stdio.h"
#include "stdlib.h"
#include "process.h"
#include "string.h"
#include "parser.h"
FILE  *fpin;
void  readsource();
void  scan(tree *);
void main()
{
  tree *root;
  int i=0;
  root=makenode(1,"begin","\0","\0","\0",0,"\0");
  fpout=fopen("d:\\yufa.xml","w");
  if(fpout==NULL)
  {
    printf("can't creat file:yufa.xml\n");
    exit(1);
  }
  fprintf(fpout,"<?xml version=\"1.0\" ?>\n");
  fprintf(fpout,"<root>\n");
  readsource();
  program(root);
  while(root->child[i]!=NULL)
	scan(root->child[i++]);
  fprintf(fpout,"</root>\n");
  fclose(fpout);
}

void readsource()
{   
	int i=0;
    fpin=fopen("d:\\output.xml","r");
	if(fpin==NULL)
	{
       printf("can't open file:output.txt\n");
       exit(0);
	}
	fscanf(fpin,"<?xml version=\"1.0\"?>\n<root>\n"); 
	for(i=0;i<=MAXNUM;i++)
    {
	    fscanf(fpin,"    <token line=\"%d\" type=\"%[^\"]\" string=\"%[^\"]\" />\n",&words[i].line,&words[i].type,&words[i].string);
		if(words[i].line==0) 
		{
			fclose(fpin);
			return;
		}
	}
}
void scan(tree *root)
{   int i=0;
	if(root!=NULL)
	{
        if(root->child[1]!=NULL)
		{
			if(!strcmp(root->nodetype,"FunDecl"))
			{
				fprintf(fpout,"<tree line=\"%d\" nodetype=\"%s\" nodestring=\"%s\" rettype=\"%s\">\n",root->treeline,root->nodetype,root->nodestring,root->rettype);
			}
			else 
			{
				fprintf(fpout,"<tree line=\"%d\" nodetype=\"%s\" nodestring=\"%s\">\n",root->treeline,root->nodetype,root->nodestring);
			}
			while(root->child[i]!=NULL)
			{
				fprintf(fpout,"<child>\n");
				scan(root->child[i++]);
				fprintf(fpout,"</child>\n");
			}
			fprintf(fpout,"</tree>\n");
		}
		else
		{
		    if(!strcmp(root->nodetype,"Para"))
			    fprintf(fpout,"<tree line=\"%d\" nodetype=\"%s\" nodestring=\"%s\" vartype=\"%s\"/>\n",root->treeline,root->nodetype,root->nodestring,root->vartype);
            else if(!strcmp(root->nodetype,"VarDecl"))
	            fprintf(fpout,"<tree line=\"%d\" nodetype=\"%s\" nodestring=\"%s\" vartype=\"%s\" modifier=\"%s\" isArray=\"%d\"/>\n",root->treeline,root->nodetype,root->nodestring,root->vartype,root->modifier,root->isarray);
			else 
		     	fprintf(fpout,"<tree line=\"%d\" nodetype=\"%s\" nodestring=\"%s\"/>\n",root->treeline,root->nodetype,root->nodestring);

		}
	}
}



⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -