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


/*************************************************************************
	> File Name: 2013301500100(05).cpp
	> Author: 秦贤康
	> Mail:qinxiankang@gmail.com 
	> Created Time: 2015年11月16日 星期一 17时50分37秒
        > url:http://blog.csdn.net/half_open/article/details/49869523
 ************************************************************************/
 
 /************************************************************/
/*      copyright hanfei.wang@gmail.com                     */
/*             2012.02.24                                   */
/************************************************************/

include "ast.h"

#define DEBUG 1

int  get_nullable ( AST_PTR tree )
{ 
  return (tree -> attribute).nullable;
}

SET *get_firstpos (AST_PTR tree )
{
      return (tree -> attribute).firstpos;

}

SET *get_lastpos (AST_PTR tree )
{
      return (tree -> attribute).lastpos;

}


SET *get_followpos (AST_PTR tree )
{
      return (tree -> attribute).followpos;

}

void nullable(AST_PTR tree)
{ 
    if (tree == NULL) {
            printf("attempt manipulate an empty tree!\n");
            return;
          
    }
  
    switch (tree -> op ) {
          case Star : 
            nullable ( tree ->lchild );
            (tree -> attribute).nullable = 1 ;
            return;
          case Seq :
            nullable ( tree -> lchild );
            nullable ( tree -> rchild );
            (tree -> attribute).nullable =  get_nullable (tree -> lchild) 
              &&  get_nullable (tree -> rchild) ;
            return;
          case Or :
            nullable ( tree -> lchild  );
            nullable ( tree -> rchild  );
             (tree -> attribute).nullable =  get_nullable (tree -> lchild) 
              ||  get_nullable (tree -> rchild) ;
            return;
          case Alpha : 
            (tree -> attribute).nullable = 0 ;
            return;
          case Epsilon :
            (tree -> attribute).nullable = 1;
            return;
          
    }
}

void firstpos(AST_PTR tree)
{ 
  SET * tmp;
    if (tree == NULL) {
            printf("attempt manipulate an empty tree!\n");
            return;
          
    }
  
    switch (tree -> op ) {
          case Star : 
            firstpos ( tree -> lchild  );
            tmp = newset();
            UNION (tmp, get_firstpos (tree -> lchild));
            (tree -> attribute).firstpos = tmp; 
            return;  
          case Seq :
            firstpos ( tree -> lchild  );
            firstpos ( tree -> rchild  );
            tmp = newset();
            UNION (tmp, get_firstpos (tree -> lchild));
            if (get_nullable(tree -> lchild))
              UNION (tmp, get_firstpos (tree ->rchild));
            (tree -> attribute).firstpos = tmp; 
            return;  
          case Or :
            firstpos ( tree -> lchild  );
            firstpos ( tree -> rchild  );
            tmp = newset ();
            UNION (tmp, get_firstpos (tree -> lchild));
            UNION (tmp, get_firstpos (tree -> rchild));
            (tree -> attribute).firstpos = tmp; 
            return;  
          case Alpha : 
            tmp = newset ();
            ADD (tmp, tree -> pos );
            (tree -> attribute).firstpos = tmp; 
            return;
          case Epsilon : /* Empty Set */
            tmp = newset ();
            (tree -> attribute).firstpos = tmp;
            return;
          
    }
}

void lastpos(AST_PTR tree)
{ 
  SET * tmp;
    if (tree == NULL) {
            printf("attempt manipulate an empty tree!\n");
            return;
          
    }
  
    switch (tree -> op ) {
          case Star : 
            lastpos ( tree -> lchild  );
            tmp = newset();
            UNION (tmp, get_lastpos (tree -> lchild));
            (tree -> attribute).lastpos = tmp; 
            return;  
          case Seq :
            lastpos ( tree -> lchild  );
            lastpos ( tree -> rchild  );
            tmp = newset();
            UNION (tmp, get_lastpos (tree -> rchild));
            if (get_nullable(tree -> rchild))
              UNION (tmp, get_lastpos (tree -> lchild));
            (tree -> attribute).lastpos = tmp; 
            return;  
          case Or :
            lastpos ( tree -> lchild  );
            lastpos ( tree -> rchild  );
            tmp = newset ();
            UNION (tmp, get_lastpos (tree -> lchild));
            UNION (tmp, get_lastpos (tree -> rchild));
            (tree -> attribute).lastpos = tmp; 
            return;  
          case Alpha : 
            tmp = newset ();
            ADD (tmp, tree -> pos );
            (tree -> attribute).lastpos = tmp;
            return;
          case Epsilon :
            tmp = newset ();
            (tree -> attribute).lastpos = tmp;
            return;
          
    }
}


void followpos_aux(AST_PTR tree)
{
      /* do preorder tree traversal  */
      SET * tmp = newset ();

    if (tree == NULL) {
            printf("attempt manipulate an empty tree!\n");
            return;
          
    }
      
    switch (tree -> op) {
          case Star : 
            UNION (tmp, get_followpos (tree));
            UNION (tmp, get_firstpos (tree -> lchild));
            (tree -> lchild -> attribute).followpos = tmp;
            followpos_aux ( tree -> lchild  );
            return;  
          case Seq :
            UNION (tmp, get_followpos (tree));
            (tree -> rchild -> attribute).followpos = tmp;
            tmp = newset();
            UNION (tmp, get_firstpos (tree -> rchild));
            if (get_nullable(tree -> rchild))
              UNION (tmp, get_followpos(tree));
            (tree -> lchild -> attribute).followpos = tmp;
            followpos_aux ( tree -> lchild  );
            followpos_aux ( tree -> rchild  );
            return;  
          case Or :
            UNION (tmp, get_followpos (tree));
            (tree -> rchild -> attribute).followpos = tmp;
            tmp = newset();
            UNION (tmp, get_followpos (tree));
            /* UNION (tmp, get_firstpos (tree -> rchild));*/
                (tree -> lchild -> attribute).followpos = tmp;
            followpos_aux ( tree -> lchild  );
            followpos_aux ( tree -> rchild  );
            return;  
          case Alpha :
            UNION (tmp, get_followpos( tree  ));
            cindex[tree ->pos].follow = tmp;
            (tree -> attribute).followpos = tmp;
            return;
          case Epsilon :
            UNION (tmp, get_followpos( tree  ));
            (tree -> attribute).followpos = tmp;
            return;
          
    }  

}

void followpos (AST_PTR tree)
{
      SET * tmp = newset ();
      /* add end mark $ to followpos of AST root */
      ADD (tmp, (tree ->rchild->pos));
      (tree -> attribute).followpos = tmp;
      (tree -> lchild -> attribute). followpos = tmp;
      (tree -> rchild -> attribute). followpos = newset();
      cindex[tree->rchild->pos].follow = newset();
      followpos_aux (tree -> lchild);
      return;

}

void test_limit_of_max_state(int x)
{
    if (x > MAX_STATES) {
            printf("maximum state exceeded! exit!\n");
            le, "}\n");
          fclose (dfafile);

    }

    int index_of_partition (int element, SET ** set_list, int set_length)
    {
          int i;
          for (i = 0 ; i <= set_length; i ++)
            if (MEMBER(set_list[i], elementpart_number);
                 }
                })
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值