词法分析器(编译原理)

本分析器能识别的单词符号及编码如下表(可改变):

#include <stdio.h>
#include <string.h>
#include <ctype.h>
int getSymbolCode(char *symbol) {
    if (strcmp(symbol, "begin") == 0) {
        return 1;
    } else if (strcmp(symbol, "if") == 0) {
        return 2;
    } else if (strcmp(symbol, "then") == 0) {
        return 3;
    } else if (strcmp(symbol, "while") == 0) {
        return 4;
    } else if (strcmp(symbol, "do") == 0) {
        return 5;
    } else if (strcmp(symbol, "end") == 0) {
        return 6;
    } else if (isalpha(symbol[0])) {
        return 10;
    } else if (isdigit(symbol[0])) {
        return 11;
    } else if (symbol[0] == '*') {
        return 13;
    } else if (symbol[0] == '+') {
        return 15;
    } else if (symbol[0] == '-') {
        return 16;
    } else if (symbol[0] == ':') {
        if (symbol[1] == '=') {
            return 18;
        } else {
            return 17;
        }
    } else if (strcmp(symbol, ">") == 0) {
        return 20;
    } else if (strcmp(symbol, "<>") == 0) {
        return 21;
    } else if (strcmp(symbol, "<=") == 0) {
        return 22;
    } else if (strcmp(symbol, ">=") == 0) {
        return 24;
    } else if (strcmp(symbol, "=") == 0) {
        return 25;
    } else if (symbol[0] == ';') {
        return 26;
    } else if (symbol[0] == '(') {
        return 27;
    } else if (symbol[0] == ')') {
        return 28;
    } else {
        return 0; // 非法字符
    }
}
void lexicalAnalyzer(char *input) {
    int start = 0;
    int end = 0;
    int length = strlen(input);
    while (end < length) {
        // 找到符号的起始位置
        while (input[start] == ' ' && start < length) {
            start++;
        }        
        // 找到符号的结束位置
        end = start;
        while (input[end] != ' ' && end < length) {
            end++;
        }
        // 提取符号
        char symbol[100];
        strncpy(symbol, input + start, end - start);
        symbol[end - start] = '\0';     
        // 获取符号的种别码并输出结果
        int symbolCode = getSymbolCode(symbol);
        printf("%s\t种别码:%d\n", symbol, symbolCode);
        start = end;
    }
}
int main() {
    char input[100];
    printf("请输入字符串:");
    fgets(input, sizeof(input), stdin);
    input[strcspn(input, "\n")] = '\0'; // 去除换行符   
    lexicalAnalyzer(input);    
    return 0;
}

代码截图:

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值