Related articles |
---|
recursive pasrer, arithmetic expansion matthieu.tourne@gmail.com (Matthieu Tourne) (2006-03-22) |
Re: recursive pasrer, arithmetic expansion clint@0lsen.net (Clint Olsen) (2006-03-27) |
From: | Clint Olsen <clint@0lsen.net> |
Newsgroups: | comp.compilers |
Date: | 27 Mar 2006 01:27:17 -0500 |
Organization: | Compilers Central |
References: | 06-03-076 |
Keywords: | parse |
Posted-Date: | 27 Mar 2006 01:27:17 EST |
On 2006-03-23, Matthieu Tourne <matthieu.tourne@gmail.com> wrote:
> I'm currently doing a parser for arithmetic expansions in a shell. but
> I'm experiencing some troubles making my parser recursive. My problem
> is: the variable foo can contain the string "2 + 2" the evaluation of
> $((foo + 3)) should be "7" I need to do parse(parse(foo) + 7), but I
> can't achieve it.
Hi:
1) Yyparse returns an integer (in Lemon it's void). It won't return the
tree you've constructed like you think it will.
2) Is the replacement for 'foo' text substitution? If so, you use a
_stack_ to lex from the text of foo. Then pop the stack when you're
done with foo. The lexer does this and the parser never sees it. This
is how #include is done: Text file inclusion introduces text fragments
into the token stream. It doesn't have to parse correctly in its
entirety.
3) If the intent is that foo form a proper expression, you can stash away
the tree constructed when you built the tree '2 + 2' when foo was
declared. Then when you see 'foo' later in an expression, you just grab
the tree you built out of a tree or hash table structure and splice it
right into your expression.
You'll need to think about what kind of evaluation you want to do in your
design (lazy or not). Consider how you handle if the symbol 'foo'
contained another symbol 'bar' which is meant to also be replaced? Do you
replace it right at foo definition time or when foo is encountered in an
expression?
Bison and flex now both support re-entrant code, so you can recursively
call them, but you need to make sure you know when to use it. Depending on
your strategy, you may not need it at all.
-Clint
Return to the
comp.compilers page.
Search the
comp.compilers archives again.