一个数字的组成由以下几个字符

正负号 + -   小数点 .   数字 0-9

比如

3

-3

3.13

-34.2234

但是符号和小数点不会出现多次

那么识别流程用图来表示 则是

整数

浮点数

一个读取C++源文件 将内容解析成一个个单独的TOKEN的代码

代码1

  1. #include <iostream>
  2. #include <fstream>
  3. #include <cctype>
  4. #include <cstring>
  5. #include <string>
  6. #include <exception>
  7.  
  8. using namespace std;
  9.  
  10. #define SOURCE_FILE_NAME "sourcefile.cpp"
  11. #define DEST_FILE_NAME "destfile.cpp"
  12.  
  13. // The input and output file streams.
  14. ifstream fin;
  15. ofstream fout;
  16.  
  17. bool GetToken(string& token){
  18. bool bRet = false;
  19. char ch;
  20.  
  21. ch = fin.get();
  22. if(ch == EOF){
  23. return false;
  24. }
  25.  
  26. if(isspace(ch)){
  27. //进入接受连续空白符(' ' '\n'等)
  28. while(isspace(ch)){
  29. token += ch;
  30. ch = fin.get();
  31. }
  32. fin.putback(ch);
  33. bRet = true;
  34. return bRet;
  35. }
  36.  
  37. if(isalpha(ch)){
  38. while(isalpha(ch)){
  39. token += ch;
  40. ch =fin.get();
  41. }
  42. fin.putback(ch);
  43. bRet = true;
  44. return bRet;
  45. }
  46.  
  47. if(isdigit(ch)){
  48. while(isdigit(ch) || ch == '.'){
  49. token += ch;
  50. ch = fin.get();
  51. }
  52. fin.putback(ch);
  53. bRet = true;
  54. return bRet;
  55. }
  56.  
  57. if(ch == '-' || ch == '+'){
  58. token += ch;
  59. ch = fin.get();
  60. while(isdigit(ch) || ch == '.'){
  61. token += ch;
  62. ch = fin.get();
  63. }
  64. fin.putback(ch);
  65. bRet = true;
  66. return bRet;
  67. }
  68.  
  69. if(ch == '<' || ch == '>'){
  70. token += ch;
  71. ch = fin.get();
  72. if(ch == '<' || ch == '>'){
  73. token += ch;
  74. }else{
  75. fin.putback(ch);
  76. }
  77. bRet = true;
  78. return bRet;
  79. }
  80.  
  81. token += ch;
  82. bRet = true;
  83. return bRet;
  84. }
  85.  
  86. int main(int argc, char *argv[])
  87. {
  88. fin.open(SOURCE_FILE_NAME);
  89. if(!fin){
  90. cout << "Open source file error.Exit!!" << endl;
  91. return -1;
  92. }
  93.  
  94. fout.open(DEST_FILE_NAME);
  95. if(!fout){
  96. cout << "Open destinaton file error.Exit!!" << endl;
  97. return -1;
  98. }
  99.  
  100. try{
  101. string token;
  102. while(GetToken(token)){
  103. cout << token ;//<< endl;
  104. token.clear();
  105. }
  106.  
  107. }catch(exception& e){
  108. cerr << e.what() << endl;
  109. }
  110.  
  111. fin.close();
  112. fout.close();
  113. cout << "Hello World!"<<endl;
  114. return 0;
  115. }

 测试文件

  1. 293048 24 895523
  2. 3.14159
  3. 235
  4. 253
  5. 52435 345
  6.  
  7. 459245
  8.  
  9. 22 .5 .35 2.0
  10.  
  11. 1
  12. 0.0
  13. 1.0
  14. 0
  15.  
  16. 02345
  17.  
  18. 63246 0.2346
  19. 34.0

 代码2

  1. #include <iostream>
  2. #include <fstream>
  3. #include <exception>
  4. #include <queue>
  5. using namespace std;
  6.  
  7. #define IN_FILE_NAME "SourceFile.cpp"
  8. #define OUT_FILE_NAME "DestinationFile.cpp"
  9.  
  10. enum STATE{
  11. state_init = 0,
  12. state_int,
  13. state_float,
  14. state_error
  15. };
  16.  
  17. class FileParse{
  18. public:
  19. FileParse(const string& infileName,const string& outfileName){
  20. fin_.open(infileName);
  21. fout_.open(outfileName);
  22. }
  23. ~FileParse(){
  24. if(fin_.is_open())
  25. fin_.close();
  26. if(fout_.is_open())
  27. fout_.close();
  28. }
  29.  
  30. bool ParseToTokens(){
  31. STATE state = state_init;
  32. bool isFinish = false;
  33. string token;
  34.  
  35. if(linestr_.empty())
  36. return false;
  37.  
  38. for(size_t i = 0;i<linestr_.size();++i){
  39. char currentChar = linestr_[i];
  40. if(currentChar == '\0')
  41. break;
  42.  
  43. switch(state){
  44. case state_init:
  45. if(isspace(currentChar)){
  46. continue;
  47. }else if(isdigit(currentChar)){
  48. state = state_int;
  49. token += currentChar;
  50. continue;
  51. }else if(currentChar == '.'){
  52. state = state_float;
  53. token += currentChar;
  54. continue;
  55. }else{
  56. state = state_error;
  57. break;
  58. }
  59. case state_int:
  60. if(isdigit(currentChar)){
  61. state = state_int;
  62. token += currentChar;
  63. continue;
  64. }else if(currentChar == '.'){
  65. state = state_float;
  66. token += currentChar;
  67. continue;
  68. }else if(isspace(currentChar)){
  69. isFinish = true;
  70. break;
  71. }else{
  72. state = state_error;
  73. break;
  74. }
  75.  
  76. case state_float:
  77. if(isdigit(currentChar)){
  78. state = state_int;
  79. token += currentChar;
  80. continue;
  81. }else if(isspace(currentChar)){
  82. isFinish = true;
  83. break;
  84. }else{
  85. state = state_error;
  86. break;
  87. }
  88.  
  89. case state_error:
  90. break;
  91. }
  92.  
  93. if(isFinish ){
  94. cout << token <<endl;
  95. token.clear();
  96. isFinish = false;
  97. state = state_init;
  98. }
  99. }
  100.  
  101. return true;
  102. }
  103.  
  104. bool run(){
  105. try{
  106. if(!fin_.is_open() || !fout_.is_open()) {
  107. throw runtime_error("open file is null");
  108. }
  109. while(1){
  110. if (fin_.eof())
  111. break;
  112. linestr_.clear();
  113. getline(fin_,linestr_);
  114. linestr_ += '\n';
  115. ParseToTokens();
  116. }
  117.  
  118. }catch(exception& e){
  119. cerr << e.what() << endl;
  120. return false;
  121. }
  122.  
  123. }
  124.  
  125. private:
  126. string linestr_;
  127. queue<string> vecToken_;
  128. ifstream fin_;
  129. ofstream fout_;
  130. };
  131.  
  132. int main(int argc, char *argv[])
  133. {
  134. FileParse a(IN_FILE_NAME,OUT_FILE_NAME);
  135. a.run();
  136. return 0;
  137. }

  显示结果

代码3 新增字符串的识别解析

  1. #include <iostream>
  2. #include <fstream>
  3. #include <exception>
  4. #include <queue>
  5. using namespace std;
  6.  
  7. #define IN_FILE_NAME "SourceFile.cpp"
  8. #define OUT_FILE_NAME "DestinationFile.cpp"
  9.  
  10. enum STATE{
  11. state_init = 0,
  12. state_int,
  13. state_float,
  14. state_word,
  15. state_error
  16. };
  17.  
  18. class FileParse{
  19. public:
  20. FileParse(const string& infileName,const string& outfileName){
  21. fin_.open(infileName);
  22. fout_.open(outfileName);
  23. }
  24. ~FileParse(){
  25. if(fin_.is_open())
  26. fin_.close();
  27. if(fout_.is_open())
  28. fout_.close();
  29. }
  30.  
  31. bool ParseToTokens(){
  32. STATE state = state_init;
  33. bool isFinish = false;
  34. string token;
  35.  
  36. if(linestr_.empty())
  37. return false;
  38.  
  39. for(size_t i = 0;i<linestr_.size();++i){
  40. char currentChar = linestr_[i];
  41. if(currentChar == '\0')
  42. break;
  43.  
  44. switch(state){
  45. case state_init:
  46. if(isspace(currentChar)){
  47. continue;
  48. }else if(isdigit(currentChar)){
  49. state = state_int;
  50. token += currentChar;
  51. continue;
  52. }else if(currentChar == '.'){
  53. state = state_float;
  54. token += currentChar;
  55. continue;
  56. }else if(isalpha(currentChar)|| currentChar == '_'){
  57. state = state_word;
  58. token += currentChar;
  59. continue;
  60. }else{
  61. state = state_error;
  62. break;
  63. }
  64. case state_word:
  65. if(isalpha(currentChar)||isdigit(currentChar)||
  66. currentChar == '_'){
  67. state = state_word;
  68. token += currentChar;
  69. continue;
  70. }else if(isspace(currentChar)){
  71. isFinish = true;
  72. break;
  73. }else{
  74. state = state_error;
  75. break;
  76. }
  77. case state_int:
  78. if(isdigit(currentChar)){
  79. state = state_int;
  80. token += currentChar;
  81. continue;
  82. }else if(currentChar == '.'){
  83. state = state_float;
  84. token += currentChar;
  85. continue;
  86. }else if(isspace(currentChar)){
  87. isFinish = true;
  88. break;
  89. }else{
  90. state = state_error;
  91. break;
  92. }
  93.  
  94. case state_float:
  95. if(isdigit(currentChar)){
  96. state = state_int;
  97. token += currentChar;
  98. continue;
  99. }else if(isspace(currentChar)){
  100. isFinish = true;
  101. break;
  102. }else{
  103. state = state_error;
  104. break;
  105. }
  106.  
  107. case state_error:
  108. break;
  109. }
  110.  
  111. if(isFinish ){
  112. cout << token <<endl;
  113. token.clear();
  114. isFinish = false;
  115. state = state_init;
  116. }
  117. }
  118.  
  119. return true;
  120. }
  121.  
  122. bool run(){
  123. try{
  124. if(!fin_.is_open() || !fout_.is_open()) {
  125. throw runtime_error("open file is null");
  126. }
  127. while(1){
  128. if (fin_.eof())
  129. break;
  130. linestr_.clear();
  131. getline(fin_,linestr_);
  132. linestr_ += '\n';
  133. ParseToTokens();
  134. }
  135.  
  136. }catch(exception& e){
  137. cerr << e.what() << endl;
  138. return false;
  139. }
  140.  
  141. }
  142.  
  143. private:
  144. string linestr_;
  145. queue<string> vecToken_;
  146. ifstream fin_;
  147. ofstream fout_;
  148. };
  149.  
  150. int main(int argc, char *argv[])
  151. {
  152. FileParse a(IN_FILE_NAME,OUT_FILE_NAME);
  153. a.run();
  154. return 0;
  155. }

  测试文本

  1. 293048 24 895523
  2. 3.14159
  3. 235
  4. 253
  5. 52435 345
  6.  
  7. MyVar0 MyVar1 MyVar2
  8. 459245
  9.  
  10. rEtUrN
  11.  
  12. TRUE false
  13.  
  14. 22 .5 .35 2.0
  15.  
  16. while
  17.  
  18. 1
  19. 0.0 var
  20. 1.0 var
  21. 0
  22.  
  23. This_is_an_identifier
  24.  
  25. 02345
  26.  
  27. _so_is_this___
  28.  
  29. 63246 0.2346
  30. 34.0

  显示结果

游戏脚本编程 文本token解析的更多相关文章

  1. 高级Bash脚本编程指南(27):文本处理命令(三)

    高级Bash脚本编程指南(27):文本处理命令(三) 成于坚持,败于止步 处理文本和文本文件的命令 tr 字符转换过滤器. 必须使用引用或中括号, 这样做才是合理的. 引用可以阻止shell重新解释出 ...

  2. shell编程系列24--shell操作数据库实战之利用shell脚本将文本数据导入到mysql中

    shell编程系列24--shell操作数据库实战之利用shell脚本将文本数据导入到mysql中 利用shell脚本将文本数据导入到mysql中 需求1:处理文本中的数据,将文本中的数据插入到mys ...

  3. 转载:[转]如何学好3D游戏引擎编程

      [转]如何学好3D游戏引擎编程 Albert 本帖被 gamengines 从 游戏引擎(Game Engine) 此文为转载,但是值得一看. 此篇文章献给那些为了游戏编程不怕困难的热血青年,它的 ...

  4. linux脚本编程技术

    linux脚本编程技术 一.什么是脚本 脚本是一个包含一系列命令序列的可执行(777)文本文件.当运行这个脚本文件时,文件中包含的命令序列将得到自动执行. 二.脚本编程 #!/bin/sh 首行固定格 ...

  5. linux脚本编程技术---8

    一.什么是脚本 脚本是一个包含一系列命令序列的可执行(777)文本文件.当运行这个脚本文件时,文件中包含的命令序列将得到自动执行. 二.脚本编程 #!/bin/sh 首行固定格式 #!表明该脚本的的解 ...

  6. 《Linux命令行与shell脚本编程大全》 第二十二章 学习笔记

    第二十二章:使用其他shell 什么是dash shell Debian的dash shell是ash shell的直系后代,ash shell是Unix系统上原来地Bourne shell的简化版本 ...

  7. javascript进阶——分离式DOM脚本编程

    编写分离式(unobstrusive)代码意味着对HTML内容的完全分离:数据来自服务器端,javascript代码用来动态化和交互.这种分离的好处是在不同浏览器之间使用是可以完全降级或升级运行,对于 ...

  8. Shell脚本编程(一):初识shell script

    Shell简介 Shell是一个命令解释器,它是一个用 C 语言编写的程序,它是用户使用 Linux 的桥梁.Shell 是指一种应用程序,这个应用程序提供了一个界面,用户通过这个界面访问操作系统内核 ...

  9. 【Shell脚本编程系列】知识储备以及建立规范的脚本

    前言 学习shell脚本编程需要的知识储备: vi/vim编辑器命令 vimrc设置要熟练 基础命令,100多个要熟练 基础和常用的网络服务命令要会:nfs . rsync. inotify . la ...

随机推荐

  1. Web GIS系统相关

    最近研究了百度 echarts 的一个很炫酷的示例: http://gallery.echartsjs.com/editor.html?c=xrJHCfsfE- 看了代码发现了很多不懂1东西,研究研究 ...

  2. 黄聪:解决Bootstrap模态框(modal)弹出后页面跑到顶部的办法

    bootstrap 3.1.1 版本解决方案: body.modal-open { position: absolute !important; }

  3. Ubuntu 16.04 LTS 常用快捷键

    在Linux下Win键就是Super键 启动器 Win(长按) 打开启动器,显示快捷键 Win + Tab 通过启动器切换应用程序 Win + 1到9 与点击启动器上的图标效果一样 Win + Shi ...

  4. tomcat窗口一闪而过

    当点击bin/startup.bat,出现黑窗口一闪而过时,肯定是因为tomcat启动报错了. 错误排查方法 首先检查java环境变量是否设置正确. 其次调试tomcat,需要修改startup.ba ...

  5. 第25课 可变参数模板(6)_function_traits和ScopeGuard的实现

    1. function_traits (1)function_traits的作用:获取函数的实际类型.返回值类型.参数个数和具体类型等.它能获取所有函数语义类型信息.可以获取普通函数.函数指针.std ...

  6. Android Studio 加载网络图片

    Android Studio是基于gradle的一个Android开发软件,在引用网络图片的时候需要连接第三方库,这里介绍 引用glide的方法. 一.在github页面搜索glide,点击第一个 二 ...

  7. nobup 与 后台运行命令

    1. Linux进程状态:R (TASK_RUNNING),可执行状态&运行状态(在run_queue队列里的状态) 2. Linux进程状态:S (TASK_INTERRUPTIBLE),可 ...

  8. Python 内置os模块的简单实用

    获取路径&目录添加文件 在自动化测试的过程,考虑到工程文件的移动或者在其他人的工作环境中运行,所以我们的路径要灵活,不能把路径写死. 推荐使用Python的内置模块OS 参照图 import ...

  9. 作着玩:登录页(纯css,不支持ie9以下)

    支持chrome FireFox 和 IE10+,(IE9也能显示,IE9以下不支持) <style type="text/css"> body{position:re ...

  10. CMake Error at cuda_compile_generated_warp.cu.o.cmake:264 (message)

    今天,我来给大家分享一下opencv安装时报的错.然后讲错是怎么解决的. 为啥老是写一些环境搭建的博客?因为环境搭建琐碎而繁杂,希望写下来,帮助大家.让大家少走弯路. 专注主业,专注算法的实现和优化. ...