help-bison
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [help-bison]How to stock information?


From: Hans Aberg
Subject: Re: [help-bison]How to stock information?
Date: Wed, 20 Jul 2005 01:42:03 +0200

On 19 Jul 2005, at 17:39, soledady wrote:

if you receive for twice time this message i'm sorry i made a mistake


It it did come twice; I reply to the second address. [Please keep the cc to Help-Bison in replies, so that others can help.]

Hello, i'm using Bison for compute a mathematic tool to resolve this
kind of thing:
u(n)=u(n-1)+3*u(n-2);u{0}=1;u{1}=2;
 the example is very simple and this tools can do more things
So the subject:
 when I execute bison It builds a syntaxical tree (doesn't it?)

Not really. Bison supplies the constructs so that you can build a syntax trees in the parser, bottom up, if you so want. One can't immediately build them top-down.

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

 how recover number (they could change of course) in a correct order
cause i need to know how the formulation is written..
i give you a parse of the code: (the print are just here in order for me
to understand how does it works)

g    :    '=' d g            {printf("=");}
        |U '{'N '}' g        {printf("U{N}");}
        |U '{ N '-'NB'}'g       {printf("U{n-%d}",$5);}
        |U '{' N '+' NB '}' g   {printf("U{N+%d}",$5);}
        |U '{' NB '}' g        {printf("U{%d}",$3);}
        |d g            {printf(" ");}
        |V g            {printf(" ");}
        |/*empty*/        {printf(" ")}
d    :    g T             {printf(" ");}
        |NB ';'            {printf(" %d ",$1);}

T    :    /*vide*/        {printf("  ");}
        |NB T            {printf("%d",$1);}
        |'*' T            {printf("*");}
        |'/' T            {printf("/");}
        |'^' T            {printf("^");}
        |'+' T            {printf("+");}
        |'-' T            {printf("-");}
        |';' T            {printf(";");}
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.

more precisely i need to generate code in java and to do that i need
which kind of fomulation its given?

My guess is that you want to write Java code to later be executed. Then you could write actions that output say strings for use in M4, or Guile, which would generate the Java output. It something similar for a program writing code in C++, and then it turned out to be tricky to place various C++ constructs, such as #include without at least a macro program. M4 is though tricky to use, therefore Guile might be better.

Also, there is a program <http://codeworker.free.fr/> dedicated to this kind of task, you may want to have a look at.


thanks for your help!

if you need more precision let's continue:
moreover it's inside of a group of result globaly like that

for this code:the indentation are here just to be clearer
begin
     Name=Suite;
    begin
     DEFINITION=RECURRENTE;
     U{N}=U{N-1}+3*U{N-2};
     U{0}=1;
     U{1}=2;
     TERME[1..8];
     VARIATION;
     TYPE;
    SI_ARITH
        begin
         FORMULE_ARITH;
         SOMME_ARITH[1..8];
        end
    SI_GEO
        begin
         FORMULE_GEO;
         SOMME_GEO[1..8];
        end
     end
end


i'have this tree (the printf("blabla"); ) the indentation are here just
to be clearer

You just assign the suitable text values to the tokens, and start to build suitable strings or macros. Assume you have

statement:
  "{" statement_body "}"

statement_body: /* Builds "u(n)=u(n-1)+3*u(n-2);u{0}=1;u{1}=2;" */

Then you need an action for "statement" like
  statement: "{" statement_body "}" {
    $$ = "begin Name=Suite; begin DEFINITION=RECURRENTE;" + $1 + ...
  }
where "+" is string concatenation. I hope you see the idea.

You can simplify token handling by using Flex, and let every token return the text that is lexed.

  Hans Aberg






reply via email to

[Prev in Thread] Current Thread [Next in Thread]