标签:std 编译 i+1 cpp strlen break art des string
以下是一个简单的词法分析器
#include <stdio.h> #include <string.h> using namespace std ; int main() { char str[150] ; // freopen("C:\\Users\\Lionel\\Desktop\\1.txt","r",stdin) ; int row = 1 , cow = 0 ; while(gets(str)) { char temp[10] ; int index = 0 , i = 0; while(i<strlen(str)+1) { if(str[i] == ‘ ‘ || str[i] == ‘\t‘ || str[i] == ‘\0‘) { temp[index] = ‘\0‘ ; if(!strcmp(temp,"if")) { printf("IF (%d,%d)\n",row,i-index+1) ; } else { bool flag = false ; for(int j = 0 ; j < index ; ++j) { if(temp[j]<‘0‘ || temp[j]>‘9‘) { flag = true ; break ; } } if(flag) { printf("ID(%s) (%d,%d)\n",temp,row,i-index+1) ; } else { printf("NUM(%s) (%d,%d)\n",temp,row,i-index+1) ; } } while((str[i] == ‘ ‘ || str[i] == ‘\t‘) && (str[i+1] == ‘ ‘ || str[i+1] == ‘\t‘)) { ++i ; } index = 0 ; } else { temp[index++] = str[i] ; } ++i ; } row++ ; } return 0 ; }
标签:std 编译 i+1 cpp strlen break art des string
原文地址:http://www.cnblogs.com/wzjhoutai/p/7142315.html