编译原理上机 实验1 简单的词法分析子程序

匿名 (未验证) 提交于 2019-12-03 00:17:01

1.

2.

*/

8*2.5-1.0e2

8

*

2.5

-

1.0e2

1126

1 G[<>]

#include <iostream> #include <string> #include <ctype.h> using namespace std;  int fun(char art) {     if((art>='a'&&art<='z')||(art>='A'&&art<='Z')|| art == '_')     {         return 1;     }     else if(isdigit(art))     {         return 1;     }     else     {         return 0;     } }  void main() {     int count = 0;     char string[20];     cin>>string;     if(string[0]>='a'&&string[0]<='z' || string[0]>='A' && string[0]<='z')     {         for(int i=1;i<strlen(string);i++)         {             if(fun(string[i]) == 0)             {                 cout<<"标识符不合法"<<endl;                 count = 1;             }         }         if(count == 0)         {             cout<<string;             cout<<"标识符合法"<<endl;         }      }     else     {         cout<<"首字符要大写"<<endl;     } }

import re a = input()  b = list(a) str1 = list() #用来存放+ - * / 在b中的位置 str2 = ['+','-','*','/','(',')'] for index,i in enumerate(b):     if i in str2:        # c = [index,i] #将运算符的位置存入str1中         str1.append(i)  l1 = re.split('[-,*,+,/,(,)]',a) for index,i in enumerate(l1):     print(i)     if index< len(l1)-1:          print(str1[index])

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!