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,51 +1,45 @@
%require "3.0.4"
%skeleton "lalr1.cc"
%defines
%define api.prefix {yy}
%define api.parser.class {Parser}
%locations
%define api.token.constructor
%skeleton "lalr1.cc"
%require "3.0"
%defines
%define api.value.type variant
%define parse.trace
%define parse.error verbose
%define api.parser.class { parser }
%define api.namespace { yy }
%define api.token.prefix {TOK_}
%code requires
{
#include <list>
#include <string>
#include <functional>
#include "../src/ParserCtx.hpp"
%code requires {
#include <string>
}
%code
{
yy::Parser::symbol_type yylex(void* yyscanner, yy::location& loc);
%code {
#include <iostream>
int scanner_lex(yy::parser::semantic_type* yylval);
#undef yylex
#define yylex scanner_lex
}
%token <std::string> WORD
%token NUMBER
%token CHAR
%token END 0;
%token FUNCTION
/* The driver is passed by reference to the parser and to the scanner. This
* provides a simple but effective pure interface, not relying on global
* variables. */
%lex-param {void *scanner} {yy::location& loc}
%parse-param {void *scanner} {yy::location& loc} { class ParserCtx& ctx }
%%
input:
/* empty */
| input element
;
element:
WORD { std::cout << "WORD: " << $1 << "\n"; }
| NUMBER { std::cout << "NUMBER\n"; }
| CHAR { std::cout << "CHAR\n"; }
;
%%
%start program;
program: FUNCTION;
%%
void yy::Parser::error(const yy::location& l, const std::string& m)
{
std::cerr << l << ": " << m << std::endl;
namespace yy {
void parser::error(const std::string& msg)
{
std::cerr << "Error: " << msg << "\n";
}
}