some changes

master
serr 2025-03-30 20:00:18 +03:00
parent 54be26ab2b
commit 8080e13a2e
1 changed files with 29 additions and 28 deletions

View File

@ -5,6 +5,7 @@
#include <string.h>
extern int yylineno;
char *last_lexeme; // Глобальная переменная для хранения текущей лексемы
void yyerror(const char *s);
%}
@ -16,36 +17,36 @@ WS [ \t]+
%%
"if" { return IF; }
"else" { return ELSE; }
"while" { return WHILE; }
"return" { return RETURN; }
"print" { return PRINT; }
"&&" { return AND; }
"||" { return OR; }
"!" { return NOT; }
">=" { return GE; }
"<=" { return LE; }
"==" { return EQ; }
"!=" { return NE; }
">" { return '>'; }
"<" { return '<'; }
"+" { return '+'; }
"-" { return '-'; }
"*" { return '*'; }
"/" { return '/'; }
"%" { return '%'; }
"(" { return '('; }
")" { return ')'; }
";" { return ';'; }
"=" { return '='; }
"{" { return '{'; }
"}" { return '}'; }
"if" { last_lexeme = strdup("if"); return IF; }
"else" { last_lexeme = strdup("else"); return ELSE; }
"while" { last_lexeme = strdup("while"); return WHILE; }
"return" { last_lexeme = strdup("return"); return RETURN; }
"print" { last_lexeme = strdup("print"); return PRINT; }
"&&" { last_lexeme = strdup("&&"); return AND; }
"||" { last_lexeme = strdup("||"); return OR; }
"!" { last_lexeme = strdup("!"); return NOT; }
">=" { last_lexeme = strdup(">="); return GE; }
"<=" { last_lexeme = strdup("<="); return LE; }
"==" { last_lexeme = strdup("=="); return EQ; }
"!=" { last_lexeme = strdup("!="); return NE; }
">" { last_lexeme = strdup(">"); return '>'; }
"<" { last_lexeme = strdup("<"); return '<'; }
"+" { last_lexeme = strdup("+"); return '+'; }
"-" { last_lexeme = strdup("-"); return '-'; }
"*" { last_lexeme = strdup("*"); return '*'; }
"/" { last_lexeme = strdup("/"); return '/'; }
"%" { last_lexeme = strdup("%"); return '%'; }
"(" { last_lexeme = strdup("("); return '('; }
")" { last_lexeme = strdup(")"); return ')'; }
";" { last_lexeme = strdup(";"); return ';'; }
"=" { last_lexeme = strdup("="); return '='; }
"{" { last_lexeme = strdup("{"); return '{'; }
"}" { last_lexeme = strdup("}"); return '}'; }
{DIGIT}+ { yylval.num = atoi(yytext); return NUMBER; }
{ID} { yylval.str = strdup(yytext); return IDENTIFIER; }
{DIGIT}+ { last_lexeme = strdup(yytext); yylval.num = atoi(yytext); return NUMBER; }
{ID} { last_lexeme = strdup(yytext); yylval.str = strdup(yytext); return IDENTIFIER; }
{WS} /* skip whitespace */
\n { yylineno++; }
. { yyerror("Invalid character"); }
. { last_lexeme = strdup(yytext); yyerror("invalid character"); }
%%