aiaiask.com©

开花即结果 提问即答案

设计、编制、调试一个具体的词法分析程序,代码
原始问题:

设计、编制、调试一个具体的词法分析程序,代码

如下#include <stdio.h>

include <string.h>

//定义单词类型常量

define IDENTIFIER 0 //标识符

define NUMBER 1 //数字

define OPERATOR 2 //操作符

//定义操作符常量,以及其对应的字符串值 (可自行扩充)

define PLUS "+" //加法运算 "+" "-" "*" "/" "%" "=" "<>" "<=" "<" ">=" ">" "( )";

char token[50];//保存当前分析出来的单词

int isDigit(char c){//判断是否是数字

    if (c >= '0' && c <= '9'){  

        return 1;  

    }else{  

        return 0;  

    }      

} 

int isLetter(char c){//判断是否是英文字母或者下划线_开头的标识符中的一部分。

    if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c == '_')){               return 1;           } else {               return 0;           }       }  void getToken(){//从输入流中得到一个token,并将token保存在token[]中。         char ch = getchar();         int i = 0;         while ((ch == '  

') || (ch == ' ') || (ch == 32)) { ch = getchar(); } if (isDigit(ch)) { while ((isDigit(ch) || isLetter(ch))) { token[i++] = ch; ch = getchar(); } token[i] = 0; ungetc(ch, stdin); printf("%s number
", token); } else if (isLetter(ch)) { while ((isDigit(ch) || isLetter(ch))) { token[i++] = ch; ch = getchar(); } token[i] = 0; ungetc(ch, stdin); printf("%s identifier
", token); } else if (strcmp(&PLUS, &token)==0) { printf("+ operator
"); }} void main() { while (!feof()) { getToken(); }}

设计、编制、调试一个具体的词法分析程序,代码
https://www.aiaiask.com/ask/7091.html
AI / 模型DeepSeek
时间 / DATEApril 7, 2023, 9:49 AM
语言zh-CN
IP / 区域河北 石家庄