|
Home > Archive > Unix Programming > February 2006 > Please, how to use a token to perform mathematicals operations in a C snipe program ?
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Please, how to use a token to perform mathematicals operations in a C snipe program ?
|
|
| freeposte 2006-02-19, 8:29 am |
| Hello dear membres of the comp.unix.programmer. Please , I've got the
following question to submit and I need your help.
Here below you'll find a lexical and a syntaxic analysers.
The lexical analyser (flex) find out variables, numbers and the '#'
character.
The syntaxic analyser (yacc) surveys "number diese number" patterns , or
"variable diese number" patterns .
Examples of correct syntax:
a#4
5#2
My intention is to consider the diese right following number, 4 and 2, in
order to be used to perform arithmetic operations.
Please is there any way to handle this "number" found out by "flex" and
returned to "yacc" as 'C' integer ?
Many Thanks
****************************************
**
The following is the lexical analyser :
****************************************
**
%{
/* need this for the call to atof() below */
#include <math.h>
#include<stdio.h>
#include<stdlib.h>
#include "y.tab.h"
%}
alpha [A-Za-z]
nums [0-9]
variable ({alpha}|{nums}|\$)({alpha}|{nums}|[_.\$])*
num1 [-+]?{nums}+\.?([eE][-+]?{nums}+)?
num2 [-+]?{nums}*\.{nums}+([eE][-+]?{nums}+)?
number {num1}|{num2}
diese [#]
%%
{number} { printf( "a number found : %s (%d)\n", yytext,
atoi( yytext ) );
return number;
}
{variable} { printf( "A variable found: %s\n", yytext );
return variable;
}
{diese} { return diese;}
"{"[\^{}}\n]*"}" /* eat up one-line comments */
[ \t\n]+ /* eat up whitespace */
.. printf( "Unrecognized character: %s\n", yytext );
%%
int yywrap ()
{
return (1);
}
****************************************
************************************
****************************************
**************************
herebelow the syntaxis analyser , please notice that the global C variable
"diese_number" is used here to replace the value of the token "number"
following the "diese" token
****************************************
************************************
****************************************
**************************
%{
#include <math.h>
#include<stdio.h>
#include<stdlib.h>
int diese_number=5;
%}
%token number
%token variable
%token diese
%start test
%%
test : test instruction
| instruction
;
instruction : number diese number
{ printf("The number following the # is : %d\n", diese_number);
//performing mathematicals operations with this number
}
| variable diese number
{ printf("The number following the # is : %d\n", diese_number);
//performing mathematicals operations with this number
}
;
%%
extern FILE *yyin;
int main (int argc, char * argv[])
{
++argv, --argc; /* skip over program name */
if ( argc > 0 )
yyin = fopen( argv[0], "r" );
else
yyin = stdin;
if ( yyparse() != 0 )
{
printf("Incorrect Syntax \n");
return 1;
}
else
{
return 0;
}
}
int yyerror (char * p)
{
printf (" %s ", p);
}
****************************************
*
The program generated parse the following test
****************************************
**
a#5
b#4
3#1
5#2
| |
| Ben Bacarisse 2006-02-19, 8:29 am |
| On Sun, 19 Feb 2006 12:56:29 +0100, freeposte wrote:
> Examples of correct syntax:
>
> a#4
> 5#2
>
> My intention is to consider the diese right following number, 4 and 2, in
> order to be used to perform arithmetic operations.
>
> Please is there any way to handle this "number" found out by "flex" and
> returned to "yacc" as 'C' integer ?
The flex rule should assign the value to be returned to yacc into
the global variable "yylval":
> {number} {
> printf( "a number found : %s (%d)\n", yytext, atoi( yytext ) );
yylval = atoi(yytex);
> return number;
> }
and yacc can use like this:
> instruction : number diese number
> { printf("The number following the # is : %d\n", $3);
> //performing mathematicals operations with this number
$1 and $3 will hold the values set by flex for the two numbers.
> }
--
Ben.
|
|
|
|
|