473,811 Members | 3,026 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

Hello dear membres of the comp.unix.progr ammer. 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}|{n ums}|[_.\$])*
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( "Unrecogniz ed character: %s\n", yytext );
%%
int yywrap ()
{
return (1);
}
*************** *************** *************** *************** *************** *
*************** *************** *************** *************** ******
herebelow the syntaxis analyser , please notice that the global C
variable
"diese_numb er" 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("Incorre ct 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 1595
Hollywood schrieb:
Hello dear membres of the comp.unix.progr ammer. 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
1535
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
1908
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; ... vector<lines> SourceVector; string one_line;
12
1851
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 then remove certain stopwords(like and, a, an, by, the etc) which is there in another txt file. Then, i need to run it thru a stemmer(a program which converts words like running to run, ie, converts them to roots words).
1
9667
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 and I was wondering if anyone here would be able to give me some tips for young players such as myself, for learning the language. Is this the best Newsgroup for support with JAVA?
66
5424
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 occurs. After the program has read the file, it should offer a menu with three choices. the first is to list all the words along with the number of occurences. The second is to let you enter a word, with the program reporting how many times the...
7
7362
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 basic, the value returned is 0. Can anyone tell me why the expression (4 and 1) return different value
2
6942
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: cannot perform PE operations on non PE output file When compiling to PE format no errors are generated, which leads me to believe that some post processing operations are being preformed. This error message does not help me figure what PE operations...
9
2520
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 *after* calling increase_arrays(), I received segmentation fault. I tried to step it through gdb, and I found out that after calling increase_arrays(), words's original value is modified, and if I tried to access it, I get <address 0x11 out of...
1
1353
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 my native dll. Does this method return a handle to the user's token? Or is there some other way to get it? Or is it better to do this in native code. Thanks,
0
9727
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10647
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10386
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10133
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9204
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6889
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4339
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3865
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3017
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.