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

📄 main.cpp

📁 一个词法语法分析器
💻 CPP
字号:


#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "globals.h"
#include "semanteme.h"



void bk(char ch,int w)
{
	for(int i=0;i<80;i++)
		putchar(ch);
}



void look(char *filename)
{
	FILE *fp;
	char ch;
	if((fp=fopen(filename,"r"))==NULL)
	{
		putchar('\n');
		bk('*',81);
		printf("\n\t\tSorry,There isn't the file %s at presnt path!\n\n",filename);
		bk('*',81);
		return;
	}
	while(!feof(fp))
	{
		ch=fgetc(fp);
		putchar(ch);
	}
	fclose(fp);
}





void newfile(char *filename)
{
	FILE *fp;
	char ch;
	if((fp=fopen(filename,"w"))==NULL)
	{
		putchar('\n');
		bk('*',81);
		printf("\n\t\tSorry,Cann't creat a new file %s at presnt path!\n\n",filename);
		bk('*',81);
		return;
	}
	puts("You should be end up whith \'$\':\n");
	while(1)
	{
		ch=getchar();
		fputc(ch,fp);
		if(ch=='$')
			break;
	}
	fclose(fp);
	getchar();
}
		


void menu()
{
	
	puts("\n                                   menu\n");
	bk('~',80);
	puts("                      1.msg  ……………〖look over the message information〗\n");       
	puts("                      2.menu ……………〖look over the menu information〗\n");
	puts("                      3.new  ……………〖creat a new source file〗\n");
	puts("                      4.load ……………〖load the resouce file〗\n");
	puts("                      5.look ……………〖look over the result〗\n");
	puts("                      6.readme …………〖for users〗\n");
	puts("                      7.exit ……………〖exit the window〗\n");
	bk('~',80);
	
}


void readme()
{
	puts("\n                             ZQ's mini_compile");
	bk('~',80);
	puts("    The project contains two groups: scanner group and sementeme group.\n"); 
	puts("    The scanner group alse go by the name of lexical analysis or scanning. \n");
	puts("The function is to read the stream of characters from left to right in order\n");
	puts("to gouped it into tokens,which are sequences of characters that have a\n");
	puts("collective meaning.\n");
	puts("    The sementeme group has two functions.One is syntax analysis or parsing.\n");
	puts("Another is semantic analysis.\n");
	puts("    The purpose of syntax analysis is to determine the source program's phrase\n");
	puts("structure. This process is also called parsing. The source program is parsed to\n");
	puts("check whether it conforms to the source language's syntax, and to construct a\n");
	puts("suitable of its phrase structure.\n");
	puts("    The parsed programs is further analyzed to determine whether it conforms to\n");
	puts("the source language's contextual constraints: scope rules, type rules.\n");
	puts("    To relate applied occurrence of an identifier in the source program to the \n");
	puts("corresponding declaration.\n");
}

void quit()
{

	bk('~',80);
	puts("\n\t\t\tThe window will be close!\n\n");
	bk('~',80);

}


long int countchar(char *str)
{
	FILE *fp;
	long int count=0;
	//gets(str);
	fp=fopen(str,"r");
	if(fp==NULL)
	{
		putchar('\n');
		bk('*',81);
		printf("\n\t\tSorry,There isn't the file %s at presnt path!\n",str);
		bk('*',81);
		return -1;
	}
	while(!feof(fp))
	{
		fgetc(fp);
		count++;
	}
	fclose(fp);
	return count;
}
void error()
{

	bk('*',80);
	puts("\n\n\t\tIt is not recognized as an internal or external command.\n\n");
	bk('*',80);
}



void msg()
{
	puts("\n\t\t\t Edition message:\n");
	bk('~',80);
	puts("\n                       ZQ's mini_compile\n");
	puts("                       Deviser : 张琼(2001051622)\n");
	bk('~',80);
}


void manger()
{
	char str1[81],sourcefilename[30],resultfilename[30],*new_arry,str[100];
	long int len;
	FILE *fp;
	menu();
	do
	{
		puts("\n>>");
		gets(str);
		if(strcmp(str,"msg")==0)
			msg();
		else if(strcmp(str,"menu")==0)
			menu();
		else if(strcmp(str,"load")==0)
		{
			puts("Input the source file name:");
	        gets(sourcefilename);
			if((len=countchar(sourcefilename))!=-1)
			{
				new_arry=new char[len];
				puts("Input the result file name:");
				gets(resultfilename);
				strFileName=resultfilename;
				fp=fopen(sourcefilename,"r");
				new_arry[0]='\0';
				while(!feof(fp))
				{
					fgets(str1,80,fp);
					puts(str1);
					strcat(new_arry,str1);
				}
				fclose(fp);
				gnRow=gnColumn=gnLocate=gnLocateStart=0;
				strSource=new_arry;
				lrparse();
				delete new_arry;
			}
		}
		else if(strcmp(str,"readme")==0)
			readme();
			else if(strcmp(str,"new")==0)
		{
			bk('~',80);
			puts("\nInput the filename that you want to creat:");
			char fn1[35];
			gets(fn1);
			newfile(fn1);
			putchar('\n');
			puts("\n\t\t  OK!  Success to creat a new source file !\n");
			bk('~',80);

		}
		else if(strcmp(str,"look")==0)
		{
			bk('~',80);
			puts("\nInput the filename that you want to look:");
			char fn1[25];
			gets(fn1);
			look(fn1);
			putchar('\n');
			bk('~',80);
		}
		else if(strcmp(str,"exit")==0)
		{
			quit();
			return;
		}
		else
			error();
	}
	while(1);
}


void main()
{
	manger();
}

⌨️ 快捷键说明

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