Guest User

My thing

a guest
Jul 17th, 2011
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 12.31 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4.  
  5. typedef struct{
  6.     char lexeme[11];
  7.     int tokentype;
  8.     int number;
  9.  
  10.     //type 0 = number
  11.     //type 1 = identifier
  12.     int type;
  13. }LexemeTable;
  14.  
  15. //function prototype
  16. int tellme (char*);
  17.  
  18. main(){
  19.     FILE* fp;
  20.     int filesize=-1,byteCounter=0;
  21.     int found=0;
  22.     int identCounter=0,numCounter=0,infoCounter=0;
  23.     int choice;
  24.     int i=0;
  25.     char ident[100] = {'\0'};
  26.     char *bytes;
  27.     LexemeTable *info;
  28.     LexemeTable tempInfo;
  29.  
  30.     info = (LexemeTable*) malloc(1*sizeof (LexemeTable));
  31.  
  32.     fp = fopen("input.txt","r");
  33.  
  34.     while( !(feof(fp))){
  35.         fgetc(fp);
  36.         filesize++;
  37.     }
  38.  
  39.  
  40.     //this makes it possible to see everything in the file with an array named bytes
  41.     fseek(fp,0,SEEK_SET);
  42.  
  43.     bytes = malloc(filesize);
  44.  
  45.     for(i=0;i<filesize;i++){
  46.         bytes[i] = fgetc(fp);
  47.     }
  48.  
  49.     //this prints out the input
  50.     i=0;
  51.     while(i<filesize){
  52.         printf("%c",bytes[i]);
  53.         i++;
  54.     }
  55.  
  56.     printf("\n \n \n");
  57.  
  58.     //byteCounter is the way we traverse through the array
  59.     //infoCounter is the counter that tells me how many tokens there are
  60.     while(byteCounter<=filesize){
  61.  
  62.         //this checks if it's a comment and skips everything
  63.         if(bytes[byteCounter] == '/' && bytes[byteCounter+1] == '*'){
  64.             while( (bytes[byteCounter] != '*') || (bytes[byteCounter+1] != '/')){
  65.                 byteCounter++;
  66.             }
  67.             byteCounter += 2;
  68.         }
  69.  
  70.         //these checks if it's any of the reserved words
  71.  
  72.         if(strcmp(ident,"begin")==0 && (bytes[byteCounter]==' '|| bytes[byteCounter]== 15 || bytes[byteCounter]== 10 || bytes[byteCounter]== 9 || filesize == byteCounter)){
  73.             strcpy(info[infoCounter].lexeme,"begin");
  74.             info[infoCounter].tokentype = 21;
  75.             infoCounter++;
  76.             found = 1;
  77.             byteCounter--;
  78.  
  79.         }else if(strcmp(ident,"end")==0 && (bytes[byteCounter]==' '|| bytes[byteCounter]==';' ||  bytes[byteCounter]=='.'||  bytes[byteCounter]== 15 || bytes[byteCounter]== 10 || bytes[byteCounter]== 9 || filesize == byteCounter)){
  80.             strcpy(info[infoCounter].lexeme,"end");
  81.             info[infoCounter].tokentype = 22;
  82.             infoCounter++;
  83.             found = 1;
  84.             byteCounter--;
  85.         }else if(strcmp(ident,"null")==0 && (bytes[byteCounter]==' '|| bytes[byteCounter]== 15 || bytes[byteCounter]== 10 || bytes[byteCounter]== 9 || filesize == byteCounter)){
  86.             strcpy(info[infoCounter].lexeme,"null");
  87.             info[infoCounter].tokentype = 1;
  88.             infoCounter++;
  89.             found = 1;
  90.             byteCounter--;
  91.  
  92.         }else if(strcmp(ident,"if")==0 && (bytes[byteCounter]==' '|| bytes[byteCounter]== 15 || bytes[byteCounter]== 10 || bytes[byteCounter]== 9 || filesize == byteCounter)){
  93.             strcpy(info[infoCounter].lexeme,"if");
  94.             info[infoCounter].tokentype = 23;
  95.             infoCounter++;
  96.             found = 1;
  97.             byteCounter--;
  98.  
  99.         }else if(strcmp(ident,"then")==0 && (bytes[byteCounter]==' '|| bytes[byteCounter]== 15 || bytes[byteCounter]== 10 || bytes[byteCounter]== 9 || filesize == byteCounter)){
  100.             strcpy(info[infoCounter].lexeme,"then");
  101.             info[infoCounter].tokentype = 24;
  102.             infoCounter++;
  103.             found = 1;
  104.             byteCounter--;
  105.  
  106.         }else if(strcmp(ident,"while")==0 && (bytes[byteCounter]==' '|| bytes[byteCounter]== 15 || bytes[byteCounter]== 10 || bytes[byteCounter]== 9 || filesize == byteCounter)){
  107.             strcpy(info[infoCounter].lexeme,"while");
  108.             info[infoCounter].tokentype = 25;
  109.             infoCounter++;
  110.             found = 1;
  111.             byteCounter--;
  112.  
  113.         }else if(strcmp(ident,"do")==0 && (bytes[byteCounter]==' '|| bytes[byteCounter]== 15 || bytes[byteCounter]== 10 || bytes[byteCounter]== 9 || filesize == byteCounter)){
  114.             strcpy(info[infoCounter].lexeme,"do");
  115.             info[infoCounter].tokentype = 26;
  116.             infoCounter++;
  117.             found = 1;
  118.             byteCounter--;
  119.  
  120.         }else if(strcmp(ident,"call")==0 && (bytes[byteCounter]==' '|| bytes[byteCounter]== 15 || bytes[byteCounter]== 10 || bytes[byteCounter]== 9 || filesize == byteCounter)){
  121.             strcpy(info[infoCounter].lexeme,"call");
  122.             info[infoCounter].tokentype = 27;
  123.             infoCounter++;
  124.             found = 1;
  125.             byteCounter--;
  126.  
  127.         }else if(strcmp(ident,"const")==0 && (bytes[byteCounter]==' '|| bytes[byteCounter]== 15 || bytes[byteCounter]== 10 || bytes[byteCounter]== 9 || filesize == byteCounter)){
  128.             strcpy(info[infoCounter].lexeme,"const");
  129.             info[infoCounter].tokentype = 28;
  130.             infoCounter++;
  131.             found = 1;
  132.             byteCounter--;
  133.  
  134.         }else if(strcmp(ident,"int")==0 && (bytes[byteCounter]==' '|| bytes[byteCounter]== 15 || bytes[byteCounter]== 10 || bytes[byteCounter]== 9 || filesize == byteCounter)){
  135.             strcpy(info[infoCounter].lexeme,"int");
  136.             info[infoCounter].tokentype = 29;
  137.             infoCounter++;
  138.             found = 1;
  139.             byteCounter--;
  140.  
  141.         }else if(strcmp(ident,"procedure")==0 && (bytes[byteCounter]==' '|| bytes[byteCounter]== 15 || bytes[byteCounter]== 10 || bytes[byteCounter]== 9 || filesize == byteCounter)){
  142.             strcpy(info[infoCounter].lexeme,"procedure");
  143.             info[infoCounter].tokentype = 30;
  144.             infoCounter++;
  145.             found = 1;
  146.             byteCounter--;
  147.  
  148.         }else if(strcmp(ident,"out")==0 && (bytes[byteCounter]==' '|| bytes[byteCounter]== 15 || bytes[byteCounter]== 10 || bytes[byteCounter]== 9 || filesize == byteCounter)){
  149.             strcpy(info[infoCounter].lexeme,"out");
  150.             info[infoCounter].tokentype = 31;
  151.             infoCounter++;
  152.             found = 1;
  153.             byteCounter--;
  154.  
  155.         }else if(strcmp(ident,"in")==0 && (bytes[byteCounter]==' '|| bytes[byteCounter]== 15 || bytes[byteCounter]== 10 || bytes[byteCounter]== 9 || filesize == byteCounter)){
  156.             strcpy(info[infoCounter].lexeme,"in");
  157.             info[infoCounter].tokentype = 32;
  158.             infoCounter++;
  159.             found = 1;
  160.             byteCounter--;
  161.  
  162.         }else if(strcmp(ident,"else")==0 && (bytes[byteCounter]==' '|| bytes[byteCounter]== 15 || bytes[byteCounter]== 10 || bytes[byteCounter]== 9 || filesize == byteCounter)){
  163.             strcpy(info[infoCounter].lexeme,"else");
  164.             info[infoCounter].tokentype = 33;
  165.             infoCounter++;
  166.             found = 1;
  167.             byteCounter--;
  168.  
  169.         }else if( (strcmp(ident,"odd")==0 || strcmp(ident,"ODD")==0) && (bytes[byteCounter]==' '|| bytes[byteCounter]== 15 || bytes[byteCounter]== 10 || bytes[byteCounter]== 9 || filesize == byteCounter)){
  170.             strcpy(info[infoCounter].lexeme,"odd");
  171.             info[infoCounter].tokentype = 8;
  172.             infoCounter++;
  173.             found = 1;
  174.             byteCounter--;
  175.         }
  176.  
  177.  
  178.         if(found==0){
  179.  
  180.             //this big if statement just makes it clear where the identifiers/numbers are
  181.             //and what is it's value/string
  182.             if(    bytes[byteCounter] == ' ' || bytes[byteCounter] == '+'|| bytes[byteCounter] == '-'|| bytes[byteCounter] == '*'|| bytes[byteCounter] == '/'
  183.                 || bytes[byteCounter] == '='|| (bytes[byteCounter] == '!' && bytes[byteCounter+1] == '=')|| bytes[byteCounter]== 15 || bytes[byteCounter]== 10 || bytes[byteCounter]== 9 ||bytes[byteCounter] == '<'|| bytes[byteCounter] == '>'|| bytes[byteCounter] == '('
  184.                 || bytes[byteCounter] == ')'|| bytes[byteCounter] == ','|| bytes[byteCounter] == ';'|| bytes[byteCounter] == '.'|| (bytes[byteCounter] == ':' && bytes[byteCounter] == '='  || filesize == byteCounter)){
  185.  
  186.                     if(ident[0] > 47 && ident[0] < 58 || ident[0]>64 && ident[0]< 91 || ident[0]>96 && ident[0]< 123){
  187.                         //functions tells me if its a number or identifier
  188.                         //0 if number
  189.                         //1 if identifier
  190.                         choice = tellme(ident);
  191.  
  192.                         if(choice == 0){
  193.                             info[infoCounter].number = atoi(ident);
  194.                             info[infoCounter].tokentype = 3;
  195.                             info[infoCounter].type = 0;
  196.  
  197.                         }else{
  198.                             strcpy(info[infoCounter].lexeme,ident);
  199.                             info[infoCounter].tokentype = 2;
  200.                             info[infoCounter].type = 1;
  201.                         }
  202.  
  203.                         infoCounter++;
  204.                     }
  205.             }
  206.  
  207.             switch(bytes[byteCounter]){
  208.             case '\n':
  209.             case ' ':
  210.                 found = 1;
  211.                 break;
  212.  
  213.             case '+':
  214.                 strcpy(info[infoCounter].lexeme,"+");
  215.                 info[infoCounter].tokentype = 4;
  216.                 infoCounter++;
  217.                 found = 1;
  218.                 break;
  219.  
  220.             case '-':
  221.                 strcpy(info[infoCounter].lexeme,"-");
  222.                 info[infoCounter].tokentype = 5;
  223.                 infoCounter++;
  224.                 found = 1;
  225.                 break;
  226.  
  227.             case '*':
  228.                 strcpy(info[infoCounter].lexeme,"*");
  229.                 info[infoCounter].tokentype = 6;
  230.                 infoCounter++;
  231.                 found = 1;
  232.                 break;
  233.  
  234.             case '/':
  235.                 strcpy(info[infoCounter].lexeme,"/");
  236.                 info[infoCounter].tokentype = 7;
  237.                 infoCounter++;
  238.                 found = 1;
  239.                 break;
  240.  
  241.             case '=':
  242.                 strcpy(info[infoCounter].lexeme,"=");
  243.                 info[infoCounter].tokentype = 9;
  244.                 infoCounter++;
  245.                 found = 1;
  246.                 break;
  247.  
  248.             case '!':
  249.                 if(bytes[byteCounter+1]== '='){
  250.                     strcpy(info[infoCounter].lexeme,"!=");
  251.                     info[infoCounter].tokentype = 10;
  252.                     infoCounter++;
  253.                     byteCounter++;
  254.                     found = 1;
  255.                 }
  256.                 break;
  257.  
  258.             case '<':
  259.  
  260.                 if(bytes[byteCounter+1]== '='){
  261.                     strcpy(info[infoCounter].lexeme,"<=");
  262.                     info[infoCounter].tokentype = 12;
  263.                     infoCounter++;
  264.                     byteCounter++;
  265.                 }else{
  266.                     strcpy(info[infoCounter].lexeme,"<");
  267.                     info[infoCounter].tokentype = 11;
  268.                     infoCounter++;
  269.                 }
  270.                 found = 1;
  271.                 break;
  272.  
  273.             case '>':
  274.  
  275.                 if(bytes[byteCounter+1]== '='){
  276.                     strcpy(info[infoCounter].lexeme,">=");
  277.                     info[infoCounter].tokentype = 14;
  278.                     infoCounter++;
  279.                     byteCounter++;
  280.  
  281.                 }else{
  282.                     strcpy(info[infoCounter].lexeme,">");
  283.                     info[infoCounter].tokentype = 13;
  284.                     infoCounter++;
  285.                 }
  286.                 found = 1;
  287.                 break;
  288.  
  289.             case '(':
  290.                 strcpy(info[infoCounter].lexeme,"(");
  291.                 info[infoCounter].tokentype = 15;
  292.                 infoCounter++;
  293.                 found = 1;
  294.                 break;
  295.  
  296.             case ')':
  297.                 strcpy(info[infoCounter].lexeme,")");
  298.                 info[infoCounter].tokentype = 16;
  299.                 infoCounter++;
  300.                 found = 1;
  301.                 break;
  302.  
  303.             case ',':
  304.                 strcpy(info[infoCounter].lexeme,",");
  305.                 info[infoCounter].tokentype = 17;
  306.                 infoCounter++;
  307.                 found = 1;
  308.                 break;
  309.  
  310.             case ';':
  311.                 strcpy(info[infoCounter].lexeme,";");
  312.                 info[infoCounter].tokentype = 18;
  313.                 infoCounter++;
  314.                 found = 1;
  315.                 break;
  316.  
  317.             case '.':
  318.                 strcpy(info[infoCounter].lexeme,".");
  319.                 info[infoCounter].tokentype = 19;
  320.                 infoCounter++;
  321.                 found = 1;
  322.                 break;
  323.  
  324.             case ':':
  325.                 if(bytes[byteCounter+1] == '='){
  326.                     strcpy(info[infoCounter].lexeme,":=");
  327.                     info[infoCounter].tokentype = 20;
  328.                     infoCounter++;
  329.                     byteCounter++;
  330.                     found = 1;
  331.                 }
  332.                 break;
  333.  
  334.             default:
  335.                 break;
  336.             }
  337.         }
  338.  
  339.         if(found ==0 && bytes[byteCounter] >-100 && bytes[byteCounter] <31)
  340.             found=2;
  341.  
  342.         //if nothing was found we will store it into an array because it could possibly
  343.         //be a number or a identifier
  344.         if(found == 0){
  345.  
  346.             ident[identCounter] = bytes[byteCounter];
  347.             identCounter++;
  348.             byteCounter++;
  349.  
  350.         }else if(found==2){
  351.             byteCounter++;
  352.             found=0;
  353.  
  354.         }else{
  355.             //this reinitializes variables and allocates memory for the struct
  356.             tempInfo = info[infoCounter-1];
  357.             info = (LexemeTable*)realloc(info,(infoCounter+1)*sizeof(LexemeTable));
  358.             info[infoCounter-1] = tempInfo;
  359.             memset(ident, 0, 10);
  360.             identCounter=0;
  361.             byteCounter++;
  362.             found = 0;
  363.  
  364.             if(info[infoCounter-1].tokentype != 2 && info[infoCounter-1].tokentype != 3){
  365.                 info[infoCounter-1].type = 3;
  366.             }
  367.  
  368.         }
  369.     }
  370.  
  371.     printf("Lexeme Table:\n");
  372.     printf("lexeme  token type\n");
  373.     for(i=0; i<infoCounter;i++){
  374.  
  375.         if(info[i].type == 0){
  376.             printf("%d      ",info[i].number);
  377.             printf("%i\n",info[i].tokentype);
  378.         }else{
  379.             printf("%s      ",info[i].lexeme);
  380.             printf("%i\n",info[i].tokentype);
  381.         }
  382.  
  383.     }
  384.  
  385.     printf("\n");
  386.     printf("Lexeme List:\n");
  387.  
  388.     for(i=0; i<infoCounter;i++){
  389.         printf("%i ",info[i].tokentype);
  390.  
  391.         if(info[i].type == 1){
  392.             printf("%s ",info[i].lexeme);
  393.         }else if(info[i].type == 0){
  394.             printf("%i ",info[i].number);
  395.         }
  396.         printf("| ");
  397.     }
  398.  
  399.     printf("\n");
  400.  
  401.  
  402.     system("pause");
  403. }
  404.  
  405.  
  406. //All this does is check if there is an error
  407. // and tells me if the array I passed in is a number or an identifier
  408. tellme(char ident[]){
  409.     int num=0,letter=0,i=0;
  410.    
  411.    
  412.     while(ident[i] != '\0'){
  413.         if(ident[i] > 47 && ident[i] < 58)
  414.             num++;
  415.         else if(ident[i]>64 && ident[i]< 91 || ident[i]>96 && ident[i]< 123)
  416.             letter++;
  417.         else if(ident[i] == '\0'){
  418.         }else{
  419.             printf("Error! Symbol that isn't accepted found! '%c'  \nNow exiting\n",ident[i]);
  420.             system("pause");
  421.             exit(-1);
  422.         }
  423.         i++;
  424.     }
  425.  
  426.     if(ident[0]>47 && ident[0]< 58 && letter >0){
  427.         printf("Error, Variable does not start with letter. %s \nNow exiting\n",ident);
  428.         system("pause");
  429.         exit(-1);
  430.     }
  431.  
  432.     if(num>5 && letter==0){
  433.         printf("Error, Number too long. %s \nNow exiting\n",ident);
  434.         system("pause");
  435.         exit(-1);
  436.     }
  437.  
  438.     if((num + letter) >11){
  439.         printf("Error, Name too long. %s \nNow exiting\n",ident);
  440.         system("pause");
  441.         exit(-1);
  442.     }
  443.  
  444.     if(letter == 0)
  445.         return 0;
  446.     else
  447.         return 1;
  448.  
  449. }
Advertisement
Add Comment
Please, Sign In to add comment