simpliest c++ example

This commit is contained in:
bronku 2025-11-20 16:35:10 +01:00
parent d587646b32
commit 909a43114b
9 changed files with 73 additions and 221 deletions

View file

@ -1,28 +1,35 @@
%option reentrant noyywrap nounput
%option c++
%option noyywrap
%option yyclass="Scanner"
%{
#include <iostream>
#include "../build/parser.hpp"
#include <string>
#define YY_DECL yy::Parser::symbol_type yylex(yyscan_t yyscanner, yy::location& loc)
#define YY_USER_ACTION loc.columns(yyleng);
#define yyterminate() return yy::Parser::make_END(loc)
class Scanner : public yyFlexLexer {
public:
Scanner(std::istream* in) : yyFlexLexer(in) {}
int yylex();
int lex(yy::parser::semantic_type* yylval) {
this->yylval = yylval;
return yylex();
}
private:
yy::parser::semantic_type* yylval;
};
int scanner_lex(yy::parser::semantic_type* yylval) {
static Scanner scanner(&std::cin);
return scanner.lex(yylval);
}
%}
%%
%{
loc.step();
%}
function {return yy::Parser::make_FUNCTION(loc);}
[ \n\r\t\v] {}
. {std::cerr << loc << ": " << "token error" << std::endl; exit(1);}
%%
[0-9]+ { return yy::parser::token::TOK_NUMBER; }
[a-zA-Z]+ { yylval->as<std::string>() = yytext; return yy::parser::token::TOK_WORD; }
[ \t\r\n]+ { /* skip */ }
. { return yy::parser::token::TOK_CHAR; }
%%