⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 1.txt

📁 一个语法分析程序
💻 TXT
字号:
/************************************/
/* 程序名称: LL(1)文法分析程序 */
/* E->E+T|E-T|T */
/* T->T*F|T/F|F */
/* F->(E)|i */
/* 程序版本: 1.0 Final */

/************************************/

/********************************************/
/* 程序相关说明 */
/* A=E’ B=T’ */
/* 0=E 1=E’ 2=T 3=T’ 4=F */
/* 0=i 1=+ 2=- 3=* 4=/ 5=( 6=) 7=# */
/************************************/

#i nclude "stdio.h"
#i nclude "malloc.h"

struct Lchar{
char char_ch;
struct Lchar *next;
}Lchar,*p,*h,*temp,*top,*base;
char curchar;
char curtocmp;
int right;
int table[5][8]={{1,0,0,0,0,1,0,0},
{0,1,1,0,0,0,1,1},
{1,0,0,0,0,1,0,0},
{0,1,1,1,1,0,1,1},
{1,0,0,0,0,1,0,0}};
int i,j;

void push(char pchar)
{
temp=malloc(sizeof(Lchar));
temp->char_ch=pchar;
temp->next=top;
top=temp;
}

void pop(void)
{
curtocmp=top->char_ch;
if(top->char_ch!=’#’)
top=top->next;
}

void doforpush(int t)
{
switch(t)
{
case 0:push(’A’);push(’T’);break;
case 5:push(’A’);push(’T’);break;
case 11:push(’A’);push(’T’);push(’+’);break;
case 12:push(’A’);push(’T’);push(’-’);break;
case 20:push(’B’);push(’F’);break;
case 25:push(’B’);push(’F’);break;
case 33:push(’B’);push(’F’);push(’*’);break;
case 34:push(’B’);push(’F’);push(’/’);break;
case 40:push(’i’);break;
case 45:push(’)’);push(’E’);push(’(’);
}
}

void changchartoint()
{
switch(curtocmp)
{
case ’A’:i=1;break;
case ’B’:i=3;break;
case ’E’:i=0;break;
case ’T’:i=2;break;
case ’F’:i=4;
}
switch(curchar)
{
case ’i’:j=0;break;
case ’+’:j=1;break;
case ’-’:j=2;break;
case ’*’:j=3;break;
case ’/’:j=4;break;
case ’(’:j=5;break;
case ’)’:j=6;break;
case ’#’:j=7;
}
}

void dosome(void)
{
int t;
for(;;)
{
pop();
curchar=h->char_ch;
printf("\n%c\t%c\n",curchar,curtocmp); 
if(curtocmp==’#’ && curchar==’#’)
break;
if(curtocmp==’A’||curtocmp==’B’||curtocmp==’E’||curtocmp==’T’||curtocmp==’F’)
{
if(curtocmp!=’#’)
{
changchartoint();
if(table[i][j])
{
t=10*i+j;
doforpush(t);
continue;
}
else
{
right=0;
break;
}
}
else
if(curtocmp!=curchar)
{
right=0;
break;
}
else
break;
}
else
if(curtocmp!=curchar)
{
right=0;
break;
}
else
{
h=h->next;
continue;
}
}
}

void main(void)
{
char ch;
right=1;
base=malloc(sizeof(Lchar));
base->next=NULL;
base->char_ch=’#’;
temp=malloc(sizeof(Lchar));
temp->next=base;
temp->char_ch=’E’;
top=temp;
h=malloc(sizeof(Lchar));
h->next=NULL;
p=h;
do{
ch=getch();
putch(ch);
if(ch==’i’||ch==’+’||ch==’-’||ch==’*’||ch==’/’||ch==’(’||ch==’)’||ch==’#’)
{
temp=malloc(sizeof(Lchar));
temp->next=NULL;
temp->char_ch=ch;
h->next=temp;
h=h->next;
}
else
{
temp=p->next;
printf("\nInput a wrong char!Input again:\n");
for(;;)
{ 
if (temp!=NULL)
printf("%c",temp->char_ch); 
else
break;
temp=temp->next;
}
}
}while(ch!=’#’);
p=p->next;
h=p;
dosome();
if(right)
printf("\nOK!\n");
else
printf("\nError!\n");
getch();
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -