⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 shiyan1.cpp

📁 这是一个编译器的课程实验里面包含了
💻 CPP
字号:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define MAX_SIZE 10000

// 关键字列表 
char key[10][20] = {"if", "int", "for", "while", "do", "return", "break", "continue"};
// 缓冲区,用来保存文件中的代码 
char code[MAX_SIZE];
// 读到的当前字符的位置 
int curpos;
// 缓冲区大小 
int codelength;

// 读取文件 
void readFile()
{
    char str[100];
    char buf[100];
    printf("please input the file name you want to load:\n");
    scanf("%s", str);
    FILE *fp = NULL;
    while(1)
    {
        fp = fopen(str, "r");
        if(fp == NULL)
        {
            printf("open file failed!\n");
            printf("please input the file name you want to load:\n");
        }
        else
        {
            break;
        }
        scanf("%s", str);
    }
    code[0] = '\0';
    while(fscanf(fp, "%s", buf) != EOF)
    {
        strcat(code, " ");
        strcat(code, buf);
    }
    codelength = strlen(code);
}
 
int isDigit(char ch)
{
    if(ch >= '0' && ch <= '9')
    {
        return 1;
    }
    return 0;
}

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

int isReserve(char *str)
{
    int i;
    for(i = 0; i < 8; i++)
    {
        if(!strcmp(str, key[i]))
        {
            return 1;
        }
    }
    return 0;
}

int isOpr(char ch)
{
    //+、-、*、/、=、>、<、>=、<=、!=
    if(ch == '+' || ch == '-' || ch == '*' || ch == '/'
    || ch == '=' || ch == '<' || ch == '>' || ch == '!')
    {
        return 1;
    }
    return 0;
}

int isTokenizier(char ch)
{
    if(ch == ',' || ch == ';' || ch == '('
    || ch == ')' || ch == '{' || ch == '}'
    || ch == '[' || ch == ']')
    {
        return 1;
    }
    return 0;
}

int toDigit(char ch)
{
    if(isDigit(ch))
    {
        return ch - '0';
    }
    return -1;
}

void outputNumber(int number)
{
    printf("(3, %c%d%c)\n", '"', number, '"');
}

void outputIdentifier(char *str)
{
    printf("(2, %c%s%c)\n", '"', str, '"');
}

void outputReserve(char *str)
{
    printf("(1, %c%s%c)\n", '"', str, '"');
}

void outputSingleOpr(char ch)
{
    printf("(4, %c%c%c)\n", '"', ch, '"');
}

void outputDoubleOpr(char ch1, char ch2)
{
    printf("(4, %c%c%c%c)\n", '"', ch1, ch2, '"');
}

void outputTokenizier(char ch)
{
    printf("(5, %c%c%c)\n", '"', ch, '"');
}

void outputError()
{
    printf("error\n");
}

void readDigit(char ch)
{
    int num = toDigit(ch);
    while(curpos < codelength && isDigit(code[curpos]))
    {
        num = num * 10 + toDigit(code[curpos]);
        curpos++;
    }
    outputNumber(num);
}

void readIdentifier(char ch)
{
    char id[100];
    int len = 0;
    id[len] = ch;
    while(curpos < codelength
    && (isAlpha(code[curpos]) || isDigit(code[curpos]) || code[curpos] == '_'))
    {
        id[++len] = code[curpos++];
    }
    id[len + 1] = '\0';
    if(isReserve(id))
    {
        outputReserve(id);
    }
    else
    {
        outputIdentifier(id);
    }
}

void readOpr(char ch)
{
    if(ch == '+' || ch == '-' || ch == '*' || ch == '/' || ch == '=')
    {
        outputSingleOpr(ch);
    }
    else
    {
        if(ch == '<' || ch == '>')
        {
            if(curpos < codelength && code[curpos] == '=')
            {
                outputDoubleOpr(ch, '=');
                curpos++;
            }
            else
            {
                outputSingleOpr(ch);
            }
        }
        else
        {
            if(curpos < codelength && code[curpos] == '=')
            {
                outputDoubleOpr(ch, '=');
                curpos++;
            }
            else
            {
                outputError();
            }
        }
    }
}

void readTokenizier(char ch)
{
    outputTokenizier(ch);
}

void solve()
{
    char curCode;
    curpos = 0;
    while(curpos < codelength)
    {
        // 读取当前字符,并将当前位置前移一个单位 
        curCode = code[curpos++];
        if(curCode == ' ')
        {
            continue;
        }
        else if(isDigit(curCode))
        {
            readDigit(curCode);
        }
        else if(isAlpha(curCode))
        {
            readIdentifier(curCode);
        }
        else if(isOpr(curCode))
        {
            readOpr(curCode);
        }
        else if(isTokenizier(curCode))
        {
            readTokenizier(curCode);
        }
        else
        {
            outputError();
        }
    }
}

void fileView()
{
    int i = 1;
    while(i < codelength)
    {
        putchar(code[i]);
        i++;
    }
    putchar('\n');
}

int main()
{
    readFile();
    fileView();
    solve();
    system("pause");
    return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -