sdut 2018编译原理 1.小C语言--词法分析程序

本文介绍了一种小C语言的词法分析方法,详细解析了语法结构,并提供了示例输入输出,帮助理解如何识别关键字、标识符、整数、界符和运算符。

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

 

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

code:

#include <iostream>
#include <string>
using namespace std;

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

void check(string temp)
{
    if(temp[0]>='0'&&temp[0]<='9')
    {
        cout<<"("<<str[2]<<","<<temp<<")"<<endl;
    }
    else
    {
        int i;
        for(i = 0;i<6;i++)
        {
            if(temp == str2[i])
            {
                cout<<"("<<str[0]<<","<<temp<<")"<<endl;
                break;
            }
        }
        if(i>=6)
        {
            cout<<"("<<str[1]<<","<<temp<<")"<<endl;
        }
    }
}

int main()
{
    string s;
    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())
                {
                    check(temp);
                }
                temp = "";
                if(i+1<len&&s[i+1] == '=')
                {
                    cout<<"("<<str[4]<<","<<s[i]<<s[i+1]<<")"<<endl;
                    i++;
                }
                else
                {
                    cout<<"("<<str[4]<<","<<s[i]<<")"<<endl;
                }
            }
            else if(s[i] == '('||s[i] == ')'||s[i] == '{'||s[i] == '}'||s[i] == ','||s[i] == ';')
            {
                if(temp.length())
                {
                    check(temp);
                }
                temp = "";
                cout<<"("<<str[3]<<","<<s[i]<<")"<<endl;
            }
            else
            {
                temp+=s[i];
            }
        }
        if(temp.length())
        {
            check(temp);
        }
    }
    return 0;
}

 

算符优先分析是编译原理中一种重要的语法分析方法,用于处理算符优先级的语法规则。以下是解决sdut编译原理oj中F - 算符优先分析题目的一般解题思路及代码示例。 ### 解题思路 1. **算符优先关系表的构建**:首先需要确定算符之间的优先关系,包括等于关系(=)、小于关系(<)和大于关系(>)。这通常通过分析文法的产生式来确定。 2. **Firstvt和Lastvt集的计算**:Firstvt集是指一个非终结符能推导出的最左算符的集合,Lastvt集是指一个非终结符能推导出的最右算符的集合。计算这两个集合有助于确定算符之间的优先关系。 3. **算符优先分析过程**:利用算符优先关系表对输入的符号串进行分析,判断其是否符合给定的文法规则。 ### 代码示例 以下是一个简单的算符优先分析的代码示例: ```python # 定义算符优先关系表 precedence_table = { ('+', '+'): '>', ('+', '-'): '>', ('+', '*'): '<', ('+', '/'): '<', ('+', '#'): '>', ('-', '+'): '>', ('-', '-'): '>', ('-', '*'): '<', ('-', '/'): '<', ('-', '#'): '>', ('*', '+'): '>', ('*', '-'): '>', ('*', '*'): '>', ('*', '/'): '>', ('*', '#'): '>', ('/', '+'): '>', ('/', '-'): '>', ('/', '*'): '>', ('/', '/'): '>', ('/', '#'): '>', ('#', '+'): '<', ('#', '-'): '<', ('#', '*'): '<', ('#', '/'): '<', ('#', '#'): '=', } # 算符优先分析函数 def operator_precedence_analysis(input_string): stack = ['#'] input_string += '#' index = 0 while True: top = stack[-1] current = input_string[index] if top == '#' and current == '#': print("分析成功") break if precedence_table[(top, current)] == '<': stack.append(current) index += 1 elif precedence_table[(top, current)] == '>': stack.pop() elif precedence_table[(top, current)] == '=': stack.pop() index += 1 else: print("分析失败") break # 测试 input_string = "3+4*2" operator_precedence_analysis(input_string) ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值