没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论










#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define NORW 13 /* of reserved words */
#define TXMAX 100 /* length of identifier table */
#define NMAX 14 /* max number of digits in numbers */
#define AL 10 /* length of identifiers */
#define AMAX 2047 /* maxinum address */
#define LEVMAX 3 /* max depth of block nesting */
#define CXMAX 200 /* size of code array */
#define STACKSIZE 500
char *symbol[32]= {"nul","ident","number","plus","minus","times","slash","oddsym",
"eql","neq","lss","leq","gtr","geq","lparen","rparen","comma",
"semicolon","period","becomes","beginsym","endsym","ifsym",
"thensym","whilesym","writesym","readsym","dosym","callsym",
"constsym","varsym","procsym"}; /* type of symbols */
char *word[NORW]={"begin","call","const","do","end","if","odd","procedure",
"read","then","var","while","write"}; /* table of reserved words */
char *wsym[NORW]={ "beginsym","callsym","constsym","dosym","endsym","ifsym",
"oddsym","procsym","readsym","thensym","varsym","whilesym","writesym"};
char *mnemonic[8]= {"lit","opr","lod","sto","cal","ini","jmp","jpc"};
char ch; /* last char read */
char id[AL+1]; /*last identifier read */
char sym[10]; /* last symbol read */
char line[81];
char a[AL+1],fname[AL+1];
enum object{constant,variable,procedur};
enum object kind;
enum fct{lit,opr,lod,sto,cal,ini,jmp,jpc};
enum listswitcher{false,true}; /*true set list object code */
enum listswitcher listswitch;
FILE *fa;
FILE *fa1, *fa2;
FILE *fin, *fout;
int num; /* last number read */

int cc; /* character count */
int ll; /* line length */
int cx; /* code allocation index */
int err;
int lev=0,tx=0,dx=3;
int linecnt=0;
struct instruction
{enum fct f; /* function code */
int l; /* level */
int a; /* displacement addr */
}; /* lit 0,a: load constant a
opr 0,a: execute opr a
lod l,a: load variable 1, a
sto l,a: store variable 1, a
cal l,a: call procedure a at level 1
int 0,a: increment t-register by a
jmp 0,a: jump to a
jpc 0,a: jump conditional to a */
struct instruction code[CXMAX+1];
struct table1
{char name[AL+1];
enum object kind;
int val,level,adr,size;
};
struct table1 table[TXMAX+1];
struct node{
char *pa[32];
}*declbegsys,*statbegsys,*facbegsys,*tempsetsys;
int in(str, set)
char *str;
struct node *set;
{
int i=0;
while(set->pa[i]!=NULL){
if(strcmp(str,set->pa[i])==0)
return( 1 );
else
i++;
}

return( 0 );
}
struct node *add(set1,set2)
struct node *set1,*set2;
{
int i=0,j=0,k=0,cnt;
struct node *pt;
pt=(struct node *)malloc(sizeof(struct node));
for(cnt=0; cnt < 32; cnt++)
pt->pa[cnt]=(char*)malloc(10*sizeof(char));
while(set1->pa[i]!=NULL)
strcpy(pt->pa[j++],set1->pa[i++]);
while(set2->pa[k]!=NULL){
if (in(set2->pa[k],set1)==0)
strcpy(pt->pa[j++],set2->pa[k++]);
else
k++;
}
pt->pa[j]=NULL;
return( pt );
}
error(int n)
{
int i;
printf ("***");
fputs ("***", fa1);
for (i=0;i<cc;i++){
printf (" ");
}
for (i=0;i<cc;i++){
fputs (" ",fa1);
}
printf ("error%d\n",n);
fprintf (fa1, "error%d\n",n);
err=err+1;
}
void get_ch( )
{

if (cc==ll+1){
if (feof(fin)){
printf ("program incomplete");
}
ll= 0;
cc= 0;
while ((!feof(fin)) && ((ch=fgetc(fin))!='\n')){
putchar(ch);
fputc(ch,fa1);
line[ll++]=ch;
}
printf ("\n");
line[ll]=ch;
fprintf (fa1,"\n");
}
ch=line[cc++];
}
void getsym( )
{
int i,j,k;
while(ch==' '||ch=='\t'||ch=='\n')
get_ch( );
if (ch>='a'&&ch<='z'){ /* id or reserved word */
k=0;
do {
if(k<AL){
a[k]=ch;
k=k+1;
}
get_ch( );
}while((ch>='a'&&ch<='z')||(ch>='0'&&ch<='9'));
a[k]='\0';
strcpy(id,a);
i=0;
j=NORW-1;
do { /* look up reserved words by binary search */
k=(i+j)/2;
if (strcmp(id,word[k])<=0) j=k-1;
if (strcmp(id,word[k])>=0) i=k+1;
}while (i<=j);
if (i-1>j) strcpy(sym,wsym[k]);
else strcpy(sym,"ident");
}

else if (ch>='0'&&ch<='9'){ /* number */
k=0;
num=0;
strcpy(sym,"number");
do {
num=10*num+(int)ch-'0';
k=k+1;
get_ch( );
}while(ch>='0'&&ch<='9');
if(k>NMAX) error(30);
}
else if (ch==':'){
get_ch( );
if (ch=='='){
strcpy(sym,"becomes");
get_ch( );
}
else strcpy(sym,"nul");
}
else if (ch=='<'){
get_ch( );
if (ch=='='){
strcpy(sym,"leq");
get_ch( );
}
else strcpy(sym,"lss");
}
else if (ch=='>'){
get_ch( );
if (ch=='='){
strcpy(sym,"geq");
get_ch( );
}
else strcpy(sym,"gtr");
}
else {
switch(ch){
case '+': strcpy(sym,"plus");break;
case '-': strcpy(sym,"minus");break;
case '*': strcpy(sym,"times");break;
case '/': strcpy(sym,"slash");break;
case '(': strcpy(sym,"lparen");break;
case ')': strcpy(sym,"rparen");break;
case '=': strcpy(sym,"eql");break;
剩余20页未读,继续阅读

bluesky2sadfsadfa
- 粉丝: 0
- 资源: 6
上传资源 快速赚钱
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助


安全验证
文档复制为VIP权益,开通VIP直接复制

- 1
- 2
前往页