Please, how to use a token to perform mathematicals operations in a C snipe program ?

H

Hollywood

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
 
M

Michael Mair

Hollywood said:
Hello dear membres of the comp.unix.programmer. Please , I've got the
following question to submit and I need your help.

This is comp.lang.c.

-Michael
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top