Time Limit: 1000 ms Memory Limit: 5000 KiB
Submit Statistic
Problem Description
小C语言文法
每行单词数不超过10个
小C语言文法如上,现在我们对小C语言写的一个源程序进行词法分析,分析出关键字、自定义标识符、整数、界符
和运算符。
关键字:main if else for while int
自定义标识符:除关键字外的标识符
整数:无符号整数
界符:{ } ( ) , ;
运算符:= + - * / < <= > >= == !=
Input
输入一个小C语言源程序,源程序长度不超过2000个字符,保证输入合法。
Output
按照源程序中单词出现顺序输出,输出二元组形式的单词串。
(单词种类,单词值)
单词一共5个种类:
关键字:用keyword表示
自定义标识符:用identifier表示
整数:用integer表示
界符:用boundary表示
运算符:用operator表示
每种单词值用该单词的符号串表示。
Sample Input
main()
{
int a, b;
if(a == 10)
{
a = b;
}
}
Sample Output
(keyword,main)
(boundary,()
(boundary,))
(boundary,{)
(keyword,int)
(identifier,a)
(boundary,)
(identifier,b)
(boundary,?
(keyword,if)
(boundary,()
(identifier,a)
(operator,==)
(integer,10)
(boundary,))
(boundary,{)
(identifier,a)
(operator,=)
(identifier,b)
(boundary,?
(boundary,})
(boundary,})
Hint
Source
cai++
#include <bits/stdc++.h>
using namespace std;
int isDigit(char a)
{
if(a-'0'>=0&&a-'0'<=9)
return 1;
else
return 0;
}
int isAlpha(char a)
{
if((a>='a'&&a<='z')||(a>='A'&&a<='Z'))
return 1;
else
return 0;
}
int isboundary(char a)
{
if(a=='{'||a=='}'||a=='('||a==')'||a==','||a==';')
return 1;
else
return 0;
}
int isoperator(char a)
{
if(a=='='||a=='+'||a=='-'||a=='*'||a=='/'||a=='<'||a=='>'||a=='!')
return 1;
else
return 0;
}
int main()
{
char s[123];
int j;
while(~scanf("%s", s))
{
int len = strlen(s);
int k = 0;
while(k<len)
{
if(s[k]==' '||s[k]=='\n')
{
k++;
continue;
}
else if(isDigit(s[k]))
{
char temp[123];
j = 0;
temp[j++] = s[k];
k++;
while(k<len&&isDigit(s[k]))
{
temp[j++] = s[k];
k++;
}
temp[j] = '\0';
cout<<"(integer,"<<temp<<")"<<endl;
}
else if(isAlpha(s[k])||s[k]=='_')
{
char temp[123];
j = 0;
temp[j++] = s[k];
k++;
while(k<len&&(isAlpha(s[k])||isDigit(s[k])||s[k]=='_'))
{
temp[j++] = s[k];
k++;
}
temp[j] = '\0';
if(strcmp(temp, "if")==0||strcmp(temp, "main")==0||strcmp(temp, "else")==0||strcmp(temp, "for")==0||strcmp(temp, "while")==0||strcmp(temp, "int")==0)
{
cout<<"(keyword,"<<temp<<")"<<endl;
}
else
{
cout<<"(identifier,"<<temp<<")"<<endl;
}
}
else if(isboundary(s[k]))
{
cout<<"(boundary,"<<s[k]<<")"<<endl;
k++;
}
else if(isoperator(s[k]))
{
if(k<len-1)
{
k++;
if(s[k]=='='&&(s[k-1]=='='||s[k-1]=='>'||s[k-1]=='<'||s[k-1]=='!'))
{
cout<<"(operator,"<<s[k-1]<<s[k]<<")"<<endl;
k++;
}
else
{
cout<<"(operator,"<<s[k-1]<<")"<<endl;
}
}
else
{
cout<<"(operator,"<<s[k]<<")"<<endl;
k++;
}
}
}
}
return 0;
}
Problem Description 小C语言文法 1. <程序>→(){<声明序列><语句序列>} 2. <声明序列>→<声明序列><声明语句>|<声明语句>|<空> 3. <声明语句>→<标识符表>; 4. <标识符表>→...
小C语言–词法分析程序 Time Limit: 1000 ms Memory Limit: 5000 KiB Problem Description 小C语言文法 1. <程序>→(){<声明序列><语句序列>} 2. <声明序列>→<声明序列><声明语句>|<声明语句>|<空...
Problem Description 小C语言文法 1. <程序>→(){<声明序列><语句序列>} 2. <声明序列>→<声明序列><声明语句>|<声明语句>|<空> 3. <声明语句>→<标识符表>; 4. <标识符表>...
Problem Description 小C语言文法 <程序>→<main关键字>(){<声明序列><语句序列>} <声明序列>→<声明序列><声明语句>|<声明语句>|<空> <声明语句>→<标识符表>; <标识符表>&ra...
小C语言--词法分析程序 Time Limit: 1000 ms Memory Limit: 65535 KiB Submit Statistic Problem Description 小C语言文法 1. <程序>→<main关键字>(){<声明序列><语句序列>} 2. <声明序...
小C语言文法 1. <程序>→(){<声明序列><语句序列>} 2. <声明序列>→<声明序列><声明语句>|<声明语句>|<空> 3. <声明语句>→<标识符表>; 4. <标识符表>→<标识符>,&l...
...
小C语言--词法分析程序 Time Limit: 1000MS Memory Limit: 5000KB Submit Statistic Problem Description 小C语言文法 1. <程序>→(){<声明序列><语句序列>} 2. <声明序列>→<声明序列><...
小C语言--词法分析程序 Time Limit: 1000MS Memory Limit: 5000KB Submit Statistic Problem Description 小C语言文法 1. <程序>→(){<声明序列><语句序列>} 2. <声明序列>→<声明序列...
1009 Product of Polynomials 思路 代码实现 原题链接 思路 (2.4x+3.2x0)∗(1.5x2+0.5x)=3.6x3+6.0x2+1.6x 和1002 A+B for Polynomials 思路差不多 系数都大为0,用下标存系数,值存指数 两个多项式都输入后,循环相乘 代码实现 原题链接 原题链接 中文链接...