Advertisement
Guest User

zwei Schiebezeiger, kein LR1

a guest
Jan 2nd, 2022
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 27.17 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5.  
  6. static int var_name_count=0;
  7.  
  8. void variable_countup(char *name, int do_countup)
  9. {
  10.  
  11.  char appendix[255];
  12.  char *base="rval";
  13.  if ( do_countup == 1 ) ++var_name_count;
  14.    itoa(var_name_count, appendix, 10);
  15.    
  16.  strcpy(name,base);
  17.  strcat( name, appendix );
  18.  return ;
  19.  
  20. }
  21.  
  22. int isnum(char c)
  23. {
  24.  if ( c>=0x30 && c <=0x39 ) return 1;
  25.  return 0;
  26.  
  27. }
  28.  
  29. unsigned char flushstring[1000],flushprefix[100],flushout[1000];
  30.  
  31. void sequencepoint()
  32. {
  33.  printf("%s%s",flushprefix,flushstring);   
  34.   flushstring[0]='\0', flushprefix[0]='\0',flushout[0]='\0';
  35.    
  36. }
  37.  
  38.  
  39. struct
  40. {
  41.  int type;
  42.  char text[255];
  43.  float mulvar;
  44.  float sumvar;
  45.  float onx;
  46.  
  47.  float ymulvar;
  48.  float ysumvar;
  49.  float yonx;
  50.  
  51. }token[255];
  52.  
  53. static int num_tokens;
  54.  
  55. struct
  56. {
  57.  unsigned char labelnum[20];   
  58.  unsigned int type;
  59. }labelstack[100];
  60.  
  61. unsigned int label_name_count=0;
  62. unsigned int label_stack_count=0;
  63.  
  64. unsigned char labelname[255];
  65.  
  66. void label_countup(char *name, int do_countup)
  67. {
  68.  
  69.  char appendix[255];
  70.  char *base="label";
  71.  if ( do_countup == 1 ) ++label_name_count;
  72.    itoa(label_name_count, appendix, 10);
  73.    
  74.  strcpy(name,base);
  75.  strcat( name, appendix );
  76.  return ;
  77.  
  78. }
  79.  
  80.  
  81. void remove_tokens( int position, int amount_removed )
  82. {
  83.  
  84.  while( position+ amount_removed <= num_tokens )
  85.  {
  86.   token[position].type=token[position+amount_removed].type;
  87.   strcpy( token[position].text, token[position+amount_removed].text );
  88.    token[position].sumvar=token[position+amount_removed].sumvar;
  89.    token[position].mulvar=token[position+amount_removed].mulvar;
  90.     token[position].onx=token[position+amount_removed].onx;
  91.   token[position].ysumvar=token[position+amount_removed].ysumvar;
  92.    token[position].ymulvar=token[position+amount_removed].ymulvar;
  93.     token[position].yonx=token[position+amount_removed].yonx;
  94.  
  95.   position++;
  96.  }
  97.  
  98.  num_tokens-=amount_removed;
  99.  
  100. }
  101.  
  102.  
  103.  
  104. int main(void)
  105. {
  106.  int position;
  107.  int txtcount;
  108.  int left_limit, right_limit;
  109.  int reduction_type=0;
  110.  char rvalnum[20];
  111.    float sumbuf,mulbuf;
  112.    float mulbufr, sumbufr;
  113.    float onxbuf;
  114.    float ysumbuf,ymulbuf;
  115.    float ymulbufr, ysumbufr;
  116.    float yonxbuf;
  117.          
  118.  
  119.    flushstring[0]='\0', flushprefix[0]='\0',flushout[0]='\0';
  120.  
  121.   newline:
  122.  
  123.     sequencepoint();
  124.  
  125.   position=0;
  126.   reduction_type=0;
  127.  do
  128.  {
  129.     sequencepoint();
  130.   txtcount=0;
  131.   scanf("%s", token[position].text );
  132.  
  133.   if(token[position].text[0]=='+'
  134.      && strlen(token[position].text )== 1 ) token[position].type='+',
  135.      txtcount++;
  136.   else
  137.    if(token[position].text[0]=='-'
  138.       && strlen(token[position].text )== 1 ) token[position].type='-',
  139.       txtcount++;
  140.   else
  141.    if(token[position].text[0]=='*'
  142.       && strlen(token[position].text )== 1 ) token[position].type='*',
  143.       txtcount++;
  144.   else
  145.    if(token[position].text[0]=='/'
  146.        && strlen(token[position].text )== 1 ) token[position].type='/',
  147.        txtcount++;
  148.     else
  149.    if(token[position].text[0]=='S'
  150.        && strlen(token[position].text )== 1 ) token[position].type='S',
  151.        txtcount++;
  152.   else
  153.    if(token[position].text[0]=='('
  154.        && strlen(token[position].text )== 1 ) token[position].type='(',
  155.        txtcount++;
  156.   else
  157.    if(token[position].text[0]==')'
  158.       && strlen(token[position].text )== 1 ) token[position].type=')',
  159.       txtcount++;      
  160.   else
  161.     if(token[position].text[0]=='='
  162.         && strlen(token[position].text )== 1 ) token[position].type='=',
  163.         txtcount++;
  164.   else
  165.     if(token[position].text[0]==':'
  166.         && strlen(token[position].text )== 1 ) token[position].type=':',
  167.         txtcount++;
  168.    else
  169.     if(token[position].text[0]==';'
  170.         && strlen(token[position].text )== 1 ) token[position].type=';',
  171.         txtcount++;
  172.   else
  173.    if ( strcmp(token[position].text,"if")==0)
  174.    {
  175.     token[position].type='i';
  176.    
  177.    }
  178.      else
  179.    if ( strcmp(token[position].text,"for")==0)
  180.    {
  181.     token[position].type='f';
  182.    
  183.    }
  184.      else
  185.    if ( strcmp(token[position].text,"next")==0)
  186.    {
  187.     token[position].type='x';
  188.    
  189.    }
  190.       else
  191.    if ( strcmp(token[position].text,"print")==0)
  192.    {
  193.     token[position].type='p';
  194.    
  195.    }
  196.      else
  197.    if ( strcmp(token[position].text,"input")==0)
  198.    {
  199.     token[position].type='\?';
  200.    
  201.    }
  202.   else
  203.     if ( isalpha( token[position].text[0] ) )
  204.     {
  205.      txtcount=0;
  206.      
  207.      while( isalpha(token[position].text[txtcount] ) )txtcount++;
  208.      while( isnum(token[position].text[txtcount] ) ) txtcount++;
  209.      token[position].type='v'; /* Variable */
  210.      token[position].onx=0;
  211.      token[position].yonx=0;
  212.      token[position].sumvar=0,token[position].mulvar=1,token[position].ymulvar=1;
  213.      if ( token[position].text[0]=='x') token[position].onx=1;
  214.      if ( token[position].text[0]=='y') token[position].yonx=1;
  215.      if ( token[position].text[txtcount] != '\0' ) return 1;
  216.     }
  217.   else
  218.     if ( isnum( token[position].text[0] ) )
  219.   {
  220.     txtcount=0;
  221.     while( isnum( token[position].text[txtcount] ) ) txtcount++;
  222.     token[position].type='n'; /* numerische Konstante */
  223.      token[position].ysumvar=token[position].sumvar=atoi(token[position].text),token[position].mulvar=0;
  224.       token[position].onx=0;
  225.       token[position].yonx=0;
  226.     if ( token[position].text[txtcount] != '\0' ) return 1;
  227.  
  228.   }
  229.    else { printf("Lexikalischer Fehler.\n"); getch(); return 1;}
  230.  
  231.   position++;
  232.  }while(token[position-1].type!= ';' );
  233.   num_tokens=position-1;
  234.  
  235.  
  236.  while(reduction_type!=';' )
  237.  {
  238.    reduction_type=';';
  239.   /* debug
  240.  
  241.     position=0;
  242.  
  243.     while(position < num_tokens+1 )
  244.     {
  245.      printf("Position: %d String: %s Typ: %c\n", position, token[position].text,
  246.       token[position].type );
  247.      position++;
  248.     }
  249.     getch();
  250.  
  251.     debug Ende  */
  252.  
  253.  
  254.   left_limit=0, right_limit=num_tokens;
  255.  
  256.  
  257.    position=right_limit-1;
  258.    while( position >=left_limit )
  259.    {
  260.     if ( token[position].type=='i' )
  261.       if ( token[position+1].type== 'v' || token[position+1].type=='n' || token[position+1].type=='r')
  262.       {
  263.        reduction_type='i', right_limit=position+1, left_limit=position;
  264.        break;
  265.       }
  266.      position--;
  267.      
  268.    }
  269.    
  270.    
  271.    
  272.    position=right_limit-1;
  273.    while( position >=left_limit )
  274.    {
  275.     if ( token[position].type=='f' )
  276.       if ( token[position+1].type== 'v' || token[position+1].type=='n' || token[position+1].type=='r')
  277.       {
  278.        reduction_type='f', right_limit=position+1, left_limit=position;
  279.        break;
  280.       }
  281.      position--;
  282.      
  283.    }
  284.    
  285.    
  286.  
  287.      
  288.    position=right_limit;
  289.    while( position >=left_limit )
  290.    {
  291.     if ( token[position].type=='x' )
  292.       {
  293.        reduction_type='n', right_limit=position+1, left_limit=position;
  294.        break;
  295.       }
  296.      position--;
  297.  
  298.    }
  299.  
  300.   position=left_limit;
  301.   while( token[position].type!= ')'&& position < num_tokens )
  302.   {
  303.    position++;
  304.   }
  305.    if ( token[position].type== ')' )
  306.    {
  307.     right_limit=position;
  308.     left_limit=position;
  309.  
  310.     while(token[left_limit].type != '(' && left_limit > 0 ) left_limit--;
  311.  
  312.     if(token[left_limit].type=='(' )
  313.     {
  314.      reduction_type='(';
  315.     }
  316.     else return 1;
  317.  
  318.    }
  319.  
  320.    position=left_limit;
  321.    while( position+2 <= right_limit )
  322.    {
  323.     if ( token[position].type=='v' || token[position].type=='n'  || token[position].type=='r' )
  324.       if ( token[position+1].type=='*' || token[position+1].type=='/' )
  325.         if ( token[position+2].type=='v' || token[position+2].type=='n'|| token[position+2].type=='r' )
  326.          { reduction_type=token[position+1].type;
  327.            right_limit=position+2, left_limit=position;
  328.            break; }
  329.      position++;
  330.    }
  331.    position=left_limit;
  332.    while( position+2 <= right_limit )
  333.    {
  334.     if ( token[position].type=='v' || token[position].type=='n' || token[position].type=='r' )
  335.       if ( token[position+1].type=='+' || token[position+1].type=='-' )
  336.         if ( token[position+2].type=='v' || token[position+2].type=='n' || token[position+2].type=='r')
  337.          { reduction_type=token[position+1].type;
  338.            right_limit=position+2, left_limit=position;
  339.            break; }
  340.      position++;
  341.    }
  342.    
  343.    
  344.    position=left_limit;
  345.    while( position+2 <= right_limit )
  346.    {
  347.     if ( token[position].type=='v' || token[position].type=='n' || token[position].type=='r' )
  348.       if ( token[position+1].type=='S' )
  349.         if ( token[position+2].type=='v' || token[position+2].type=='n' || token[position+2].type=='r')
  350.          { reduction_type=token[position+1].type;
  351.            right_limit=position+2, left_limit=position;
  352.            break; }
  353.      position++;
  354.    }
  355.  
  356.  
  357.    position=right_limit-2;
  358.    while( position >=left_limit )
  359.    {
  360.     if ( token[position].type=='v' )
  361.      if( token[position+1].type=='=' )
  362.       if ( token[position+2].type== 'v' || token[position+2].type=='n' || token[position+2].type=='r')
  363.       {
  364.        reduction_type='=', right_limit=position+2, left_limit=position;
  365.        break;
  366.       }
  367.      position--;
  368.  
  369.    }
  370.    
  371.  
  372.    position=right_limit-2;
  373.    while( position >=left_limit )
  374.    {
  375.     if ( token[position].type=='v'|| token[position+2].type=='n' || token[position+2].type=='r')
  376.      if( token[position+1].type==':' )
  377.       if ( token[position+2].type== 'v' || token[position+2].type=='n' || token[position+2].type=='r')
  378.       {
  379.        reduction_type=':', right_limit=position+2, left_limit=position;
  380.        break;
  381.       }
  382.      position--;
  383.  
  384.    }
  385.    
  386.    
  387.    
  388.    position=right_limit-1;
  389.    while( position >=left_limit )
  390.    {
  391.     if ( token[position].type=='p' )
  392.       if ( token[position+1].type== 'v' || token[position+1].type=='n' || token[position+1].type=='r')
  393.       {
  394.        reduction_type='p', right_limit=position+1, left_limit=position;
  395.        break;
  396.       }
  397.      position--;
  398.     }
  399.    
  400.    
  401.    position=right_limit-1;
  402.    while( position >=left_limit )
  403.    {
  404.     if ( token[position].type=='\?' )
  405.       if ( token[position+1].type== 'v' )
  406.       {
  407.        reduction_type='\?', right_limit=position+1, left_limit=position;
  408.        break;
  409.       }
  410.      position--;
  411.     }
  412.    
  413.    if ( reduction_type=='('  ) /* Klammern */
  414.    {
  415.      if ( right_limit-left_limit != 2 ) { printf("Fehlerhafter Klammerinhalt.\n");
  416.            getch(); return 1; }
  417.     if ( token[left_limit+1].type!='n' && token[left_limit+1].type!='v' &&
  418.          token[left_limit+1].type !='r' ) { printf("Syntaxfehler bei Klammerung.\n");
  419.           return 1; }
  420.  
  421.     variable_countup( rvalnum,1 );
  422.  
  423.     if( token[left_limit+1].type!= 'n' )
  424.        sprintf(flushout,"mov [%s],[%s]\n",rvalnum,token[left_limit+1].text ),strcat(flushstring,flushout);
  425.     else
  426.        sprintf(flushout,"mov [%s],%s\n",rvalnum,token[left_limit+1].text ),strcat(flushstring,flushout);
  427.  
  428.       sumbuf=token[left_limit+1].sumvar,
  429.       mulbuf=token[left_limit+1].mulvar;
  430.       onxbuf=token[left_limit+1].onx;
  431.       ysumbuf=token[left_limit+1].ysumvar,
  432.       ymulbuf=token[left_limit+1].ymulvar;
  433.       yonxbuf=token[left_limit+1].yonx;
  434.     remove_tokens(left_limit,2 );
  435.  
  436.     token[left_limit].type='r';
  437.     token[left_limit].sumvar=sumbuf;
  438.     token[left_limit].mulvar=mulbuf;
  439.     token[left_limit].onx=onxbuf;
  440.     token[left_limit].ysumvar=ysumbuf;
  441.     token[left_limit].ymulvar=ymulbuf;
  442.     token[left_limit].yonx=yonxbuf;
  443.     strcpy( token[left_limit].text, rvalnum );
  444.    }
  445.    else
  446.    if ( reduction_type=='+'  ) /* Addition */
  447.    {
  448.  
  449.     variable_countup( rvalnum,1 );
  450.  
  451.     if( token[left_limit+2].type!= 'n' )
  452.        sprintf(flushout,"mov ax,[%s]\n",token[left_limit+2].text ),strcat(flushstring,flushout);
  453.     else
  454.        sprintf(flushout,"mov ax,%s\n",token[left_limit+2].text ),strcat(flushstring,flushout);
  455.  
  456.     if( token[left_limit].type!= 'n' )
  457.        sprintf(flushout,"add ax,[%s]\n",token[left_limit].text ),strcat(flushstring,flushout);
  458.     else
  459.        sprintf(flushout,"add ax,%s\n",token[left_limit].text ),strcat(flushstring,flushout);
  460.  
  461.      sprintf(flushout,"mov [%s],ax\n", rvalnum ),strcat(flushstring,flushout);
  462.  
  463.       sumbuf=token[left_limit].sumvar+token[left_limit+2].sumvar,
  464.       mulbuf=token[left_limit].mulvar*token[left_limit].onx+token[left_limit+2].mulvar*token[left_limit+2].onx;
  465.       onxbuf=token[left_limit].mulvar*token[left_limit].onx+token[left_limit+2].mulvar*token[left_limit+2].onx;
  466.          if ( onxbuf!=0)onxbuf=1; else onxbuf=0;
  467.    
  468.       ysumbuf=token[left_limit].ysumvar+token[left_limit+2].ysumvar,
  469.       ymulbuf=token[left_limit].ymulvar*token[left_limit].yonx+token[left_limit+2].ymulvar*token[left_limit+2].yonx;
  470.       yonxbuf=token[left_limit].ymulvar*token[left_limit].yonx+token[left_limit+2].ymulvar*token[left_limit+2].yonx;
  471.          if ( yonxbuf!=0)yonxbuf=1; else yonxbuf=0;
  472.    
  473.          
  474.     remove_tokens(left_limit,2 );
  475.  
  476.     token[left_limit].type='r';
  477.     strcpy( token[left_limit].text, rvalnum );
  478.    
  479.     token[left_limit].sumvar=sumbuf;
  480.     token[left_limit].mulvar=mulbuf;
  481.     token[left_limit].onx=onxbuf;
  482.      token[left_limit].ysumvar=ysumbuf;
  483.     token[left_limit].ymulvar=ymulbuf;
  484.     token[left_limit].yonx=yonxbuf;
  485.  
  486.  
  487.    }
  488.    else
  489.    if ( reduction_type=='-'  ) /* Subtraktion */
  490.    {
  491.      variable_countup( rvalnum,1 );
  492.  
  493.     if( token[left_limit].type!= 'n' )
  494.        sprintf(flushout,"mov ax,[%s]\n",token[left_limit].text ),strcat(flushstring,flushout);
  495.     else
  496.        sprintf(flushout,"mov ax,%s\n",token[left_limit].text ),strcat(flushstring,flushout);
  497.  
  498.     if( token[left_limit+2].type!= 'n' )
  499.        sprintf(flushout,"sub ax,[%s]\n",token[left_limit+2].text ),strcat(flushstring,flushout);
  500.     else
  501.        sprintf(flushout,"sub ax,%s\n",token[left_limit+2].text ),strcat(flushstring,flushout);
  502.  
  503.      sprintf(flushout,"mov [%s],ax\n", rvalnum ),strcat(flushstring,flushout);
  504.      
  505.      
  506.       sumbuf=token[left_limit].sumvar-token[left_limit+2].sumvar;
  507.        mulbuf=token[left_limit].mulvar*token[left_limit].onx-token[left_limit+2].mulvar*token[left_limit+2].onx;
  508.        onxbuf=token[left_limit].mulvar*token[left_limit].onx+token[left_limit+2].mulvar*token[left_limit+2].onx;
  509.           if ( onxbuf!=0)onxbuf=1; else onxbuf=0;
  510.       ysumbuf=token[left_limit].ysumvar-token[left_limit+2].ysumvar;
  511.        ymulbuf=token[left_limit].ymulvar*token[left_limit].yonx-token[left_limit+2].ymulvar*token[left_limit+2].yonx;
  512.        yonxbuf=token[left_limit].ymulvar*token[left_limit].yonx+token[left_limit+2].ymulvar*token[left_limit+2].yonx;
  513.           if ( yonxbuf!=0)yonxbuf=1; else yonxbuf=0;
  514.          
  515.              
  516.     remove_tokens(left_limit,2 );
  517.  
  518.    
  519.     token[left_limit].type='r';
  520.     strcpy( token[left_limit].text, rvalnum );
  521.     token[left_limit].sumvar=sumbuf;
  522.     token[left_limit].mulvar=mulbuf;
  523.     token[left_limit].onx=onxbuf;
  524.     token[left_limit].ysumvar=ysumbuf;
  525.     token[left_limit].ymulvar=ymulbuf;
  526.     token[left_limit].yonx=yonxbuf;
  527.    
  528.  
  529.    }
  530.    else
  531.    if ( reduction_type=='*'  ) /* Multiplikation */
  532.    {
  533.      variable_countup( rvalnum,1 );
  534.  
  535.     if( token[left_limit+2].type!= 'n' )
  536.        sprintf(flushout,"mov ax,[%s]\n",token[left_limit+2].text ),strcat(flushstring,flushout);
  537.     else
  538.        sprintf(flushout,"mov ax,%s\n",token[left_limit+2].text ),strcat(flushstring,flushout);
  539.  
  540.     if( token[left_limit].type!= 'n' )
  541.        sprintf(flushout,"mul ax,[%s]\n",token[left_limit].text ),strcat(flushstring,flushout);
  542.     else
  543.        sprintf(flushout,"mul ax,%s\n",token[left_limit].text ),strcat(flushstring,flushout);
  544.  
  545.      sprintf(flushout,"mov [%s],ax\n", rvalnum ),strcat(flushstring,flushout);
  546.  
  547.  
  548.       sumbuf=(token[left_limit].sumvar)*(token[left_limit+2].sumvar),
  549.       mulbuf=((token[left_limit].sumvar+token[left_limit].mulvar))*((token[left_limit+2].sumvar+token[left_limit+2].mulvar));
  550.      
  551.       if ( token[left_limit+2].onx==1&&token[left_limit+2].sumvar!=0)mulbuf-=(token[left_limit+2].sumvar)*
  552.                                                                             ((token[left_limit].sumvar+token[left_limit].mulvar));
  553.       if ( token[left_limit].onx==1&&token[left_limit].sumvar!=0)mulbuf-=(token[left_limit].sumvar)*
  554.                                                                             ((token[left_limit+2].sumvar+token[left_limit+2].mulvar));
  555.      
  556.       if ( token[left_limit].onx==0&&token[right_limit].onx==0)onxbuf=0; else onxbuf=1;
  557.    
  558.    
  559.       ysumbuf=(token[left_limit].ysumvar)*(token[left_limit+2].ysumvar),
  560.       ymulbuf=((token[left_limit].ysumvar+token[left_limit].ymulvar))*((token[left_limit+2].ysumvar+token[left_limit+2].ymulvar));
  561.      
  562.       if ( token[left_limit+2].yonx==1&&token[left_limit+2].ysumvar!=0)ymulbuf-=(token[left_limit+2].ysumvar)*
  563.                                                                             ((token[left_limit].ysumvar+token[left_limit].ymulvar));
  564.       if ( token[left_limit].yonx==1&&token[left_limit].ysumvar!=0)ymulbuf-=(token[left_limit].ysumvar)*
  565.                                                                             ((token[left_limit+2].ysumvar+token[left_limit+2].ymulvar));
  566.      
  567.       if ( token[left_limit].yonx==0&&token[right_limit].yonx==0)yonxbuf=0; else yonxbuf=1;
  568.    
  569.    
  570.    
  571.     remove_tokens(left_limit,2 );
  572.  
  573.     token[left_limit].type='r';
  574.     strcpy( token[left_limit].text, rvalnum );
  575.    
  576.     token[left_limit].sumvar=sumbuf;
  577.     token[left_limit].mulvar=mulbuf;
  578.     token[left_limit].onx=onxbuf;
  579.  
  580.     token[left_limit].ysumvar=ysumbuf;
  581.     token[left_limit].ymulvar=ymulbuf;
  582.     token[left_limit].yonx=yonxbuf;
  583.  
  584.    
  585.    }
  586.    else
  587.    if ( reduction_type=='/'  ) /* Division */
  588.    {
  589.      variable_countup( rvalnum,1 );
  590.  
  591.     if( token[left_limit].type!= 'n' )
  592.        sprintf(flushout,"mov ax,[%s]\n",token[left_limit].text ),strcat(flushstring,flushout);
  593.     else
  594.        sprintf(flushout,"mov ax,%s\n",token[left_limit].text ),strcat(flushstring,flushout);
  595.  
  596.     if( token[left_limit+2].type!= 'n' )
  597.        sprintf(flushout,"div ax,[%s]\n",token[left_limit+2].text ),strcat(flushstring,flushout);
  598.     else
  599.        sprintf(flushout,"div ax,%s\n",token[left_limit+2].text ),strcat(flushstring,flushout);
  600.  
  601.      sprintf(flushout,"mov [%s],ax\n", rvalnum ),strcat(flushstring,flushout);
  602.  
  603.    
  604.    
  605.         if ( token[left_limit+2].sumvar!=0&&token[left_limit+2].mulvar!=0)
  606.         {
  607.       sumbuf=(token[left_limit].sumvar/token[left_limit+2].sumvar)*token[left_limit].mulvar*token[left_limit].onx/token[left_limit+2].mulvar*token[left_limit+2].onx,
  608.       mulbuf=(token[left_limit].sumvar/token[left_limit+2].sumvar)*token[left_limit].mulvar*token[left_limit].onx/token[left_limit+2].mulvar*token[left_limit+2].onx;
  609.        } else sumbuf=-1, mulbuf=-1;
  610.    
  611.         if ( token[left_limit+2].ysumvar!=0&&token[left_limit+2].ymulvar!=0)
  612.         {
  613.          ysumbuf=(token[left_limit].ysumvar/token[left_limit+2].ysumvar)*token[left_limit].ymulvar*token[left_limit].yonx/token[left_limit+2].ymulvar*token[left_limit+2].yonx,
  614.          ymulbuf=(token[left_limit].ysumvar/token[left_limit+2].ysumvar)*token[left_limit].ymulvar*token[left_limit].yonx/token[left_limit+2].ymulvar*token[left_limit+2].yonx;
  615.        } else ysumbuf=-1, ymulbuf=-1;
  616.    
  617.    
  618.     remove_tokens(left_limit,2 );
  619.  
  620.     token[left_limit].type='r';
  621.     strcpy( token[left_limit].text, rvalnum );
  622.      
  623.      token[left_limit].sumvar=sumbuf;
  624.     token[left_limit].mulvar=mulbuf;
  625.     token[left_limit].onx=(mulbuf > 0 ? 1 : 0 );
  626.      token[left_limit].ysumvar=ysumbuf;
  627.     token[left_limit].ymulvar=ymulbuf;
  628.     token[left_limit].yonx=(ymulbuf > 0 ? 1 : 0 );
  629.    
  630.    }
  631.    else
  632.    if ( reduction_type=='S'  ) /* Algebra-Solve */
  633.    {
  634.      variable_countup( rvalnum,1 );
  635.  
  636.          
  637.        mulbuf=token[left_limit].mulvar*token[left_limit].onx,mulbufr=token[left_limit+2].mulvar*token[left_limit+2].onx;
  638.        sumbuf=token[left_limit].sumvar,sumbufr=token[left_limit+2].sumvar;
  639.          
  640.        ymulbuf=token[left_limit].ymulvar*token[left_limit].yonx,ymulbufr=token[left_limit+2].ymulvar*token[left_limit+2].yonx;
  641.        ysumbuf=token[left_limit].ysumvar,ysumbufr=token[left_limit+2].ysumvar;
  642.          
  643.        printf("%f %f %f %f  y: %f %f %f %f\n",sumbuf,sumbufr,mulbuf,mulbufr,ysumbuf,ysumbufr,ymulbuf,ymulbufr);
  644.        
  645.        mulbuf-=mulbufr;
  646.        sumbufr-=sumbuf;
  647.        
  648.        ysumbuf=ysumbufr-ysumbuf;
  649.        ymulbuf=ymulbufr-ymulbuf;
  650.        
  651.        
  652.        if ( mulbuf!=0) sumbufr/=mulbuf,ysumbuf/=mulbuf,ymulbuf/=mulbuf;
  653.      
  654.      sprintf(flushout,"mov ax,%d\n", (int)sumbufr ),strcat(flushstring,flushout);
  655.      sprintf(flushout,"mov [%s],ax\n", rvalnum ),strcat(flushstring,flushout);
  656.  
  657.     remove_tokens(left_limit,2 );
  658.  
  659.     token[left_limit].type='r';
  660.     token[left_limit].sumvar=ysumbuf;
  661.     token[left_limit].mulvar=ymulbuf;
  662.     token[left_limit].onx=1, token[left_limit].yonx=0;
  663.     strcpy( token[left_limit].text, rvalnum );
  664.  
  665.    }
  666.    else
  667.    if ( reduction_type== '='  ) /* Zuweisung */
  668.    {
  669.      variable_countup( rvalnum,1 );
  670.  
  671.      if ( token[left_limit].type=='r' ){  printf("Zuweisung zu R-Wert !\n"); return 1; }
  672.  
  673.     if( token[left_limit+2].type!= 'n' )
  674.        sprintf(flushout,"mov ax,[%s]\n"
  675.               "mov [%s],ax\n",token[left_limit+2].text, token[left_limit].text ),strcat(flushstring,flushout);
  676.     else
  677.        sprintf(flushout,"mov ax,%s\n"
  678.               "mov [%s],ax\n",token[left_limit+2].text, token[left_limit].text ),strcat(flushstring,flushout);
  679.  
  680.      sprintf(flushout,"mov [%s],ax\n", rvalnum ),strcat(flushstring,flushout);
  681.  
  682.     remove_tokens(left_limit,2 );
  683.  
  684.     token[left_limit].type='r';
  685.     strcpy( token[left_limit].text, rvalnum );
  686.  
  687.    }
  688.    else
  689.    if ( reduction_type== ':'  ) /* Vergleich */
  690.    {
  691.      variable_countup( rvalnum,1 );
  692.  
  693.    
  694.     if( token[left_limit+2].type!= 'n' )
  695.        sprintf(flushout,"mov ax,[%s]\n"
  696.               "cmp [%s],ax\n",token[left_limit+2].text, token[left_limit].text ),strcat(flushstring,flushout);
  697.     else
  698.        sprintf(flushout,"mov ax,%s\n"
  699.               "cmp ax,[%s]\n"
  700.               "mov ax,0\n"
  701.               "repz mov ax,1\n",token[left_limit+2].text, token[left_limit].text ),strcat(flushstring,flushout);
  702.  
  703.      sprintf(flushout,"mov [%s],ax\n", rvalnum ),strcat(flushstring,flushout);
  704.  
  705.     remove_tokens(left_limit,2 );
  706.  
  707.     token[left_limit].type='r';
  708.     strcpy( token[left_limit].text, rvalnum );
  709.  
  710.    }
  711.    else
  712.    if ( reduction_type== 'p'  ) /* Ausgabe */
  713.    {
  714.      variable_countup( rvalnum,1 );
  715.  
  716.     if( token[left_limit+1].type!= 'n' )
  717.        sprintf(flushout,"\nPRINT [%s]\n",token[left_limit+1].text),strcat(flushstring,flushout);
  718.      else
  719.        sprintf(flushout,"\nPRINT %s\n",token[left_limit+1].text),strcat(flushstring,flushout);
  720.      
  721.     remove_tokens(left_limit,1 );
  722.  
  723.     token[left_limit].type='r';
  724.     strcpy( token[left_limit].text, rvalnum );
  725.  
  726.    }
  727.    
  728.    else
  729.    if ( reduction_type== '\?'  ) /* Vergleich */
  730.    {
  731.      variable_countup( rvalnum,1 );
  732.  
  733.      if ( token[left_limit].type=='r' || token[left_limit].type=='n' ){  printf("Zuweisung zu R-Wert oder Konstante!\n"); return 1; }
  734.  
  735.     if( token[left_limit+1].type== 'v' )
  736.        sprintf(flushout,"INPUT [%s]\n",token[left_limit+1].text),strcat(flushstring,flushout);
  737.        
  738.     remove_tokens(left_limit,1 );
  739.  
  740.     token[left_limit].type='r';
  741.     strcpy( token[left_limit].text, rvalnum );
  742.    
  743.    }
  744.    else
  745.    if ( reduction_type=='i')
  746.    {
  747.     labelstack[label_stack_count].type='i';
  748.     label_countup(labelstack[label_stack_count].labelnum,1);
  749.     label_stack_count++;
  750.  
  751.     if ( token[left_limit+1].type=='n')
  752.     sprintf(flushout,"cmp %s,TRUE\n"
  753.           "jz %s\n",token[left_limit+1].text,
  754.            labelstack[label_stack_count-1].labelnum),strcat(flushstring,flushout);
  755.       else
  756.       sprintf(flushout,"cmp [%s],TRUE\n"
  757.            "jz %s\n",token[left_limit+1].text,
  758.            labelstack[label_stack_count-1].labelnum),strcat(flushstring,flushout);
  759.        
  760.     remove_tokens(left_limit,1 );
  761.      token[left_limit].type='c';
  762.    
  763.    }
  764.    else if ( reduction_type=='f')
  765.    {
  766.     labelstack[label_stack_count].type='f';
  767.     label_countup(labelstack[label_stack_count].labelnum,1);
  768.     label_stack_count++;
  769.     labelstack[label_stack_count].type='f';
  770.     label_countup(labelstack[label_stack_count].labelnum,1);
  771.     label_stack_count++;
  772.     if ( token[left_limit+1].type=='n')
  773.     sprintf(flushout,"\n:%s\n",labelstack[label_stack_count-2].labelnum),strcat(flushprefix,flushout),
  774.            
  775.             sprintf(flushout,
  776.             "cmp %s,TRUE\n"
  777.             "jz %s\n",token[left_limit+1].text,labelstack[label_stack_count-1].labelnum),strcat(flushstring,flushout);
  778.    else
  779.     sprintf(flushprefix,"\n:%s\n",labelstack[label_stack_count-2].labelnum),
  780.       sprintf(flushout,
  781.              "cmp [%s],TRUE\n"
  782.             "jz %s\n",token[left_limit+1].text,labelstack[label_stack_count-1].labelnum),strcat(flushstring,flushout);
  783.        
  784.           remove_tokens(left_limit,1 );
  785.      token[left_limit].type='c';
  786.    
  787.            
  788.    }
  789.    else if ( reduction_type=='n')
  790.    {
  791.     if ( label_stack_count==0 ) return;
  792.    if(label_stack_count<2)
  793.        if(labelstack[label_stack_count-1].type=='f') return;
  794.    
  795.     if ( labelstack[label_stack_count-1].type=='i')sprintf(flushout,":%s\n",labelstack[label_stack_count-1].labelnum),strcat(flushstring,flushout),
  796.        label_stack_count--;
  797.     else
  798.     if ( labelstack[label_stack_count-1].type=='f')sprintf(flushout,"\njmp %s\n"
  799.                                                            "\n%s:\n",labelstack[label_stack_count-2].labelnum,
  800.                                                            labelstack[label_stack_count-1].labelnum),strcat(flushstring,flushout),
  801.                                                            label_stack_count-=2 ;
  802.     else return;
  803.    
  804.      token[left_limit].type='c';
  805.    
  806.    }
  807.    else if ( reduction_type==';')
  808.    {
  809.     if ( right_limit> 1 ) return;
  810.     remove_tokens(left_limit,1 );
  811.     goto newline;
  812.    
  813.    }
  814.    else
  815.    {
  816.     if( num_tokens > 1 ) { printf("Operator fehlt.\n"); getch(); return 1; }
  817.     else if ( num_tokens==1 )
  818.        if( ( token[0].type!= 'v' && token[0].type!= 'r' &&
  819.           token[0].type!='n') || token[1].type != ';' )
  820.           { printf("Operator fehlt.\n"); getch(); return 1; }
  821.  
  822.     if ( num_tokens==0 && token[0].type!=';' ) { printf("Syntaxfehler.\n");
  823.           getch(); return 1; }
  824.  
  825.    }
  826.  
  827.   }
  828.   getch();
  829. }
  830.  
  831. /*
  832. this is crappier than Cobol, but anyway...
  833.  
  834. Test run ...
  835.  
  836. x = 0 ;
  837. y = 0 ;
  838. xc = 0 ;
  839. yc = 0 ;
  840. for ( yc : 0 ) ;
  841. x = 0 ;
  842. for ( xc : 0 ) ;
  843. x = x + 1 ;
  844. if ( x : 10 ) ;
  845. xc = 1 ;
  846. next ;
  847. screen = 1 ;
  848. next ;
  849. xc = 0 ;
  850. y = y + 1 ;
  851. if ( y : 100 ) ;
  852. yc = 1 ;
  853. next ;
  854. next ;
  855. next ;
  856.  
  857.  
  858. produced...
  859.  
  860. mov ax,0
  861. mov [x],ax
  862. mov [rval1],ax
  863. mov ax,0
  864. mov [y],ax
  865. mov [rval2],ax
  866. mov ax,0
  867. mov [xc],ax
  868. mov [rval3],ax
  869. mov ax,0
  870. mov [yc],ax
  871. mov [rval4],ax
  872.  
  873. :label1
  874. mov ax,0
  875. cmp ax,[yc]
  876. mov ax,0
  877. repz mov ax,1
  878. mov [rval5],ax
  879. mov [rval6],[rval5]
  880. cmp [rval6],TRUE
  881. jz label2
  882. mov ax,0
  883. mov [x],ax
  884. mov [rval7],ax
  885.  
  886. :label3
  887. mov ax,0
  888. cmp ax,[xc]
  889. mov ax,0
  890. repz mov ax,1
  891. mov [rval8],ax
  892. mov [rval9],[rval8]
  893. cmp [rval9],TRUE
  894. jz label4
  895. mov ax,1
  896. add ax,[x]
  897. mov [rval10],ax
  898. mov ax,[rval10]
  899. mov [x],ax
  900. mov [rval11],ax
  901. mov ax,10
  902. cmp ax,[x]
  903. mov ax,0
  904. repz mov ax,1
  905. mov [rval12],ax
  906. mov [rval13],[rval12]
  907. cmp [rval13],TRUE
  908. jz label5
  909. mov ax,1
  910. mov [xc],ax
  911. mov [rval14],ax
  912. :label5
  913. mov ax,1
  914. mov [screen],ax
  915. mov [rval15],ax
  916.  
  917. jmp label3
  918.  
  919. label4:
  920. mov ax,0
  921. mov [xc],ax
  922. mov [rval16],ax
  923. mov ax,1
  924. add ax,[y]
  925. mov [rval17],ax
  926. mov ax,[rval17]
  927. mov [y],ax
  928. mov [rval18],ax
  929. mov ax,100
  930. cmp ax,[y]
  931. mov ax,0
  932. repz mov ax,1
  933. mov [rval19],ax
  934. mov [rval20],[rval19]
  935. cmp [rval20],TRUE
  936. jz label6
  937. mov ax,1
  938. mov [yc],ax
  939. mov [rval21],ax
  940. :label6
  941.  
  942. jmp label1
  943.  
  944. label2:
  945. */
  946.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement