#include <iostream> using namespace std; void error(const char *what) { fprintf(stderr, "ERROR: %s\n", what); cin.get(); exit(1); } void compileLiteral(const char *& s) { int v = 0; while (*s >= '0' && *s <= '9') { v = v*10 + *s++ - '0'; } printf(" mov eax, %i\n", v); } void compileSymbol(const char *& s) { printf(" mov eax, dword ptr "); while ((*s >= 'a' && *s <= 'z') || (*s >= 'A' && *s <= 'Z') || (*s >= '0' && *s <= '9') || (*s == '_')) { putchar(*s++); } printf("\n"); } void compileExpression(const char *&); void compileTerm(const char *& s) { if (*s >= '0' && *s <= '9') { // Number compileLiteral(s); } else if ((*s >= 'a' && *s <= 'z') || (*s >= 'A' && *s <= 'Z') || (*s == '_')) { // Variable compileSymbol(s); } else if (*s == '-') { // Unary negation s++; compileTerm(s); printf(" neg eax\n"); } else if (*s == '(') { // Parenthesized sub-expression s++; compileExpression(s); if (*s != ')') error("')' expected"); s++; } else { error("Syntax error"); } } void compileMulDiv(const char *& s) { compileTerm(s); for (;;)/> { if (*s == '*') { s++; printf(" push eax\n"); compileTerm(s); printf(" mov ebx, eax\n"); printf(" pop eax\n"); printf(" imul ebx\n"); } else if (*s == '/') { s++; printf(" push eax\n"); compileTerm(s); printf(" mov ebx, eax\n"); printf(" pop eax\n"); printf(" idiv ebx\n"); } else break; } } void compileAddSub(const char *& s) { compileMulDiv(s); for (;;)/> { if (*s == '+') { s++; printf(" push eax\n"); compileMulDiv(s); printf(" mov ebx, eax\n"); printf(" pop eax\n"); printf(" add ebx\n"); } else if (*s == '-') { s++; printf(" push eax\n"); compileMulDiv(s); printf(" mov ebx, eax\n"); printf(" pop eax\n"); printf(" sub ebx\n"); } else break; } } void compileExpression(const char *& s) { compileAddSub(s); } int main(int argc, const char *argv[]) { if (argc != 2) { error("Syntax: simple-compiler <expr>"); cout << endl; } compileExpression(argv[1]); return 0; }
I need to understand this code
Page 1 of 13 Replies - 733 Views - Last Post: 05 December 2012 - 06:29 AM
#1
I need to understand this code
Posted 04 December 2012 - 08:56 PM
I have this code for a recursive descent parser but i don't really understand it and im having trouble following the operation of it. I was given this code by my teacher and he told me to read through to understand my assignment better but i dont really get it, i dont even understand how you can even use the code to parse anything, i believe this may be written from a C coder perspective while i am a c++ user, then again i could be wrong. just need to know what all everything means.
Replies To: I need to understand this code
#2
Re: I need to understand this code
Posted 04 December 2012 - 09:33 PM
Do you understand what a recursive descent parser is? This is a pretty straightforward parser.
Start from from main and read line by line. Where do you find yourself lost?
Start from from main and read line by line. Where do you find yourself lost?
#3
Re: I need to understand this code
Posted 04 December 2012 - 09:36 PM
i understand the bulk of the code and what the functions do however im not sure on how you would use this code, he said that i can run it and play with it to see what outputs i get but i don't understand how to put input into it to get ANY output at all, im fairly new so please try to bare with me on this.
also every time i run this code i continue to get the error string from the error function in the upper portion of the code.
also every time i run this code i continue to get the error string from the error function in the upper portion of the code.
This post has been edited by yotic: 04 December 2012 - 09:38 PM
#4
Re: I need to understand this code
Posted 05 December 2012 - 06:29 AM
If you look at the main function, you should see compileExpression(argv[1]). That means you need to enter the expression as a commandline argument.
Page 1 of 1