passing first test
This commit is contained in:
parent
1ce662294b
commit
9c493f543a
3 changed files with 19 additions and 11 deletions
16
src/lexer.l
16
src/lexer.l
|
|
@ -1,14 +1,22 @@
|
||||||
%option c++
|
%option c++
|
||||||
%option noyywrap
|
%option noyywrap
|
||||||
%option yyclass="Scanner"
|
%option yyclass="Scanner"
|
||||||
|
%x STRING
|
||||||
|
|
||||||
%{
|
%{
|
||||||
#include "scanner.hpp"
|
#include "scanner.hpp"
|
||||||
%}
|
%}
|
||||||
|
|
||||||
%%
|
%%
|
||||||
[0-9]+ { return yy::parser::token::TOK_NUMBER; }
|
[0-9]+ { yylval->as<std::string>() = yytext; return yy::parser::token::TOK_NUMBER; }
|
||||||
[a-zA-Z]+ { yylval->as<std::string>() = yytext; return yy::parser::token::TOK_WORD; }
|
[_a-zA-Z]+ { yylval->as<std::string>() = yytext; return yy::parser::token::TOK_IDENTIFIER; }
|
||||||
[ \t\r\n]+ { /* skip */ }
|
\n { return yy::parser::token::TOK_NEWLINE; }
|
||||||
. { return yy::parser::token::TOK_CHAR; }
|
\= { return yy::parser::token::TOK_EQUALS; }
|
||||||
|
\+ { return yy::parser::token::TOK_PLUS; }
|
||||||
|
\- { return yy::parser::token::TOK_MINUS; }
|
||||||
|
\" { yymore(); BEGIN(STRING); }
|
||||||
|
<STRING>\"\" { yymore(); }
|
||||||
|
<STRING>\" { BEGIN(INITIAL); yylval->as<std::string>() = yytext; return yy::parser::token::TOK_STRING; }
|
||||||
|
<STRING>. { yymore(); }
|
||||||
|
. { /* the last rule, ignore anything else (there should be nothing) */}
|
||||||
%%
|
%%
|
||||||
|
|
@ -22,7 +22,7 @@ void dump_tokens(Scanner &scanner)
|
||||||
|
|
||||||
std::cout << Parser::symbol_name(static_cast<kind>(tok));
|
std::cout << Parser::symbol_name(static_cast<kind>(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<std::string>();
|
std::cout << " " << lval.as<std::string>();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
12
src/parser.y
12
src/parser.y
|
|
@ -24,9 +24,11 @@
|
||||||
|
|
||||||
%parse-param { Scanner* scanner }
|
%parse-param { Scanner* scanner }
|
||||||
|
|
||||||
%token <std::string> WORD
|
%token <std::string> IDENTIFIER
|
||||||
%token NUMBER
|
%token <std::string> STRING
|
||||||
%token CHAR
|
%token <int> NUMBER
|
||||||
|
%token EQUALS NEWLINE PLUS MINUS EOF
|
||||||
|
|
||||||
|
|
||||||
%%
|
%%
|
||||||
input:
|
input:
|
||||||
|
|
@ -35,9 +37,7 @@ input:
|
||||||
;
|
;
|
||||||
|
|
||||||
element:
|
element:
|
||||||
WORD { std::cout << "WORD: " << $1 << "\n"; }
|
IDENTIFIER { std::cout << "IDENTIFIER: " << $1 << "\n"; }
|
||||||
| NUMBER { std::cout << "NUMBER\n"; }
|
|
||||||
| CHAR { std::cout << "CHAR\n"; }
|
|
||||||
;
|
;
|
||||||
|
|
||||||
%%
|
%%
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue