编译原理小c语言,编译原理(SDUTACM小C语言--词法分析程序)

nput

输入一个小C语言源程序,源程序长度不超过2000个字符,保证输入合法。

Output

按照源程序中单词出现顺序输出,输出二元组形式的单词串。

(单词种类,单词值)

单词一共5个种类:

关键字:用keyword表示

自定义标识符:用identifier表示

整数:用integer表示

界符:用boundary表示

运算符:用operator表示

每种单词值用该单词的符号串表示。#include

#include

using namespace std;

string S[5]= {"keyword","identifier","integer","boundary","operator"};

string T[6]= {"main","if","else","for","while","int"};

void panduan(string s)

{

if(s[0]>='0'&&s[0]<='9') //开头是数字肯定就为数字

{

cout<

}

else

{

int f=1;

for(int i=0; i<6; i++)

{

if(s==T[i])

{

f=0;

cout<

break;

}

}

if(f==1)

{

cout<

}

}

}

int main()

{

string s;

while(cin>>s)

{

int len=s.length();

string temp="";

for(int i=0; i

{

//操作符

if(s[i] == '=' || s[i] == '+' || s[i] == '-'||s[i] == '*'|| s[i] == '/' || s[i] == '' || s[i] == '!')

{

if(temp.length())

{

panduan(temp);

}

temp="";

if(i+1

{

cout<

i++;

}

else

{

cout<

}

}

//界符

else if(s[i] == '(' || s[i] == ')' || s[i] == '{'||s[i] == '}'|| s[i] == ',' || s[i] ==';')

{

if(temp.length())

{

panduan(temp);

}

temp="";

cout<

}

//不是界符也不是操作符,就存到临时字符串里面,等待判断

else

{

temp=temp+s[i];

}

}

if(temp.length())

{

panduan(temp);

}

}

return 0;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值