博客
关于我
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/

    你可能感兴趣的文章
    org.apache.dubbo.common.serialize.SerializationException: com.alibaba.fastjson2.JSONException: not s
    查看>>
    sqlserver学习笔记(三)—— 为数据库添加新的用户
    查看>>
    org.apache.http.conn.HttpHostConnectException: Connection to refused
    查看>>
    org.apache.ibatis.binding.BindingException: Invalid bound statement错误一例
    查看>>
    org.apache.ibatis.exceptions.PersistenceException:
    查看>>
    org.apache.ibatis.exceptions.TooManyResultsException: Expected one result (or null) to be returned
    查看>>
    org.apache.ibatis.type.TypeException: Could not resolve type alias 'xxxx'异常
    查看>>
    org.apache.poi.hssf.util.Region
    查看>>
    org.apache.xmlbeans.XmlOptions.setEntityExpansionLimit(I)Lorg/apache/xmlbeans/XmlOptions;
    查看>>
    org.apache.zookeeper.KeeperException$ConnectionLossException: KeeperErrorCode = ConnectionLoss for /
    查看>>
    org.hibernate.HibernateException: Unable to get the default Bean Validation factory
    查看>>
    org.hibernate.ObjectNotFoundException: No row with the given identifier exists:
    查看>>
    org.springframework.boot:spring boot maven plugin丢失---SpringCloud Alibaba_若依微服务框架改造_--工作笔记012
    查看>>
    SQL-CLR 类型映射 (LINQ to SQL)
    查看>>
    org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
    查看>>
    org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
    查看>>
    org.springframework.web.multipart.MaxUploadSizeExceededException: Maximum upload size exceeded
    查看>>
    org.tinygroup.serviceprocessor-服务处理器
    查看>>
    org/eclipse/jetty/server/Connector : Unsupported major.minor version 52.0
    查看>>
    org/hibernate/validator/internal/engine
    查看>>