[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Question: programmable calculator (help for a newbie)
From: |
Hans Aberg |
Subject: |
Re: Question: programmable calculator (help for a newbie) |
Date: |
Thu, 13 Nov 2003 19:08:43 +0100 |
[Replies to the Help-Bison list.]
At 13:18 +0100 2003/11/13, Julián Calderón Almendros wrote:
>My only problem is: How do I evaluate a function? I take the expression of
>the function and I puts it into one string and that into a map<string
>name_of_function,string expresion_of_function>. I don't know if the problem
>idea is well expressed because my english is very bad.
If you want to have context sensitive definitions, as in your grammar, you
need to create a lookup table. You can store grammar type in this lookup
table and probably an object value as well. Then, when the lexer sees an
identifier, it checks if the name is on the lookup table, and returns the
grammar type and the object value as well; otherwise it just returns the
"identifier" grammar type. One can also have a stacked table, to create the
types of environments that are common in computer languages like C/C++,
etc. (For example, push it for every "{" and pop it for every "}").
Then your grammar might contain say (using pseudocode for the action):
%token unary_function
%token left_parenthesis "("
%token right_parenthesis ")"
...
%
...
expression:
...
| unary_function "(" expression ")" {
$$.value = apply_unary($1.value, $3.value);
}
At 13:18 +0100 2003/11/13, Julián Calderón Almendros wrote:
>The scheme of the grammar was more or less :
...
>%%
>input:
> | input line
>;
>line: TK_EOL
> | expression TK_EOL
> | def_funct TK_EOL
>;
>expression: value
> | TK_OPEN expression TK_BINARY_OPERATION
>TK_VALUE TK_CLOSE
> | TK_OPEN TK_UNARY_PREFIX_OPERATION expression
>TK_CLOSE
> | TK_OPEN expression TK_UNARY_SUFIX_OPERATION
>TK_CLOSE
>;
>value: TK_NUM
> | TK_VAR
> | TK_EVAL_FUNCTION TK_FUNCTION TK_OPEN lst_args
>TK_CLOSE
>;
>...
>;
>def_funct: TK_FUNCT_NAME TK_OPEN formal_lst_args TK_ASIGNATION
>expression
>;
>...
>;
>%%
>____________________________________________________________________________
>____
>My only problem is: How do I evaluate a function? I take the expression of
>the function and I puts it into one string and that into a map<string
>name_of_function,string expresion_of_function>. I don't know if the problem
>idea is well expressed because my english is very bad.
Hans Aberg