fork download
  1. %{
  2. #include <iostream>
  3. #include <string>
  4. #include <unordered_map>
  5. using namespace std;
  6.  
  7. unordered_map<string, int> symbol_table;
  8. int location_counter = 0;
  9. %}
  10.  
  11. %%
  12. ^START[ \t]+[0-9]+ { location_counter = atoi(yytext + 5); }
  13. ^[A-Za-z][A-Za-z0-9]*:[ \t]*DS[ \t]+[0-9]+ { symbol_table[string(yytext, yyleng - 4)] = location_counter; }
  14. ^[A-Za-z][A-Za-z0-9]*:[ \t]* { symbol_table[string(yytext, yyleng - 1)] = location_counter; }
  15. .* { if (strcmp(yytext, "END") != 0) location_counter++; }
  16. %%
  17.  
  18. int main(int argc, char** argv) {
  19. // Run the lexer on the input
  20. yylex();
  21.  
  22. // Output the final symbol table
  23. cout << "Symbol Table:" << endl;
  24. for (const auto& pair : symbol_table) {
  25. cout << pair.first << "\t" << pair.second << endl;
  26. }
  27.  
  28. return 0;
  29. }
  30.  
  31. int yywrap() {
  32. return 1;
  33. }
  34.  
Success #stdin #stdout #stderr 0.03s 6928KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
ERROR: /home/J3grM2/prog:33:1: Syntax error: Unexpected end of file
ERROR: '$runtoplevel'/0: Undefined procedure: program/0
   Exception: (3) program ? EOF: exit