Need somebody good in C++ and rust programming on urgent basis
%{ #include <stdio.h> #include <stdlib.h> #include <string.h> #include <stdbool.h> int yylex(); typedef struct Var { char* name; char* value; } VAR; VAR symbols[10]; int position = 0; int varLocation = 0; bool exists(char* name){ //Check if variable already exists in array /* printf("inputed: %s\n", name); */ for(int i=0; i<10; i++){ VAR *ptr_v; ptr_v = &symbols[i]; char* varName = (*ptr_v).name; /* printf("checking: %s\n", varName); */ if(varName != NULL){ if(strcmp(name,varName) == 0){ /* printf("exists\n"); */ varLocation = i; return true; } } } /* printf("doesn't exist\n"); */ return false; } void add(VAR v){ //Adds variable, if it already exists update value VAR *ptr_v; ptr_v = &v; char* varName = (*ptr_v).name; char* varValue = (*ptr_v).value; /* printf("name: %s var: %s\n", varName, varValue); */ if(exists(varName)){ /* printf("updating value of variable\n"); */ symbols[varLocation].value = varValue; } else{ int i = 0; while (symbols[i].name != NULL) { i++; } symbols[i].name = varName; symbols[i].value = varValue; } } char* getVal(char* name){ //Gets the value of a variable /* printf("searching for var\n"); */ for(int i=0; i<10; i++){ char* tempName = symbols[i].name; /* printf("temp: %s\n", tempName); */ if(strcmp(tempName,name) == 0){ return symbols[i].value; } } } void printArray(){ //For testing /* printf("PRINTING ARRAY\n"); */ for(int i=0; i<10; i++){ char* name = symbols[i].name; char* value = symbols[i].value; /* printf("name: %s value: %s\n",name,value); */ } } %} %union{ char* str; } %start program %token <str> STRINGLIT VARIABLE %token FIRST REST CONS EQUALS PRINT NEWLINE %type <str> expression %% program: statement | program statement ; statement: print_statement | assign_statement; print_statement: PRINT VARIABLE { /* print the variable data */ char* name = $2; printf("output: %s\n",getVal(name)); printArray(); } ; assign_statement: VARIABLE EQUALS expression { char* name = $1; char* value = $3; VAR newVar = {name,value}; add(newVar); /* printf("ASSIGNMENT STATEMENT n:%s v:%s", name, value); */ } ; expression: STRINGLIT { char* withoutQuotes = &$1[1]; withoutQuotes[strlen(withoutQuotes) - 1] = NULL; $$ = withoutQuotes; /* printf("STRINGLIT"); */ } | FIRST VARIABLE { char* string = strdup(getVal($2)); string[1] = NULL; $$ = string; } | REST VARIABLE { char* string = strdup(getVal($2)); char *ptr_s = string; ptr_s++; /* printf("rest string: %s\n", ptr_s); */ $$ = ptr_s; } | CONS VARIABLE VARIABLE { char* string1 = getVal($2); char* string2 = getVal($3); char* resultString = malloc(strlen(string1) + strlen(string2) +1); strcpy(resultString,string1); strcat(resultString,string2); /* printf("result: %s\n", resultString ); */ $$ = resultString; } ; %% void yyerror(const char *str) { fprintf(stderr,"error %s\n",str); } int yywrap() { return 1; } int main(int argc, char **argv) { extern FILE *yyin; yyin = fopen(argv[1], "r"); //sets up lexer to use this file as input yyparse(); fclose(yyin); }