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

📄 cifa.cpp

📁 这个是用VC编的词法分析器
💻 CPP
字号:
// Cifa.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <cstdio>
#include <string>
#include <iomanip>
using namespace std;

bool flag=false;//若之前一个token为end,则为true
const int num_of_key = 9;
const int num_of_sign = 7;
const int ID = 6;
const int INT = 7;
const int LT = 8;
const int LE = 9;
const int EQ = 10;
const int NE = 11;
const int GT = 12;
const int GE = 13;
const int M = 18;
const int SIGN = 19;
string key_word[num_of_key];
char alphabet[26];
char sign[7] = {'+','-','*','/',';',',','.'};

void intialize()
{
    key_word[0] = "BEGIN", key_word[1] = "END",  key_word[2] = "IF";
    key_word[3] = "ELSE",  key_word[4] = "THEN", key_word[5] = "DIV";
	key_word[6] = "PROGRAM",   key_word[7] = "integer",key_word[8] = "VAR";
    for(int i = 0;i < 26;i++)
	{
	    
		alphabet[i] = 97 + i;//a---97
	}
	
}

bool Is_alphabet(char c)//check if it is a letter;
{
    for(int i = 0;i < 26;i++)
	{
	     if(c == alphabet[i] || c == (alphabet[i] - 32))  return true;//A---65
	}
	return false;
}
bool Is_num(char c)//check if it is a number;
{
    for(int i = 0;i < 10;i++)
	{
	     if(c == (i + 48)) return true;//0----48
	}
	return false;
}

bool Is_sign(char c)//check if it is a sign
{
     for(int i = 0;i < num_of_sign;i++)
	 {
	      if(c == sign[i])  return true;
	 }
	 return false;
}

int Is_key_word(string s)
{
    for(int i = 0;i < num_of_key;i++)
	{
		if(!s.compare(key_word[i])) return i + 1;
	
	}
	return -1;
}

void display_source(FILE* fp)
{  
	char c = '0';
    while((c = getc(fp)) != EOF)   
	{
	    cout<<c;
	}
}
void scan(FILE* fp)
{
	string str,s = "       ";
    int i = 0;
	char c = '0';
	int id;
	int count = 1;
	while((c = getc(fp)) != EOF)
	{
		 str = "";
		 if(c == 10) count++;
		 
		 if(c == '{')
		 {
		    str.append(1,c);
			 while(((c = getc(fp)) != EOF) && c != '}')
			 {
			      str.append(1,c);
			 }
			 str.append(1,c);
		
			 str = "";
		 }
		
	     else if(Is_alphabet(c))
		 {
		     str.append(1,c);
			 while(((c = getc(fp)) != EOF) && (Is_alphabet(c) || Is_num(c)))
			 {
			      str.append(1,c);
			 }
			 id = Is_key_word(str);
			 if(id == -1) cout<<"("<<ID<<","<<"'"<<str<<"'"<<")"<<endl;
			 else cout<<"("<<id<<","<<"'"<<str<<"'"<<")"<<endl;

			 if(str == "END") flag=true;

			 str = "";
			 if(c == EOF) return;
			 fseek(fp,-1,1);
		 }
		 else if(Is_num(c))
		 {
		     str.append(1,c);
			 c = getc(fp);
			
			 while((c != EOF) && Is_num(c))
			 {
			       str.append(1,c);
				   c = getc(fp); 
				   
			 }
		
			if(Is_alphabet(c))
			 {
				   while((c != EOF) && c != 32)//32---space
				   {
				         str.append(1,c);
				         c = getc(fp);
				   }
			 
				   cout<<"the line:"<<count<<s<<" illegal  id "<<endl;
                   fseek(fp,-1,1);
				   str = "";
			 }
			 else
			 {
			       id = INT;
				   cout<<"("<<id<<","<<"'"<<str<<"'"<<")"<<endl;

			       
			       str = "";
			       fseek(fp,-1,1);
			 }
			
		 }
		 else if(Is_sign(c))
		 {
		     while((c != EOF) && (Is_sign(c)))
			 {
			     id = SIGN;
				 if(flag == true && c != '.')
					 cout<<"the line:"<<count<<s<<"the illegal sign of error"<<endl;
				 else cout<<"("<<id<<","<<"'"<<c<<"'"<<")"<<endl;
				 c = fgetc(fp);
			 }
			 fseek(fp,-1,1);
		 }
		 else
		 {
			 str.append(1,c);
		     switch(c)
			 {
			     case ':': c = getc(fp);
					       if(c == '=') 
						   {
						        id = EQ;
								str.append(1,c);
							cout<<"("<<id<<","<<"'"<<str<<"'"<<")"<<endl;
								
								str = "";
						   }
						   else
						   {
						        id = M;
								cout<<"("<<id<<","<<"'"<<str<<"'"<<")"<<endl;
							
								str = "";
								fseek(fp,-1,1);
						   }
						   break;
				 case '<':c = fgetc(fp);
					      if(c == '=')
						  {
						      id = LE;
							  str.append(1,c);
							  cout<<"("<<id<<","<<"'"<<str<<"'"<<")"<<endl;
							
							  str = "";
						  }
						  else if(c == '>')
						  {
						       id = NE;
							   str.append(1,c);
							   cout<<"("<<id<<","<<"'"<<str<<"'"<<")"<<endl;
						
							   str = "";
						  }
						  else
						  {
							  id = LT;
							  cout<<"("<<id<<","<<"'"<<str<<"'"<<")"<<endl;
							 
							  str = "";
						       fseek(fp,-1,1);//retract
						  }
						  break;
				 case '>':c = fgetc(fp);
					      if(c == '=')
						  {
						      id = GE;
							  str.append(1,c);
							  cout<<"("<<id<<","<<"'"<<str<<"'"<<")"<<endl;
							 
							  str = "";
						  }
						  else
						  {
						       id = GT;
							   cout<<"("<<id<<","<<"'"<<str<<"'"<<")"<<endl;
							   
							   str = "";
							   fseek(fp,-1,1);
						  }
						  break;
				 case '(':
				 case ')':cout<<"("<<id<<","<<"'"<<str<<"'"<<")"<<endl;
					      break;
				default:if(c != 32 && c != 10)
						 {
						     
								
							 cout<<"the line:"<<count<<s<<"the illegal sign of error"<<endl;
						 }
                         break;
			 }
		 }
		 if(c == EOF) return; 
	}

}


int main(int argc, char* argv[])
{
	char* file = "baihui.txt";
	FILE* fp = fopen("baihui.txt","r");
	intialize();
	
	cout<<"原程序:"<<endl<<endl;
	display_source(fp);
	
	fp = fopen("baihui.txt","r");
	
	cout<<endl<<endl<<endl<<"分析后程序:"<<endl<<endl;
    scan(fp);

	return 0;
}

⌨️ 快捷键说明

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