Chinaunix首页 | 论坛 | 博客
  • 博客访问: 173589
  • 博文数量: 35
  • 博客积分: 287
  • 博客等级: 二等列兵
  • 技术积分: 353
  • 用 户 组: 普通用户
  • 注册时间: 2011-08-02 21:01
个人简介

君子务本,本立则道生。

文章分类
文章存档

2014年(4)

2013年(12)

2012年(19)

分类: C/C++

2012-05-04 08:47:58

编最近上的编译原理课呢做实验,输入一个文法,然后判断是不是LL1文法,根据清华大学张素琴老师主编的《编译原理》第二版80页的算法,写了短程序:

  1. //compile.c

  2. #include<stdio.h>
  3. #include<stdlib.h>
  4. #include<string.h>
  5. #define length 100

  6. typedef struct {
  7.     char T[length];
  8.     char first[length][length];
  9.     int len;
  10. } ter,*Ter;


  11. typedef struct {
  12.     char N[length];
  13.     char first[length][length];
  14.     char follow[length][length];
  15.     char p_emp[length];
  16.     char n_emp[length];
  17.     int len;
  18. }*NTer,nter;

  19. //define Produce data
  20. typedef struct {
  21.     char front;
  22.     char back[length];

  23. }*Pro,prox;

  24. typedef struct {
  25.     Pro pro[length];
  26.     char string_first[length][length];
  27.     char select[length][length];
  28.     int len;

  29. }*Prod,prodx;

  30. //define G

  31. typedef struct {
  32.     Ter t_symbol;
  33.     NTer n_symbol;
  34.     Prod produce;
  35.     char start_symbol;
  36. }*G,grame;

  37. int belong_Arr(char x,char arr[])
  38. {
  39.     int i=0;
  40.     while(arr[i]!='\0') {
  41.         if(arr[i]==x)
  42.             return 1;
  43.         i++;
  44.     }
  45.     return 0;
  46. }

  47. int join(char a[],char b[],int a_len,int b_len)
  48. {
  49.     int i;

  50.     for(i=0; i<b_len; i++)
  51.         if((!belong_Arr(b[i],a))&&(b[i]!='&')) {
  52.             a[a_len]=b[i];
  53.             a_len++;
  54.         }
  55.     return a_len;

  56. }

  57. int join_arr(char a[],char b[])
  58. {
  59.     int i,len;
  60.     len=0;
  61.     while(a[len]!='\0')
  62.         len++;

  63.     i=0;
  64.     while(b[i]!='\0') {
  65.         if((!belong_Arr(b[i],a))&&(b[i]!='&')) {
  66.             a[len]=b[i];
  67.             len++;
  68.         }
  69.         i++;

  70.     }
  71.     return len;
  72. }

  73. int belong_N(char x,NTer n)
  74. {
  75.     int i=0;
  76.     for(; i<n->len; i++) {
  77.         if(x==n->N[i])
  78.             return 1;
  79.     }
  80.     return 0;
  81. }

  82. int belong_T(char x,Ter t)
  83. {
  84.     int i=0;
  85.     for(; i<t->len; i++) {
  86.         if(x==t->T[i])
  87.             return 1;
  88.     }
  89.     return 0;
  90. }


  91. typedef struct {
  92.     char syb;
  93.     int tag;
  94. }*elm,el;

  95. typedef struct {
  96.     elm elms[length];
  97.     int len;
  98. }*x_table,x_tab;





  99. void find_null(Prod pr,NTer n,Ter t)
  100. {
  101.     int i=0;
  102.     int j=0;
  103.     int k=0;
  104.     int m,a;
  105.     int b;
  106.     int q;
  107.     int loop=1;
  108.     int flag[n->len];
  109.     char temp;
  110.     //作为标记,辨别产生世是否删除
  111.     int tags[pr->len];
  112.     x_table tab=(x_table)malloc(sizeof(x_tab));
  113.     Prod p=(Prod)malloc(sizeof(prodx));


  114.     p->len=pr->len;

  115.     for(i=0; i<pr->len; i++) {
  116.         p->pro[i]=(Pro)malloc(sizeof(prox));
  117.         p->pro[i]->front=pr->pro[i]->front;
  118.         strcpy(p->pro[i]->back,pr->pro[i]->back);
  119.     }

  120.     for(i=0; i<p->len; i++) {
  121.         tags[i]=1;
  122.     }

  123.     for(i=0; i<n->len; i++) {
  124.         tab->elms[i]=(elm)malloc(sizeof(el));
  125.         tab->elms[i]->syb=n->N[i];
  126.         tab->elms[i]->tag=0;
  127.     }


  128.     tab->len=n->len;

  129.     for(i=0; i<p->len; i++) {
  130.         j=0;
  131.         while((temp=p->pro[i]->back[j++])!='#') {
  132.             if(belong_T(temp,t)) {
  133.                 tags[i]=0;
  134.                 break;
  135.             }

  136.         }
  137.     }



  138.     //确定以某一非终结符为左部的产生是的个数;判断以某一非终结符为左部的所有产生是都被删除,则将此非终结符标记否 即为 2 此为 步骤2_1



  139.     for(i=0; i<tab->len; i++) {
  140.         k=0;
  141.         m=0;
  142.         for(j=0; j<p->len; j++) {
  143.             if(tab->elms[i]->syb==p->pro[j]->front) {
  144.                 k++;
  145.                 if(tags[j]==0)
  146.                     m++;
  147.             }
  148.         }
  149.         if(k==0)
  150.             continue;
  151.         else if(k==m) {
  152.             tab->elms[i]->tag=2;

  153.         }


  154.     }


  155.     //判断 右部 是不是 yi bu xiu 是则 标记 1 , 此为步骤 2_2

  156.     for(i=0; i<p->len; i++) {
  157.         if(p->pro[i]->back[0]=='#') {
  158.             tags[i]=0;
  159.             for(j=0; j<tab->len; j++) {
  160.                 if(tab->elms[j]->syb==p->pro[i]->front) {
  161.                     tab->elms[j]->tag=1;
  162.                 }
  163.             }
  164.         }
  165.     }




  166.     k=0,j=0;
  167.     for(i=0; i<tab->len; i++) {
  168.         if(tab->elms[i]->tag==1) {
  169.             n->p_emp[k]=tab->elms[i]->syb;
  170.             k++;
  171.         } else if(tab->elms[i]->tag==2) {
  172.             n->n_emp[j]=tab->elms[i]->syb;
  173.             j++;
  174.         }


  175.     }

  176.     n->p_emp[k]='\0';
  177.     n->n_emp[j]='\0';

  178.     printf("==============================================\n");
  179.     printf("==============================================\n");
  180.     printf("U---------Unknown\n");
  181.     printf("Y---------Can produce empty set\n");
  182.     printf("N---------Can't produce empty set\n");



  183.     printf("The first time scaning\n");
  184.     for(i=0; i<tab->len; i++)
  185.         printf("%c ",tab->elms[i]->syb);
  186.     printf("\n");
  187.     for(i=0; i<tab->len; i++) {
  188.         if(tab->elms[i]->tag==0)
  189.             printf("U ");
  190.         else if(tab->elms[i]->tag==1)
  191.             printf("Y ");
  192.         else
  193.             printf("N ");
  194.     }
  195.     printf("\n");

  196.     m=0,j=0,k=0;




  197.     //步骤3_1



  198.     while(loop==1) {
  199.         loop=0;
  200.         for(j=0; j<tab->len; j++) {
  201.             flag[j]=tab->elms[j]->tag;
  202.         }


  203.         for(i=0; i<p->len; i++) {

  204.             if(tags[i]==1) {
  205.                 k=0;
  206.                 while(p->pro[i]->back[k]!='#') {
  207.                     if(belong_N(p->pro[i]->back[k],n)) {

  208.                         for(j=0; j<tab->len; j++) {
  209.                             if(tab->elms[j]->syb==p->pro[i]->back[k]) {
  210.                                 if(tab->elms[j]->tag==1) {

  211.                                     m=k;
  212.                                     while('\0'!=p->pro[i]->back[m]) {
  213.                                         p->pro[i]->back[m]=p->pro[i]->back[m+1];

  214.                                         //printf("%c\n",p->pro[i]->back[m]);
  215.                                         m++;
  216.                                     }
  217.                                     k=k-1;
  218.                                     if('#'==p->pro[i]->back[0]) {

  219.                                         for(b=0; b<tab->len; b++) {
  220.                                             if(tab->elms[b]->syb==p->pro[i]->front) {
  221.                                                 tab->elms[b]->tag=1;
  222.                                                 q=b;
  223.                                             }

  224.                                         }


  225.                                         for(b=0; b<p->len; b++) {
  226.                                             if(p->pro[b]->front==tab->elms[q]->syb) {
  227.                                                 tags[b]=0;
  228.                                             }
  229.                                         }
  230.                                     }
  231.                                 }

  232.                                 else if(tab->elms[j]->tag==2) {
  233.                                     tags[i]=0;

  234.                                     b=0;
  235.                                     for(a=0; a<p->len; a++) {
  236.                                         if((p->pro[a]->front==p->pro[i]->front)&&(tags[a]==1)) {
  237.                                             tags[a]=0;
  238.                                         }
  239.                                     }
  240.                                     for(a=0; a<p->len; a++) {
  241.                                         if((p->pro[a]->front==p->pro[i]->front)&&(tags[a]==1)) {
  242.                                             b++;
  243.                                         }

  244.                                     }

  245.                                     if(b==0) {
  246.                                         for(a=0; a<tab->len; a++) {
  247.                                             if((tab->elms[a]->syb==p->pro[i]->front)&&(tab->elms[a]->tag==0)) {
  248.                                                 tab->elms[a]->tag=2;
  249.                                             }
  250.                                         }


  251.                                     }


  252.                                 }
  253.                             }


  254.                         }


  255.                     }
  256.                     k++;
  257.                 }

  258.             }
  259.         }
  260.         for(i=0; i<tab->len; i++) {
  261.             if(tab->elms[i]->tag!=flag[i]) {
  262.                 loop=1;
  263.                 break;
  264.             }
  265.         }

  266.     }

  267.     printf("The second time scaning\n");
  268.     for(i=0; i<tab->len; i++)
  269.         printf("%c ",tab->elms[i]->syb);
  270.     printf("\n");
  271.     for(i=0; i<tab->len; i++) {
  272.         if(tab->elms[i]->tag==0)
  273.             printf("U ");
  274.         else if(tab->elms[i]->tag==1)
  275.             printf("Y ");
  276.         else
  277.             printf("N ");
  278.     }


  279.     k=0,j=0,m;
  280.     for(i=0; i<tab->len; i++) {
  281.         if(tab->elms[i]->tag==1) {
  282.             //n_pro_e[k]=tab->elms[i]->syb;
  283.             n->p_emp[k]=tab->elms[i]->syb;
  284.             k++;
  285.         } else if(tab->elms[i]->tag==2) {
  286.             //npro_e[j]=tab->elms[i]->syb;
  287.             n->n_emp[j]=tab->elms[i]->syb;
  288.             j++;
  289.         }


  290.     }
  291.     n->p_emp[k]='\0';
  292.     n->n_emp[j]='\0';




  293.     return ;
  294. }
  295. void get_first_set(Prod p,NTer n,Ter t)
  296. {

  297.     int i,j,k,temp;
  298.     int flag,a,b;
  299.     int m;
  300.     int tags[n->len];
  301.     //int lock[n->len];
  302.     int loop=1;
  303.     int judge[n->len];

  304.     printf("\n==============================================\n");
  305.     printf("\nBeging finding first set\n");




  306.     //X is included in Vt,F(X)={X};
  307.     for(i=0; i<t->len; i++) {
  308.         t->first[i][0]=t->T[i];
  309.     }

  310.     printf("The terminal symnol's First Set\n");
  311.     for(i=0; i<t->len; i++)
  312.         printf("First(%c)={%c}\n",t->T[i],t->first[i][0]);


  313.     printf("\n");

  314.     for(i=0; i<n->len; i++)
  315.         tags[i]=0;
  316.     

  317.     for(i=0; i<p->len; i++) {
  318.         //X->a..... a is included in first[X]
  319.         if(belong_T(p->pro[i]->back[0],t)) {
  320.             for(j=0; j<n->len; j++) {
  321.                 if(p->pro[i]->front==n->N[j]) {
  322.                     temp=tags[j]++;
  323.                     n->first[j][temp]=p->pro[i]->back[0];

  324.                     //lock[j]=0;
  325.                     break;
  326.                 }
  327.             }
  328.         }
  329.         //X-># & is included in First[X]
  330.         else if(p->pro[i]->back[0]=='#') {
  331.             for(j=0; j<n->len; j++) {
  332.                 if(p->pro[i]->front==n->N[j]) {
  333.                     temp=tags[j]++;
  334.                     n->first[j][temp]='&';

  335.                     //    lock[j]=0;
  336.                     break;
  337.                 }
  338.             }

  339.         }
  340.     }

  341.     while(loop==1) {
  342.         loop=0;
  343.         for(i=0; i<n->len; i++)
  344.             judge[i]=tags[i];
  345.         for(i=0; i<p->len; i++) {

  346.             //flag=0;
  347.             if(p->pro[i]->back[0]=='#')
  348.                 continue;
  349.             if(belong_T(p->pro[i]->back[0],t))
  350.                 continue;
  351.             for(a=0; a<n->len; a++)
  352.                 if(n->N[a]==p->pro[i]->front)
  353.                     break;
  354.             j=0;
  355.             while('#'!=p->pro[i]->back[j]) {
  356.                 if(belong_Arr(p->pro[i]->back[j],n->p_emp)) {
  357.                     for(b=0; b<n->len; b++)
  358.                         if(n->N[b]==p->pro[i]->back[j])
  359.                             break;
  360.                     tags[a]=join(n->first[a],n->first[b],tags[a],tags[b]);
  361.                     j++;
  362.                 } else if(belong_Arr(p->pro[i]->back[j],n->n_emp)) {
  363.                     for(b=0; b<n->len; b++)
  364.                         if(n->N[b]==p->pro[i]->back[j])
  365.                             break;
  366.                     tags[a]=join(n->first[a],n->first[b],tags[a],tags[b]);

  367.                     break;
  368.                 } else if(belong_T(p->pro[i]->back[j],t)) {
  369.                     if(!belong_Arr(p->pro[i]->back[j],n->first[a])) {
  370.                         n->first[a][tags[a]++]=p->pro[i]->back[j];
  371.                     }
  372.                     break;

  373.                 }

  374.             }
  375.             if(p->pro[i]->back[j]=='#')
  376.                 if(!belong_Arr('&',n->first[a]))
  377.                     n->first[a][tags[a]++]='&';
  378.         }

  379.         for(i=0; i<n->len; i++)
  380.             if(judge[i]!=tags[i]) {
  381.                 loop=1;
  382.                 break;
  383.             }

  384.     }

  385.     printf("The not_terminal symbol's First Set\n");
  386.     for(m=0; m<n->len; m++) {
  387.         printf("First(%c)={",n->N[m]);
  388.         //printf("%d ",tags[i]);
  389.         for(j=0; j<tags[m]; j++)
  390.             printf("%c ",n->first[m][j]);

  391.         printf("}\n");
  392.     }

  393.     printf("\n");
  394.     printf("\n");
  395.     for(i=0; i<p->len; i++) {
  396.         if(p->pro[i]->back[0]=='#') {
  397.             p->string_first[i][0]='&';
  398.         }

  399.         else if(!belong_Arr(p->pro[i]->back[0],n->p_emp))
  400.             if(belong_T(p->pro[i]->back[0],t)) {
  401.                 for(j=0; j<t->len; j++)
  402.                     if(p->pro[i]->back[0]==t->T[j])
  403.                         break;
  404.                 join_arr(p->string_first[i],t->first[j]);
  405.             } else {
  406.                 for(j=0; j<n->len; j++)
  407.                     if(p->pro[i]->back[0]==n->N[j])
  408.                         break;
  409.                 join_arr(p->string_first[i],n->first[j]);
  410.             }
  411.         else {
  412.             j=0;
  413.             while(p->pro[i]->back[j]!='#') {
  414.                 if(belong_Arr(p->pro[i]->back[j],n->p_emp)) {
  415.                     for(k=0; k<n->len; k++)
  416.                         if(n->N[k]==p->pro[i]->back[j])
  417.                             break;
  418.                     temp=join_arr(p->string_first[i],n->first[k]);
  419.                 } else {
  420.                     for(k=0; k<n->len; k++)
  421.                         if(n->N[k]==p->pro[i]->back[j])
  422.                             break;
  423.                     temp=join_arr(p->string_first[i],n->first[k]);
  424.                     break;
  425.                 }
  426.                 j++;
  427.             }
  428.             if(p->pro[i]->back[j]=='#')
  429.                 p->string_first[i][temp]='&';
  430.         }
  431.     }
  432.     printf("-----------------------------------------\n");
  433.     printf("The First Set of the right of produce\n");

  434.     for(i=0; i<p->len; i++) {

  435.         printf("First(");
  436.         j=0;
  437.         if(p->pro[i]->back[0]=='#')
  438.             printf("&");
  439.         else
  440.             while(p->pro[i]->back[j]!='#') {
  441.                 printf("%c",p->pro[i]->back[j]);
  442.                 j++;
  443.             }
  444.         printf(")={");
  445.         j=0;
  446.         while(p->string_first[i][j]!='\0') {
  447.             printf("%c ",p->string_first[i][j]);
  448.             j++;
  449.         }
  450.         printf("}\n");
  451.     }
  452.     printf("\n\n");

  453.     return ;


  454. }
  455. void get_follow_set(Prod p,NTer n,Ter t,char s_syb)
  456. {
  457.     int i,j,k,len,m,x,loop;
  458.     int tags[n->len];
  459.     int flags[n->len];
  460.     for(i=0; i<n->len; i++) {
  461.         tags[i]=0;
  462.         flags[i]=0;
  463.     }
  464.     printf("=====================================================\n");

  465.     printf("start finding follow set\n");
  466.     for(i=0; i<n->len; i++)
  467.         if(s_syb==n->N[i]) {
  468.             n->follow[i][0]='#';
  469.             tags[i]++;
  470.             break;
  471.         }
  472.     loop=1;
  473.     while(loop==1) {
  474.         loop=0;
  475.         for(i=0; i<n->len; i++)
  476.             flags[i]=tags[i];
  477.         for(i=0; i<p->len; i++) {
  478.             j=0;
  479.             while(p->pro[i]->back[j]!='#') {
  480.                 if(belong_N(p->pro[i]->back[j],n)) {
  481.                     for(k=0; k<n->len; k++)
  482.                         if(n->N[k]==p->pro[i]->back[j])
  483.                             break;
  484.                     if(p->pro[i]->back[j+1]!='#') {
  485.                         if(belong_N(p->pro[i]->back[j+1],n)) {
  486.                             for(m=0; m<n->len; m++)
  487.                                 if(n->N[m]==p->pro[i]->back[j+1])
  488.                                     break;
  489.                             tags[k]=join_arr(n->follow[k],n->first[m]);
  490.                             if(belong_Arr(n->N[m],n->p_emp)) {
  491.                                 for(x=0; x<n->len; x++)
  492.                                     if(n->N[x]==p->pro[i]->front)
  493.                                         break;
  494.                                 tags[k]=join_arr(n->follow[k],n->follow[x]);

  495.                             }
  496.                         } else if(belong_T(p->pro[i]->back[j+1],t)) {
  497.                             if(!belong_Arr(p->pro[i]->back[j+1],n->follow[k])) {
  498.                                 len=strlen(n->follow[k]);
  499.                                 n->follow[k][len]=p->pro[i]->back[j+1];
  500.                                 tags[k]++;
  501.                             }
  502.                         }

  503.                     }


  504.                     else {
  505.                         for(x=0; x<n->len; x++)
  506.                             if(n->N[x]==p->pro[i]->front)
  507.                                 break;
  508.                         tags[k]=join_arr(n->follow[k],n->follow[x]);
  509.                     }
  510.                 }
  511.                 j++;
  512.             }
  513.         }

  514.         for(i=0; i<n->len; i++)
  515.             if(flags[i]!=tags[i]) {
  516.                 loop=1;
  517.                 break;
  518.             }
  519.     }


  520.     printf("The not terminal symbols follow sets are:\n");

  521.     for(i=0; i<n->len; i++) {
  522.         j=0;
  523.         printf("Follow(%c)={",n->N[i]);
  524.         while(n->follow[i][j]!='\0') {
  525.             printf("%c ",n->follow[i][j]);
  526.             j++;
  527.         }
  528.         printf("}\n");
  529.     }
  530. }
  531. void get_select_set(Prod p, NTer n, Ter t) {
  532.     int i, j, k;
  533.     int flag = 0;
  534.     for (i = 0; i < p->len; i++) {

  535.         if (p->pro[i]->back[0] == '#') {
  536.             for (k = 0; k < n->len; k++)
  537.                 if (n->N[k] == p->pro[i]->front)
  538.                     break;

  539.             join_arr(p->select[i], n->follow[k]);
  540.             continue;
  541.         }
  542.         j = 0;
  543.         flag = 0;
  544.         while (p->pro[i]->back[j] != '#') {
  545.             if (!belong_Arr(p->pro[i]->back[j], n->p_emp)) {
  546.                 //flag=1;
  547.                 break;
  548.             }
  549.             j++;
  550.         }
  551.         if (p->pro[i]->back[j] == '#') {
  552.             flag = 1;
  553.         }
  554.         if (flag == 0) {
  555.             join_arr(p->select[i], p->string_first[i]);
  556.         } else if (flag == 1) {
  557.             join_arr(p->select[i], p->string_first[i]);
  558.             for (k = 0; k < n->len; k++)
  559.                 if (n->N[k] == p->pro[i]->front)
  560.                     break;
  561.             join_arr(p->select[i], n->follow[k]);
  562.         }

  563.     }

  564.     printf("=============================================\n");
  565.     printf("\nThe select set:\n");

  566.     for (i = 0; i < p->len; i++) {
  567.         printf("select(%c->", p->pro[i]->front);
  568.         j = 0;
  569.         while (p->pro[i]->back[j] != '#') {
  570.             printf("%c", p->pro[i]->back[j]);
  571.             j++;
  572.         }
  573.         printf(")={");
  574.         j = 0;
  575.         while (p->select[i][j] != '\0') {
  576.             printf("%c ", p->select[i][j]);
  577.             j++;
  578.         }
  579.         printf("}\n");

  580.     }
  581.     printf("=============================================\n");
  582. }
  583. int isLL1(Prod p)
  584. {
  585.     int i=0;
  586.     int j;
  587.     for(i=0;i<p->len-1;i++)
  588.         for(j=i+1;j<p->len;j++){
  589.             if(p->pro[i]->front==p->pro[j]->front)
  590.             if(strcmp(p->select[i],p->select[j])==0){
  591.                 printf("\nThis is not LL1\n");
  592.                 return 0;
  593.             }
  594.         }


  595.     printf("\nThis is LL1\n");
  596.     printf("\n===========================================\n");
  597.     printf("\n===========================================\n");
  598.     return 1;
  599. }

  600. int main()
  601. {
  602.     int i=0,j=0;
  603.     char temp;
  604.     char tem[100];

  605. //char pro_e[length],npro_e[length];//the symbol can can't produce empty

  606.     G g=(G)malloc(sizeof(grame));
  607.     g->t_symbol=(Ter)malloc(sizeof(ter));
  608.     g->n_symbol=(NTer)malloc(sizeof(nter));
  609.     g->produce=(Prod)malloc(sizeof(prodx));



  610. //get not-terminal symbol
  611.     printf("Enter not-terminal\n");
  612.     while(1) {
  613.         scanf("%c",&temp);
  614.         if(temp=='$') {
  615.             g->n_symbol->len=i;
  616.             i=0;
  617.             break;
  618.         }
  619.         g->n_symbol->N[i++]=temp;

  620.     }




  621. //get terminal symbol
  622.     getchar();
  623.     printf("Enter terminal\n");
  624.     while(1) {
  625.         scanf("%c",&temp);
  626.         if(temp=='$') {
  627.             g->t_symbol->len=i;
  628.             i=0;
  629.             break;
  630.         }
  631.         g->t_symbol->T[i++]=temp;

  632.     }

  633. //get start symbol-------->S

  634.     printf("Enter the start symbol(end pressing Enter)\n");
  635.     getchar();
  636.     scanf("%c",&temp);
  637.     g->start_symbol=temp;

  638. //get produce
  639.     j=0;
  640.     printf("Enter Produce\n");

  641.     while(1) {
  642.         scanf("%s",tem);

  643.         if(tem[0]=='$') {
  644.             g->produce->len=j;
  645.             j=0;
  646.             break;
  647.         }

  648.         g->produce->pro[j]=(Pro)malloc(sizeof(prox));
  649.         g->produce->pro[j]->front=tem[0];
  650.         for(i=3; i<sizeof(tem); i++) {
  651.             g->produce->pro[j]->back[i-3]=tem[i];
  652.         }
  653.         j++;
  654.     }

  655. //printf("%s\n",g->produce->pro[0]->back);

  656.     find_null(g->produce,g->n_symbol,g->t_symbol);
  657.     get_first_set(g->produce,g->n_symbol,g->t_symbol);
  658.     get_follow_set(g->produce,g->n_symbol,g->t_symbol,g->start_symbol);
  659.     get_select_set(g->produce,g->n_symbol,g->t_symbol);
  660.     isLL1(g->produce);
  661.     return 0;

  662. }
这段代码呢,里面写了点注释,如果各位高手们看到我写的代码的不足之处,请之处,谢谢……

对了,这里附上两个测试用例:
1:EeTtF$ +*()i$ E E->Te# e->+Te# e-># T->Ft# t->*Ft# t-># F->i# F->(E)# $
2 : SACB$ ab$ S S->Ab# S->Ba# A->aC# C->A# C-># B->a# $

如果在Linux环境下的话,我们可以用管道命令来简化输入,把上面的两个例子分别放在两个文件两面,如果命名为test,则在终端输入:cat test|./compile
 
声明:此程序,我是在gcc编译环境下编译的,在windows中用VC的话运行好像是有错的,可以试试用Dev
阅读(7688) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

七星鲁王宫2018-12-19 15:59:41

小写字母是终结符 怎么会有产生式