实验一

题目:词法分析
目的:通过该实验使学生掌握描述词法的文法,同时能够进行词法分析。
要求:对输入的字符串进行词法分析,形成词法分析产生的符号表。
内容:给定描述词法的文法为:

  • 文法一:

    • 〈无符号整型数值型数据〉→〈数字〉〈数字〉*
    • 〈数字〉→0|1|2|3|4|5|6|7|8|9
  • 文法二:〈算符〉→+|-|*|/
    利用以上二个文法对从键盘输入的字符串进行词法分析,对符合这两个文法的字符串建立单词符号表;对不符合这两个文法的字符串给出错误信息。编程序实现该功能,并上机调试。

使用的数据结构:

  • 1.一个字符数组s,用来接受从键盘输入的字符串。
  • 2.定义一个结构体,利用该结构体定义一个数组stable,用来存放字符串中字符的属性(类型class和值value)。
  • 3.定义一个整型数组ei,大小与字符数组相同,记录对应字符是否是错误字符。

算法描述:
exp1

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

#define LEN 100
#define DIGIT 0
#define OPER 1
#define ERROR -1

struct node
{
    char charType;
    int val;
};

//判断字符类型
int judgeType(char c)
{
    if(c >= '0' && c <= '9')
        return DIGIT;
    else if (c == '+' || c == '-' || c == '*' || c == '/')
        return OPER;
    else
        return ERROR;
}

void errorSTR(char in[], int ei[], int index)
{
    while(in[index] != '\0')
    {
        if(judgeType(in[index]) == ERROR)
            ei[index] = 1;
        ++index;
    }

    //输出出错字符位置
    int i;
    int cnt = 0;
    for(i = 0; i < index; ++i)
    {
        if(ei[i] == 1)
        {
            printf("The %dth character is error!\n", i + 1);
            ++cnt;
        }
    }
    printf("%d error(s)!\n", cnt);
}

int main()
{
    char in[LEN];
    int ei[LEN];
    int i = 0;
    //左侧字符为数字标识
    bool isFrontDigit = false;
    bool isError = false;
    int stableIndex = 0;
    //ei表置0
    memset(ei, 0, sizeof(ei));
    struct node stable[LEN];
    gets(in);
    while (in[i] != '\0')
    {
        //忽略所有空格
        if(in[i] == ' ')
        {
            ++i;
            continue;
        }
        else
        {
            //出错字符
            if(judgeType(in[i]) == ERROR)
            {
                errorSTR(in, ei, i);
                isError = true;
                break;
            }
            //算符
            else if(judgeType(in[i]) == OPER)
            {
                stable[stableIndex].charType = in[i];
                ++stableIndex;
                isFrontDigit = false;
            }
            //数字
            else if(judgeType(in[i]) == DIGIT)
            {
                if(isFrontDigit)
                {
                    --stableIndex;
                    stable[stableIndex].val = stable[stableIndex].val * 10 + in[i] - '0';
                    ++stableIndex;
                }
                else
                {
                    stable[stableIndex].charType = 'n';
                    stable[stableIndex].val = in[i] - '0';
                    ++stableIndex;
                    isFrontDigit = true;
                }
            }
        }
        ++i;
    }
    if(!isError)
    {
        printf("0 error(s)!\n\n");
        printf("Print stable:\n\n");
        //打印stable
        for(i = 0; i < stableIndex; ++i)
        {
            if(stable[i].charType == 'n')
                printf("|  n  |  %d\t|\n", stable[i].val);
            else
                printf("|  o  |  %c\t|\n", stable[i].charType);
        }
    }
    return 0;
}



扫一扫在手机打开当前页