In my bison/flex program, right after yyparse() is called, a leading tab is printed, but I don't know why. Can you see what's wrong?
This calls the bison code, and right after yyparse() returns, a tab is printed.
void parseArguments(int argc, char** argv)
130 {
131 int i;
132
133 int sum = 0;
134 // calculate the length of buffer we need
135 for(i = 1; i < argc; i++)
136 {
137 sum += strlen(argv[i]) + 1;
138 }
139
140 if(sum <= 0)
141 return;
142
143 // make us a buffer and zero it out
144 char tempBuffer[sum];
145 memset(tempBuffer, 0, sum);
146
147 // pointer to walk through our buffer
148 int pos = 0;
149
150 // copy arguments into the buffer
151 for(i = 1; i < argc; i++)
152 {
153 memcpy(tempBuffer+pos, argv[i], strlen(argv[i]));
154 pos += strlen(argv[i]);
155 sprintf(tempBuffer+pos++, " ");
156 }
157
158 // give our arguments to lex for parsing
159 configBuffer(tempBuffer);
160
// use bison parsing
163 int returnVal = yyparse(); // after this returns a tab character has been printed
164 if(returnVal != 0)
165 {
166 printf("yyparse failed!\n");
167 }
168
All my bison rules are just regular expressions paired with a return statement. The only code of interest in bison that I could see affecting this would be this:
64 %%
65 void configBuffer(char* arguments)
66 {
67 #ifdef DEBUG
68 printf("Given the buffer: %s\n", arguments);
69 #endif
70 yy_delete_buffer(YY_CURRENT_BUFFER);
71
72 yy_scan_string(arguments);
73 }
I tried the suggestions given by several people, but still not luck. Here is my full flex file:
%{
#include <string.h>
#include "CommandParser.tab.h"
%}
%%
\t {
printf("TAB!\n");
}
" " {
printf("SPACE!\n");
}
\n {
return;
}
-p {
return PRINTMODE;
}
-x {
return XORMODE;
}
-n {
return NOTMODE;
}
-a {
return ANDMODE;
}
-o {
return ORMODE;
}
-r {
return RANGEFLAG;
}
-l {
return LENGTHFLAG;
}
0[xX][0-9a-fA-F]+ {
int retVal = sscanf(yytext, "%x",&(yylval.int_val));
if(retVal != 1)
return;
return NUMBER;
}
[0-9]+ {
yylval.int_val = atoi(yytext);
return NUMBER;
}
['"].*+['"] {
yylval.string_val = strdup(yytext);
return ARGUMENT;
}
[^ \t\n]+ {
yylval.string_val = strdup(yytext);
return ARGUMENT;
}
%%
void configBuffer(char* arguments)
{
#define DEBUG
#ifdef DEBUG
printf("Given the buffer: %s:\n", arguments);
#endif
yy_delete_buffer(YY_CURRENT_BUFFER);
yy_scan_string(arguments);
}