%option noyywrap
%{
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void encode_word(const char* word);
void encode_string(const char* str);
void encode_number(const char* num);
void encode_variable
(const char
* var);%}
DIGIT [0-9]
ID [a-zA-Z][a-zA-Z0-9_]*
NUM {DIGIT}+
STRING \"([^\"\\]|\\.)*\"
%%
"IF" { encode_word("\x01"); }
"THEN" { encode_word("\x02"); }
"END" { encode_word("\x03"); }
"FOR" { encode_word("\x04"); }
"TO" { encode_word("\x05"); }
"NEXT" { encode_word("\x06"); }
"WHILE" { encode_word("\x07"); }
"DO" { encode_word("\x08"); }
"SKIP" { encode_word("\x09"); }
"CONTINUE" { encode_word("\x0A"); }
"PRINT" { encode_word("\x0B"); }
"PRINTLN" { encode_word("\x0C"); }
"AND" { encode_word("\x0D"); }
"OR" { encode_word("\x0E"); }
"NOT" { encode_word("\x0F"); }
"=" { encode_word("\x11"); }
"<>" { encode_word("\x12"); }
"<=" { encode_word("\x13"); }
">=" { encode_word("\x14"); }
"<" { encode_word("\x15"); }
">" { encode_word("\x16"); }
{NUM} {
char buf[32];
snprintf(buf, sizeof(buf), "\x10%c%c", (yytext[0] >> 8) & 0xFF, yytext[0] & 0xFF);
printf("%s", buf);
}
{ID} { encode_variable(yytext); }
{STRING} { encode_string(yytext); }
[ \t\n\r] ;
. { putchar(yytext[0]); }
%%
void encode_word(const char* word) {
putchar(word[0]);
}
void encode_string(const char* str) {
printf("%s", str);
}
void encode_number(const char* num) {
unsigned short value = (unsigned short)strtoul(num, NULL, 10);
printf("\x10%c%c", (value >> 8) & 0xFF, value & 0xFF);
}
void encode_variable
(const char
* var) { printf("\x17");
}
putchar('\x17');
}
int main(int argc, char** argv) {
yylex();
return 0;
}