Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Peg Grammar Parser Grasshopper Language
//g++ 7.4.0 /*TEST GRAMMAR AST PARSER FOR THE NEW GHC (GRASSHOPPER) COMPUTER LANGUAGE by: Robin Deatherage state machine PEG Grammer Parser Written in C++ */ /* * ALL INTEGERS ARE IN FACT DOUBLES * ALL INTEGERS ARE STORED AS BOTH DOUBLES AND AS CHAR STRING LITERALS * VARIABLE DATA TYPES CAN BE CHANGED BY THEIR RESPECTIVE v[vcnt].vtp; HANDLERS * EXTENDED ITERATIONS IN THE MAIN LOOP ARE HANDLED BY STATE SWITCHES * VARIABLES, CLASS'S, FUNCTIONS, LIST, DICTIONARIES ARE ALL INDEXED BY STATE COUNTS * HASHTABLES FOR LIST STACK ARRAYS STORE EACH ADDED LIST'S NAME AND ITERATION INDEX LOCATION * * */ #include <iostream> #include <sstream> //<<<<<<<<< useful #include <string.h> #include <string> #ifndef VMAX #define VMAX 195 #endif const double pi = 3.14159; const char NL = '\n'; struct ListIntHash { char lnm; // LIST NAME int ls; //START ITER int le; // END ITER }LH[20]; // SET LOW FOR TESTING struct ListInt { // ALL WE STORE HERE IS VALUES, THE HASHTABLE ABOVE PROVIDES WHICH LIST AND IT'S DATA // int ltp; // char lnm[VMAX]; double ldval; //---WE STORE BOTH DATA TYPES DOUBLE AND CHAR STRING LITERAL // char lval; }LD[20]; // SET LOW FOR TESTING struct Exp { // EXPRESSIONS FOR MATH FOR INT, DOUBLE, FLOAT ETC. int etp; // DATA TYPE int eop; // OPERATOR TYPE char elh[VMAX]; // LEFT HAND char erh[VMAX]; // RIGHT HAND char eval[VMAX]; // EXPRESSION VALUE }; struct Var { int vtp; char vnm[VMAX]; double dval; //---WE STORE BOTH DATA TYPES DOUBLE AND CHAR STRING LITERAL char val[VMAX]; }; void fetchval(struct Var p, char target[], int ndx) { }; using namespace std; //----will remove after testing enum ghexpress {}; enum gs_dtypes {INT = 1, CHAR, DOUBLE, STRING, FLOAT, BOOL, LONG, UNSIGNED}; enum gs_keywords {FOR = 1, WHILE, IF, ELSE, OPEN, FOPEN, CLOSE, TRY, CASE, };// MORE TO ADD AS WE MOVE ON enum gh_var_states {VOFF, VON, }; enum gh_print_states {POFF, PON, }; double LHAND; double RHAND; double VARHAND; std::stringstream ss; // state machine PEG Grammer Parser // vtype---intger=1, char=2, double=3, string=4, float=5, bool=6 // ABSTRACT SYNTAX TREE'S REQUIRE STATE MACHINE COUNTS AND SWITCHES FOR NODE TRAVERSING int vstate = 0;// DETERMINES VARIABLE STATE 1=ON 0=OFF int vcnt = 0; // DETERMINES VARIABLE COUNT, INDEXES THE VARIABLE STORING LINKED LIST ARRAY STACK int vtype = 0; // DETERMINES VARIABLE DATA TYPE int ststate = 0;// DETERMINES STRING STATE 1=ON 0=OFF int scnt = 0; // DETERMINES STRING WORD COUNT UNTIL WE REACH THE END OF STRING int prstate = 0; // DETERMINES WHAT PRINT STATE WE ARE IN CURRENTLY int linecount = 1; // WILL BE USED FOR ALL ERROR LINE REPORTING FOR THE NEW LANGUAGE FILE OBJECT int lst_i_count = 0; // LIST INTEGER COUNTER int lst_i_value_count = 0; int lst_i_state = 0; // LIST INTEGER STATE SWITCH int lst_s_count = 0; // LIST CHAR STRING COUNTER int lst_s_state = 0; // LIST CHAR STRING STATE SWITCH int main() { struct Var v[20]; // SET FOR TESTING AT 20, IN PRODUCTION THIS WILL BE AT 4096 MALLOC PLUS //CHAR STR IS OUR TEST PEG GRAMMAR FOR THE AST PARSER BELOW, ITS ENTRY WILL BE READ FROM (ARGV CHAR ARGC) char str[] = "INT x EQUAL 100 SCOLN NWLN INT y EQUAL 10 SCOLN NWLN\ PRINT LPAREN x RPAREN SCOLN NWLN FLOAT f1 EQUAL 4.579 SCOLN NWLN\ LISTINT myarray EQUAL LBRK 1 , 2 , 3 , 4 , 5 RBRK SCOLN NWLN\ CHAR stx EQUAL DBL I Am A String! DBL SCOLN NWLN PRINT LPAREN DBL I am a string literal. DBL RPAREN SCOLN NWLN\ CHAR xm EQUAL DBL A short sentense for you. DBL SCOLN NWLN PRINT LPAREN xm RPAREN SCOLN NWLN INT z EQUAL 5 SCOLN NWLN\ VARLIST LPAREN RPAREN NWLN PRINT LPAREN 43 RPAREN SCOLN NWLN PRINT LPAREN f1 RPAREN SCOLN NWLN x EQUAL x ADD y SCOLN NWLN"; // ------removed --- x EQUAL x PLUS y SCOLN NWLN const char s[2] = " "; char *tok; char *tok2; char *tok3; char *tok4; char st[50]; tok = strtok(str, s); // LOAD FIRST TOKEN FOR THE LOOP while( tok != NULL ) { // BEGIN STATE MACHINE NODE TRAVERSALS //if(!strcmp(tok, "INT")) { // tok = strtok(0, s); // SUSPECTED = EQUAL // if(!strcmp(tok, "EQUAL")) { // tok = strtok(0, s); // SUSPECTED number // } //} else if(!strcmp(tok, "INT")) { //printf("-INT\n"); v[vcnt].vtp = INT; cout << "-Store variable type" << endl; vstate = 1; // THE LOOK-AHEAD THAT SIGNALS INTEGER IN THE TRAVERSAL ON THE NEXT ITERATION BELOW tok = strtok(0, s);//SUSPECDTED VARIABLE OR FUNCTION NAME strcpy(v[vcnt].vnm, tok);// STORE THE VARIABLE cout << "-Store Variable Name" << endl; vcnt++;// INCREMENT THE COUNTER after initial var NM NAME is stored count tok = strtok(0, s);//SUSPECTED = EQUAL if(!strcmp(tok, "EQUAL")) { //cout << "-INT EQUAL" << endl; tok = strtok(0, s);// SUSPECTED VALUE IF IS LEFT HAND DIGIT ELSE ASSIGNED OR LEFT HAND VARIABLE if(isdigit(tok[0])) { // PERCIEVED "VAR = DIGIT?" cout << "--IS DIGIT " << tok << endl; tok2 = strtok(0, s); //USING TOK2 ONLY AS A LOOK-AHEAD if((!strcmp(tok2, "SCOLN"))) { // CONFIRM IT ENDS WITHOUT AN OPERATOR (,),+,-,/,* ETC. //cout << "---TOK2 HAS SCOLN(SEMICOLON) END OF BLOCK" << endl; // ITS A DATA VALUE WE STORE IT strcpy(v[vcnt-1].val, tok); // STORE IT AS ASSIGNED DATA VAR VAL VALUE, ***REQUIRES SUBTRACTING 1 FROM VCNT v[vcnt-1].dval = atof(tok); //strcpy(v[vcnt-1].val, "null"); vstate = 0; // RESET VSTATE=0 cout << "-Stored INT Variable Value" << endl; } //END IF //HERE STORE TOKEN NUMBER OR VAR AS LEFT-HAND LH INTO STRUCT EXPRESSION } else if(isalpha(tok[0])) { // WE HAVE AN INTEGER ASSIGNED VARIABLE, CLASS OR FUNCTION CALLER cout << "--IS ALPHA====== " << tok << endl; // // ADD DATA TO STRUCT EXPRESSION STORAGE FOR CALCULATING MATH //NOT IMPLIMENTED YET----------------------XXX tok2 = strtok(0, s); // LOOK-AHEAD TO CHECK FOR "(" LPAREN --FOR CLASS OR FUNCTION CALLER if((!strcmp(tok2, "LPAREN"))) { //TO DETERMINE A FUNCTION OR CLASS CALLER cout << "--do integer return function " << tok << endl; // --------EXECUTE FUNCTION HERE --NOT IMPLIMENTED YET---------XXX } // END IF LPAREN } // END ISALPHA } // END IF EQUAL } else if(!strcmp(tok, "CHAR")) { //printf("-CHAR\n"); v[vcnt].vtp = CHAR; tok = strtok(0, s); // SUSPECTED VARIABLE NAME strcpy(v[vcnt].vnm, tok); // STORE THE VARIABLE NAME vcnt++; // INCREMENT THE COUNTER after initial var NM NAME is stored count tok = strtok(0, s); // SHOULD BE AN EQUAL HERE //cout << "-CHAR EQUAL" << tok << endl; tok = strtok(0, s); // SHOULD BE A FIRST DBL DOUBLE QUOTE HERE //tok = strtok(0, s); // GET FIRST DBL DOUBLEQUOTE ststate = 2; // SET STATE FOR CHAR BUFFER CONCATENATIONS BEGINNING ON NEXT ITERATION } else if(ststate == 2) { //tok = strtok(0, s); //GEt first word char group from char string if(!strcmp(tok, "DBL")) { //IF SECOND DBL CHAR STRING ENDS HERE.... ststate = 0; //ENDS THE CHAR STRING FETCHING ROUTINE strcpy(v[vcnt-1].val, st); // store the full sentense or char string cout << "-STORED CHAR VALUE IF STSTATE ==2 --" << vcnt-1 <<endl; strcpy(st, ""); //reset st buffer } else { //cout << "-CHAR'S FOUND AFTER FIRST DBL---"<< tok << endl; strcat (st, tok); // ADD CHAR STRING GROUP TO BUFFER..... strcat (st, " ");//adds the white space---needs a fix ---XXX } //} else //if(!strcmp(tok, "DOUBLE")) { // printf("-Double\n"); // //vcnt++; //NOT STORED YET //vstate = 3; } else if(!strcmp(tok, "FLOAT")) { //printf("-INT\n"); v[vcnt].vtp = INT; cout << "-Store FLOAT variable type" << endl; vstate = 1; //THE LOOK-AHEAD THAT SIGNALS FLOAT IN THE TRAVERSAL ON THE NEXT ITERATION BELOW tok = strtok(0, s);//SUSPECDTED VARIABLE OR FUNCTION NAME strcpy(v[vcnt].vnm, tok);// STORE THE VARIABLE cout << "-Store FLOAT Variable Name" << endl; vcnt++;// INCREMENT THE COUNTER after initial var NM NAME is stored count tok = strtok(0, s);//SUSPECTED = EQUAL if(!strcmp(tok, "EQUAL")) { //cout << "-INT EQUAL" << endl; tok = strtok(0, s);//SUSPECTED VALUE IF IS LEFT HAND DIGIT ELSE ASSIGNED OR LEFT HAND VARIABLE if(isdigit(tok[0])) { // PERCIEVED "VAR = DIGIT?" cout << "--IS FLOAT DIGIT " << tok << endl; tok2 = strtok(0, s); //USING TOK2 ONLY AS A LOOK-AHEAD if((!strcmp(tok2, "SCOLN"))) {//CONFIRM IT ENDS WITHOUT AN OPERATOR (,),+,-,/,* ETC. //cout << "---TOK2 HAS SCOLN(SEMICOLON) END OF BLOCK" << endl; // ITS A DATA VALUE WE STORE IT strcpy(v[vcnt-1].val, tok); // STORE IT AS ASSIGNED DATA VAR VAL VALUE, ***REQUIRES SUBTRACTING 1 FROM VCNT v[vcnt-1].dval = atof(tok); //strcpy(v[vcnt-1].val, "null"); vstate = 0; //RESET VSTATE=0 cout << "-Stored FLOAT Variable Value" << endl; } //END IF //HERE STORE TOKEN NUMBER OR VAR AS LEFT-HAND LH INTO STRUCT EXPRESSION } else if(isalpha(tok[0])) { // WE HAVE AN FLOAT ASSIGNED VARIABLE, CLASS OR FUNCTION CALLER cout << "--IS ALPHA ......====== " << tok << endl; // // ADD DATA TO STRUCT EXPRESSION STORAGE FOR CALCULATING MATH //NOT IMPLIMENTED YET----------------------XXX tok2 = strtok(0, s); // LOOK-AHEAD TO CHECK FOR "(" LPAREN --FOR CLASS OR FUNCTION CALLER if((!strcmp(tok2, "LPAREN"))) { //TO DETERMINE A FUNCTION OR CLASS CALLER cout << "--do FLOAT return function " << tok << endl; // --------EXECUTE FUNCTION HERE --NOT IMPLIMENTED YET---------XXX } //END IF LPAREN } //END ISALPHA } //END IF EQUAL } else if(!strcmp(tok, "BOOL")) { printf("-Bool\n"); //vcnt++; //NOT STORED YET vstate = 6; } else if(!strcmp(tok, "PRINT")) { //printf("-Print----\n"); tok = strtok(0, s); // SKIP LPAREN tok = strtok(0, s); //GET VARIABLE NAME, DIGIT OR DBL FOR CHAR LITERAL STRING //cout << "MAGIC OF PRINT--> "<<tok <<endl; cout << "MAGIC OF PRINT--> " <<endl; if(!strcmp(tok, "DBL")) { //NOW WE KNOW IT IS A CHAR STRING LITERAL---------LITERAL prstate = 1; // SET PRINT STATE TO 1 FOR LITERAL PRINT PROCESS BELOW // ADD LISTINT AND LISTS ARRAYS HERE---------------------------------------XXX } else if(isdigit(tok[0])) { // CHECK FOR DIGIT cout << "--PRINT IS DIGIT actual-- " << tok << endl; } else if(isalpha(tok[0])) { // CHECK FOR ALPHANUMERIC VAR NAME cout << "--PRINT VARIABLE named--->>>"<< tok <<endl; cout <<"\""; for(int i = 0; i <= vcnt; i++){ if(strcmp(v[i].vnm, tok)==0) {//IF TRUE THEN COUT IT -----XXXbreak; if(v[i].vtp == INT) { cout << v[i].dval; } else if(v[i].vtp == CHAR) { cout << v[i].val; } // ADD IN BOOL AND STRING LATER------------------------------XXX } //END IF } //END FOR cout <<"\"\n"; } //END IF TOK } else if(!strcmp(tok, "DEF")) { printf("-Define Function\n"); //vcnt++; //vstate = 2; //---------------------------------------------------------------------------------------------WE HERE*** } else if(!strcmp(tok, "LISTINT")) { printf("-Begin List Array\n"); tok = strtok(0, s); // EXPECTED VARIABLE NAME cout << "LIST VARIABLE NAME= "<< tok <<endl; // LIST ARRAY VARIABLE NAME tok2 = strtok(0, s); // EXPECTED EQUAL cout << tok2 <<endl; // tok3 = strtok(0, s); // EXPECTED LBRK cout << tok3 <<endl; // lst_i_state = 1; } else if(lst_i_state == 1) { cout<<" ---- Got LISTINT STATE--"<<endl; //cout << tok <<endl; // LIST ARRAY VARIABLE NAME if(!strcmp(tok, "RBRK")) { lst_i_state = 0; //reset listint state } else if(!strcmp(tok, ",")) { // COMA'S cout << "" <<endl; // WE DO NOTHING } else { cout << "STORED LISTINT VALUE= "<< tok <<endl; // WE STORE THE DATA BETWEEN THE COMA'S lst_i_value_count++; //WE INCREMENT EACH DATA VALUE, THIS BECOMES THE INDEX ITER FOR THE HASHTABLE END TAG le=? } // -------SOME MORE TO TRY AND TEST WITH-------- //} else //if(!strcmp(tok, "EQUAL")) { // printf("-Operator =\n"); //} else //if(!strcmp(tok, "ADD")) { // printf("-Operator +\n"); //} else //if(!strcmp(tok, "MINUS")) { // printf("-Operator -\n"); //} else //if(!strcmp(tok, "DIV")) { // printf("-Operator /\n"); //} else //if(!strcmp(tok, "DBL")) { // printf("-Define DBL\n"); } else if(!strcmp(tok, "NWLN")) { printf("-Define NWLN\n"); linecount++; // IMPORTANT FOR FETCHING LINE NUMBERS FROM ERRORS IN TOKEN STREAM FILE. } else if(!strcmp(tok, "SCOLN")) { printf("-Define SCOLN\n"); } else if(!strcmp(tok, "LPAREN")) { printf("-Define LPAREN\n"); } else if(!strcmp(tok, "RPAREN")) { printf("-Define RPAREN\n"); } else if(!strcmp(tok, "EQUAL")) { printf("-Define EQUAL\n"); } else if(!strcmp(tok, "VARLIST")) { // GOOD LINKED LIST MIMICKED ARRAY DESIGN printf("-VARLIST--- \n"); //if(v[i].vtp == DOUBLE) { cout << "["; for(int i = 0; i <= vcnt; i++) { //if(v[i].vtp == DOUBLE) { if(i != vcnt-1) cout << v[i].dval <<", "; else cout << v[i].dval <<"]\n"; } //END FOR } else if(prstate == 1) { //tok = strtok(0, s); //GEt first word char group from char string if(!strcmp(tok, "DBL")) { //IF SECOND DBL CHAR STRING ENDS HERE.... prstate = 0; //ENDS THE CHAR STRING FETCHING ROUTINE //strcpy(v[vcnt-1].val, st); // store the full sentense or char string // cout << vcnt-1 <<endl; cout << "\"" << st << "\"" <<endl; strcpy(st, ""); //reset st buffer } else { //cout << "-CHAR'S FOUND AFTER FIRST DBL---"<< tok << endl; strcat (st, tok); // ADD CHAR STRING GROUP TO BUFFER..... strcat (st, " "); // adds the space---------------------------need a solution here---XXX } } else { // HERE WE HAVE TO PICK UP ALL ALREADY DEFINED VARIABLES BEING REASSIGNED if(isalpha(tok[0])) { cout << "HAVE A REASSIGNED VAR " << tok <<endl; tok2 = strtok(0, s); // LOOK-AHEAD TO CHECK FOR EQUAL if(!strcmp(tok2, "EQUAL")) { cout << tok << tok2 <<"========"; tok2 = strtok(0, s); // ADVANCE TOK2 AGAIN TO CHECK FOR LEFT HAND VAR OR NUMBER //FETCH THE VARIABLE VALUE HERE if(isdigit(tok2[0])) { cout << "FOUND A DIGIT TOK2 " << tok2 <<endl; // STORE IT HERE AS REASSIGNED VAR------------------------NOT YET IMPLIMENTED---XXX } else { cout << " ----- " << tok <<" -- "<< tok2 <<endl; tok3 = strtok(0, s); // lets fetch the operator tok4 = strtok(0, s); // WE SET THE RIGHT HAND VAR IF EXIST cout << " ----- " << tok3 << tok4 <<endl; // GET FIRST VAR VALUE******************1st value for(int i = 0; i <= vcnt; i++) { if(strcmp(v[i].vnm, tok2)==0) {//IF TRUE THEN COUT IT -----XXXbreak; cout << v[i].val <<endl; //LHAND = stof(v[i].val); LHAND = v[i].dval; } //END IF } //END FOR // GET SECOND VAR VALUE*****************2nd value for(int i = 0; i <= vcnt; i++) { if(strcmp(v[i].vnm, tok4)==0) {//IF TRUE THEN COUT IT -----XXXbreak; cout << v[i].val <<endl; //RHAND = stof(v[i].val); RHAND = v[i].dval; } //END IF } //END FOR-------------------------------------- if(!strcmp(tok3, "ADD")) { cout << LHAND + RHAND <<endl; VARHAND = LHAND + RHAND; //strcpy(v[vcnt-1].val, st); // } else if(!strcmp(tok3, "SUB")) { cout << LHAND - RHAND <<endl; //strcpy(v[vcnt-1].val, st); // } else if(!strcmp(tok3, "DIV")) { cout << LHAND / RHAND <<endl; //strcpy(v[vcnt-1].val, st); // } else if(!strcmp(tok3, "MULT")) { cout << LHAND * RHAND <<endl; //strcpy(v[vcnt-1].val, st); // } //END IF OPERATOR------------------------------ //----GET OUR FIRST VARIABLE VALUE for(int i = 0; i <= vcnt; i++){ if(strcmp(v[i].vnm, tok)==0) {//IF TRUE THEN COUT IT -----XXXbreak; cout << "tttttttttt " << v[i].dval <<endl; //VARHAND = stod(v[i].val); //double dbl = 2222; //char* tptr = (char*)(&VARHAND); //ss << VARHAND; //<<<<<<<<< extract the string from the stringstream object //string sr = ss.str(); //cout << sr; //strcpy(v[i].val, sr); //v[i].val = sr; //cout<< "NEW VALUE= "<< v[i].val <<endl; } //END IF } //END FOR } } //END IF EQUAL } //END IF ALPHA } //END TOK tok = strtok(NULL, s); //flush it } //END WHILE // TEST OF STRUCT VARIABLE DATA STORES cout << "\n\n\n\nINT= " << v[0].vtp <<"--"; cout << "VAR NAME= " << v[0].vnm <<"--"; cout << "VAR VALUE of x= " <<v[0].val <<"\n"; cout << "VAR VALUE of y= " <<v[1].val <<"\n"; // v[2] the float not implimented yet cout << "VAR VALUE of x + y= " << atoi(v[0].val) + atoi(v[1].val) <<"\n"; // TEST MATH X + Y cout << "VAR VALUE of stx= " << "\"" << v[3].val << "\"" << "\n"; cout << "VAR VALUE of xm= " << "\"" << v[4].val << "\"" <<"\n"; cout << st << "VCNT= " << vcnt << " Line Count= "<< linecount <<endl; // TEST CHAR STRING BUFFER FETCHER } //END MAIN
run
|
edit
|
history
|
help
0
Podejrzana karteczka :0
segmented sieve
ConversionOperator
Nobita's candies problem
p30
Jilebi Nimki
char array in class
sa
Binary tree balanced or not
Sorting sort function stl in c++