I'm building a parser for mathematical functions, i'm still at the very basic but something has gone wrong and i can't find the solution. If i compile the code and i use the function 100+100 as input, it gives me this output:
$ ./test.exe
Enter your function please: 100+100
function[2] is numeric: 0
function[1] is numeric: 0
function[0] is numeric: 1
function[0] to function[2] is the first argument
function[4] is numeric: 1
function[5] is numeric: 0
function[6] is numeric: 0
function[4] to function[6] is the second argument
arg1 == 10↕aDĖ" && arg2 == 100
Please help me, because i'm really growing desperate.
Btw, i know the things like the function array aren't ideal, but i needed to test the principle before i was really going to make it.
Code://second mathematical function parser test #include <stdio.h> #include <stdlib.h> #include <math.h> #define PLUS 43 int main() { char function[200]; int i, scan1, scan2; char arg1[200], arg2[200]; printf("Enter your function please: "); fgets(function,sizeof(function)-1,stdin); for(i=0;i<strlen(function);i++) { //If function[i] is a + operator if(function[i] == PLUS) { //Scan the string for numbers (arguments) for(scan1=i-1;scan1>=0;scan1--) { /*If function[i] is not a numeric value, then break because you are at the end of the argument*/ if(function[scan1] < 48 || function[scan1] > 58) { break; } printf("function[%d] is numeric: %d \n",scan1,(function[scan1] - 48)); } printf("function[%d] to function[%d] is the first argument\n",scan1,(i-1)); //because the loop still does scan1-- one more time scan1++; //scan the string for the second argument for(scan2=i+1;scan2<strlen(function);scan2++) { if(function[scan2] < 48 || function[scan2] > 58) { break; } printf("function[%d] is numeric: %d \n",scan2,(function[scan2] - 48)); } //because the loop still does scan2++ one more time scan2--; printf("function[%d] to function[%d] is the second argument\n",(i+1),scan2); //copy them to a buffer of their own strncpy(arg1,function+scan1,(i-1)); strncpy(arg2,function+(i+1),scan2); printf("arg1 == %s && arg2 == %s",arg1,arg2); } } return 0; }
Last edited by Shokora; 09-27-2007 at 12:28 PM.
If this project is going to be reused you need to reorganize your code or you will become hopelessly lost. you need to tokenize your data before you can do anything else. you have the right idea but you need to make it more generic and organized.
Code://second mathematical function parser test #include <stdio.h> #include <stdlib.h> #include <math.h> #include <string.h> #define PLUS 43 #define FunctionSize 200 int parsenumber(char *function, int start) { for(int i=start;i<FunctionSize;i++) { if(function[scan1] < 48 || function[scan1] > 58) { return i-1; } } } int main() { char function[FunctionSize]; int i, scan1, scan2; char arg1[FunctionSize], arg2[FunctionSize]; printf("Enter your function please: "); fgets(function,sizeof(function)-1,stdin); // get the first digit int StartofNumberOne, EndofNumberOne, StartofNumberTwo, EndofNumberTwo; StartofNumberOne=0; EndofNumberOne=parsenumber(function,StartofNumberOne); StartofNumberTwo=EndofNumberOne+2; EndofNumberTwo=parsenumber(function,StartofNumberTwo);
thanks i'll try to be more consistent in the future.
I solved my problem, I accidently didn't include string.h and strncpy(); so happens to need it... T_T
I think in this case the devil is in the details. Take a look at it from a more abstract perspective. In particular, to build a parser, you'll want to learn about Finite State Machines. Then on to Parsers and Lexical Analyzers. For a beginning programmer, it may prove difficult to grasp, but I think these concepts are what you are struggling with.
Once you've looked that material over you should start with some pseudo-code.
Also, I suggest staying away from string functions and sizeof() at all costs. It's the easiest way to avoid buffer overflow issues.
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks