fork download
  1. %option noyywrap
  2.  
  3. %{
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7.  
  8. void encode_word(const char* word);
  9. void encode_string(const char* str);
  10. void encode_number(const char* num);
  11. void encode_variable(const char* var);
  12. %}
  13.  
  14. DIGIT [0-9]
  15. ID [a-zA-Z][a-zA-Z0-9_]*
  16. NUM {DIGIT}+
  17. STRING \"([^\"\\]|\\.)*\"
  18.  
  19. %%
  20.  
  21. "IF" { encode_word("\x01"); }
  22. "THEN" { encode_word("\x02"); }
  23. "END" { encode_word("\x03"); }
  24. "FOR" { encode_word("\x04"); }
  25. "TO" { encode_word("\x05"); }
  26. "NEXT" { encode_word("\x06"); }
  27. "WHILE" { encode_word("\x07"); }
  28. "DO" { encode_word("\x08"); }
  29. "SKIP" { encode_word("\x09"); }
  30. "CONTINUE" { encode_word("\x0A"); }
  31. "PRINT" { encode_word("\x0B"); }
  32. "PRINTLN" { encode_word("\x0C"); }
  33. "AND" { encode_word("\x0D"); }
  34. "OR" { encode_word("\x0E"); }
  35. "NOT" { encode_word("\x0F"); }
  36. "=" { encode_word("\x11"); }
  37. "<>" { encode_word("\x12"); }
  38. "<=" { encode_word("\x13"); }
  39. ">=" { encode_word("\x14"); }
  40. "<" { encode_word("\x15"); }
  41. ">" { encode_word("\x16"); }
  42.  
  43. {NUM} {
  44. char buf[32];
  45. snprintf(buf, sizeof(buf), "\x10%c%c", (yytext[0] >> 8) & 0xFF, yytext[0] & 0xFF);
  46. printf("%s", buf);
  47. }
  48.  
  49. {ID} { encode_variable(yytext); }
  50.  
  51. {STRING} { encode_string(yytext); }
  52.  
  53. [ \t\n\r] ;
  54.  
  55. . { putchar(yytext[0]); }
  56.  
  57. %%
  58.  
  59. void encode_word(const char* word) {
  60. putchar(word[0]);
  61. }
  62.  
  63. void encode_string(const char* str) {
  64. printf("%s", str);
  65. }
  66.  
  67. void encode_number(const char* num) {
  68. unsigned short value = (unsigned short)strtoul(num, NULL, 10);
  69. printf("\x10%c%c", (value >> 8) & 0xFF, value & 0xFF);
  70. }
  71.  
  72. void encode_variable(const char* var) {
  73. printf("\x17");
  74. while (*var) {
  75. putchar(*var++);
  76. }
  77. putchar('\x17');
  78. }
  79.  
  80. int main(int argc, char** argv) {
  81. yylex();
  82. return 0;
  83. }
  84.  
Success #stdin #stdout #stderr 0.03s 6948KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
ERROR: /home/V90qOK/prog:4:1: Syntax error: Operator expected
ERROR: /home/V90qOK/prog:83:1: Syntax error: Unexpected end of file
ERROR: '$runtoplevel'/0: Undefined procedure: program/0
   Exception: (3) program ? EOF: exit