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;
}

1 comment:

  1. This is my first comment here which I just wanted to give a quick shout out and tell you I truly enjoy reading your blog posts. Thank you for sharing. visit when is rhema university post utme form coming out

    ReplyDelete