%{
#include <stdio.h>
#include <stdlib.h>
void decimal_to_hexadecimal(int decimal) {
char hex[100];
int index = 0;
if (decimal == 0) {
printf("Hexadecimal: 0\n");
return;
}
printf("Decimal: %d\n", decimal);
// Perform the conversion by repeated division
while (decimal > 0) {
int remainder = decimal % 16;
if (remainder < 10) {
hex[index] = '0' + remainder;
} else {
hex[index] = 'A' + (remainder - 10);
}
printf("Step: Decimal = %d / 16, Remainder = %d, Quotient = %d\n", decimal, remainder, decimal / 16);
decimal = decimal / 16;
index++;
}
// Hexadecimal digits are stored in reverse order
printf("Hexadecimal: ");
for (int i = index - 1; i >= 0; i--) {
printf("%c", hex[i]);
}
printf("\n");
}
%}
%%
[0-9]+ {
int decimal = atoi(yytext);
decimal_to_hexadecimal(decimal);
}
\n ;
. ; // Ignore all other characters
%%
int main() {
yylex();
return 0;
}
JXsKI2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxzdGRsaWIuaD4KCnZvaWQgZGVjaW1hbF90b19oZXhhZGVjaW1hbChpbnQgZGVjaW1hbCkgewogICAgY2hhciBoZXhbMTAwXTsKICAgIGludCBpbmRleCA9IDA7CiAgICAKICAgIGlmIChkZWNpbWFsID09IDApIHsKICAgICAgICBwcmludGYoIkhleGFkZWNpbWFsOiAwXG4iKTsKICAgICAgICByZXR1cm47CiAgICB9CgogICAgcHJpbnRmKCJEZWNpbWFsOiAlZFxuIiwgZGVjaW1hbCk7CiAgICAKICAgIC8vIFBlcmZvcm0gdGhlIGNvbnZlcnNpb24gYnkgcmVwZWF0ZWQgZGl2aXNpb24KICAgIHdoaWxlIChkZWNpbWFsID4gMCkgewogICAgICAgIGludCByZW1haW5kZXIgPSBkZWNpbWFsICUgMTY7CiAgICAgICAgaWYgKHJlbWFpbmRlciA8IDEwKSB7CiAgICAgICAgICAgIGhleFtpbmRleF0gPSAnMCcgKyByZW1haW5kZXI7CiAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgaGV4W2luZGV4XSA9ICdBJyArI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