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

📄 hh.cpp

📁 小型词法分析器。输入程序文件片段
💻 CPP
字号:
// file.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include<iostream.h>
#include<fstream.h>

int j;
char ch;
int code;

void GetChar();
void GetBC();
void Concat();
int IsDigital();
int IsLetter();
int Reserve();
void Retract();

ifstream inf;
ofstream outf;
char strToken[50];
void main()
{	
	cout<<"若要再测试其他文件,请在输入文件里面输入文件,再运行程序。Thank you."<<endl;
 inf.open("输入文件.txt");
	if(!inf)
	{
		cout<<"Error open file!"<<endl;
		return;
	}
    while(!inf.eof())
	{ 					
		ch=' ';	
		j=0;
		inf>>ch;
		if(IsLetter())
		{ 
			while(IsLetter()||IsDigital())
			{
			Concat();
		    GetChar();
			}
			Retract();
		    code=Reserve();
			for(int t=j;t<=50;t++)//将数组的后面多余字符全部置空,以免后面输出多余字符。
			{strToken[t]=NULL;}
			if(code==14)
			cout<<"(14,\""<<strToken<<"\")"<<endl;
		    else 
			cout<<"("<<code<<",\""<<strToken<<"\")"<<endl;
		}
	    else if(IsDigital())
		{
		    while(IsDigital())
			{
			Concat();
			GetChar();
			}
		Retract();
        for(int t=j;t<=50;t++)
			{strToken[t]=NULL;}
		cout<<"(15,\""<<strToken<<"\")"<<endl;
		}
	    else
		if(ch==';') 
		cout<<"(4,\";\")"<<endl;
		else
		if(ch=='{') cout<<"(5,\"{\")"<<endl;
		else
		if(ch=='}') cout<<"(6,\"}\")"<<endl;
		else
		if(ch=='(') cout<<"(7,\"(\")"<<endl;
		else
		if(ch==')') cout<<"(8,\")\")"<<endl;
		else
		if(ch=='+') cout<<"(10,\"+\")"<<endl;
		else
		if(ch=='*') cout<<"(11,\"*\")"<<endl;
		else
		if(ch=='=') 
		{
            GetChar();
			if (ch=='=')
			cout<<"(13,\"==\")"<<endl;
			else
			{
				cout<<"(13,\"=\")"<<endl;
				Retract();
			}
		}
		else
		if(ch=='>') //判断>=关系运算符
		{
            GetChar();
			if(ch==' ')
			cout<<"(13,\">\")"<<endl;
			else if (ch=='=')
			cout<<"(13,\">=\")"<<endl;
		}
		else
		if(ch=='<') //判断<=关系运算符
		{
            GetChar();
			if(ch==' ')
			cout<<"(13,\"<\")"<<endl;
			else if (ch=='=')
			cout<<"(13,\"<=\")"<<endl;
		}
		else
		if(ch==',') cout<<"(17,\",\")"<<endl;
		else if(ch!=' '&&ch!='

⌨️ 快捷键说明

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