- 25
- 0
- 约3.49千字
- 约 6页
- 2018-01-02 发布于河南
- 举报
编译原理上机实验程序
程序源代码:
#includestdio.h
#includestdlib.h
#includestring.h
#includectype.h
#define LEN sizeof(struct Node)
struct Node
{
char data;
struct Node *next;
};
struct Node *head,*p;
char ch;
char *key[7]={main,int,char,if,else,for,while};
char token[20];
void getch()
{
ch=p-data;
p=p-next;
}
void output(struct Node *head)
{
if(!head)
{
printf(Something wrong with head node!);
exit(1);
}
p=head-next;
while(p-next!=NULL)
{
printf(%c,p-data);
p=p-next;
}
printf(\n);
}
void getbc()
{
while(ch==)
getch();
}
void concat()
{
unsigned int i;
i=strlen(token);
token[i]=ch;
token[i+1]=\0;
}
int letter(char ch)
{
return isalpha((int)ch);
}
int digit(char ch)
{
return isdigit((int)ch);
}
int reserve()
{
int k;
for(k=0;k7;k++)
{
if(strcmp(key[k],token)==0)
return(k+1);
}
return 10;
}
void retract()
{
struct Node *Q;
Q=head-next;
while(Q-next!=p)
Q=Q-next;
p=Q;
}
void back(int a,char *b)
{
printf((%d,%s)\n,a,b);
}
void scaner()
{
int c;
token[0]=NULL;
getch();
getbc();
if(letter(ch))
{
while(letter(ch)||digit(ch))
{
concat();
getch();
}
retract();
c=reserve();
if(c!=10)
back(c,token);
else
back(10,token);
}
else if(digit(ch))
{
while(digit(ch))
{ concat();
getch();
}
retract();
printf((20,%d)\n,atoi(token));
}
else
switch(ch)
{
case+:
back(22,+);
break;
case-:
back(23,-);
break;
case*:
back(24,*);
break;
case/:
back(25,/);
break;
case(:
back(26,();
break;
case):
back(27,));
break;
case{:
back(28,{);
break;
case}:
back(29,});
break;
case,:
back(30,,);
break;
case;:
back(31,:);
break;
case::
back(32,:);
break;
case=:
getch();
if(ch===)
back(37,==);
else
{
retract();
back(21,=);
}
break;
case:
getch();
if(ch===)
back(35,=);
el
您可能关注的文档
最近下载
- RB308A-rev1.7-晶准-恒佳盛一级代理.pdf VIP
- 小学奥数 较复杂的乘法原理.pdf VIP
- 2024年陕西省中考数学真题(学生版+解析版).docx
- 小学奥数 加乘法原理.docx VIP
- 2025烟台南山学院单招《数学》复习提分资料(含答案详解).docx VIP
- 湖南省湘一名校2026届高三上学期12月质量检测物理试卷(含答案).pdf VIP
- 2025年上海高考数学二轮复习:热点题型4 统计与概率(六大题型)原卷版+解析.pdf VIP
- WordExcel2010中文版办公专家从入门到精通.pdf VIP
- 新教材人教A版高中数学选择性必修第三册习题答案.docx VIP
- 专题08 古诗鉴赏(解析版)备战2024年中考语文真题题源解密(全国通用.docx VIP
原创力文档

文档评论(0)