Robert Aroutiounian Robert Aroutiounian - 1 month ago 16
C Question

Unrecognized Rule in lex file

I was wondering why I keep getting an unrecognized run on these two lines


(\${number})

({number})


Here is my code

/* % must be in the first column (no indentation) */

/* to avoid warnning about yywrap() if we do not need it */
%option noyywrap

%{ // must start in the first column
#include "l7t1.h"
int number_of_target_words = 0; // TODO: complete
int sum_of_numbers = 0; // TODO: complete
int totDol = 0;
int numChar;
int targetArr[9];
%} // must start in the first column

/* aliasses */
letter [A-Za-z]
digit [0-9]
number ({digit})+(\.)?({digit})* // TODO: complete
word {letter}+ // TODO: complete

/* matching rules */
%%

bank(s) {/* match action */
number_of_target_words++;
printf("<!>");
ECHO;
printf("<!>\n");
targetArr[BANK] += 1;
return BANK;
}

dollar(s) {
number_of_target_words++;
printf("<!>");
ECHO;
printf("<!>\n");
targetArr[DOLLAR] += 1;
return DOLLAR;
} // TODO: complete

finance(s|d) {
number_of_target_words++;
printf("<!>");
ECHO;
printf("<!>\n");
targetArr[FINANCE] += 1;
return FINANCE;
}

budget(s|ed) {
number_of_target_words++;
printf("<!>");
ECHO;
printf("<!>\n");
targetArr[BUDGET] += 1;
return BUDGET;
}

fund(ing|s|ed) {
number_of_target_words++;
printf("<!>");
ECHO;
printf("<!>\n");
targetArr[FUND] += 1;
return FUND;
}

stock(s|ed) {
number_of_target_words++;
printf("<!>");
ECHO;
printf("<!>\n");
targetArr[STOCK] += 1;
return STOCK;
}

bond(s|ed) {
number_of_target_words++;
printf("<!>");
ECHO;
printf("<!>\n");
targetArr[BOND] += 1;
return BOND;
}

invest(ing|ment(s)?|er|ed) {
number_of_target_words++;
printf("<!>");
ECHO;
printf("<!>\n");
return INVEST;
}

(\${number}) {
numChar = strtod(yytext+1, NULL);
totDol += numChar;
sum_of_numbers += numChar;
printf("<$>%f<$>\n", numChar);
} // TODO: complete

({number}) {
numChar = strtod(yytext, NULL);
sum_of_numbers += numChar;
printf("<#>%f<#>\n", numChar);
}

%%


I am unsure as to why this is happening, I believe my aliases are correct, but if someone could double check I would really appreciate it.

Answer

Flex is not C its uses its own language. A language where whitespace counts and all comments need their own line(s). So: get rid of the line-comments after the definitions.

/* Does not work */
letter [A-Za-z]
digit [0-9]
number ({digit})+(\.)?({digit})* // TODO: complete
word {letter}+ // TODO: complete


/* Does work */
letter [A-Za-z]
digit [0-9]
number ({digit})+(\.)?({digit})*
word {letter}+
Comments