日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

C語言設計實現掃描器的自動機的示例詳解_C 語言

作者:heart_6662 ? 更新時間: 2023-02-04 編程語言

題目內容

內容:

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

思路

題目中的輸出形式是:(1)相應單詞的Token序列;(2)標識符表,常數表。因此,我們可以在上述代碼中調整輸出的格式。

對于 Token 序列的輸出,我們可以將其格式化為如下形式:

Token序列:(<單詞種類>, <單詞內容>)

例如:(KEYWORD, int)(IDENTIFIER, x)(DELIMITER, =)(INTEGER, 10)

對于標識符表和常整數表的輸出,我們可以將其格式化為如下形式:

標識符表: x y

常整數表: 10 20

為了實現這種輸出格式,我們可以在輸出 Token 序列之后,再輸出標識符表和常整數表。

代碼

#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;
}

原文鏈接:https://juejin.cn/post/7181998174955896869

欄目分類
最近更新