博客
关于我
c之词法分析程序
阅读量:768 次
发布时间:2019-03-24

本文共 589 字,大约阅读时间需要 1 分钟。

一、实现目的本部分主要实现两个功能:首先,从源程序文件中读取有效字符并生成内部表示文件;其次,完成词法分析的实现。

二、实现环境开发工具:Visual Studio 2013关键头文件:#include <stdio.h>#include <string.h>#include <windows.h>#define PRINT(NUM, NAME) printf("%d,"%s"\n", NUM, NAME)

代码解释:

  • 文件打开与读取
    • 以只读模式打开指定文件
    • 使用fscanf读取一行内容,采用空格终止读取方式
  • 字符处理与标识符生成
    • 对特殊字符(如逗号、分号等)单独处理
    • 对运算符(如+、-、*等)与赋值符号(如=)进行分类
    • 对字母字符进行标识符生成
    • 对字母字符进行断句判断
  • 最后关闭文件并暂停系统
  • 代码示例:

    int main() {    int a = 10;    int b = 20;    int c = 0;    while (1) {        if (a > b) {            break;        }        a += 1;        c += a;    }}

    结果展示:程序将执行了以下操作:

  • 文件打开并读取内容
  • 然后对字符进行分类处理
  • 最终输出结果
  • (以下展示为代码运行示意图,实际输出将根据程序执行结果而定)

    转载地址:http://mflkk.baihongyu.com/

    你可能感兴趣的文章
    notepad++最详情汇总
    查看>>
    notepad++正则表达式替换字符串详解
    查看>>
    notepad如何自动对齐_notepad++怎么自动排版
    查看>>
    Notes on Paul Irish's "Things I learned from the jQuery source" casts
    查看>>
    Notification 使用详解(很全
    查看>>
    NotImplementedError: Cannot copy out of meta tensor; no data! Please use torch.nn.Module.to_empty()
    查看>>
    NotImplementedError: Could not run torchvision::nms
    查看>>
    nova基于ubs机制扩展scheduler-filter
    查看>>
    Now trying to drop the old temporary tablespace, the session hangs.
    查看>>
    nowcoder—Beauty of Trees
    查看>>
    np.arange()和np.linspace()绘制logistic回归图像时得到不同的结果?
    查看>>
    np.power的使用
    查看>>
    NPM 2FA双重认证的设置方法
    查看>>
    npm build报错Cannot find module ‘webpack/lib/rules/BasicEffectRulePlugin‘解决方法
    查看>>
    npm build报错Cannot find module ‘webpack‘解决方法
    查看>>
    npm ERR! ERESOLVE could not resolve报错
    查看>>
    npm ERR! fatal: unable to connect to github.com:
    查看>>
    npm ERR! Unexpected end of JSON input while parsing near '...on":"0.10.3","direc to'
    查看>>
    npm ERR! Unexpected end of JSON input while parsing near ‘...“:“^1.2.0“,“vue-html-‘ npm ERR! A comp
    查看>>
    npm error Missing script: “server“npm errornpm error Did you mean this?npm error npm run serve
    查看>>