473,406 Members | 2,259 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

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

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

Feb 19 '06 #1
1 1580
Hollywood schrieb:
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
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Feb 19 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
by: Jenski182 | last post by:
Is this from a good coder? or a rubbish one? Whichever way, it was paid for his services, just want to know if its worth while.... Thanks Jen x #include <iostream.h> #include <conio.h>
6
by: kittykat | last post by:
Hello, I am writing a program that will read each line of a file into a vector of vectors. This data will then be analysed. Here is what i have done: typedef vector<string> lines; ......
12
by: Hp | last post by:
Hi All, Thanks a lot for all your replies. My requirement is as follows: I need to read a text file, eliminate certain special characters(like ! , - = + ), and then convert it to lower case and...
1
by: David Van D | last post by:
Hi there, A few weeks until I begin my journey towards a degree in Computer Science at Canterbury University in New Zealand, Anyway the course tutors are going to be teaching us JAVA wth bluej...
66
by: genestarwing | last post by:
QUESTION: Write a program that opens and read a text file and records how many times each word occurs in the file. Use a binary search tree modified to store both a word and the number of times it...
7
by: rguarnieri | last post by:
Hi! I'm trying to create a query with a boolean expression like this: select (4 and 1) as Value from Table1 this query return always -1, but when I make the same calculation in visual...
2
by: Remi.Arntzen | last post by:
I tried to compile a program to a elf shared library for a Linux platform on a windows machine with the command: gcc -o Prog.so -shared -Wl,--oformat -Wl,elf32-i386 however this results in ld:...
9
by: weidongtom | last post by:
Hi, I've written the code that follows, and I use the function add_word(), it seems to work fine *before* increase_arrays() is called that uses realloc() to allocate more memory to words. But...
1
by: Roshan | last post by:
Hi, In my program I need the handle to the current user's token to perform some operations in native code. I am trying to get the handle using WindowsIdentity.GetCurrent().Token and pass it to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.