JMU软件工程编译原理实验一

实验一 手工编程实现词法分析器

1.实验目的: 熟悉词法分析阶段的要求,掌握利用状态图手工实现词法分析器的方法。

2.实验设备: 硬件:PC 机一台 软件:Windows系统,高级语言集成开发环境

3.实验内容: 根据词法要求手工实现词法分析器

4.实验要求及步骤

  1. 理解以下词法表述和状态图表示。

  1. 参考教科书3.2.4章节编写代码实现词法分析器。实现语言不限,推荐使用C语言,不可用脚本类语言;不允许使用任何语言的正规式控件实现实验要求。
  2. 提交词法分析器代码与实验报告。实验报告为word形式文档,内容是10对如下形式的输入输出。

输入语句任意自设,可包含错误单词($ERR类);若含错误单词,则词法分析器运行需报告错误,错误信息含第一个错误单词位置。

代码如下:

#include "stdlib.h"
#include "ctype.h"
#include "string.h"
#include "stdio.h"
#define _MYTEX
#define STRSIZE 1024

void init();        
void getChar();    
void getBC();     
void concat();      
int reserve();      
void retract();    
void insertID();   
void insertConst(); 
void over();        
int run();          


char ch;            
char *strToken;     
int strTokenLen;   
char *str;         
int strIndex;      
int strSize;       
char resTable[5][5] = {"DIM\0", "IF\0", "DO\0", "STOP\0", "END\0"}; 


void init() {
    ch = '\0';
    str = (char *) malloc(STRSIZE * sizeof(char));
    str[0] = '\0';
    strIndex = 0;
    strToken = (char *) malloc(STRSIZE * sizeof(char));
    strToken[0] = '\0';
    strTokenLen = 0;
} 

void getChar() {
    ch = str[strIndex++];
} 

void getBC() {
    while (isblank(ch)) {
        getChar();
    }
} 

void concat() {
    strToken[strTokenLen++] = ch;
    strToken[strTokenLen] = '\0';
} 

int reserve() {
    int i = 0;
    for (i = 0; i < 5; i++) {
        if (strcmp(strToken, resTable[i]) == 0) {
            printf("$%s\n", resTable[i]);
            return i;
        }
    }
    return -1;
} 

void retract() {
    strIndex--;
    ch = '\0';
} 

void insertID() {
    printf("$ID\t\t %-20s\n", strToken);
}

void insertConst() {
    printf("$INT\t %-20s\n", strToken);
}

void over() {
    free(strToken);
    strToken = (char *) malloc(STRSIZE * sizeof(char));
    strTokenLen = 0;
}

int run() {
    init();
    printf("\nPlease input code: ");
    fflush(stdout);
    scanf("%[^\n]", str);
    scanf("%c", &ch);
    strSize = strlen(str);
    printf("编码\t值\n---------------\n");
    int status = 1;
    while (status) {
        getChar();
        getBC();
        if (isalpha(ch)) {
            while (isalnum(ch)) {
                concat();
                getChar();
            }
            retract();
            int resCode = reserve();
            if (resCode == -1) {
                insertID();
            }
        } else if (isdigit(ch)) {
            while (isdigit(ch)) {
                concat();
                getChar();
            }
            retract();
            insertConst();
        } else if (ch == '=') {
            printf("$ASSIGN\n");
        } else if (ch == '+') {
            printf("$PLUS\n");
        } else if (ch == '*') {
            getChar();
            if (ch == '*') {
                printf("$POWER\n");
            } else {
                retract();
                printf("$STAR\n");
            }
        } else if (ch == ',') {
            printf("$COMMA\n");
        } else if (ch == '(') {
            printf("$LPAR\n");
        } else if (ch == ')') {
            printf("$RPAR\n");
        } else {
            if (strIndex > strSize) {
                status = 0;
            } else if (!isblank(ch)) {
                printf("$ERR\t %-20c\n", ch);
                status = 0;
            }
        }
        over();
    }
    return strSize;
}

int main ()
{
    while (run()) {
    	
    }
    return 0;
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

hiOoo.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值