yz454170989 2015-10-26 15:40 采纳率: 52.6%
浏览 1746
已结题

求帮备注解释 C 代码,编译原理 虎书中的

虎书绪论里的联系,太凶。。。大学生表示看着很困难
求给定语句中任意子表达式内的print语句的参数个数,对一个直线式程序语言写的程序进行"解释"。

三个头文件

(1)prog1.h
A_stm prog(void);

(2)slp.h
typedef struct A_stm_ *A_stm;
typedef struct A_exp_ *A_exp;
typedef struct A_expList_ *A_expList;
typedef enum {A_plus,A_minus,A_times,A_div} A_binop;

struct A_stm_ {enum {A_compoundStm, A_assignStm, A_printStm} kind;
union {struct {A_stm stm1, stm2;} compound;
struct {string id; A_exp exp;} assign;
struct {A_expList exps;} print;
} u;
};
A_stm A_CompoundStm(A_stm stm1, A_stm stm2);
A_stm A_AssignStm(string id, A_exp exp);
A_stm A_PrintStm(A_expList exps);

struct A_exp_ {enum {A_idExp, A_numExp, A_opExp, A_eseqExp} kind;
union {string id;
int num;
struct {A_exp left; A_binop oper; A_exp right;} op;
struct {A_stm stm; A_exp exp;} eseq;
} u;
};
A_exp A_IdExp(string id);
A_exp A_NumExp(int num);
A_exp A_OpExp(A_exp left, A_binop oper, A_exp right);
A_exp A_EseqExp(A_stm stm, A_exp exp);

struct A_expList_ {enum {A_pairExpList, A_lastExpList} kind;
union {struct {A_exp head; A_expList tail;} pair;
A_exp last;
} u;
};

A_expList A_PairExpList(A_exp head, A_expList tail);
A_expList A_LastExpList(A_exp last);

(3)util.h
#include

typedef char *string;
typedef char bool;

#define TRUE 1
#define FALSE 0

void *checked_malloc(int);
string String(char *);

typedef struct U_boolList_ *U_boolList;
struct U_boolList_ {bool head; U_boolList tail;};
U_boolList U_BoolList(bool head, U_boolList tail);

三个源文件
(1)prog1.c
#include "util.h"
#include "slp.h"
//要分析的语句:
A_stm prog(void) {

return
A_CompoundStm(A_AssignStm("a",
A_OpExp(A_NumExp(5), A_plus, A_NumExp(3))),
A_CompoundStm(A_AssignStm("b",
A_EseqExp(A_PrintStm(A_PairExpList(A_IdExp("a"),
A_LastExpList(A_OpExp(A_IdExp("a"), A_minus,
A_NumExp(1))))),
A_OpExp(A_NumExp(10), A_times, A_IdExp("a")))),
A_PrintStm(A_LastExpList(A_IdExp("b")))));
}

(2)slp.c
#include "util.h"
#include "slp.h"

A_stm A_CompoundStm(A_stm stm1, A_stm stm2) {
A_stm s = checked_malloc(sizeof *s);
s->kind=A_compoundStm; s->u.compound.stm1=stm1; s->u.compound.stm2=stm2;
return s;
}

A_stm A_AssignStm(string id, A_exp exp) {
A_stm s = checked_malloc(sizeof *s);
s->kind=A_assignStm; s->u.assign.id=id; s->u.assign.exp=exp;
return s;
}

A_stm A_PrintStm(A_expList exps) {
A_stm s = checked_malloc(sizeof *s);
s->kind=A_printStm; s->u.print.exps=exps;
return s;
}

A_exp A_IdExp(string id) {
A_exp e = checked_malloc(sizeof *e);
e->kind=A_idExp; e->u.id=id;
return e;
}

A_exp A_NumExp(int num) {
A_exp e = checked_malloc(sizeof *e);
e->kind=A_numExp; e->u.num=num;
return e;
}

A_exp A_OpExp(A_exp left, A_binop oper, A_exp right) {
A_exp e = checked_malloc(sizeof *e);
e->kind=A_opExp; e->u.op.left=left; e->u.op.oper=oper; e->u.op.right=right;
return e;
}

A_exp A_EseqExp(A_stm stm, A_exp exp) {
A_exp e = checked_malloc(sizeof *e);
e->kind=A_eseqExp; e->u.eseq.stm=stm; e->u.eseq.exp=exp;
return e;
}

A_expList A_PairExpList(A_exp head, A_expList tail) {
A_expList e = checked_malloc(sizeof *e);
e->kind=A_pairExpList; e->u.pair.head=head; e->u.pair.tail=tail;
return e;
}

A_expList A_LastExpList(A_exp last) {
A_expList e = checked_malloc(sizeof *e);
e->kind=A_lastExpList; e->u.last=last;
return e;
}

(3)util,c
/*

  • util.c - commonly used utility functions. */

#include
#include
#include
#include "util.h"
void *checked_malloc(int len)
{void *p = malloc(len);
if (!p) {
fprintf(stderr,"\nRan out of memory!\n");
exit(1);
}
return p;
}

string String(char *s)
{string p = checked_malloc(strlen(s)+1);
strcpy(p,s);
return p;
}

U_boolList U_BoolList(bool head, U_boolList tail)
{ U_boolList list = checked_malloc(sizeof(*list));
list->head = head;
list->tail = tail;
return list;
}

  • 写回答

1条回答 默认 最新

  • threenewbee 2015-10-26 15:42
    关注

    先搞清楚算法,而不是直接看代码。特别是C语言,这种原始而简陋的语言,直接看代码看到的都是一些分配内存、字符串拷贝之类的底层操作。不明白算法看程序就是给自己添堵。

    评论

报告相同问题?

悬赏问题

  • ¥20 beats蓝牙耳机怎么查看日志
  • ¥15 Fluent齿轮搅油
  • ¥15 八爪鱼爬数据为什么自己停了
  • ¥15 交替优化波束形成和ris反射角使保密速率最大化
  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏