武汉大学编译原理第三次作业

来源:互联网 发布:出租车司机 知乎 编辑:程序博客网 时间:2024/06/11 05:01
/************************************************************//*      copyright hanfei.wang@gmail.com                     *//*             2013.03.20                                   *//*             2013301500100 计科三班 秦贤康                *//*             2013.03.20                                   *//************************************************************/#include <ctype.h>#include <stdlib.h>#include "ast.h"/* reg -> term  reg'   reg' -> '|' term reg' | epsilon   term -> kleene term'   term' -> kleene term' | epsilon   kleene -> fac kleene'    kleene' -> * kleene' | epsilon   fac -> alpha | '(' reg ')'      2013301500100*/static char input_buffer[MAX_STATES] = "\0";FOLLOW_INDEX cindex[MAX_STATES] = {0}; /*  pos index to the corresponding character and     follow set */static char * current = input_buffer;static int pos = 0;  void next_token(void){  if ( !*current ) {    current = input_buffer;    if ( !fgets(input_buffer, MAX_STATES - 1, stdin) ) {      *current = '\0';      return;    }  } else current ++;  while ( isspace (*current) )     current ++;  //  printf("current char is %c\n", *current); }AST_PTR reg();AST_PTR reg1(AST_PTR term_left);AST_PTR term ();AST_PTR term1(AST_PTR kleene_left);AST_PTR kleene();AST_PTR kleene1( AST_PTR fac_left);AST_PTR fac();AST_PTR start(){  AST_PTR leaf, root = reg ();  pos ++;  leaf = mkLeaf('$', pos);  root = mkOpNode(Seq, root, leaf);  if ( *current != '\0' )     printf("the parser finished at %c, before the end of RE\n", *current);  return root;}//学生自己完成以下部分/* reg -> term  reg' */ AST_PTR reg(){  return reg(term());}/*  reg' -> '|' term reg' | epsilon */AST_PTR reg1(AST_PTR term_left){AST_PTR temp=term_left;    if(*current=='!'){temp=mkOpNode(Or,term_left,mkEpsilon());next_token();    }if(*current=='|'){next_token();temp=reg1(mkOpNode(Or,term_left,term()));}  return temp;}/*  term -> kleene term' */AST_PTR term (){  return term1(kleene());}/*  term' -> kleene term' | epsilon */AST_PTR term1(AST_PTR kleene_left){AST_PTR temp=kleene_left,temp1;if(*current=='\0')return kleene_left;     if(*current=='!'){ temp=mkOpNode(Seq,kleene_left,mkEpsilon()); next_token();    }else {temp1=kleene();if(temp1){temp1=mkOpNode(Seq,kleene_left,temp1);temp=term1(temp1);}}return temp;}/*  kleene -> fac kleene' */AST_PTR kleene(){return kleene1(fac());}/*   kleene' -> * kleene' | epsilon */AST_PTR kleene1( AST_PTR fac_left){AST_PTR temp=fac_left;if(*current=='!'){temp=mkOpNode(Seq,fac_left,mkEpsilon());next_token();}else if(*current=='*'&&fac_left != NULL){next_token();temp=kleene1(mkOpNode(Star,fac_left,NULL));}return temp;}/*   fac -> alpha | '(' reg ')' */AST_PTR fac(){ AST_PTR temp=NULL;if(*current=='('){next_token();temp=reg();}if(*current==')'){next_token();return NULL;}if(*current=='!')return mkEpsilon();else if(*current!='*'&&*current!='|'){ temp=mkLeaf(*current,pos++);next_token();} return temp;}


0 0
原创粉丝点击