编译原理实验--LL(1)预测分析,根据LL(1)文法,写出各文法左部的FIRST集 FOLLOW集 SELECT集.然后根据SELECT集 写出预测分析表.本程序是在SELECT集建立后,程序根据预测分析表进行分析.
/**
*@Create: 2006-11-21
*@Description: LL(1) forcast Analyse
*@Author: oDon
*@Corporation: xxxxxxx CO.
*@Copyright: yuanonline@hotmail.com --oDon--
0 1 2 3 4 5 6 7
---------------------------------------table---------------------------------------
| | I | + | - | * | / | ( | ) | # |
------------------------------------------------------------------------------------
| E ->TG ->TG |
------------------------------------------------------------------------------------
| G ->+TG ->-TG ->~ ->~ |
------------------------------------------------------------------------------------
| T ->FS ->FS |
------------------------------------------------------------------------------------
| S ->~ ->~ *FS /FS ->~ ->~ |
------------------------------------------------------------------------------------
| F ->i ->(E) |
-----------------------------------------------------------------------------------
*/
#include " stdio.h "
#define SIZE 20
#define EMPTY 14
#define TABLE_SIZE 5
typedef struct table
{
char head;
char pre[8][4];
} table;
char ExpressionStr[SIZE]; // Inputed Analysed Expression
char AnalysisStack[SIZE]; // Analysis Stack
unsigned char analysis_Index; //
unsigned char analy_Index; // Current TOP_Index of Expression
unsigned char exp_Index; // Current Bottom_Index of Expression
unsigned char step;
table tb[TABLE_SIZE]; // forcast table---------------
/***************************************
@name: InitTable
@Describe: Initlize the forcast table
@Param: void
@Return: void
*****************************************/
void InitTable()
{
// table E
tb[0].head = 'E';
strcpy(tb[0].pre[0], "GT");
strcpy(tb[0].pre[5], "GT");
//table G
tb[1].head = 'G';
strcpy(tb[1].pre[1], "GT+");
strcpy(tb[1].pre[2], "GT-");
strcpy(tb[1].pre[6], "~");
strcpy(tb[1].pre[7], "~");
//table T
tb[2].head = 'T';
strcpy(tb[2].pre[0], "SF");
strcpy(tb[2].pre[5], "SF");
//table S
tb[3].head = 'S';
strcpy(tb[3].pre[1], "~");
strcpy(tb[3].pre[2], "~");
strcpy(tb[3].pre[3], "SF*");
strcpy(tb[3].pre[4], "SF/");
strcpy(tb[3].pre[6], "~");
strcpy(tb[3].pre[7], "~");
//table F
tb[4].head = 'F';
strcpy(tb[4].pre[0], "i");
strcpy(tb[4].pre[5], ")E(");
}
/***************************************
@name: PrintGenerate
@Describe: Printing Generate expression
@Param: void
@Return: void
*****************************************/
void PrintGenerate( char * ch)
{
int i = 0;
for(i = strlen(ch) - 1; i >= 0; i--)
{
printf("%c",ch[i]);
}
printf(" ");
}
/*******************************************
*@name: Trace
*@Description: Format output
*@Param: void
*@Return: void
********************************************/
void Trace()
{
int i = 0;
int j = 0;
printf(" ");
printf("%d ",step++);
while(AnalysisStack[i] != '
*@Create: 2006-11-21
*@Description: LL(1) forcast Analyse
*@Author: oDon
*@Corporation: xxxxxxx CO.
*@Copyright: yuanonline@hotmail.com --oDon--
0 1 2 3 4 5 6 7
---------------------------------------table---------------------------------------
| | I | + | - | * | / | ( | ) | # |
------------------------------------------------------------------------------------
| E ->TG ->TG |
------------------------------------------------------------------------------------
| G ->+TG ->-TG ->~ ->~ |
------------------------------------------------------------------------------------
| T ->FS ->FS |
------------------------------------------------------------------------------------
| S ->~ ->~ *FS /FS ->~ ->~ |
------------------------------------------------------------------------------------
| F ->i ->(E) |
-----------------------------------------------------------------------------------
*/
#include " stdio.h "
#define SIZE 20
#define EMPTY 14
#define TABLE_SIZE 5
typedef struct table
{
char head;
char pre[8][4];
} table;
char ExpressionStr[SIZE]; // Inputed Analysed Expression
char AnalysisStack[SIZE]; // Analysis Stack
unsigned char analysis_Index; //
unsigned char analy_Index; // Current TOP_Index of Expression
unsigned char exp_Index; // Current Bottom_Index of Expression
unsigned char step;
table tb[TABLE_SIZE]; // forcast table---------------
/***************************************
@name: InitTable
@Describe: Initlize the forcast table
@Param: void
@Return: void
*****************************************/
void InitTable()
{
// table E
tb[0].head = 'E';
strcpy(tb[0].pre[0], "GT");
strcpy(tb[0].pre[5], "GT");
//table G
tb[1].head = 'G';
strcpy(tb[1].pre[1], "GT+");
strcpy(tb[1].pre[2], "GT-");
strcpy(tb[1].pre[6], "~");
strcpy(tb[1].pre[7], "~");
//table T
tb[2].head = 'T';
strcpy(tb[2].pre[0], "SF");
strcpy(tb[2].pre[5], "SF");
//table S
tb[3].head = 'S';
strcpy(tb[3].pre[1], "~");
strcpy(tb[3].pre[2], "~");
strcpy(tb[3].pre[3], "SF*");
strcpy(tb[3].pre[4], "SF/");
strcpy(tb[3].pre[6], "~");
strcpy(tb[3].pre[7], "~");
//table F
tb[4].head = 'F';
strcpy(tb[4].pre[0], "i");
strcpy(tb[4].pre[5], ")E(");
}
/***************************************
@name: PrintGenerate
@Describe: Printing Generate expression
@Param: void
@Return: void
*****************************************/
void PrintGenerate( char * ch)
{
int i = 0;
for(i = strlen(ch) - 1; i >= 0; i--)
{
printf("%c",ch[i]);
}
printf(" ");
}
/*******************************************
*@name: Trace
*@Description: Format output
*@Param: void
*@Return: void
********************************************/
void Trace()
{
int i = 0;
int j = 0;
printf(" ");
printf("%d ",step++);
while(AnalysisStack[i] != '