473,394 Members | 1,748 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,394 software developers and data experts.

bison RPN

oll3i
679 512MB
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

Expand|Select|Wrap|Line Numbers
  1. %{
  2. #define MAX_PATH_LENGTH 100
  3. typedef struct {
  4.   char rpn[MAX_PATH_LENGTH];
  5. } CharArray;
  6. #define YYSTYPE CharArray
  7. #include <math.h>
  8. #include <stdio.h>
  9. #include <ctype.h>
  10. #include <string.h>
  11. extern YYSTYPE yylval;
  12. int par=0;
  13. char a1[10];
  14. char a2[10];
  15. char o[10];
  16. char rpn[100];
  17. char* rpn_s (char *a1,char *a2,char *o);
  18. void setP(int p);
  19.  
  20. %}
  21. /* BISON Declarations */
  22. %token RPN
  23. %left '-' '+'
  24. %left '*' '/'
  25. %right '^'    /* exponentiation        */
  26. /* Grammar follows */
  27. %%
  28. input:    /* empty string */
  29.         | input line
  30. ;
  31. line:     '\n'
  32.         | exp '\n'  { printf ("%s\n", rpn); }
  33. ;
  34. exp:      RPN                { sprintf(a1, "%d", $1);  strcpy(rpn, rpn_s(a1, "", "")); }
  35.         | exp '+' exp        { sprintf(a1, "%d", $1);sprintf(a2, "%d", $3);sprintf(o, "%d", $2);strcpy(rpn, rpn_s(a1, a2, o)); }
  36.         | exp '-' exp        { sprintf(a1, "%d", $1);sprintf(a2, "%d", $3);sprintf(o, "%d", $2);strcpy(rpn, rpn_s(a1, a2, o)); }
  37.         | exp '*' exp        { sprintf(a1, "%d", $1);sprintf(a2, "%d", $3);sprintf(o, "%d", $2);strcpy(rpn, rpn_s(a1, a2, o)); }
  38.         | exp '/' exp        { sprintf(a1, "%d", $1);sprintf(a2, "%d", $3);sprintf(o, "%d", $2);strcpy(rpn, rpn_s(a1, a2, o)); } 
  39.         | exp '^' exp        { sprintf(a1, "%d", $1);sprintf(a2, "%d", $3);sprintf(o, "%d", $2);strcpy(rpn, rpn_s(a1, a2, o)); }
  40.         | '(' exp ')'        { $$ = $2;setP(1);}
  41. ;
  42. %%
  43. yylex ()
  44. {
  45.   int c;
  46.   /* skip white space  */
  47.   while ((c = getchar ()) == ' ' || c == '\t')  
  48.     ;
  49.   /* process numbers   */
  50.   if (c == '.' || isdigit (c))                
  51.     {
  52.       ungetc (c, stdin);
  53.       scanf ("%s", &yylval);
  54.       return RPN;
  55.     }
  56.   /* return end-of-file  */
  57.   if (c == EOF)                            
  58.     return 0;
  59.   /* return single chars */
  60.   return c;                                
  61. }
  62. yyerror (s)  /* Called by yyparse on error */
  63.      char *s;
  64. {
  65.   printf ("%s\n", s);
  66. }
  67. main ()
  68. {
  69.   yyparse ();
  70. }
  71.  
  72.  
and here functions

thank You
Jan 17 '09 #1
1 2432
oll3i
679 512MB
error are in the function so i have to paste it too
Expand|Select|Wrap|Line Numbers
  1.  
  2. char *rpn_s (char *a1,char *a2,char *o) {
  3.  
  4.  
  5.  
  6.  
  7.  
  8.  
  9.  
  10.  
  11. char rpn_a[50];
  12. char rpn_o[50];
  13. char rpn[100];
  14. int len_a = strlen(rpn_a);
  15. int len_o = strlen(rpn_o);
  16.  
  17.  
  18. rpn_a[len_a]=a1;
  19. len_a = strlen(rpn_a);
  20. rpn_a[len_a]=a2;
  21.  
  22.  
  23.  
  24.  
  25. if(par==1){
  26. par=0;
  27. char temp;
  28. char rpn_o_temp[len_o];
  29. rpn_o_temp[0]=o;
  30. int i;
  31.  
  32.  
  33. for(i=1;i<=len_o;i++){
  34. temp=rpn_o[i-1];
  35. rpn_o_temp[i]=temp;
  36. }
  37. strcpy(rpn_o, rpn_o_temp);
  38.  
  39.  
  40.  
  41.  
  42.  
  43.  
  44.  
  45.  
  46. }else rpn_o[len_o]=o;
  47.  
  48.  
  49. strcpy( rpn, rpn_a);
  50. strcat(rpn, " " );
  51. strcat( rpn, rpn_o);
  52.  
  53.  
  54.  
  55.  
  56.  
  57.  
  58.  
  59.  
  60. return (rpn); 
  61. }
  62.  
seems like i need two dim array of strings ?
Jan 18 '09 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

1
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...
8
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) ============== %{...
3
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...
4
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...
3
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:
13
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...
1
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...
2
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...
1
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...
3
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: ...
0
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...
0
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
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
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,...
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
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...

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.