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

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

Description

小C语言文法 
1. <程序>→<main关键字>(){<声明序列><语句序列>}
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

每行单词数不超过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;
    }
}

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,})

注意下面还有一个版本??

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
    char str[85], s[85];
    int i, j, len;
    memset(str,0,sizeof(str));
    while(gets(str) != NULL)
    {
    len = strlen(str);
    j = 0;
    memset(s, 0, sizeof(s));
    for(i = 0; i < len; i++)
    {
        if((str[i] >= 'A' && str[i] <= 'Z') || (str[i] >= 'a' && str[i] <= 'z'))
        {
            for(; i < len; i++)
            {
                if((str[i] >= 'A' && str[i] <= 'Z') || (str[i] >= 'a' && str[i] <= 'z') || (str[i] >= '0' && str[i] <= '9'))
                {
                    s[j++] = str[i];
                }
                else
                {
                    break;
                }
            }
            s[j] = '\0';
            if(strcmp(s,"main") == 0 || strcmp(s,"if") == 0 || strcmp(s,"else") == 0 || strcmp(s,"for") == 0 || strcmp(s,"while") == 0 || strcmp(s,"int") == 0)
            {
                printf("(keyword,%s)\n",s);
            }
            else
            {
                printf("(identifier,%s)\n",s);
            }
            memset(s,0,sizeof(s));
            j = 0;
            i = i - 1;
        }
        else if(str[i] == '(' || str[i] == ')' || str[i] == '{' || str[i] == '}' || str[i] == ',' || str[i] == ';')
        {
            printf("(boundary,%c)\n",str[i]);
        }
        else if(str[i] >= '0' && str[i] <= '9')
        {
                for(; i < len; i++)
                {
                    if(str[i] >= '0' && str[i] <= '9')
                    {
                        s[j++] = str[i];
                    }
                    else
                    {
                        break;
                    }
                }
                s[j] = '\0';
                printf("(integer,%s)\n",s);
                memset(s,0,sizeof(s));
                j = 0;
                i = i - 1;
        }
        else if(str[i] == '=' || str[i] == '+' || str[i] == '-' || str[i] == '*' || str[i] == '/' || str[i] == '<' || str[i] == '>' || str[i] == '!')
            {
                if(i == len - 1)
                {
                    printf("(operator,%c)\n",str[i]);
                }
                else
                {
                    if(str[i + 1] == '=')
                    {
                        printf("(operator,%c%c)\n",str[i],str[i + 1]);
                        i += 1;
                    }
                    else
                    {
                        printf("(operator,%c)\n",str[i]);
                    }
                }
            }
    }
    memset(str,0,sizeof(str));
    }
    return 0;
}

注释 ?! 

#include <bits/stdc++.h>

using namespace std;

char str[85]; // 这是读入的字符串
char s[85]; // 判断的临时字符串
int main()
{
    int len, i, j; // len 是读入的每一行的长度, i 遍历读入的 str 的下标,j 是需要判断的字符串的当前长度
    while(cin.getline(str,85)) // 读入,包含有空格的情况
    {
        len = strlen(str);
        for(i = 0; i < len; i ++)
        {
            j = 0; // 每次都是新的字符串,要将当前串长度变成零
            memset(s,0,sizeof(s)); // 初始化
            if((str[i] >= 'A' && str[i] <= 'Z') || (str[i] >= 'a' && str[i] <= 'z')) // 先判断是否是字母,即判断是关键字还是自定义标识符
            {
                for(; i < len;  i ++) // 字母开头,里面包含数字则是自定义标识符
                {
                    if((str[i] >= 'A' && str[i] <= 'Z') || (str[i] >= 'a' && str[i] <= 'z') || (str[i] >= '0' && str[i] <='9'))
                    {
                        s[j ++] = str[i];
                    }
                    else break;
                }
                s[j] = '\0';  // 字符串的终止
                if(strcmp(s,"int") == 0 || strcmp(s,"main") == 0 || strcmp(s,"for") == 0 || strcmp(s,"if") == 0 || strcmp(s,"else") == 0
                        || strcmp(s,"while") == 0)
                {
                    printf("(keyword,%s)\n",s);
                }
                else
                {
                    printf("(identifier,%s)\n",s);

                }
                i --; // 如果当前的是下一个分解符,则需要对其进行判断
                // 比如 int main() 在读入到 (  时跳出循环,如果不进行 i -- 操作, 则下次直接进行判断 ) ,就少判断了一个边界符
            }
            else if(str[i] == '(' || str[i] == ')' || str[i] == '}' || str[i] == '{' || str[i] == ',' || str[i] == ';')
            {
                printf("(boundary,%c)\n",str[i]); // 边界符是一个字符,只需要判读即可
            }
            else if(str[i] >= '0' && str[i] <='9') // 判断数字,和判断关键字一样
            {
                for(; i < len; i ++)
                {
                    if(str[i] >= '0' && str[i] <= '9')
                    {
                        s[j++] =str[i];
                    }
                    else break;
                }
                s[j] = '\0';
                printf("(integer,%s)\n",s);
                i -- ; // 进行必要的回退
            }
            else if(str[i] == '=' || str[i] == '+' || str[i] == '-' || str[i] == '*' || str[i] == '/'
                    || str[i] == '>' || str[i] == '<' || str[i] == '!')
            {
                if(i == len - 1) // 因为操作符需要判断有 == 的情况,所以先判断是否到结尾,然后再进行 == 判断
                {
                    printf("(operator,%c)\n",str[i]);
                }
                else
                {
                    if(str[i + 1] == '=')
                    {
                        printf("(operator,%c%c)\n",str[i],str[i+1]);
                        i += 1; // 跳过当前的 =
                    }
                    else  printf("(operator,%c)\n",str[i]);
                }
            }
        }
    }
    return 0;
}

也有可能AC不了?

#include <bits/stdc++.h>

using namespace std;

string S[5] = {"keyword", "identifier", "integer", "boundary", "operator"};
string T[6] = {"main", "if", "else", "for", "while", "int"};

string s;
void panduan(string s)
{
    if(s[0]>='0'&&s[0]<='9')
    {
        cout<<"("<<S[2]<<","<<s<<")"<<endl;
    }
    else
    {
        int f=1;
        for(int i=0;i<6;i++)
        {
            if(s == T[i])
            {
                f = 0;
                cout<<"("<<S[0]<<","<<s<<")"<<endl;
                break;
            }
        }
        if(f==1)
        {
             cout<<"("<<S[1]<<","<<s<<")"<<endl;
        }
    }
}
int main()
{
    while(cin>>s)
    {
        int len = s.length();
        string temp = "";
        for(int i=0;i<len;i++)
        {
           if(s[i]=='=' || s[i]=='!' || s[i]=='+' || s[i]=='-' || s[i]=='*' || s[i]=='/' || s[i]=='>' || s[i]=='<')
           {
               if(temp.length())
               {
                   panduan(temp);
               }
               temp = "";
               if(i+1<len && s[i+1]=='=')
               {
                   cout<<"("<<S[4]<<","<<s[i]<<s[i+1]<<")"<<endl;
                   i++;
               }
               else
               {
                   cout<<"("<<S[4]<<","<<s[i]<<")"<<endl;
               }
           }
           else if(s[i]==';' || s[i]==',' || s[i]=='(' || s[i]==')' || s[i]=='{' || s[i] == '}')
            {
                if(temp.length())
               {
                   panduan(temp);
               }
               temp = "";
               cout<<"("<<S[3]<<","<<s[i]<<")"<<endl;
            }
            else
            {
                temp += s[i];
            }

        }
         if(temp.length())
         {
                   panduan(temp);
         }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/Mercury_Lc/article/details/108685127