%{ #include int yylex(); int yyerror(const char* error); int current_line = 1; %} %code requires {#include } %start input %define api.value.type {std::string} %token KEYWORD %token STRING %token NUMBER %token IDENTIFIER %token OPERATOR %token OTHER %token NEWLINE %token DECORATOR %token COMMENT %% input: %empty | program { std::cout << " \
1. " << $1 << "
"; } program: expr | program expr {$$ = $1 + $2;} expr: block | nonblock nonblock: KEYWORD { $$ = "" + $1 + ""; } | STRING { $$ = "" + $1 + ""; } | NUMBER { $$ = "" + $1 + ""; } | IDENTIFIER { $$ = "" + $1 + ""; } | OPERATOR { $$ = "" + $1 + ""; } | NEWLINE { $$ = $1 + "" + std::to_string(++current_line) + ". ";} | COMMENT { $$ = "" + $1 + ""; } | DECORATOR { $$ = "" + $1 + ""; } | OTHER block: '{' program '}' {$$ = "{" + std::string("
") + $2 + "
}";} %% int yyerror(const char* error) { std::cerr << "error:\n" << error; return 0; } int main() { yyparse(); return 0; }