52 lines
2.0 KiB
Plaintext
52 lines
2.0 KiB
Plaintext
%{
|
|
#include "cpl.tab.h"
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
extern int yylineno;
|
|
char *last_lexeme; // Глобальная переменная для хранения текущей лексемы
|
|
void yyerror(const char *s);
|
|
%}
|
|
|
|
%option noyywrap
|
|
|
|
DIGIT [0-9]
|
|
ID [a-zA-Z][a-zA-Z0-9_]*
|
|
WS [ \t]+
|
|
|
|
%%
|
|
|
|
"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}+ { 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++; }
|
|
. { last_lexeme = strdup(yytext); yyerror("invalid character"); }
|
|
|
|
%% |