I wrote specs for bison for a program that returns an arithmetic expression in RPN but it returns garbage
eg. 3+4 returns Đ"D¨*@ćxy"Đ"ć y"
here is my code -
%{
-
#define MAX_PATH_LENGTH 100
-
typedef struct {
-
char rpn[MAX_PATH_LENGTH];
-
} CharArray;
-
#define YYSTYPE CharArray
-
#include <math.h>
-
#include <stdio.h>
-
#include <ctype.h>
-
#include <string.h>
-
extern YYSTYPE yylval;
-
int par=0;
-
char a1[10];
-
char a2[10];
-
char o[10];
-
char rpn[100];
-
char* rpn_s (char *a1,char *a2,char *o);
-
void setP(int p);
-
-
%}
-
/* BISON Declarations */
-
%token RPN
-
%left '-' '+'
-
%left '*' '/'
-
%right '^' /* exponentiation */
-
/* Grammar follows */
-
%%
-
input: /* empty string */
-
| input line
-
;
-
line: '\n'
-
| exp '\n' { printf ("%s\n", rpn); }
-
;
-
exp: RPN { sprintf(a1, "%d", $1); strcpy(rpn, rpn_s(a1, "", "")); }
-
| exp '+' exp { sprintf(a1, "%d", $1);sprintf(a2, "%d", $3);sprintf(o, "%d", $2);strcpy(rpn, rpn_s(a1, a2, o)); }
-
| exp '-' exp { sprintf(a1, "%d", $1);sprintf(a2, "%d", $3);sprintf(o, "%d", $2);strcpy(rpn, rpn_s(a1, a2, o)); }
-
| exp '*' exp { sprintf(a1, "%d", $1);sprintf(a2, "%d", $3);sprintf(o, "%d", $2);strcpy(rpn, rpn_s(a1, a2, o)); }
-
| exp '/' exp { sprintf(a1, "%d", $1);sprintf(a2, "%d", $3);sprintf(o, "%d", $2);strcpy(rpn, rpn_s(a1, a2, o)); }
-
| exp '^' exp { sprintf(a1, "%d", $1);sprintf(a2, "%d", $3);sprintf(o, "%d", $2);strcpy(rpn, rpn_s(a1, a2, o)); }
-
| '(' exp ')' { $$ = $2;setP(1);}
-
;
-
%%
-
yylex ()
-
{
-
int c;
-
/* skip white space */
-
while ((c = getchar ()) == ' ' || c == '\t')
-
;
-
/* process numbers */
-
if (c == '.' || isdigit (c))
-
{
-
ungetc (c, stdin);
-
scanf ("%s", &yylval);
-
return RPN;
-
}
-
/* return end-of-file */
-
if (c == EOF)
-
return 0;
-
/* return single chars */
-
return c;
-
}
-
yyerror (s) /* Called by yyparse on error */
-
char *s;
-
{
-
printf ("%s\n", s);
-
}
-
main ()
-
{
-
yyparse ();
-
}
-
-
and here functions
thank You
1 2432
error are in the function so i have to paste it too -
-
char *rpn_s (char *a1,char *a2,char *o) {
-
-
-
-
-
-
-
-
-
char rpn_a[50];
-
char rpn_o[50];
-
char rpn[100];
-
int len_a = strlen(rpn_a);
-
int len_o = strlen(rpn_o);
-
-
-
rpn_a[len_a]=a1;
-
len_a = strlen(rpn_a);
-
rpn_a[len_a]=a2;
-
-
-
-
-
if(par==1){
-
par=0;
-
char temp;
-
char rpn_o_temp[len_o];
-
rpn_o_temp[0]=o;
-
int i;
-
-
-
for(i=1;i<=len_o;i++){
-
temp=rpn_o[i-1];
-
rpn_o_temp[i]=temp;
-
}
-
strcpy(rpn_o, rpn_o_temp);
-
-
-
-
-
-
-
-
-
}else rpn_o[len_o]=o;
-
-
-
strcpy( rpn, rpn_a);
-
strcat(rpn, " " );
-
strcat( rpn, rpn_o);
-
-
-
-
-
-
-
-
-
return (rpn);
-
}
-
seems like i need two dim array of strings ?
Sign in to post your reply or Sign up for a free account.
Similar topics
by: DOKNIK |
last post by:
Hi,
I am trying to install PHP with the Apache DSO Installation on AIX 5.1.
I have successfully configured and installed Apache and can view the Test
Apache page successfully
The installation...
|
by: pavel.orehov |
last post by:
Hi,
I am using flex and bizon to write HTTP parser.
I am passing well flex and bison tools but can't compile their output.
================= Flex file (http_parser.lpp) ==============
%{...
|
by: Santa |
last post by:
I am trying to run bison tool, I am getting "/usr/share/bison.simple:
No such file or directory" error, I have file bison.simple in
cygwin/bin directory, I am trying to copy into /usr/share...
|
by: daniele.calisi |
last post by:
Hi everyone,
after some days spent in long searches with google, I still not have
found a solution with this problem:
I need to use flex (or flex++) and bison (but not bison++) in order to...
|
by: John Sasso |
last post by:
In my Yacc .y file I defined:
%union {
int value;
struct Symbol Sym;
}
The Symbol struct I defined in a header file I #included in the Prologue
section of the .y file as:
|
by: jc |
last post by:
I have written a parser using bison and flex to read ASAP2 file for CAN
communications. entire development was done in an unix environment and
now the code is ready to be integrated to an existing...
|
by: Adam S |
last post by:
I'm using bison to generate a C++ parser targeted for a microcontroller
application. The command to generated C++ parser using bison is:
bison --skeleton=lalr1.cc <myfile.ypp>
and makes heavy...
|
by: eng.sharif |
last post by:
i want use bison and flex in visual stdio 2005 for make small
compiler by use Custom Build Rules in Visual C++ 2005 "FlexBison.rules
file "
but when i compile "example.y" i get error...
|
by: eng.sharif |
last post by:
want use bison and flex in visual stdio 2005 for make small
compiler by use Custom Build Rules in Visual C++ 2005
"FlexBison.rules
file "
but when i compile "example.y" i get error...
|
by: pendell |
last post by:
Okay.
So I've just installed Fedora core 7 fresh from my machine. It comes
with G++, and bison installed directly.
So I try to make a program that uses bison. C++ chokes with the
error:
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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,...
|
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,...
|
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...
| |