• 大小: 856KB
    文件类型: .zip
    金币: 1
    下载: 0 次
    发布日期: 2021-05-05
  • 语言: 其他
  • 标签: 词法分析  

资源简介

相关博客请访问https://blog.csdn.net/boss_crabe/article/details/80760252

资源截图

代码片段和文件信息

#include 
#include 
#include 
#include 
#include 
using namespace std;

const int MAX_SIZE = 1000000;

set reserved_words;

void init_reserved_words() {
  reserved_words.insert(“Const“);
  reserved_words.insert(“Var“);
  reserved_words.insert(“if“);
  reserved_words.insert(“then“);
  reserved_words.insert(“else“);
  reserved_words.insert(“while“);
  reserved_words.insert(“do“);
  reserved_words.insert(“begin“);
  reserved_words.insert(“end“);
}

bool isdigit(char c) { return c >= ‘0‘ && c <= ‘9‘; }

bool isalpha(char c) { return (c >= ‘a‘ && c <= ‘z‘) || (c >= ‘A‘ && c <= ‘Z‘); }

void lexical_analysis(int flag) {
  freopen(“file_out.txt“ “w“ stdout);
  char s[MAX_SIZE];
  char tmp[105];
  int len tmp_len pos cnt = 0;
  while(gets(s)) {
    len = strlen(s);
    cnt++;
    pos = 0;
    while (pos < len && s[pos] == ‘ ‘) pos++;
    if (flag) printf(“%3d:“ cnt);
    if (flag) {
      if (pos == len)
        puts(““);
      else
        printf(“ %s\n“ s + pos);
    }
    while (pos < len) {
      while (pos < len && s[pos] == ‘ ‘) pos++;
      if (pos == len) break;
      tmp_len = 0;
      if (flag) printf(“    %3d: “ cnt);
      if (isdigit(s[pos])) {
        while (pos < len && isdigit(s[pos]))
          tmp[tmp_len++] = s[pos++];

        tmp[tmp_len] = ‘\0‘;
        if (flag) printf(“Number : %s\n“ tmp);
        else printf(“%d %s\n“ cnt tmp);
      }
      else if (isalpha(s[pos])) {
        while (pos < len && (isalpha(s[pos]) || isdigit(s[pos])))
          tmp[tmp_len++] = s[pos++];
        tmp[tmp_len] = ‘\0‘;
        if (flag) {
          if (reserved_words.count(tmp))
            printf(“Reserved words : %s\n“ tmp);
          else
            printf(“Identifier : %s\n“ tmp);
        }
        else {
          if (reserved_words.count(tmp))
            printf(“%d %s\n“ cnt tmp);
          else
            printf(“%d %s\n“ cnt tmp);
        }
      }
      else {
        while(pos < len && !(isdigit(s[pos]) || isalpha(s[pos]) || s[pos] == ‘ ‘))
          tmp[tmp_len++] = s[pos++];

        tmp[tmp_len] = ‘\0‘;
        if (flag)
          printf(“%s\n“ tmp);
        else
          printf(“%d %s\n“ cnt tmp);
      }
    }
  }
}

int main() {
  freopen(“file_in.txt“ “r“ stdin);
  init_reserved_words();
  lexical_analysis(0);
  return 0;
}

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2018-06-21 14:28  词法分析\
     文件          56  2017-12-14 20:23  词法分析\file_in.txt
     文件         152  2018-01-09 14:57  词法分析\file_out.txt
     文件         648  2018-01-09 14:56  词法分析\file_show.txt
     文件        2448  2017-12-15 13:05  词法分析\词法分析.cpp
     文件     1899811  2018-01-09 14:57  词法分析\词法分析.exe
     文件       14519  2018-06-21 14:28  词法分析\词法分析_谢俊峰.docx
     文件        2975  2018-01-09 14:56  词法分析\词法分析展示.cpp
     文件     1899811  2018-01-09 14:56  词法分析\词法分析展示.exe

评论

共有 条评论