Reputation: 47
I am writing a simple tokenization program for a calculator program to come later. Right now I have the program coded to output 'FALSE' whenever 'f' is entered by the user. What I am currently working on is making it so when the user enters '//' followed by a string, the program outputs "COMMENT: // string I entered". Here is my issue: Because I have 'f' and other single characters programmed to do other things, my program currently creates this type of output:
**User enters:**
//Protoypes
**Desired Output:**
COMMENT: //Prototypes
**Actual Output:**
COMMENT: // Pro
TRUEo
TRUEypes
Does anyone have a way of fixing this?
Here's the code so far:
%{
#include <stdio.h>
%}
%%
"//" printf("\nCOMMENT: // ", yytext);
"(" printf("\nLPAREN");
")" printf("\nRPAREN");
"=" printf("\nASSIGN");
"F" printf("\nFALSE");
"T" printf("\nTRUE");
"and" printf("\nAND");
"bye" printf("\nQUIT");
"else" printf("\nELSE");
"exit" printf("\nQUIT");
"f" printf("\nFALSE");
"false" printf("\nFALSE");
"if" printf("\nIf");
"implies" printf("\nIMPLIES");
\n printf("\nNEWLINE");
"not" printf("\nNOT");
"or" printf("\nOR");
"quit" printf("\nQUIT");
"stop" printf("\nQUIT");
"t" printf("\nTRUE");
"then" printf("\nTHEN");
"true" printf("\nTRUE");
"xor" printf("\nXOR");
%%
main(){
yylex();
}
Upvotes: 1
Views: 1581
Reputation: 310903
You need a final rule that catches anything else and ignores it. The default is to print any unmatched input:
. ;
Upvotes: 1