(→The Solution) |
|||
(2 intermediate revisions by the same user not shown) | |||
Line 7: | Line 7: | ||
== The Solution == | == The Solution == | ||
− | <text> | + | <source lang="text"> |
%option 8bit noyywrap yylineno stack | %option 8bit noyywrap yylineno stack | ||
%{ | %{ | ||
Line 32: | Line 32: | ||
"/*" yy_push_state(X_COMMENT); | "/*" yy_push_state(X_COMMENT); | ||
− | |||
<X_COMMENT>"*/" yy_pop_state(); | <X_COMMENT>"*/" yy_pop_state(); | ||
<X_COMMENT>.|\n ; | <X_COMMENT>.|\n ; | ||
Line 44: | Line 43: | ||
return yylex(); | return yylex(); | ||
} | } | ||
− | </ | + | </source> |
− | [[category:Compiladores]] | + | [[category:Compiladores|Flex Lexical Analyzer]] |
[[category:Ensino]] | [[category:Ensino]] |
A internacionalização de software é uma tarefa que requer a localização de todas as cadeias de caracteres que existem em programas. Para facilitar o processo de tradução das mensagens, crie um analisador lexical (utilizando a ferramenta Flex) que permita localizar e apresentar na saída todas as cadeias de caracteres de programas escritos em C++, assim como a linha em que ocorrem. Note que nem todas as ocorrências do carácter '\"' correspondem a delimitadores de cadeias de caracteres.
[EXPLANATION COMING SOON]
%option 8bit noyywrap yylineno stack
%{
#include <iostream>
inline void yyerror(const char *msg) {
std::cerr << "Error at " << yylineno << ": " << msg << std::endl;
}
%}
%x X_CHAR X_STRING X_COMMENT
%%
\' yy_push_state(X_CHAR);
<X_CHAR>\\\' ;
<X_CHAR>\' yy_pop_state();
<X_CHAR>. ;
<X_CHAR>\n yyerror("newline in character constant");
\" yy_push_state(X_STRING); std::cout << std::endl << yylineno << ": ";
<X_STRING>\\\" ECHO;
<X_STRING>\\\\ ECHO;
<X_STRING>\" yy_pop_state();
<X_STRING>. ECHO;
<X_STRING>\n yyerror("newline in string");
"/*" yy_push_state(X_COMMENT);
<X_COMMENT>"*/" yy_pop_state();
<X_COMMENT>.|\n ;
"//".*$ ;
.|\n ; /* ignore the rest */
%%
int main() {
return yylex();
}