Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <stdio.h>
- #include <string.h>
- typedef struct{
- char lexeme[11];
- int tokentype;
- int number;
- //type 0 = number
- //type 1 = identifier
- int type;
- }LexemeTable;
- //function prototype
- int tellme (char*);
- main(){
- FILE* fp;
- int filesize=-1,byteCounter=0;
- int found=0;
- int identCounter=0,numCounter=0,infoCounter=0;
- int choice;
- int i=0;
- char ident[100] = {'\0'};
- char *bytes;
- LexemeTable *info;
- LexemeTable tempInfo;
- info = (LexemeTable*) malloc(1*sizeof (LexemeTable));
- fp = fopen("input.txt","r");
- while( !(feof(fp))){
- fgetc(fp);
- filesize++;
- }
- //this makes it possible to see everything in the file with an array named bytes
- fseek(fp,0,SEEK_SET);
- bytes = malloc(filesize);
- for(i=0;i<filesize;i++){
- bytes[i] = fgetc(fp);
- }
- //this prints out the input
- i=0;
- while(i<filesize){
- printf("%c",bytes[i]);
- i++;
- }
- printf("\n \n \n");
- //byteCounter is the way we traverse through the array
- //infoCounter is the counter that tells me how many tokens there are
- while(byteCounter<=filesize){
- //this checks if it's a comment and skips everything
- if(bytes[byteCounter] == '/' && bytes[byteCounter+1] == '*'){
- while( (bytes[byteCounter] != '*') || (bytes[byteCounter+1] != '/')){
- byteCounter++;
- }
- byteCounter += 2;
- }
- //these checks if it's any of the reserved words
- if(strcmp(ident,"begin")==0 && (bytes[byteCounter]==' '|| bytes[byteCounter]== 15 || bytes[byteCounter]== 10 || bytes[byteCounter]== 9 || filesize == byteCounter)){
- strcpy(info[infoCounter].lexeme,"begin");
- info[infoCounter].tokentype = 21;
- infoCounter++;
- found = 1;
- byteCounter--;
- }else if(strcmp(ident,"end")==0 && (bytes[byteCounter]==' '|| bytes[byteCounter]==';' || bytes[byteCounter]=='.'|| bytes[byteCounter]== 15 || bytes[byteCounter]== 10 || bytes[byteCounter]== 9 || filesize == byteCounter)){
- strcpy(info[infoCounter].lexeme,"end");
- info[infoCounter].tokentype = 22;
- infoCounter++;
- found = 1;
- byteCounter--;
- }else if(strcmp(ident,"null")==0 && (bytes[byteCounter]==' '|| bytes[byteCounter]== 15 || bytes[byteCounter]== 10 || bytes[byteCounter]== 9 || filesize == byteCounter)){
- strcpy(info[infoCounter].lexeme,"null");
- info[infoCounter].tokentype = 1;
- infoCounter++;
- found = 1;
- byteCounter--;
- }else if(strcmp(ident,"if")==0 && (bytes[byteCounter]==' '|| bytes[byteCounter]== 15 || bytes[byteCounter]== 10 || bytes[byteCounter]== 9 || filesize == byteCounter)){
- strcpy(info[infoCounter].lexeme,"if");
- info[infoCounter].tokentype = 23;
- infoCounter++;
- found = 1;
- byteCounter--;
- }else if(strcmp(ident,"then")==0 && (bytes[byteCounter]==' '|| bytes[byteCounter]== 15 || bytes[byteCounter]== 10 || bytes[byteCounter]== 9 || filesize == byteCounter)){
- strcpy(info[infoCounter].lexeme,"then");
- info[infoCounter].tokentype = 24;
- infoCounter++;
- found = 1;
- byteCounter--;
- }else if(strcmp(ident,"while")==0 && (bytes[byteCounter]==' '|| bytes[byteCounter]== 15 || bytes[byteCounter]== 10 || bytes[byteCounter]== 9 || filesize == byteCounter)){
- strcpy(info[infoCounter].lexeme,"while");
- info[infoCounter].tokentype = 25;
- infoCounter++;
- found = 1;
- byteCounter--;
- }else if(strcmp(ident,"do")==0 && (bytes[byteCounter]==' '|| bytes[byteCounter]== 15 || bytes[byteCounter]== 10 || bytes[byteCounter]== 9 || filesize == byteCounter)){
- strcpy(info[infoCounter].lexeme,"do");
- info[infoCounter].tokentype = 26;
- infoCounter++;
- found = 1;
- byteCounter--;
- }else if(strcmp(ident,"call")==0 && (bytes[byteCounter]==' '|| bytes[byteCounter]== 15 || bytes[byteCounter]== 10 || bytes[byteCounter]== 9 || filesize == byteCounter)){
- strcpy(info[infoCounter].lexeme,"call");
- info[infoCounter].tokentype = 27;
- infoCounter++;
- found = 1;
- byteCounter--;
- }else if(strcmp(ident,"const")==0 && (bytes[byteCounter]==' '|| bytes[byteCounter]== 15 || bytes[byteCounter]== 10 || bytes[byteCounter]== 9 || filesize == byteCounter)){
- strcpy(info[infoCounter].lexeme,"const");
- info[infoCounter].tokentype = 28;
- infoCounter++;
- found = 1;
- byteCounter--;
- }else if(strcmp(ident,"int")==0 && (bytes[byteCounter]==' '|| bytes[byteCounter]== 15 || bytes[byteCounter]== 10 || bytes[byteCounter]== 9 || filesize == byteCounter)){
- strcpy(info[infoCounter].lexeme,"int");
- info[infoCounter].tokentype = 29;
- infoCounter++;
- found = 1;
- byteCounter--;
- }else if(strcmp(ident,"procedure")==0 && (bytes[byteCounter]==' '|| bytes[byteCounter]== 15 || bytes[byteCounter]== 10 || bytes[byteCounter]== 9 || filesize == byteCounter)){
- strcpy(info[infoCounter].lexeme,"procedure");
- info[infoCounter].tokentype = 30;
- infoCounter++;
- found = 1;
- byteCounter--;
- }else if(strcmp(ident,"out")==0 && (bytes[byteCounter]==' '|| bytes[byteCounter]== 15 || bytes[byteCounter]== 10 || bytes[byteCounter]== 9 || filesize == byteCounter)){
- strcpy(info[infoCounter].lexeme,"out");
- info[infoCounter].tokentype = 31;
- infoCounter++;
- found = 1;
- byteCounter--;
- }else if(strcmp(ident,"in")==0 && (bytes[byteCounter]==' '|| bytes[byteCounter]== 15 || bytes[byteCounter]== 10 || bytes[byteCounter]== 9 || filesize == byteCounter)){
- strcpy(info[infoCounter].lexeme,"in");
- info[infoCounter].tokentype = 32;
- infoCounter++;
- found = 1;
- byteCounter--;
- }else if(strcmp(ident,"else")==0 && (bytes[byteCounter]==' '|| bytes[byteCounter]== 15 || bytes[byteCounter]== 10 || bytes[byteCounter]== 9 || filesize == byteCounter)){
- strcpy(info[infoCounter].lexeme,"else");
- info[infoCounter].tokentype = 33;
- infoCounter++;
- found = 1;
- byteCounter--;
- }else if( (strcmp(ident,"odd")==0 || strcmp(ident,"ODD")==0) && (bytes[byteCounter]==' '|| bytes[byteCounter]== 15 || bytes[byteCounter]== 10 || bytes[byteCounter]== 9 || filesize == byteCounter)){
- strcpy(info[infoCounter].lexeme,"odd");
- info[infoCounter].tokentype = 8;
- infoCounter++;
- found = 1;
- byteCounter--;
- }
- if(found==0){
- //this big if statement just makes it clear where the identifiers/numbers are
- //and what is it's value/string
- if( bytes[byteCounter] == ' ' || bytes[byteCounter] == '+'|| bytes[byteCounter] == '-'|| bytes[byteCounter] == '*'|| bytes[byteCounter] == '/'
- || bytes[byteCounter] == '='|| (bytes[byteCounter] == '!' && bytes[byteCounter+1] == '=')|| bytes[byteCounter]== 15 || bytes[byteCounter]== 10 || bytes[byteCounter]== 9 ||bytes[byteCounter] == '<'|| bytes[byteCounter] == '>'|| bytes[byteCounter] == '('
- || bytes[byteCounter] == ')'|| bytes[byteCounter] == ','|| bytes[byteCounter] == ';'|| bytes[byteCounter] == '.'|| (bytes[byteCounter] == ':' && bytes[byteCounter] == '=' || filesize == byteCounter)){
- if(ident[0] > 47 && ident[0] < 58 || ident[0]>64 && ident[0]< 91 || ident[0]>96 && ident[0]< 123){
- //functions tells me if its a number or identifier
- //0 if number
- //1 if identifier
- choice = tellme(ident);
- if(choice == 0){
- info[infoCounter].number = atoi(ident);
- info[infoCounter].tokentype = 3;
- info[infoCounter].type = 0;
- }else{
- strcpy(info[infoCounter].lexeme,ident);
- info[infoCounter].tokentype = 2;
- info[infoCounter].type = 1;
- }
- infoCounter++;
- }
- }
- switch(bytes[byteCounter]){
- case '\n':
- case ' ':
- found = 1;
- break;
- case '+':
- strcpy(info[infoCounter].lexeme,"+");
- info[infoCounter].tokentype = 4;
- infoCounter++;
- found = 1;
- break;
- case '-':
- strcpy(info[infoCounter].lexeme,"-");
- info[infoCounter].tokentype = 5;
- infoCounter++;
- found = 1;
- break;
- case '*':
- strcpy(info[infoCounter].lexeme,"*");
- info[infoCounter].tokentype = 6;
- infoCounter++;
- found = 1;
- break;
- case '/':
- strcpy(info[infoCounter].lexeme,"/");
- info[infoCounter].tokentype = 7;
- infoCounter++;
- found = 1;
- break;
- case '=':
- strcpy(info[infoCounter].lexeme,"=");
- info[infoCounter].tokentype = 9;
- infoCounter++;
- found = 1;
- break;
- case '!':
- if(bytes[byteCounter+1]== '='){
- strcpy(info[infoCounter].lexeme,"!=");
- info[infoCounter].tokentype = 10;
- infoCounter++;
- byteCounter++;
- found = 1;
- }
- break;
- case '<':
- if(bytes[byteCounter+1]== '='){
- strcpy(info[infoCounter].lexeme,"<=");
- info[infoCounter].tokentype = 12;
- infoCounter++;
- byteCounter++;
- }else{
- strcpy(info[infoCounter].lexeme,"<");
- info[infoCounter].tokentype = 11;
- infoCounter++;
- }
- found = 1;
- break;
- case '>':
- if(bytes[byteCounter+1]== '='){
- strcpy(info[infoCounter].lexeme,">=");
- info[infoCounter].tokentype = 14;
- infoCounter++;
- byteCounter++;
- }else{
- strcpy(info[infoCounter].lexeme,">");
- info[infoCounter].tokentype = 13;
- infoCounter++;
- }
- found = 1;
- break;
- case '(':
- strcpy(info[infoCounter].lexeme,"(");
- info[infoCounter].tokentype = 15;
- infoCounter++;
- found = 1;
- break;
- case ')':
- strcpy(info[infoCounter].lexeme,")");
- info[infoCounter].tokentype = 16;
- infoCounter++;
- found = 1;
- break;
- case ',':
- strcpy(info[infoCounter].lexeme,",");
- info[infoCounter].tokentype = 17;
- infoCounter++;
- found = 1;
- break;
- case ';':
- strcpy(info[infoCounter].lexeme,";");
- info[infoCounter].tokentype = 18;
- infoCounter++;
- found = 1;
- break;
- case '.':
- strcpy(info[infoCounter].lexeme,".");
- info[infoCounter].tokentype = 19;
- infoCounter++;
- found = 1;
- break;
- case ':':
- if(bytes[byteCounter+1] == '='){
- strcpy(info[infoCounter].lexeme,":=");
- info[infoCounter].tokentype = 20;
- infoCounter++;
- byteCounter++;
- found = 1;
- }
- break;
- default:
- break;
- }
- }
- if(found ==0 && bytes[byteCounter] >-100 && bytes[byteCounter] <31)
- found=2;
- //if nothing was found we will store it into an array because it could possibly
- //be a number or a identifier
- if(found == 0){
- ident[identCounter] = bytes[byteCounter];
- identCounter++;
- byteCounter++;
- }else if(found==2){
- byteCounter++;
- found=0;
- }else{
- //this reinitializes variables and allocates memory for the struct
- tempInfo = info[infoCounter-1];
- info = (LexemeTable*)realloc(info,(infoCounter+1)*sizeof(LexemeTable));
- info[infoCounter-1] = tempInfo;
- memset(ident, 0, 10);
- identCounter=0;
- byteCounter++;
- found = 0;
- if(info[infoCounter-1].tokentype != 2 && info[infoCounter-1].tokentype != 3){
- info[infoCounter-1].type = 3;
- }
- }
- }
- printf("Lexeme Table:\n");
- printf("lexeme token type\n");
- for(i=0; i<infoCounter;i++){
- if(info[i].type == 0){
- printf("%d ",info[i].number);
- printf("%i\n",info[i].tokentype);
- }else{
- printf("%s ",info[i].lexeme);
- printf("%i\n",info[i].tokentype);
- }
- }
- printf("\n");
- printf("Lexeme List:\n");
- for(i=0; i<infoCounter;i++){
- printf("%i ",info[i].tokentype);
- if(info[i].type == 1){
- printf("%s ",info[i].lexeme);
- }else if(info[i].type == 0){
- printf("%i ",info[i].number);
- }
- printf("| ");
- }
- printf("\n");
- system("pause");
- }
- //All this does is check if there is an error
- // and tells me if the array I passed in is a number or an identifier
- tellme(char ident[]){
- int num=0,letter=0,i=0;
- while(ident[i] != '\0'){
- if(ident[i] > 47 && ident[i] < 58)
- num++;
- else if(ident[i]>64 && ident[i]< 91 || ident[i]>96 && ident[i]< 123)
- letter++;
- else if(ident[i] == '\0'){
- }else{
- printf("Error! Symbol that isn't accepted found! '%c' \nNow exiting\n",ident[i]);
- system("pause");
- exit(-1);
- }
- i++;
- }
- if(ident[0]>47 && ident[0]< 58 && letter >0){
- printf("Error, Variable does not start with letter. %s \nNow exiting\n",ident);
- system("pause");
- exit(-1);
- }
- if(num>5 && letter==0){
- printf("Error, Number too long. %s \nNow exiting\n",ident);
- system("pause");
- exit(-1);
- }
- if((num + letter) >11){
- printf("Error, Name too long. %s \nNow exiting\n",ident);
- system("pause");
- exit(-1);
- }
- if(letter == 0)
- return 0;
- else
- return 1;
- }
Advertisement
Add Comment
Please, Sign In to add comment