小C语言--词法分析程序

小C语言--词法分析程序

Time Limit: 1000MS  Memory Limit: 5000KB
Problem Description

小C语言文法 
1. <程序>→(){<声明序列><语句序列>}
2. <声明序列>→<声明序列><声明语句>|<声明语句>|<空>
3. <声明语句>→<标识符表>;
4. <标识符表>→<标识符>,<标识符表>|<标识符>
5. <语句序列>→<语句序列><语句>|<语句>
6. <语句>→< if语句>|< while语句>|< for语句>|<复合语句>|<赋值语句>
7. < if语句>→< if关键字>(<表达式>)<复合语句>|(<表达式>)<复合语句>< else关键字><复合语句>
8. < while语句>→< while关键字>(<表达式>)<复合语句>
9. < for语句>→< for关键字>(<表达式>;<表达式>;<表达式>)<复合语句>
10. <复合语句>→{<语句序列>}
11. <赋值语句>→<表达式>;
12. <表达式>→<标识符>=<算数表达式>|<布尔表达式>
13. <布尔表达式>→<算数表达式> |<算数表达式><关系运算符><算数表达式>
14. <关系运算符>→>|<|>=|<=|==|!=
15. <算数表达式>→<算数表达式>+<项>|<算数表达式>-<项>|<项>
16. <项>→<项>*<因子>|<项>/<因子>|<因子>
17. <因子>→<标识符>|<无符号整数>|(<算数表达式>)
18. <标识符>→<字母>|<标识符><字母>|<标识符><数字>
19. <无符号整数>→<数字>|<无符号整数><数字>
20. <字母>→a|b|…|z|A|B|…|Z
21. <数字>→0|1|2|3|4|5|6|7|8|9

22. < main关键字>→main
23. < if关键字>→if
24. < else关键字>→else
25. < for关键字>→for
26. < while关键字>→while
27. < int关键字>→int


小C语言文法如上,现在我们对小C语言写的一个源程序进行词法分析,分析出关键字、自定义标识符、整数、界符
和运算符。
关键字:main if else for while int
自定义标识符:除关键字外的标识符
整数:无符号整数
界符:{ } ( ) , ;
运算符:= + - * / < <= > >= == !=

Input

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

Output

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

(单词种类,单词值)

单词一共5个种类:

关键字:用keyword表示
自定义标识符:用identifier表示
整数:用integer表示
界符:用boundary表示
运算符:用operator表示

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

Example Input
main() 
{
    int a, b;
    if(a == 10)
    {
        a = b;
    }
}
Example 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
这是简单的此法分析题目:在遇到空格、制表时不做处理,在遇到换行时行数自加(line,这没用到);遇到界符直接判断;在遇到数字开头时,一直判断到非数字,则这一段数字表示整数(因为题目明确说明了);遇到字母或下划线开头的,则一直判断到非字母、数字、下划线,再将这段字符串与题目中的6个关键字比较后判断是关键字还是自定义标识符;最后一定是运算符了,若下一个输入字符是“=”,则一定是两个字节的运算符,否则就是一个的。


#include<stdio.h>
#include<string.h>
int isDigit(char ch)
{
    if(ch>='0'&&ch<='9')
        return 1;
    else return 0;
}

int isLitter(char ch)
{
    if(ch>='a'&&ch<='z'||ch>='A'&&ch<='Z')
        return 1;
    else return 0;
}

int isKeyword(char tmp[])
{
    if(!strcmp(tmp,"main")|| !strcmp(tmp,"if") || !strcmp(tmp,"else") || !strcmp(tmp,"for") || !strcmp(tmp,"while") || !strcmp(tmp,"int"))
        return 1;
    else return 0;
}
void get()
{
    char ch;
    int line=0;//行数
    char tmp[100];
    int i=0;
    while(~scanf("%c",&ch))
    {
        if(ch==' '||ch=='\t')//为空格或是tab
            continue;
        //回车
        else if(ch=='\n')
            line++;
        //是否是界符
        else if(ch=='('||ch==')'||ch=='{'||ch=='}'||ch==','||ch==';')
            printf("(boundary,%c)\n",ch);

        else if(isDigit(ch))//如果是数字
        {
            while(isDigit(ch))
            {
                tmp[i++]=ch;
                scanf("%c",&ch);
            }
            tmp[i]='\0';//加上结束符
            i=0;
            printf("(integer,%s)\n",tmp);
            ungetc(ch,stdin);//将字符退回到输入流中
        }
        else if(isLitter(ch)||ch=='_')
        {
            while(isLitter(ch)||isDigit(ch)||ch=='_')
            {
                tmp[i++]=ch;
                scanf("%c",&ch);
            }
            tmp[i]='\0';
            i=0;
            if(isKeyword(tmp))  //关键字
                printf("(keyword,%s)\n",tmp);
            else            //自定义标识符
                printf("(identifier,%s)\n",tmp);
            ungetc(ch,stdin);
        }

        //是否是运算符
        else
        {
            char c=ch;
            // if(ch=='='||ch=='<'||ch=='>'||ch=='!')
            // {
            scanf("%c",&ch);
            if(ch=='=')     //判断是否是两个字节操作符
                printf("(operator,%c=)\n",ch);
            // }
            // else
            // {
            else        //单个字节操作符
            {
                printf("(operator,%c)\n",c);
                ungetc(ch,stdin);
            }
        }

    }
}
int main()
{
    get();
}


  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值