kaizen3 wrote:Ok, so I am a student of programming, and I need help in my project.
We were asked to create a 'compiler' using C language... This compiler must be able to read a set of lines. The main purpose of the compiler is to be able to solve infix mathematical problems.
set a = 5
set b = 10
set c = a + b
Now, my problem is, I dont know how to parse the input of the user... Any help would be greatly appreciated.. I am not asking for the whole code.. just some guides. Thank you and god bless.
int main(int argc, char * argv) // done out of habit, this is unused in the code
char arr; //holds a line of input data
fgets(arr, sizeof(arr), stdin); //now arr has a line of input
int keyword=0; //this will go to the switch table to signify actions dependant upon what is found to be the initial keyword.
else if(strstr(arr, "set"))
keyword=2; //maybe extra keywords if necessary...but be warned, doing it will cause issues if someone names a variable like this "set feprint ...".
case 1: // print statement
case 2: // if it gets here it means that the word "set" was in the input. Code for assigning a value to a variable will be needed
and with the addition of parenthesis (i.e. “(“ and “)”) to override precedence and for grouping. For example, the expression “9 * 8 + 5 * (4 % 3)” evaluates to “52” while “9 * (8 + 5 * 4 % 3)” is equal to “117”.
fashizzlepop wrote:Just for the record, this is more of an intrpreter than compiler. It never builds an executable.
thetan wrote:The moral of the compiler vs interpreter story is to not get caught up in
kaizen3 wrote:Thanks for the help!
Yesterday, my professor asked us to use BISON. He told us that this would be a very useful program in building this compiler/interpreter... Actually it pissed me off, after writing a very long code, I would have to re-do it with this program called Bison.
But really, I appreciated all your help!
I would be posting here again if I encounter more problems using Bison
Users browsing this forum: No registered users and 0 guests