我正在为稍后的计算器程序编写一个简单的标记化程序。现在,只要用户输入“f”,我就会将程序编码为输出'FALSE'。我目前正在做的是当用户输入'//'后跟一个字符串时,程序输出“COMMENT://我输入的字符串”。这是我的问题:因为我有'f'和其他单个字符编程来做其他事情,我的程序目前创建这种类型的输出:
**User enters:**
//Protoypes
**Desired Output:**
COMMENT: //Prototypes
**Actual Output:**
COMMENT: // Pro
TRUEo
TRUEypes
有没有人有办法解决这个问题?
到目前为止,这是代码:
%{
#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();
}
答案 0 :(得分:1)
你需要一个能够捕捉到其他任何东西而忽略它的最终规则。默认设置是打印任何不匹配的输入:
. ;