From 9c493f543ae5428cb4b96c5356e20a41a5da0c0d Mon Sep 17 00:00:00 2001 From: bronku Date: Fri, 21 Nov 2025 18:08:06 +0100 Subject: [PATCH] passing first test --- src/lexer.l | 16 ++++++++++++---- src/main.cpp | 2 +- src/parser.y | 12 ++++++------ 3 files changed, 19 insertions(+), 11 deletions(-) diff --git a/src/lexer.l b/src/lexer.l index c1fc72f..b18dcb6 100644 --- a/src/lexer.l +++ b/src/lexer.l @@ -1,14 +1,22 @@ %option c++ %option noyywrap %option yyclass="Scanner" +%x STRING %{ #include "scanner.hpp" %} %% -[0-9]+ { return yy::parser::token::TOK_NUMBER; } -[a-zA-Z]+ { yylval->as() = yytext; return yy::parser::token::TOK_WORD; } -[ \t\r\n]+ { /* skip */ } -. { return yy::parser::token::TOK_CHAR; } +[0-9]+ { yylval->as() = yytext; return yy::parser::token::TOK_NUMBER; } +[_a-zA-Z]+ { yylval->as() = yytext; return yy::parser::token::TOK_IDENTIFIER; } +\n { return yy::parser::token::TOK_NEWLINE; } +\= { return yy::parser::token::TOK_EQUALS; } +\+ { return yy::parser::token::TOK_PLUS; } +\- { return yy::parser::token::TOK_MINUS; } +\" { yymore(); BEGIN(STRING); } +\"\" { yymore(); } +\" { BEGIN(INITIAL); yylval->as() = yytext; return yy::parser::token::TOK_STRING; } +. { yymore(); } +. { /* the last rule, ignore anything else (there should be nothing) */} %% \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 7e9011c..67ca52f 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -22,7 +22,7 @@ void dump_tokens(Scanner &scanner) std::cout << Parser::symbol_name(static_cast(tok)); - if (tok == Parser::token::TOK_WORD) + if (tok == Parser::token::TOK_IDENTIFIER || tok == Parser::token::TOK_NUMBER || tok == Parser::token::TOK_STRING) { std::cout << " " << lval.as(); } diff --git a/src/parser.y b/src/parser.y index 317e1da..f9a182c 100644 --- a/src/parser.y +++ b/src/parser.y @@ -24,9 +24,11 @@ %parse-param { Scanner* scanner } -%token WORD -%token NUMBER -%token CHAR +%token IDENTIFIER +%token STRING +%token NUMBER +%token EQUALS NEWLINE PLUS MINUS EOF + %% input: @@ -35,9 +37,7 @@ input: ; element: - WORD { std::cout << "WORD: " << $1 << "\n"; } - | NUMBER { std::cout << "NUMBER\n"; } - | CHAR { std::cout << "CHAR\n"; } + IDENTIFIER { std::cout << "IDENTIFIER: " << $1 << "\n"; } ; %%