「已注销」 2022-12-27 20:59 采纳率: 100%
浏览 95
已结题

求下面问题的C语言代码

【题目内容】
内容:
1.设计扫描器的自动机;
2.设计翻译、生成Token的算法;
3.编写代码并上机调试运行通过。
要求:
扫描器可识别的单词包括:关键字、界符、标识符和常整型数。
其中关键字表、界符表、标识符表、常整数表如下:
关键字表K(1int 2void 3break 4float 5while 6do 7struct 8coust 9case 10for 11return 12if 13default 14else)
界符表 P(1 - 2 / 3 ( 4 ) 5 -- 6<= 7< 8+ 9* 10> 11= 12, 13; 14++ 15 { 16 } 17 ' 18 " )
标识符表I (1 2 3 4 5 6 7 8 9 10 11 12 13 14)
常整数表C(1 2 3 4 5 6 7 8 9 10 11 12 13 14)
【输入形式】
源程序文件。
【输出形式】
(1)相应单词的Token序列;
(2)标识符表,常数表。
【测试用例1】
输入:x10=x+y1*120+10;
输出:
Token序列:(I 1)(P 11)等等
标识符表:x10 x y1
常数表:120 10

  • 写回答

2条回答 默认 最新

  • heart_6662 2022-12-27 23:06
    关注

    你这个代码要写很长啊(((φ(◎ロ◎;)φ))) ,望采纳!!!点击该回答右侧的“采纳”按钮即可采纳!!!

    #include <stdio.h>
    #include <string.h>
    #include <ctype.h>
    
    // 定义单词种类
    typedef enum {
      KEYWORD,   // 关键字
      DELIMITER, // 界符
      IDENTIFIER,// 标识符
      INTEGER    // 常整型数
    } TokenType;
    
    // 关键字表
    char *keywords[] = {
      "int", "void", "break", "float", "while", "do",
      "struct", "const", "case", "for", "return", "if",
      "default", "else"
    };
    
    // 界符表
    char delimiters[] = {
      '-', '/', '(', ')', '--', '<=', '<', '+', '*', '>',
      '=', ',', ';', '++', '{', '}', '\'', '"'
    };
    
    // 标识符表
    char *identifiers[100];
    int identifier_count = 0;
    
    // 常整数表
    int integers[100];
    int integer_count = 0;
    
    // Token 序列
    struct Token {
      TokenType type; // 单词种类
      char *lexeme;   // 单词内容
      int value;      // 单词值
    } tokens[100];
    int token_count = 0;
    
    // 读入的源程序
    char source[100];
    int source_pos = 0;
    
    // 读入下一个字符
    char get_char() {
      return source[source_pos++];
    }
    
    // 跳过空白符
    void skip_space() {
      while (isspace(source[source_pos])) source_pos++;
    }
    
    // 读入单词
    void get_token() {
      skip_space();
    
      char ch = get_char();
    
      // 如果是字母,则读入单词
      if (isalpha(ch)) {
        int lexeme_pos = 0;
        char lexeme[100];
        while (isalpha(ch) || isdigit(ch)) {
          lexeme[lexeme_pos++] = ch;
          ch = get_char();
        }
        lexeme[lexeme_pos] = '\0';
        source_pos--; // 将最后读入的非字母或数字字符放回去
    
        // 判断是否为关键字
    for (int i = 0; i < 14; i++) {
    if (strcmp(keywords[i], lexeme) == 0) {
    tokens[token_count].type = KEYWORD;
    tokens[token_count].lexeme = lexeme;
    token_count++;
    return;
    }
    }
    
    
    // 如果不是关键字,则加入标识符表
    identifiers[identifier_count] = lexeme;
    tokens[token_count].type = IDENTIFIER;
    tokens[token_count].lexeme = lexeme;
    tokens[token_count].value = identifier_count;
    identifier_count++;
    token_count++;
    return;
    }
    
    // 如果是数字,则读入常整数
    if (isdigit(ch)) {
    int value = 0;
    while (isdigit(ch)) {
    value = value * 10 + (ch - '0');
    ch = get_char();
    }
    source_pos--; // 将最后读入的非数字字符放回去
    
    
    // 加入常整数表
    integers[integer_count] = value;
    tokens[token_count].type = INTEGER;
    tokens[token_count].value = value;
    integer_count++;
    token_count++;
    return;
    }
    
    // 如果是界符,则读入界符
    for (int i = 0; i < 18; i++) {
    if (delimiters[i] == ch) { 
    // 加入 Token 序列
    tokens[token_count].type = DELIMITER;
    tokens[token_count].lexeme = ch;
    token_count++;
    return;
    }
    }
    }
    
    int main() {
    // 读入源程序
    scanf("%s", source);
    
    // 读入 Token 序列
    while (source_pos < strlen(source)) {
    get_token();
    }
    
    // 输出 Token 序列
    for (int i = 0; i < token_count; i++) {
    if (tokens[i].type == KEYWORD) {
    printf("(KEYWORD, %s)\n", tokens[i].lexeme);
    } else if (tokens[i].type == IDENTIFIER) {
    printf("(IDENTIFIER, %s)\n", tokens[i].lexeme);
    } else if (tokens[i].type == INTEGER) {
    printf("(INTEGER, %d)\n", tokens[i].value);
    } else if (tokens[i].type == DELIMITER) {
    printf("(DELIMITER, %c)\n", tokens[i].lexeme);
    }
    }
    
    // 输出标识符表
    printf("\nIdentifier Table:\n");
    for (int i = 0; i < identifier_count; i++) {
    printf("%s\n", identifiers[i]);
    }
    
    // 输出常整数表
    printf("\nInteger Table:\n");
    for (int i = 0; i < integer_count; i++) {
    printf("%d\n", integers[i]);
    }
    
    return 0;
    }
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 1月5日
  • 已采纳回答 12月28日
  • 创建了问题 12月27日

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效