%{ int soma = 0, total = 0; %} numero [0-9]+ %x ContextoSoma %% {numero} { total += atoi(yytext); } "+" { BEGIN ContextoSoma; } {numero} { soma += atoi(yytext); total += atoi(yytext); } "+" { printf("Soma parcial: %d\n", soma); BEGIN INITIAL; } .|\n { ; } .|\n { ; } %% /* "=" { printf("Soma parcial: %d\n",soma); soma = 0; } */ int yywrap() { return(1); } int main() { yylex(); printf("Soma total: %d\n",total); return 0; }