Reputation: 645
I have written Flex stuff in a previous class but none of my previously working code is solving the issue I am having.
I have searched around StackOverflow for a solution but none of them have solved it.
I have:
Here is my code (I removed all the tokens and such because there is a lot of them):
%{
...
int numLines = 0;
void printTokenInfo(char* tokenType, char* lexeme);
void handleComments(char* text);
%}
WSPACE [ \t\r]+
NEWLINE \n
DIGIT [0-9]
LETTER [a-zA-Z]
IDENT ({LETTER}|_)({LETTER}|{DIGIT}|_)*
INTCONST {DIGIT}+
CHARCONST "'"{LETTER}+"'"
%%
...
%%
// User-written code goes here
void printTokenInfo(char* tokenType, char* lexeme)
{
printf("A");
printf("TOKEN: %s LEXEME: %s\n", tokenType, lexeme);
}
void handleComments(char* text)
{
printf("%s\n", text);
}
int yywrap() { return 1; }
int main()
{
do {
yylex();
} while (!feof(yyin));
return 0;
}
Here is how I am compiling and running it:
flex FILENAME.l
g++ lex.yy.c -o lexer
lexer < INPUT.txt
And the instructor provided us with input files but none of them have worked. They all fail with 'premature EOF' or 'bad character'
Any ideas?
Upvotes: 0
Views: 389
Reputation: 645
Well, I think I finally discovered the answer... Try running it with the complete path rather than just the name of your compiled lexer. I discovered the 'actual' path by running it under gdb (Which admittedly should have been my first instinct).
gdb lexer
(gdb) run < INPUT.txt
Originally, I was trying to run it with:
lexer < INPUT.txt
But I discovered by running it with gdb that this worked:
/nethome/users/mjc7w6/Classes/lexer < INPUT.txt
Edit: Someone chimed in on my Facebook with a further improvement. If the above solution fixes it for you, you might need to edit your ~/.bashrc with the following:
export PATH=/nethome/users/mjc7w6/Classes:$PATH
Or however you find that path to be set-up.
Upvotes: 2