编译原理实验一——简单词法分析
来源:互联网 发布:java开源大全注册 编辑:程序博客网 时间:2024/05/21 21:38
[实验任务]
完成以下正则文法所描述的Pascal语言子集单词符号的词法分析程序。
<标识符>→字母︱ <标识符>字母︱ <标识符>数字
<无符号整数>→数字︱ <无符号整数>数字
<单字符分界符> →+ ︱- ︱* ︱; ︱(︱)
<双字符分界符>→<大于>=︱<小于>=︱<小于>>︱<冒号>=︱<斜竖>*
<小于>→<
<等于>→=
<大于>→>
<冒号> →:
<斜竖> →/
该语言的保留字 :begin end if then else for do while and or not 说明: 1 该语言大小写不敏感。
2 字母为a-z A-Z,数字为0-9。
3可以对上述文法进行扩充和改造。
4 ‘/*……*/’为程序的注释部分。
[设计要求]
1、给出各单词符号的类别编码。
2、词法分析程序应能发现输入串中的错误。
3、词法分析作为单独一遍编写,词法分析结果为二元式序列组成的中间文件。
4、设计两个测试用例(尽可能完备),并给出测试结果。
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include "demo.h"
char token[20];
int lookup(char *token) {
for (int i = 0; i < 11; i++) {
if (strcmp(token, KEY_WORDS[i]) == 0) {
return i+1;
}
}
return 0;
}
char getletter(FILE *fp) {
return tolower(fgetc(fp));
}
void out(FILE *fp, int c, char *value) {
fprintf(fp, "%d,%s/n", c, value);
}
void report_error(FILE *fp, char ch) {
fprintf(fp, "There must be some error./n");
fprintf(fp, "%c.../n", ch);
fprintf(fp, "^/n");
}
void scanner(FILE *infile, FILE *outfile) {
char ch;
int i, c;
do {
do {
ch = getletter(infile);
} while(isspace(ch));
if (isalpha(ch)) {
token[0] = ch;
ch = getletter(infile);
i = 1;
while (isalnum(ch)) {
token[i] = ch;
i++;
ch = getletter(infile);
}
token[i] = '/0';
fseek(infile, -1, 1);
c = lookup(token);
if (c == 0) {
out(outfile, ID, token);
}
else {
out(outfile, c, " ");
}
}
else {
if (isdigit(ch)) {
token[0] = ch;
ch = getletter(infile);
i = 1;
while (isdigit(ch)) {
token[i] = ch;
i++;
ch = getletter(infile);
}
token[i] = '/0';
fseek(infile, -1, 1);
out(outfile, INT, token);
}
else {
switch(ch) {
case '<':
{
ch = getletter(infile);
if (ch == '=') {
out(outfile, LE, " ");
}
else if (ch == '>') {
out(outfile, NE, " ");
}
else {
fseek(infile, -1, 1);
out(outfile, LT, " ");
}
break;
}
case '=':
{
out(outfile, EQ, " ");
break;
}
case '>':
{
ch = getletter(infile);
if (ch == '=') {
out(outfile, GE, " ");
}
else {
fseek(infile, -1, 1);
out(outfile, GT, " ");
}
break;
}
case ':':
{
ch = getletter(infile);
if (ch == '=') {
out(outfile, FU, " ");
}
else {
fseek(infile, -1, 1);
out(outfile, MAO, " ");
}
break;
}
case '/':
{
ch = getletter(infile);
if (ch == '*') {
out(outfile, ZHU, " ");
}
else {
fseek(infile, -1, 1);
out(outfile, XIE, " ");
}
break;
}
case '+':
{
out(outfile, JIA, " ");
break;
}
case '-':
{
out(outfile, JIAN, " ");
break;
}
case '*':
{
out(outfile, CHEN, " ");
break;
}
case ';':
{
out(outfile, FEN, " ");
break;
}
case '(':
{
out(outfile, ZUO, " ");
break;
}
case ')':
{
out(outfile, YOU, " ");
break;
}
default:
{
if (ch != EOF) {
report_error(outfile, ch);
}
break;
}
}
}
}
} while(ch != EOF);
return;
}
void main() {
FILE *in, *out;
in = fopen("pascal.txt", "r");
out = fopen("result.txt", "w");
scanner(in, out);
fprintf(out, "0,#");
fclose(in);
fclose(out);
printf("Finished!/n");
}
- 编译原理实验一——简单词法分析
- 编译原理--实验一 词法分析
- 编译原理实验一 词法分析程序
- 哈工大编译原理实验一词法分析
- 哈工大编译原理实验1——词法分析
- 编译原理实验----词法分析
- 编译原理实验---词法分析
- 编译原理简单词法分析
- 编译原理实验 之 词法分析
- 编译原理实验改进版(词法分析)
- 【编译原理】实验1.词法分析
- 编译原理实验1-词法分析
- 编译原理课程实验--词法分析
- 编译原理实验之词法分析
- 编译原理——词法分析
- 实验一 简单词法分析程序设计
- 哈工大软件学院编译原理实验1——词法分析
- 编译原理——实验 1.1用 FLEX自动构造词法分析程序
- 【转】ASP.NET状态存储管理九大兵器
- Windows消息列表
- 公共交通剪影
- I'll Move Soon
- Weblogic7.0使用jspSmartUpload的秘籍
- 编译原理实验一——简单词法分析
- 编译原理实验二——语法分析(预测分析)
- Eclipse开发jsp目录结构
- 迷糊中……有点神智不清了。。。。
- 用VC实现的禁用光驱程序
- EPL 4 by monkeycz
- 扬虚子
- 自学者的悲哀
- 北京第9届.NET俱乐部参与有感