63 lines
No EOL
1.4 KiB
C++
63 lines
No EOL
1.4 KiB
C++
#include "parser.hpp"
|
|
#include "scanner.hpp"
|
|
#include <iostream>
|
|
#include <fstream>
|
|
|
|
// for debugging, and unit testing the lexer
|
|
void dump_tokens(Scanner &scanner)
|
|
{
|
|
yy::parser::semantic_type lval;
|
|
|
|
while (true)
|
|
{
|
|
int tok = scanner.lex(&lval);
|
|
if (tok == 0)
|
|
{
|
|
std::cout << "EOF\n";
|
|
break;
|
|
}
|
|
|
|
using Parser = yy::parser;
|
|
using kind = Parser::symbol_kind_type;
|
|
|
|
std::cout << Parser::symbol_name(static_cast<kind>(tok));
|
|
|
|
if (tok == Parser::token::TOK_IDENTIFIER || tok == Parser::token::TOK_NUMBER || tok == Parser::token::TOK_STRING)
|
|
{
|
|
std::cout << " " << lval.as<std::string>();
|
|
}
|
|
|
|
std::cout << "\n";
|
|
}
|
|
}
|
|
|
|
int main(int argc, char *argv[])
|
|
{
|
|
std::istream *input_stream = &std::cin;
|
|
std::ifstream file_stream;
|
|
|
|
bool token_dump = false;
|
|
|
|
for (int i = 1; i < argc; ++i)
|
|
{
|
|
std::string arg = argv[i];
|
|
if (arg == "--file" && i + 1 < argc)
|
|
{
|
|
file_stream.open(argv[i + 1]);
|
|
input_stream = &file_stream;
|
|
}
|
|
else if (arg == "--dump-tokens")
|
|
{
|
|
token_dump = true;
|
|
}
|
|
}
|
|
|
|
Scanner scanner(*input_stream);
|
|
if (token_dump)
|
|
{
|
|
dump_tokens(scanner);
|
|
return 0;
|
|
}
|
|
yy::parser parser(&scanner);
|
|
return parser.parse();
|
|
} |