一、文件部分
anly.cpp 进行词法分析
tools.h 词法分析所用到的工具
二、方法介绍
/*判断是否为大写字母*/ bool IsUpper( const char& ch_char ); /*判断是否为小写字母*/ bool IsLower( const char& ch_char ); /*判断是否为字母*/ bool IsLetter( const char& ch_char ); /*判断是否为数字*/ bool IsNumber( const char& ch_char ); /*判断是否为标识符*/ bool IsIdentifier( const char& ch_char ); /*判断组中有没有目标字符串*/ bool IsStringInStrings( const C_String& o_string, C_String ao_strings[], const int& stringsLength );
讯享网
三、代码部分
tools.h
讯享网# ifndef __TOOLS_H__ # define __TOOLS_H__ # include "bits/stdc++.h" using namespace std; typedef std::string C_String ; //新类型 typedef std::ifstream C_Ifstream ; typedef std::stringstream C_StringStream ; const int KEYWORDS_LENGTH = 7; C_String keywords[KEYWORDS_LENGTH] = { "print", "println", "integer", "boolean", "floating", "string", "function" }; bool IsUpper( const char& ch_char ){ //大写 return ch_char >= 'A' && ch_char <= 'Z'; } bool IsLower( const char& ch_char ){ //小写 return ch_char >= 'a' && ch_char <= 'z'; } bool IsLetter( const char& ch_char ){ //字母 return IsUpper( ch_char ) || IsLower( ch_char ); } bool IsNumber( const char& ch_char ){ //数字 return ( ch_char >= '0' && ch_char <= '9' ) || ch_char == '.'; } bool IsIdentifier( const char& ch_char ){ //后标识符 return IsLetter( ch_char ) || IsNumber( ch_char ) || ch_char == '_'; } bool IsStringInStrings( const C_String& o_string, C_String ao_strings[], const int& stringsLength ){ for( unsigned int index = 0; index < stringsLength; index++ ){ if( ao_strings[index] == o_string ) return true; }return false; } # endif
anly.cpp
/* * anly.cpp * * Created on: 2021.5.25 * Author: LB_303 */ # include "tools.h" void Analyze( C_String text ){ //分析 //缓冲字符串 C_String o_bufferSave = ""; for( unsigned int index = 0; index < text.length(); index++ ){ //缓冲字符串清空 o_bufferSave = ""; //是字母或是下划线 if( IsLetter( text[index] ) || text[index] == '_' ){ //获取这个单词 while( IsIdentifier( text[index] ) ){ //将单词内容逐个塞入缓冲字符串 o_bufferSave = o_bufferSave + text[index]; index++; }index--;//退位 //是关键字 if( IsStringInStrings( o_bufferSave, keywords, KEYWORDS_LENGTH ) ){ //打印信息 cout << "关键字 ->" << o_bufferSave << endl; //不是 }else{ //打印信息 cout << "标识符 ->" << o_bufferSave << endl; } }else if( IsNumber( text[index] ) ){//是数字 //获取整个数字 while( IsNumber( text[index] ) ){ //将数字逐一塞到缓冲字符串 o_bufferSave = o_bufferSave + text[index]; index++; }index--;//退位 //打印信息 cout << "数字 ->" << o_bufferSave << endl; }else{//符号处理 int bufferTotal = 1; switch( text[index] ){ //左方括号 case '[': //向缓冲字符串塞入左方括号 o_bufferSave += '['; //挪到下一位 index++; //方括号内的内容塞到缓冲字符串 while( bufferTotal != 0 ){ if( text[index] == '[' ){ bufferTotal++; }else if( text[index] == ']' ){ bufferTotal--; } o_bufferSave += text[index]; index++; }index--; //打印信息 cout << "方括号组 ->" << o_bufferSave << endl; //除去头尾大括号 o_bufferSave = o_bufferSave.substr( 1, o_bufferSave.length() - 2 ); //分析方括号内内容 Analyze( o_bufferSave ); break; //左大括号 case '{': //向缓冲字符串塞入左大括号 o_bufferSave += '{'; //挪到下一位 index++; //大括号内的内容塞到缓冲字符串 while( bufferTotal != 0 ){ if( text[index] == '{' ){ bufferTotal++; }else if( text[index] == '}' ){ bufferTotal--; } o_bufferSave += text[index]; index++; }index--; //打印信息 cout << "大括号组 ->" << o_bufferSave << endl; //除去头尾大括号 o_bufferSave = o_bufferSave.substr( 1, o_bufferSave.length() - 2 ); //分析大括号内内容 Analyze( o_bufferSave ); break; //左括号 case '(': //向缓冲字符串塞入左括号 o_bufferSave += '('; //挪到下一位 index++; //括号内的内容塞到缓冲字符串 while( bufferTotal != 0 ){ if( text[index] == '(' ){ bufferTotal++; }else if( text[index] == ')' ){ bufferTotal--; } o_bufferSave += text[index]; index++; }index--; //打印信息 cout << "括号组 ->" << o_bufferSave << endl; //除去头尾括号 o_bufferSave = o_bufferSave.substr( 1, o_bufferSave.length() - 2 ); //分析括号内内容 Analyze( o_bufferSave ); break; //单引号 case '\'': //向右一个发现单引号 if( text[index+1] == '\'' ){ //向缓冲字符串塞入 o_bufferSave += "''"; //向右不是单引号 }else{ //向缓冲字符串塞入第一个单引号 o_bufferSave += '\''; //挪到下一位 index++; //获取单引号之间的内容塞到缓冲字符串里面 while( text[index] != '\'' ){ o_bufferSave += text[index]; index++; }o_bufferSave += text[index];//再将第二格单引号塞入缓冲字符串 //打印信息 cout << "字符字面表达式 ->" << o_bufferSave << endl; } break; //同上 case '"': if( text[index+1] == '"' ){ o_bufferSave += "\"\""; }else{ o_bufferSave += '"'; index++; while( text[index] != '"' ){ o_bufferSave += text[index]; index++; }o_bufferSave += text[index]; cout << "字符字面表达式 ->" << o_bufferSave << endl; } break; //分号 case ';': cout << "分号->" << text[index] << endl; break; //其他 default: break; } } } return; } int main( int args, char* argv[] ){ /* * 文件路径 * 文件读取流 * 文件内容存放处 * 字符串流 */ char ach_filePath[] = "D:/anly/anly/text.txt";//此处换成要分析的文件路径 ifstream o_fileInput ( ach_filePath ); C_String o_fileText = "" ; C_StringStream o_stringstream ; if( !o_fileInput ){ cout << "文件打开失败"; }else{ //字符流读取文件内容,包括空格换行 o_stringstream << o_fileInput.rdbuf(); //字符流将内容导入文件内容存放处,包括空格和换行 o_fileText = o_stringstream.str(); //分析 Analyze( o_fileText ); } return 0; }
四、运行结果

(本人第一次写博客,写的不好请谅解一下~)
(个人原创)
(Rìnso bi LB_303 áns ei data 2021.ECF .)

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/11501.html