Click here to Skip to main content
15,886,833 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I am having trouble understanding the formula. Can someone explain using the main as the syntax.

main()
{
}

How do I break that into a recursive formula to parse?

Is the formula just a formula to base some code on? Or is it also included with some code in the symbol table or something.

I am trying to parse my own main function in a source.
If this is not the approach, then what approach do I take to get c to understand my languages main so I can use rr for statements? This is not homework

What I have tried:

Tried reading some tuts on rr. I do not understand how exactly the rr gets its structure, or defined in the way it does to make a language. And if you use rr for statements and expressions how do you parse the function.
Posted
Updated 25-Apr-19 13:34pm
v12
Comments
Dave Kreskowiak 25-Apr-19 12:25pm    
A recursive function is just one that calls itself repeatedly until a bailout condition is met.

The Main function is never used for this.
Richard Deeming 25-Apr-19 12:41pm    
Are you talking about a recursive descent parser[^]?

I.e. trying to parse C++ syntax, rather than write a recursive C++ function.
Lymisy 25-Apr-19 12:48pm    
Yes a rr parser with syntax
Richard Deeming 25-Apr-19 13:10pm    
If you're looking for something to use in real code, perhaps this project might help:
foonathan/cppast: Library to parse and work with the C++ AST[^]

If this is a learning exercise, then it's rather ambitious. The C++ grammar[^] is quite large. It would probably be easier to start with something simpler.
Lymisy 25-Apr-19 20:08pm    
Thanks @ Richard Deeming I have been searching this for a while now, and you have pointed me into the right direction.

The main function can never be used in a recursive manner because main is a special function, it is the entry point of your program.
Quote:
How do I break that into a recursive formula?

Short answer, you don't because the function is empty.
In order to break a function into recursive code, you need a function with a recursive definition like factorial or Fibonacci.
Recursion - Wikipedia[^]
[Update]
Quote:
Can someone explain a recursive descent using a main C

In this sentence, the missing word parser matters.
When you ask for help, only a classmate can know what you talk about without details because he already know them. But we don't know the details.
For us, your title tells in a confuse way that you want to do a recursive function of main when instead you when to parse main with a recursive descent parser.
 
Share this answer
 
v2
Comments
k5054 25-Apr-19 12:49pm    
Quote:The main function can never be used in a recursive manner because main is a special function, it is the entry point of your program.

That's not strictly true. main() is a function like any other, and is actually called from crt0, the c run-time startup code (at least in a unix-like environment). The following code will compile and run:
#include <stdio.h>

int main(int argc, char **argv)
{
    if(argc == 1) {
        return 1;
    }
    else {
        return 1 + main(argc-1, argv+1);
    }
}

That being said, calling main() recursively is something that would require a very special set of circumstances - so don't do it unless you really, really, really need to, and understand the consequences of doing so.
Patrice T 25-Apr-19 13:40pm    
Even if "not strictly true", it is at least a bad habit.
k5054 25-Apr-19 15:33pm    
Turns out that recursing on main() is explicitly prohibited in C++. That's another situation where a valid C program would not be a valid C++ program.
Lymisy 25-Apr-19 19:44pm    
Sorry @Patrice T I did not know that it mattered until lunch today. I did not have a clear definition of what I was looking for. Why does everyone think I am a student? I am not. Just want to build a simple compiler. I apologize for the mistake.
Patrice T 25-Apr-19 19:56pm    
No problem, I just wanted to highlight the difficulty to help you when you don't say things.
"Why does everyone think I am a student?"
For us, it does not imply that you are in a school, we don't know your background.
For us, you are a student at least because you are studying the subject.
The main function isn't usually recursive: it's not impossible to do that, but it complicates matters when you try (if only because it's the first function called in your app, so it's pretty much impossible to set up the data it's going to process before the first call to it).

Normally, you'd do something like this:
C++
int main()
   {
   /* Set up the data */
   ...
   /* Call recursive function */
   int i = func(parameterValue);
   ...
   return 0;
   }
int func(int x)
   {
   ...
   func(modifiedValueOfX);
   ...
   }
Think of it that way, and then re-read your homework question.
 
Share this answer
 
a recursive function must have an exit condition, else it runs theoreticly infinite but in reality the stack runs out of memory because every call eats some memory.

Best examples for recursive functions are Fibonacci or factorial function.

I like the Divide-and-conquer algorithm for its power. Thats what programming makes fun. ;-)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900