How to save/restore the call stack?

K

kj

I've programmed C for many years, but I confess I don't even know
where to begin with this one...

I have a parser that proceeds down the text being parsed, and
generates a syntax tree for a potentially deeply nested structure.
This parser works fine, but it requires receiving all the input
text at once in one string.

But there are situations in which it would be nice if the input
string could be fed to the parser in smaller sequential chunks.
Here's where I draw a blank. It seems to me that for this to work
I'd need some way to have the parser save the states of the call
stack and the input buffer, and then restore it once it is called
again with the next chunk of input...

Assuming that this is even remotely on the right track, is there
a way for C code to save and restore the callstack? Can someone
point me to where I can learn more about how to do this?

TIA!

Kynn
 
B

BartC

kj said:
I've programmed C for many years, but I confess I don't even know
where to begin with this one...

I have a parser that proceeds down the text being parsed, and
generates a syntax tree for a potentially deeply nested structure.
This parser works fine, but it requires receiving all the input
text at once in one string.

But there are situations in which it would be nice if the input
string could be fed to the parser in smaller sequential chunks.
Here's where I draw a blank. It seems to me that for this to work
I'd need some way to have the parser save the states of the call
stack and the input buffer, and then restore it once it is called
again with the next chunk of input...

This doesn't sound right. You mean you want it to return at that point, so
the caller can grab some more text, then you want to call it again and it
carries on where it left off?

I don't think you need to mess about with the stack; there must be half a
dozen ways of achieving the result. When the parser runs out of text, it
calls a function that gets some more text. Or the parser calls a function to
obtain the next character, and /that/ function worries about getting the
next load of text.

Unless you want some sort of lookahead, but the callstack idea won't help
there.
 
K

Keith Thompson

kj said:
I've programmed C for many years, but I confess I don't even know
where to begin with this one...

I have a parser that proceeds down the text being parsed, and
generates a syntax tree for a potentially deeply nested structure.
This parser works fine, but it requires receiving all the input
text at once in one string.

But there are situations in which it would be nice if the input
string could be fed to the parser in smaller sequential chunks.
Here's where I draw a blank. It seems to me that for this to work
I'd need some way to have the parser save the states of the call
stack and the input buffer, and then restore it once it is called
again with the next chunk of input...

Assuming that this is even remotely on the right track, is there
a way for C code to save and restore the callstack? Can someone
point me to where I can learn more about how to do this?

No, there's no standard way in C to do that. (Well, you *might* be
able to do something with setjmp() and longjmp(), but I haven't used
them enough myself to be able to go into any detail.)

It sounds like you have a recursive-descent parser. You probably need
to use some other parsing technique. Questions about parsing
techniques aren't really C-specific, so comp.programming or perhaps
comp.compilers might be a better place to ask. A bit of Googling for
information about parsing might be a good start.
 
R

robertwessel2

I've programmed C for many years, but I confess I don't even know
where to begin with this one...

I have a parser that proceeds down the text being parsed, and
generates a syntax tree for a potentially deeply nested structure.
This parser works fine, but it requires receiving all the input
text at once in one string.

But there are situations in which it would be nice if the input
string could be fed to the parser in smaller sequential chunks.
Here's where I draw a blank.  It seems to me that for this to work
I'd need some way to have the parser save the states of the call
stack and the input buffer, and then restore it once it is called
again with the next chunk of input...

Assuming that this is even remotely on the right track, is there
a way for C code to save and restore the callstack?  Can someone
point me to where I can learn more about how to do this?


In standard C, about the best you can do is make the parser a state
machine.

What you’re asking about specifically is called a coroutine, and is a
terribly useful (and seriously undersupported) construct. Coroutines
are (unfortunately) not supported by standard C, but are implemented
on many platforms – “fibers” in Windows and POSIX systems provide (at
least) the setcontext family of functions which can be trivially used
to implement coroutines. You can also use threads (again not standard
C, but available on many platforms), to trivially emulate coroutines,
although that (might) not be particularly efficient, depending on the
implementation and the number of context switches you need to do.
 
T

Tony

In standard C,

For the masses, let me translate that:

"If you are oppressed or a masochist, C is for you: just board this train to
a nice relaxing shower"
 
G

Guest

("Out of my league discussion", that said, :)

why? I think the OP is on the wrong track. Instead of saving and
restoring the stack (if he really wants to go that way take a look at
co-routines (which are *not* part of C and hence would take some
effort to implement)) the get_token() idea seems fare more sensible.

"lookahead" (!). Seems an archaic and irrelevant concept in light of memory
available and "ubiquitousness" of separate compliation.

could you expand on that? Maybe the string he is parsing is megabytes
long.
Or maybe the data comes from a socket. It might take a long time to
read the
string so it makes sense to start parsing before you have all the
string.

To me, "lookahead" looks like "look ahead (to a language that is devoid of
the wrong way to do things

and again, in english.

"a journal is just a journal, but a log is really a log!!"

Adding a apace to "lookahead" adds no semantics so far as I am
concerned.

Are you just gnomic for gnomic's sake?
 
G

Guest

For the masses, let me translate that:

"If you are oppressed or a masochist, C is for you: just board this train to
a nice relaxing shower"- Hide quoted text -

I invoke Godwin's Law
 
J

jfbode1029

Do you also invoke Newton's First Law of Motion when someone starts
walking?
You loose

Nick can be a bit unrestrained at times. You lose on the spelling
front, though.
 
D

Dik T. Winter

>
> That's a stupid NON-answer (at best). Just curb it "troll". :p

Perhaps, I will give the solution for Sparc Solaris and you can possibly
tell me how to do that in C.

(1) execute a trap instruction with argument 3 (this will flush the
register windows)
(2) create a pointer to the current frame and from that follow the call
stack.

Now, how to do that in C? Especially (1)?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top