递归下降分析法的实现方案
递归下降分析法的原理是利用函数之间的递归调用模拟语法树自上而下的构造过程,具体实现方法概述如下:
1)每个非终结符对应一个解析函数;
2)产生式右侧为该产生式左侧非终结符所对应解析函数的“函数体”;
3)产生式右侧终结符对应从输入串中“消耗”该终结符的操作;
4)产生式中的‘|’对应函数体中的“if-else”语句;
5)对于扩展的BNF文法,产生式中的‘{}’对应函数体中的“while”语句。
说明:
此部分中的数据来源为前一篇(词法分析)的结果
[cpp] view plain copy
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #define MAX_ID_LENGTH 256
- #define MAX_TABLE_SIZE 2048
- #define SUCCESS 1
- typedef enum
- {
- T_0=0, T_1=1, T_2=2, T_INT=3, T_IF=4, T_ELSE=5,
- T_WHILE=6, T_FOR=7, T_READ=8, T_WRITE=9, T_BEGIN=10, T_FOUNCTION=11,
- T_END=12, T_THEN=13, T_ADD=14, T_SUB=15, T_MUL=16, T_DIV=17,
- T_L=18, T_LE=19, T_G=20, T_GE=21, T_NE=22, T_E=23,
- T_ASSIGN=24, T_LPAR=25, T_RPAR=26, T_COM=27, T_SEM=28, T_RETURN=29,
- T_ERROR=30, T_SYMBOL=31, T_CONSTANT=32
- } Token;
- typedef struct
- {
- Token read;
- struct
- size_t
- } MYLEX;
- MYLEX lex_head,error_temp;
- MYLEX* current;
- Token error_TOKEN=T_0;
- /*初始化信息*/
- /*函数申明*/
- int item(void);
- int Block(void);
- int Factor(void);
- int Variable(void);
- int Function(void);
- int Constant(void);
- int Parameter (void
- int Identifier (void);
- int Main_Program(void);
- int Function_call(void);
- void emergency_exit(void);
- int Read_statement(void);
- int Write_statement(void);
- int Execute_statement(void);
- MYLEX* Advance(MYLEX* read);
- MYLEX* Read_file(MYLEX* read);
- int Relational_operators(void);
- int Assignment_statement(void);
- int Variable_declaration(void);
- int Function_declaration(void);
- int
- int Conditional_statement(void);
- int Arithmetic_expression(void);
- int Declarative_statement(void);
- int Conditional_expression(void);
- int Execute_statement_table(void);
- int
- int Declarative_statement_table(void);
- /*退出*/
- void emergency_exit(void)
- {
- MYLEX* EXIT_TEEMP=lex_head.next;
- current=EXIT_TEEMP;
- while(!current)
- {
- current=EXIT_TEEMP->next;
- free(EXIT_TEEMP);
- }
- exit(0);
- }
- /*错误处理*/
- int
- {
- FILE *error_file=fopen("error.txt","a");
- if(!error_file)
- {
- "can`t open file \n");
- }
- if(error_temp.line<current->line)
- {
- error_temp=*current;
- error_TOKEN=aim;
- "error in line:%d\tTOKEN:%d\texpeced Token is :%d\n",
- error_temp.line,error_temp.read,error_TOKEN);
- }
- fclose(error_file);
- return
- }
- //************************************
- // Method: Read_file
- // FullName: Read_file
- // Access: public
- // Returns: int
- // Qualifier:读文件
- // Parameter: void
- //************************************
- MYLEX* Read_file(MYLEX* current)
- {
- current->line=0;
- current->next=NULL;
- current->read=T_0;
- MYLEX* Temp;
- Temp=current;
- FILE* READ_FILE=fopen("GRAMER.txt","r");
- if(!READ_FILE)
- {
- "can`t load file GRAMER.txt, a file like GRAMER.txt is expectd here \n");
- exit(0);
- }
- while(!feof(READ_FILE))
- {
- sizeof(MYLEX));
- Temp=Temp->next;
- if(!Temp)
- {
- "can`t malloc a space for this program!\n");
- emergency_exit();
- }
- Temp->line=0;
- Temp->next=NULL;
- Temp->read=T_0;
- "%d\t%d\n",&(Temp->line),&(Temp->read));
- }
- fclose(READ_FILE);
- return
- }
- //************************************
- // Method: Advance
- // FullName: Advance
- // Access: public
- // Returns: Token
- // Qualifier: 前进移向下一个TOKEN
- // Parameter: void
- //************************************
- MYLEX* Advance(MYLEX* current)
- {
- FILE*EAT_FILE=fopen("symtab2.txt","a");
- FILE* LEFT_FILE=fopen("detail.txt","a");
- if((!EAT_FILE)||(!LEFT_FILE))
- {
- "can`t open file");
- emergency_exit();
- }
- MYLEX* Temp=current->next;
- "you have eat the one is :%3d\n\n follow is \n",current->read);
- fprintf(EAT_FILE,
- "\nyou have eat the one is line:%3d\tToken:%3d\n",
- current->line,current->read);
- fprintf(LEFT_FILE,
- "\n\ncurrent eat the one is line:%3d\tToken:%3d\nfollow is\n",
- current->line,current->read);
- for(Temp; Temp!=NULL; Temp=Temp->next)
- {
- "line:%3d \t token=%3d\n",
- Temp->line,Temp->read);
- "line:%3d \t token=%3d\n",
- Temp->line,Temp->read);
- }
- fclose(EAT_FILE);
- fclose(LEFT_FILE);
- if(current==NULL)
- {
- "all work is to be done!,there is an error in your project!\n ");
- emergency_exit();
- }
- return
- }
- //************************************
- // Method: main
- // FullName: main
- // Access: public
- // Returns: int
- // Qualifier: 主程序
- // Parameter: int agrc
- // Parameter: char * agrv[]
- //************************************
- int main(int agrc, char*agrv[])
- {
- error_temp.line=0;
- error_temp.next=NULL;
- error_temp.read=T_0;
- "\t编译原理实验二\n\tXXXXXXXXX\n\t\t—NEWPLAN\n");
- current=Read_file(&lex_head);
- "pause");
- if(Main_Program())
- {
- "\n\ncongratulation\tsuccessfully!\n\n");
- "del error.txt");
- }
- else
- {
- " sorry you have failed!\n");
- "error in line:%d\tTOKEN:%d\texpeced Token is :%d\n",
- error_temp.line,error_temp.read,error_TOKEN);
- }
- "pause");
- emergency_exit();/*释放空间,函数返回*/
- return
- }
- //************************************
- // Method: match
- // FullName: match
- // Access: public
- // Returns: int
- // Qualifier: 匹配
- // Parameter: Token read
- // Parameter: Token aim
- //************************************
- int
- {
- return
- }
- //************************************
- // Method: Main_Program
- // FullName: Main_Program
- // Access: public
- // Returns: int
- // Qualifier: 开始主程序
- // Parameter: void
- //************************************
- int Main_Program(void)
- {
- return
- }
- //************************************
- // Method: Block
- // FullName: Block
- // Access: public
- // Returns: int
- // Qualifier:/*分程序*/
- // Parameter: Token read
- //************************************
- int Block(void)
- {
- MYLEX* Temp=current;
- return
- Execute_statement_table()&&match(current->read,T_END))?SUCCESS:(current=Temp,!SUCCESS);
- }
- //************************************
- // Method: Declarative_statement_table
- // FullName: Declarative_statement_table
- // Access: public
- // Returns: int
- // Qualifier:/*说明语句表*/
- // Parameter: Token read
- //************************************
- int
- {
- MYLEX* Temp=current;
- if(!Declarative_statement())
- {
- current=Temp;
- return
- }
- /*采用扩展的BNF方法消除左递归*/
- for(;;)
- {
- Temp=current;
- if(!match(current->read,T_SEM))
- break;
- if(!Declarative_statement())
- {
- current=Temp;
- break;
- }
- }
- return
- }
- //************************************
- // Method: Declarative_statement
- // FullName: Declarative_statement
- // Access: public
- // Returns: int
- // Qualifier:/*说明语句*/
- // Parameter: Token read
- //************************************
- int Declarative_statement(void)
- {
- MYLEX* Temp=current;
- return
- (current=Temp,Function_declaration()? SUCCESS:!SUCCESS);
- }
- //************************************
- // Method: Execute_statement_table
- // FullName: Execute_statement_table
- // Access: public
- // Returns: int
- // Qualifier:/*执行语句表*/
- // Parameter: Token read
- //************************************
- int Execute_statement_table(void)
- {
- MYLEX* Temp=current;
- if(!Execute_statement())
- {
- current=Temp;
- return
- }
- for(;;)
- {
- Temp=current;
- if(!match(current->read,T_SEM))
- break;
- if(!Execute_statement())
- {
- current=Temp;
- break;
- }
- }
- return
- }
- //************************************
- // Method: Execute_statement
- // FullName: Execute_statement
- // Access: public
- // Returns: int
- // Qualifier:/*执行语句*/
- // Parameter: Token read
- //************************************
- int Execute_statement(void)
- {
- MYLEX* Temp=current;
- if(Read_statement())
- return
- current=Temp;
- if(Write_statement())
- return
- current=Temp;
- if(Conditional_statement())
- return
- current=Temp;
- if(Assignment_statement())
- return
- current=Temp;
- return
- }
- //************************************
- // Method: Variable_declaration
- // FullName: Variable_declaration
- // Access: public
- // Returns: int
- // Qualifier:/*变量说明*/
- // Parameter: Token read
- //************************************
- int Variable_declaration(void)
- {
- MYLEX* Temp=current;
- return
- }
- //************************************
- // Method: Function_declaration
- // FullName: Function_declaration
- // Access: public
- // Returns: int
- // Qualifier:/*函数说明*/
- // Parameter: Token read
- //************************************
- int Function_declaration(void)
- {
- MYLEX* Temp=current;
- return
- match(current->read, T_LPAR)&&Parameter()&&match(current->read, T_RPAR)&&
- match(current->read,T_SEM)&&Function())? SUCCESS:(current=Temp,!SUCCESS);
- }
- //************************************
- // Method: Variable
- // FullName: Variable
- // Access: public
- // Returns: int
- // Qualifier:/*变量*/
- // Parameter: Token read
- //************************************
- int Variable(void)
- {
- return
- }
- //************************************
- // Method: Identifier
- // FullName: Identifier
- // Access: public
- // Returns: int
- // Qualifier:/*标识符*/
- // Parameter: Token read
- //************************************
- int Identifier (void)
- {
- return
- }
- //************************************
- // Method: Parameter
- // FullName: Parameter
- // Access: public
- // Returns: int
- // Qualifier:/*参数*/
- // Parameter: Token read
- //************************************
- int Parameter (void)
- {
- MYLEX* Temp=current;
- return
- }
- //************************************
- // Method: Function
- // FullName: Function
- // Access: public
- // Returns: int
- // Qualifier:/*函数体*/
- // Parameter: Token read
- //************************************
- int Function(void)
- {
- MYLEX* Temp=current;
- return
- Execute_statement_table()&&match(current->read,T_END))?SUCCESS:(current=Temp,!SUCCESS);
- }
- //************************************
- // Method: Read_statement
- // FullName: Read_statement
- // Access: public
- // Returns: int
- // Qualifier:/*读语句*/
- // Parameter: Token read
- //************************************
- int Read_statement(void)
- {
- MYLEX* Temp=current;
- return
- match(current->read,T_RPAR))?SUCCESS:(current=Temp,!SUCCESS);
- }
- //************************************
- // Method: Write_statement
- // FullName: Write_statement
- // Access: public
- // Returns: int
- // Qualifier:/*写语句*/
- // Parameter: Token read
- //************************************
- int Write_statement(void)
- {
- MYLEX* Temp=current;
- return
- match(current->read,T_RPAR))?SUCCESS:(current=Temp,!SUCCESS);
- }
- //************************************
- // Method: Assignment_statement
- // FullName: Assignment_statement
- // Access: public
- // Returns: int
- // Qualifier:/*赋值语句*/
- // Parameter: Token read
- //************************************
- int Assignment_statement(void)
- {
- MYLEX* Temp=current;
- return
- Arithmetic_expression())? SUCCESS:(current=Temp,!SUCCESS);
- }
- //************************************
- // Method: Conditional_statement
- // FullName: Conditional_statement
- // Access: public
- // Returns: int
- // Qualifier:/*条件语句*/
- // Parameter: Token read
- //************************************
- int Conditional_statement(void)
- {
- MYLEX* Temp=current;
- return
- match(current->read,T_THEN)&&Execute_statement()&&
- match(current->read,T_ELSE)&&Execute_statement()
- )?SUCCESS:(current=Temp,!SUCCESS);
- }
- //************************************
- // Method: Arithmetic_expression
- // FullName: Arithmetic_expression
- // Access: public
- // Returns: int
- // Qualifier:/*算术表达式*/
- // Parameter: Token read
- //************************************
- int Arithmetic_expression(void)
- {
- MYLEX* Temp=current;
- if(!item())
- {
- current=Temp;
- return
- }
- for(;;)
- {
- Temp=current;
- if(!match(current->read,T_SUB))
- break;
- if(!item())
- {
- current=Temp;
- break;
- }
- }
- return
- }
- //************************************
- // Method: item
- // FullName: item
- // Access: public
- // Returns: int
- // Qualifier:/*项*/
- // Parameter: Token read
- //************************************
- int item(void)
- {
- MYLEX* Temp=current;
- if(!Factor())
- {
- current=Temp;
- return
- }
- for(;;)
- {
- Temp=current;
- if(!match(current->read,T_MUL))
- break;
- if(!Factor())
- {
- current=Temp;
- break;
- }
- }
- return
- }
- //************************************
- // Method: Constant
- // FullName: Constant
- // Access: public
- // Returns: int
- // Qualifier:/*常数*/
- // Parameter: Token read
- //************************************
- int Constant(void)
- {
- return
- }
- //************************************
- // Method: Conditional_expression
- // FullName: Conditional_expression
- // Access: public
- // Returns: int
- // Qualifier:/*条件表达式*/
- // Parameter: Token read
- //************************************
- int Conditional_expression(void)
- {
- MYLEX* Temp=current;
- return
- Arithmetic_expression())? SUCCESS:(current=Temp,!SUCCESS);
- }
- //************************************
- // Method: Relational_operators
- // FullName: Relational_operators
- // Access: public
- // Returns: int
- // Qualifier:/*关系运算符*/
- // Parameter: Token read
- //************************************
- int Relational_operators(void)
- {
- return
- match(current->read,T_GE)||match(current->read,T_E)||match(current->read,T_NE)
- )?SUCCESS:!SUCCESS;
- }
- //************************************
- // Method: Factor
- // FullName: Factor
- // Access: public
- // Returns: int
- // Qualifier:/*因子*/
- // Parameter: Token read
- //************************************
- int Factor(void)
- {
- MYLEX* Temp=current;
- if(Function_call())
- return
- current=Temp;
- if(Variable())
- return
- current=Temp;
- if(Constant())
- return
- current=Temp;
- return
- }
- //************************************
- // Method: Function_call
- // FullName: Function_call
- // Access: public
- // Returns: int
- // Qualifier:/*函数调用*/
- // Parameter: Token read
- //************************************
- int Function_call(void)
- {
- MYLEX* Temp=current;
- return
- match(current->read,T_RPAR))?SUCCESS:(current=Temp,!SUCCESS);
- }
源码版本2(更容易理解)
[cpp] view plain copy
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #define MAX_ID_LENGTH 256
- #define MAX_TABLE_SIZE 2048
- #define SUCCESS 1
- typedef enum
- {
- T_0=0,
- T_1=1,
- T_2=2,
- T_INT=3,
- T_IF=4,
- T_ELSE=5,
- T_WHILE=6,
- T_FOR=7,
- T_READ=8,
- T_WRITE=9,
- T_BEGIN=10,
- T_FOUNCTION=11,
- T_END=12,
- T_THEN=13,
- T_ADD=14,
- T_SUB=15,
- T_MUL=16,
- T_DIV=17,
- T_L=18,
- T_LE=19,
- T_G=20,
- T_GE=21,
- T_NE=22,
- T_E=23,
- T_ASSIGN=24,
- T_LPAR=25,
- T_RPAR=26,
- T_COM=27,
- T_SEM=28,
- T_RETURN=29,
- T_ERROR=30,
- T_SYMBOL=31,
- T_CONSTANT=32
- } Token;
- typedef struct
- {
- Token read;
- struct
- size_t
- }MYLEX;
- #ifndef SUCCESS
- #define SUCCESS 1
- #endif
- MYLEX lex_head,error_temp;
- MYLEX* current;
- Token error_TOKEN=T_0;
- /*初始化信息*/
- /*函数申明*/
- int item(void);
- int Block(void);
- int Factor(void);
- int Variable(void);
- int Function(void);
- int Constant(void);
- int Parameter (void
- int Identifier (void);
- int Main_Program(void);
- int Function_call(void);
- void emergency_exit(void);
- int Read_statement(void);
- int Write_statement(void);
- int Execute_statement(void);
- MYLEX* Advance(MYLEX* read);
- MYLEX* Read_file(MYLEX* read);
- int Relational_operators(void);
- int Assignment_statement(void);
- int Variable_declaration(void);
- int Function_declaration(void);
- int
- int Conditional_statement(void);
- int Arithmetic_expression(void);
- int Declarative_statement(void);
- int Conditional_expression(void);
- int Execute_statement_table(void);
- int
- int Declarative_statement_table(void);
- void emergency_exit(void)
- {
- MYLEX* EXIT_TEEMP=lex_head.next;
- current=EXIT_TEEMP;
- while(!current)
- {
- current=EXIT_TEEMP->next;
- free(EXIT_TEEMP);
- }
- exit(0);
- }
- int
- {
- FILE *error_file=fopen("error.txt","a");
- if(!error_file)
- {
- "can`t open file \n");
- }
- if(error_temp.line<current->line)
- {
- error_temp=*current;
- error_TOKEN=aim;
- "error in line:%d\tTOKEN:%d\texpeced Token is :%d\n",
- error_temp.line,error_temp.read,error_TOKEN);
- }
- fclose(error_file);
- return
- }
- //************************************
- // Method: Read_file
- // FullName: Read_file
- // Access: public
- // Returns: int
- // Qualifier:读文件
- // Parameter: void
- //************************************
- MYLEX* Read_file(MYLEX* current)
- {
- current->line=0;
- current->next=NULL;
- current->read=T_0;
- MYLEX* Temp;
- Temp=current;
- FILE* READ_FILE=fopen("GRAMER.txt","r");
- if(!READ_FILE)
- {
- "can`t load file GRAMER.txt, a file like GRAMER.txt is expectd here \n");
- exit(0);
- }
- while(!feof(READ_FILE))
- {
- sizeof(MYLEX));
- Temp=Temp->next;
- if(!Temp)
- {
- "can`t malloc a space for this program!\n");
- emergency_exit();
- }
- Temp->line=0;
- Temp->next=NULL;
- Temp->read=T_0;
- "%d\t%d\n",&(Temp->line),&(Temp->read));
- }
- fclose(READ_FILE);
- return
- }
- //************************************
- // Method: Advance
- // FullName: Advance
- // Access: public
- // Returns: Token
- // Qualifier: 前进移向下一个TOKEN
- // Parameter: void
- //************************************
- MYLEX* Advance(MYLEX* current)
- {
- FILE*EAT_FILE=fopen("symtab2.txt","a");
- FILE* LEFT_FILE=fopen("detail.txt","a");
- if((!EAT_FILE)||(!LEFT_FILE))
- {
- "can`t open file");
- emergency_exit();
- }
- MYLEX* Temp=current->next;
- "you have eat the one is :%3d\n\n follow is \n",current->read);
- fprintf(EAT_FILE,
- "\nyou have eat the one is line:%3d\tToken:%3d\n",
- current->line,
- current->read);
- fprintf(LEFT_FILE,
- "\n\ncurrent eat the one is line:%3d\tToken:%3d\nfollow is\n",
- current->line,
- current->read);
- for(Temp; Temp!=NULL; Temp=Temp->next)
- {
- "line:%3d \t token=%3d\n",
- Temp->line,Temp->read);
- "line:%3d \t token=%3d\n",
- Temp->line,Temp->read);
- }
- fclose(EAT_FILE);
- fclose(LEFT_FILE);
- if(current==NULL)
- {
- "all work is to be done!,there is an error in your project!\n ");
- emergency_exit();
- }
- return
- }
- //************************************
- // Method: main
- // FullName: main
- // Access: public
- // Returns: int
- // Qualifier: 主程序
- // Parameter: int agrc
- // Parameter: char * agrv[]
- //************************************
- int main(int agrc, char*agrv[])
- {
- error_temp.line=0;
- error_temp.next=NULL;
- error_temp.read=T_0;
- "\t编译原理实验二\n\tXXXXXXXXX\n\tNEWPLAN\n");
- current=Read_file(&lex_head);
- "pause");
- if(Main_Program())
- {
- "\n\ncongratulation\tsuccessfully!\n\n");
- "del error.txt");
- }
- else
- {
- " sorry you have failed!\n");
- "error in line:%d\tTOKEN:%d\texpeced Token is :%d\n",
- error_temp.line,error_temp.read,error_TOKEN);
- }
- "pause");
- emergency_exit();/*释放空间,函数返回*/
- return
- }
- //************************************
- // Method: match
- // FullName: match
- // Access: public
- // Returns: int
- // Qualifier: 匹配
- // Parameter: Token read
- // Parameter: Token aim
- //************************************
- int
- {
- if(read==aim)
- return
- error(current,aim);
- return
- // return read==aim? SUCCESS : !SUCCESS;
- }
- //************************************
- // Method: Main_Program
- // FullName: Main_Program
- // Access: public
- // Returns: int
- // Qualifier: 开始主程序
- // Parameter: void
- //************************************
- int Main_Program(void)
- {
- if(!Block())
- return
- return
- }
- //************************************
- // Method: Block
- // FullName: Block
- // Access: public
- // Returns: int
- // Qualifier:/*分程序*/
- // Parameter: Token read
- //************************************
- int Block(void)
- {
- MYLEX* Temp=current;
- if(!match(current->read,T_BEGIN))
- {
- current=Temp;
- return
- }
- current= Advance(current);
- if(!Declarative_statement_table())
- {
- current=Temp;
- return
- }
- if(!match(current->read,T_SEM))
- {
- current=Temp;
- return
- }
- current=Advance(current);
- if(!Execute_statement_table())
- {
- current=Temp;
- return
- }
- if(!match(current->read,T_END))
- {
- current=Temp;
- return
- }
- current=Advance(current);
- return
- }
- //************************************
- // Method: Declarative_statement_table
- // FullName: Declarative_statement_table
- // Access: public
- // Returns: int
- // Qualifier:/*说明语句表*/
- // Parameter: Token read
- //************************************
- int
- {
- MYLEX* Temp=current;
- if(!Declarative_statement())
- {
- current=Temp;
- return
- }
- /*采用扩展的BNF方法消除左递归*/
- for(;;)
- {
- if(!match(current->read,T_SEM))
- break;
- Temp=current;
- current =Advance(current);
- if(!Declarative_statement())
- {
- current=Temp;
- break;
- }
- }
- return
- }
- //************************************
- // Method: Declarative_statement
- // FullName: Declarative_statement
- // Access: public
- // Returns: int
- // Qualifier:/*说明语句*/
- // Parameter: Token read
- //************************************
- int Declarative_statement(void)
- {
- MYLEX* Temp=current;
- if(Variable_declaration())
- {
- return
- }
- else
- {
- current=Temp;
- }
- if(Function_declaration())
- return
- current=Temp;
- return
- }
- //************************************
- // Method: Execute_statement_table
- // FullName: Execute_statement_table
- // Access: public
- // Returns: int
- // Qualifier:/*执行语句表*/
- // Parameter: Token read
- //************************************
- int Execute_statement_table(void)
- {
- MYLEX* Temp=current;
- if(!Execute_statement())
- {
- current=Temp;
- return
- }
- for(;;)
- {
- if(!match(current->read,T_SEM))
- break;
- Temp=current;
- current=Advance(current);
- if(!Execute_statement())
- {
- current=Temp;
- break;
- }
- }
- return
- }
- //************************************
- // Method: Execute_statement
- // FullName: Execute_statement
- // Access: public
- // Returns: int
- // Qualifier:/*执行语句*/
- // Parameter: Token read
- //************************************
- int Execute_statement(void)
- {
- MYLEX* Temp=current;
- if(Read_statement())
- {
- return
- }
- else
- {
- current=Temp;
- }
- if(Write_statement())
- {
- return
- }
- else
- {
- current=Temp;
- }
- if(Conditional_statement())
- {
- return
- }
- else
- {
- current=Temp;
- }
- if(Assignment_statement())
- {
- return
- }
- current=Temp;
- return
- }
- //************************************
- // Method: Variable_declaration
- // FullName: Variable_declaration
- // Access: public
- // Returns: int
- // Qualifier:/*变量说明*/
- // Parameter: Token read
- //************************************
- int Variable_declaration(void)
- {
- MYLEX* Temp=current;
- if(!match(current->read , T_INT))
- {
- current=Temp;
- return
- }
- current =Advance(current);
- if(Variable())
- return
- current=Temp;
- return
- }
- //************************************
- // Method: Function_declaration
- // FullName: Function_declaration
- // Access: public
- // Returns: int
- // Qualifier:/*函数说明*/
- // Parameter: Token read
- //************************************
- int Function_declaration(void)
- {
- MYLEX* Temp=current;
- if(!match(current->read, T_INT))
- {
- current=Temp;
- return
- }
- current=Advance(current);
- if(!match(current->read, T_FOUNCTION))
- {
- current=Temp;
- return
- }
- current=Advance(current);
- if(!Identifier())
- {
- current=Temp;
- return
- }
- if(!match(current->read, T_LPAR))
- {
- current=Temp;
- return
- }
- current=Advance(current);
- if(!Parameter())
- {
- current=Temp;
- return
- }
- if(!match(current->read, T_RPAR))
- {
- current=Temp;
- return
- }
- current=Advance(current);
- if(!match(current->read,T_SEM))
- {
- current=Temp;
- return
- }
- current =Advance(current);
- if(!Function())
- {
- current=Temp;
- return
- }
- return
- }
- //************************************
- // Method: Variable
- // FullName: Variable
- // Access: public
- // Returns: int
- // Qualifier:/*变量*/
- // Parameter: Token read
- //************************************
- int Variable(void)
- {
- MYLEX* Temp=current;
- if(! match(current->read,T_SYMBOL))
- {
- current=Temp;
- return
- }
- current=Advance(current);
- return
- }
- //************************************
- // Method: Identifier
- // FullName: Identifier
- // Access: public
- // Returns: int
- // Qualifier:/*标识符*/
- // Parameter: Token read
- //************************************
- int Identifier (void)
- {
- MYLEX* Temp=current;
- if(! match(current->read,T_SYMBOL))
- {
- current=Temp;
- return
- }
- current=Advance(current);
- return
- }
- //************************************
- // Method: Parameter
- // FullName: Parameter
- // Access: public
- // Returns: int
- // Qualifier:/*参数*/
- // Parameter: Token read
- //************************************
- int Parameter (void)
- {
- MYLEX* Temp=current;
- if(!Variable())
- {
- current=Temp;
- return
- }
- return
- }
- //************************************
- // Method: Function
- // FullName: Function
- // Access: public
- // Returns: int
- // Qualifier:/*函数体*/
- // Parameter: Token read
- //************************************
- int Function(void)
- {
- MYLEX* Temp=current;
- if(!match(current->read, T_BEGIN))
- {
- current=Temp;
- return
- }
- current=Advance(current);
- if(!Declarative_statement_table())
- {
- current=Temp;
- return
- }
- if(!match(current->read, T_SEM))
- {
- current=Temp;
- return
- }
- current=Advance(current);
- if(!Execute_statement_table())
- {
- current=Temp;
- return
- }
- if(!match(current->read,T_END))
- {
- current=Temp;
- return
- }
- current=Advance(current);
- return
- }
- //************************************
- // Method: Read_statement
- // FullName: Read_statement
- // Access: public
- // Returns: int
- // Qualifier:/*读语句*/
- // Parameter: Token read
- //************************************
- int Read_statement(void)
- {
- MYLEX* Temp=current;
- if(!match(current->read,T_READ))
- {
- current=Temp;
- return
- }
- current=Advance(current);
- if(!match(current->read,T_LPAR))
- {
- current=Temp;
- return
- }
- current=Advance(current);
- if(!Variable())
- {
- current=Temp;
- return
- }
- if(!match(current->read,T_RPAR))
- {
- current=Temp;
- return
- }
- current=Advance(current);
- return
- }
- //************************************
- // Method: Write_statement
- // FullName: Write_statement
- // Access: public
- // Returns: int
- // Qualifier:/*写语句*/
- // Parameter: Token read
- //************************************
- int Write_statement(void)
- {
- MYLEX* Temp=current;
- if(!match(current->read,T_WRITE))
- {
- current=Temp;
- return
- }
- current=Advance(current);
- if(!match(current->read,T_LPAR))
- {
- current=Temp;
- return
- }
- current=Advance(current);
- if(!Variable())
- {
- current=Temp;
- return
- }
- if(!match(current->read,T_RPAR))
- {
- current=Temp;
- return
- }
- current=Advance(current);
- "current to be got is:line=%d\t %d\n",current->line,current->read);
- return
- }
- //************************************
- // Method: Assignment_statement
- // FullName: Assignment_statement
- // Access: public
- // Returns: int
- // Qualifier:/*赋值语句*/
- // Parameter: Token read
- //************************************
- int Assignment_statement(void)
- {
- MYLEX* Temp=current;
- if(!Variable())
- {
- current=Temp;
- return
- }
- if(!match(current->read,T_ASSIGN))
- {
- current=Temp;
- return
- }
- current =Advance(current);
- if(Arithmetic_expression())
- return
- current=Temp;
- return
- }
- //************************************
- // Method: Conditional_statement
- // FullName: Conditional_statement
- // Access: public
- // Returns: int
- // Qualifier:/*条件语句*/
- // Parameter: Token read
- //************************************
- int Conditional_statement(void)
- {
- MYLEX* Temp=current;
- if(!match(current->read,T_IF))
- {
- current=Temp;
- return
- }
- current=Advance(current);
- if(!Conditional_expression())
- {
- current=Temp;
- return
- }
- if(!match(current->read,T_THEN))
- {
- current=Temp;
- return
- }
- current=Advance(current);
- if(!Execute_statement())
- {
- current=Temp;
- return
- }
- if(!match(current->read,T_ELSE))
- {
- current=Temp;
- return
- }
- current=Advance(current);
- if(Execute_statement())
- return
- current=Temp;
- return
- }
- //************************************
- // Method: Arithmetic_expression
- // FullName: Arithmetic_expression
- // Access: public
- // Returns: int
- // Qualifier:/*算术表达式*/
- // Parameter: Token read
- //************************************
- int Arithmetic_expression(void)
- {
- MYLEX* Temp=current;
- if(!item())
- {
- current=Temp;
- return
- }
- for(;;)
- {
- if(!match(current->read,T_SUB))
- break;
- Temp=current;
- current=Advance(current);
- if(!item())
- {
- current=Temp;
- break;
- }
- }
- return
- }
- //************************************
- // Method: item
- // FullName: item
- // Access: public
- // Returns: int
- // Qualifier:/*项*/
- // Parameter: Token read
- //************************************
- int item(void)
- {
- MYLEX* Temp=current;
- if(!Factor())
- {
- current=Temp;
- return
- }
- for(;;)
- {
- if(!match(current->read,T_MUL))
- break;
- Temp=current;
- current=Advance(current);
- if(!Factor())
- {
- current=Temp;
- break;
- }
- }
- return
- }
- //************************************
- // Method: Constant
- // FullName: Constant
- // Access: public
- // Returns: int
- // Qualifier:/*常数*/
- // Parameter: Token read
- //************************************
- int Constant(void)
- {
- MYLEX* Temp=current;
- if(!match(current->read,T_CONSTANT))
- {
- return
- }
- current=Advance(current);
- return
- }
- //************************************
- // Method: Conditional_expression
- // FullName: Conditional_expression
- // Access: public
- // Returns: int
- // Qualifier:/*条件表达式*/
- // Parameter: Token read
- //************************************
- int Conditional_expression(void)
- {
- MYLEX* Temp=current;
- if(!Arithmetic_expression())
- {
- current=Temp;
- return
- }
- "line = %d,token=%d\n");
- if(!Relational_operators())
- {
- current=Temp;
- return
- }
- if(!Arithmetic_expression())
- {
- current=Temp;
- return
- }
- return
- }
- //************************************
- // Method: Relational_operators
- // FullName: Relational_operators
- // Access: public
- // Returns: int
- // Qualifier:/*关系运算符*/
- // Parameter: Token read
- //************************************
- int Relational_operators(void)
- {
- if(match(current->read,T_L))
- {
- current=Advance(current);
- return
- }
- if(match(current->read,T_LE))
- {
- current=Advance(current);
- return
- }
- if(match(current->read,T_G))
- {
- current=Advance(current);
- return
- }
- if(match(current->read,T_GE))
- {
- current=Advance(current);
- return
- }
- if(match(current->read,T_E))
- {
- current=Advance(current);
- return
- }
- if(match(current->read,T_NE))
- {
- current=Advance(current);
- return
- }
- return
- }
- //************************************
- // Method: Factor
- // FullName: Factor
- // Access: public
- // Returns: int
- // Qualifier:/*因子*/
- // Parameter: Token read
- //************************************
- int Factor(void)
- {
- MYLEX* Temp=current;
- if(Function_call())
- {
- return
- }
- else
- {
- current=Temp;
- }
- if(Variable())
- {
- return
- }
- else
- {
- current=Temp;
- }
- if(Constant())
- {
- return
- }
- current=Temp;
- return
- }
- //************************************
- // Method: Function_call
- // FullName: Function_call
- // Access: public
- // Returns: int
- // Qualifier:/*函数调用*/
- // Parameter: Token read
- //************************************
- int Function_call(void)
- {
- MYLEX* Temp=current;
- if(!Identifier())
- {
- current=Temp;
- return
- }
- if(!match(current->read,T_LPAR))
- {
- current=Temp;
- return
- }
- current=Advance(current);
- if( !Arithmetic_expression())
- {
- current=Temp;
- return
- }
- if(!match(current->read,T_RPAR))
- {
- current=Temp;
- return
- }
- current=Advance(current);
- return
- }
结果分析
成功结果
失败结果: