# I need to understand this code

Page 1 of 1

## 3 Replies - 520 Views - Last Post: 05 December 2012 - 06:29 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=302790&amp;s=6089db0b8010eb86b930d173eec9b3fc&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 yotic

Reputation: -3
• Posts: 50
• Joined: 11-October 11

# 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.

```#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;
}
}

{
compileMulDiv(s);
for (;;)/>
{
if (*s == '+')
{
s++;
printf("    push eax\n");
compileMulDiv(s);
printf("    mov  ebx, eax\n");
printf("    pop  eax\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)
{
}

int main(int argc, const char *argv[])
{
if (argc != 2)
{
error("Syntax: simple-compiler <expr>");
cout << endl;
}
compileExpression(argv[1]);
return 0;
}

```

Is This A Good Question/Topic? 0

## Replies To: I need to understand this code

### #2 Oler1s

• D.I.C Lover

Reputation: 1396
• Posts: 3,884
• Joined: 04-June 09

## 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?

### #3 yotic

Reputation: -3
• Posts: 50
• Joined: 11-October 11

## 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.

This post has been edited by yotic: 04 December 2012 - 09:38 PM

### #4 Oler1s

• D.I.C Lover

Reputation: 1396
• Posts: 3,884
• Joined: 04-June 09

## 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.