%{ #include "c_analyzer.tab.h" int line_number = 1; void yyerror(const char *s) { fprintf(stderr, "\033[91m\nError at line %i: %s near '%s'\033[0m\n", line_number, s, yytext); exit(-1); } %} %% "{" { return LBRACE; } "}" { return RBRACE; } "(" { return LPAREN; } ")" { return RPAREN; } ";" { return SEMICOLON; } "=" { return ASSIGN; } "+" { return PLUS; } "-" { return MINUS; } "*" { return MULT; } "/" { return DIV; } "%" { return MOD; } "&&" { return AND; } "||" { return OR; } "!" { return NOT; } "<" { return LT; } ">" { return GT; } "==" { return EQ; } "return" { return RET; } "print" { return PRINT; } "while" { return WHILE; } "do" { return DO; } "for" { return FOR; } "if" { return IF; } "else" { return ELSE; } "func" { return FUNC; } "," { return COMMA; } "//" { int c; while ((c = input()) != '\n' && c != 0); if (c == '\n') line_number++; } "/*" { int c, prev = 0; while ((c = input()) != 0) { if (c == '\n') line_number++; if (prev == '*' && c == '/') break; prev = c; } if (c == 0) yyerror("Unterminated comment"); } [0-9]+ { yylval.str = strdup(yytext); return NUMBER; } [a-zA-Z_][a-zA-Z0-9_]* { yylval.str = strdup(yytext); return IDENTIFIER; } [ \t] ; \n { line_number++; } . { yyerror("Invalid character"); } %% int yywrap() { return 1; }