Add c++ support

This commit is contained in:
2025-12-04 00:03:08 +01:00
parent 0474558835
commit 5762271fd0
8 changed files with 130 additions and 99 deletions

59
z5.l
View File

@@ -1,45 +1,52 @@
%{
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <string>
#include "def.tab.hh"
#define INFILE_ERROR 1
#define OUTFILE_ERROR 2
extern int yylineno;
void yyerror(char *msg, ...);
#define YY_DECL int yyFlexLexer::yylex()
%}
%option noyywrap
%option c++
%option yylineno
%%
"let" {fprintf(stdout, "LET\n"); return LET;}
"Int" {fprintf(stdout, "INT_TYPE\n"); return INT_TYPE;}
":" {fprintf(stdout, ":\n"); return COLON;}
";" {fprintf(stdout, ";\n"); return SEMICOLON;}
\+ {fprintf(stdout, "+\n"); return '+';}
\* {fprintf(stdout, "*\n"); return '*';}
\( {fprintf(stdout, "(\n"); return '(';}
\) {fprintf(stdout, ")\n"); return ')';}
\/ {fprintf(stdout, "/\n"); return '/';}
\- {fprintf(stdout, "-\n"); return '-';}
\= {fprintf(stdout, "=\n"); return '=';}
"let" {std::fprintf(stdout, "LET\n"); return LET;}
"Int" {std::fprintf(stdout, "INT_TYPE\n"); return INT_TYPE;}
":" {std::fprintf(stdout, ":\n"); return COLON;}
";" {std::fprintf(stdout, ";\n"); return SEMICOLON;}
\+ {std::fprintf(stdout, "+\n"); return '+';}
\* {std::fprintf(stdout, "*\n"); return '*';}
\( {std::fprintf(stdout, "(\n"); return '(';}
\) {std::fprintf(stdout, ")\n"); return ')';}
\/ {std::fprintf(stdout, "/\n"); return '/';}
\- {std::fprintf(stdout, "-\n"); return '-';}
\= {std::fprintf(stdout, "=\n"); return '=';}
[0-9]+ {
fprintf(stdout, "liczba: %s\n", yytext);
std::fprintf(stdout, "liczba: %s\n", yytext);
yylval.ival = atoi(yytext);
return INT_LIT;
}
[A-Za-z_][A-Za-z0-9_]* {
fprintf(stdout, "identyfikator: %s\n", yytext);
std::fprintf(stdout, "identyfikator: %s\n", yytext);
yylval.text = strdup(yytext);
return ID;
}
[ \t]+ {;}
\n {yylineno++;}
. {yyerror("Blad leksykalny\n");}
[ \t]+ {/* ignoruj białe znaki */}
\n {/* nowa linia */}
. {
std::fprintf(stderr, "Blad leksykalny w linii %d\n", lineno());
exit(1);
}
%%
void yyerror(char *msg, ...)
{
printf("%d: %s", yylineno, msg);
exit(1);
}