Tuesday, March 1, 2016

LEX program that changes all numbers from decimal to hexadecimal notation, printing a summary statistic to stderr




%{
/* a LEX program that changes all numbers
   from decimal to hexadecimal notation,
   printing a summary statistic to stderr
*/
#include <stdlib.h>
#include <stdio.h>
int count = 0;
%}
digit [0-9]
number {digit}+
%%
{number} {
int n = atoi(yytext);
printf("%x", n);
if (n > 9) count++;
}
%%
main() {
yylex();
fprintf(stderr, "number of replacements = %d", count);
return 0;
}

No comments:

Post a Comment