没有合适的资源?快使用搜索试试~ 我知道了~
首页编译原理实验词法分析,语法分析源代码
资源详情
资源评论
资源推荐
[实验任务]
完成以下正则文法所描述的 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、设计两个测试用例(尽可能完备),并给出测试结果。
demo.cpp #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");
}
本 文 来 自 CSDN 博 客 , 转 载 请 标 明 出 处 : http://blog.csdn.net/ffee/archive/
2006/05/31/766483.aspx
[实验任务]
1、实现 LL(1)分析中控制程序(表驱动程序);
2、完成以下描述算术表达式的 LL(1)文法的 LL(1)分析程序(LL(1)分析表见教材)。
剩余39页未读,继续阅读
shuangwuzhang
- 粉丝: 0
- 资源: 1
上传资源 快速赚钱
- 我的内容管理 收起
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
会员权益专享
最新资源
- c++校园超市商品信息管理系统课程设计说明书(含源代码) (2).pdf
- 建筑供配电系统相关课件.pptx
- 企业管理规章制度及管理模式.doc
- vb打开摄像头.doc
- 云计算-可信计算中认证协议改进方案.pdf
- [详细完整版]单片机编程4.ppt
- c语言常用算法.pdf
- c++经典程序代码大全.pdf
- 单片机数字时钟资料.doc
- 11项目管理前沿1.0.pptx
- 基于ssm的“魅力”繁峙宣传网站的设计与实现论文.doc
- 智慧交通综合解决方案.pptx
- 建筑防潮设计-PowerPointPresentati.pptx
- SPC统计过程控制程序.pptx
- SPC统计方法基础知识.pptx
- MW全能培训汽轮机调节保安系统PPT教学课件.pptx
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论1