On 20 Jul 2005, at 07:49, soledady wrote:
can i construct a top-down grammar?
It isn't the grammar that is top-down or not -- it is the parse tree
that one build. The Bison generated parser builds it bottom-up,
because Bison uses LALR(1). The other method is LL(k). In Bison, one
sometimes has to introduce a global variable, which carries some
semantic information. That is essentially a top-down construct.
For example, if you have a rule
term:
term "+" term {...}
then $1, $2, and $3 will be used to assign values to the RHS
occurrences of "term", which can be forwarded to the LHS by the use
of $$. If you let $$ be the root of a tree, and $1, $2 and $3 be
branches attached to $$, you get a syntax tree:
$$
/ | \
$1 $2 $3
the $$ could be use again in other rule couldn't it?
Every rule and token has its own semantic value, represented locally
in the rule actions by the symbol $$.
just a precision what the meaning of RHS and LHS(Right...? and Left
something?)
See
http://en.wikipedia.org/wiki/LHS
http://en.wikipedia.org/wiki/RHS
so for this part of an example (without all the rest of the example)
U{N}=U{N-1}+3*U{N-2}; U{0}=1; U{1}=2;
it's give me:
*3+ ; 1 2 =U{1}=U{0} U{n-2} U{n-1} =U{N}
how can i do to recover correctly which number for which part of
code ?
My guess is that you ask for the semantic value. It will be held in
the variables $$ and $k.
ok but how can i do to know that *3+ means +3*
and to know which kind of term (u(n) or u(n+1) and so on...) are in
use
Yo define a grammar, just as you already have started, and then in
each action you have the input semantic values $1, $2, ..., $k, where
k is the rule length, and then you use them to define a value of $$.
Bison generates a parser that makes sure that his hangs together.
statement:
"{" statement_body "}"
statement_body: /* Builds "u(n)=u(n-1)+3*u(n-2);u{0}=1;u{1}=2;" */
yes but how can i built this expression
You have to build a grammar. This is difficult at firts. The Bison
manual has a calculator example. The book by Aho, Sethi, Ullman,
"Compilers" (the "dragon book") has an example of using Lex and Yacc.
Look in grammar-like BNF notation of already existing computer
languages, as that of the C/C++ standards. The newsgroup
comp.compilers has a FAQ, published there monthly, which you can look
into.
Hans Aberg
_______________________________________________
Help-bison@gnu.org http://lists.gnu.org/mailman/listinfo/help-bison