I am attempting to create a lexical analyzer that will return the length of a token in a text file.
I have a text file with a single letter 'a' in it.
The following is my lex.l file
%option noyywrap
%{
%}
/* regular definitions */
delim [ \t\n]
ws {delim}+
letter [A-Za-z]
digit [0-9]
%%
{ws} {/* no action */}
letter {return 1;}
%%
The following is the main program file that uses the YYText() and YYLeng() function.
#include <stdio.h>
#include <stdlib.h>
#include "lex.yy.cc"
using namespace std;
int OpenInputOutput(int argc, char *argv[], ifstream & lexerFIn, ofstream & lexerFOut)
{
// open input
if (argc > 1) {
lexerFIn.open(argv[1]);
if (lexerFIn.fail()) {
cerr << "Input file cannot be opened\n";
return 0;
}
}
else {
cerr << "Input file not specified\n";
return 0;
}
// open output
lexerFOut.open("Output.txt");
if (lexerFOut.fail()) {
cerr << "Output file cannot be opened\n";
return 0;
}
}
int main(int argc, char *argv[])
{
yyFlexLexer lexer;
while (lexer.yylex() != 0) {
cout << lexer.YYText() << endl;
cout << lexer.YYLeng() << endl;
}
return 0;
}
When I run the program with the aforementioned text file, with the command ./a "sample.txt", it writes 'a' on a file. Why doesn't it cout YYText() or YYLeng() or write the length of the character in the output file?
{letter}?letter(without the braces) matches exactly that six-letter literal. - Tim Landscheidt