141 lines
4.2 KiB
Plaintext
141 lines
4.2 KiB
Plaintext
%require "3.2"
|
|
%language "c++"
|
|
|
|
%code requires {
|
|
#include <cstdint>
|
|
#include <iostream>
|
|
#include "elna/source/driver.hpp"
|
|
|
|
#if !defined(yyFlexLexerOnce)
|
|
#include <FlexLexer.h>
|
|
#endif
|
|
|
|
namespace elna::source
|
|
{
|
|
class lexer;
|
|
}
|
|
}
|
|
|
|
%code provides {
|
|
namespace elna::source
|
|
{
|
|
|
|
class lexer: public yyFlexLexer
|
|
{
|
|
public:
|
|
yy::location location;
|
|
|
|
lexer(std::istream& arg_yyin)
|
|
: yyFlexLexer(&arg_yyin)
|
|
{
|
|
}
|
|
|
|
yy::parser::symbol_type lex();
|
|
};
|
|
|
|
}
|
|
}
|
|
|
|
%define api.token.raw
|
|
%define api.token.constructor
|
|
%define api.value.type variant
|
|
%define parse.assert
|
|
|
|
%parse-param {elna::source::lexer& lexer}
|
|
%parse-param {elna::source::driver& driver}
|
|
%locations
|
|
|
|
%header
|
|
|
|
%code {
|
|
#define yylex lexer.lex
|
|
}
|
|
%start program;
|
|
|
|
%token <std::string> IDENTIFIER "identifier"
|
|
%token <std::int32_t> NUMBER "number"
|
|
%token <bool> BOOLEAN
|
|
%token IF THEN WHILE DO
|
|
%token CONST VAR PROCEDURE
|
|
%token BEGIN_BLOCK END_BLOCK
|
|
%token TRUE FALSE
|
|
%token LEFT_PAREN RIGHT_PAREN SEMICOLON DOT COMMA
|
|
%token GREATER_EQUAL LESS_EQUAL LESS_THAN GREATER_THAN NOT_EQUAL EQUALS
|
|
%token PLUS MINUS MULTIPLICATION DIVISION
|
|
%token ASSIGNMENT COLON HAT AT
|
|
|
|
%type <std::unique_ptr<elna::source::integer_literal>> integer_literal;
|
|
%type <std::unique_ptr<elna::source::constant_definition>> constant_definition;
|
|
%type <std::vector<std::unique_ptr<elna::source::constant_definition>>> constant_definition_part constant_definitions;
|
|
%type <std::unique_ptr<elna::source::declaration>> variable_declaration;
|
|
%type <std::vector<std::unique_ptr<elna::source::declaration>>> variable_declarations variable_declaration_part;
|
|
%type <std::unique_ptr<elna::source::type_expression>> type_expression;
|
|
%type <std::unique_ptr<elna::source::statement>> statement;
|
|
%%
|
|
program: constant_definition_part variable_declaration_part statement DOT
|
|
{
|
|
std::vector<std::unique_ptr<elna::source::definition>> definitions($1.size());
|
|
std::vector<std::unique_ptr<elna::source::definition>>::iterator definition = definitions.begin();
|
|
|
|
for (auto& constant : $1)
|
|
{
|
|
*definition++ = std::move(constant);
|
|
}
|
|
driver.tree = std::make_unique<elna::source::program>(elna::source::position{},
|
|
std::move(definitions), std::move($2),
|
|
std::move($3));
|
|
}
|
|
integer_literal: NUMBER
|
|
{
|
|
$$ = std::make_unique<elna::source::integer_literal>(elna::source::make_position(@1), $1);
|
|
};
|
|
statement: BEGIN_BLOCK END_BLOCK
|
|
{
|
|
$$ = std::make_unique<elna::source::compound_statement>(elna::source::make_position(@1));
|
|
};
|
|
type_expression:
|
|
HAT IDENTIFIER
|
|
{
|
|
$$ = std::make_unique<elna::source::type_expression>(elna::source::make_position(@1), $2, true);
|
|
}
|
|
| IDENTIFIER
|
|
{
|
|
$$ = std::make_unique<elna::source::type_expression>(elna::source::make_position(@1), $1, false);
|
|
}
|
|
variable_declaration: IDENTIFIER COLON type_expression
|
|
{
|
|
$$ = std::make_unique<elna::source::declaration>(elna::source::make_position(@1),
|
|
$1, std::move($3));
|
|
};
|
|
variable_declarations:
|
|
variable_declaration COMMA variable_declarations
|
|
{
|
|
std::swap($$, $3);
|
|
$$.emplace($$.cbegin(), std::move($1));
|
|
}
|
|
| variable_declaration { $$.emplace_back(std::move($1)); }
|
|
variable_declaration_part:
|
|
/* no variable declarations */ {}
|
|
| VAR variable_declarations SEMICOLON { std::swap($$, $2); }
|
|
constant_definition: IDENTIFIER EQUALS integer_literal
|
|
{
|
|
$$ = std::make_unique<elna::source::constant_definition>(elna::source::make_position(@1),
|
|
$1, std::move($3));
|
|
};
|
|
constant_definitions:
|
|
constant_definition COMMA constant_definitions
|
|
{
|
|
std::swap($$, $3);
|
|
$$.emplace($$.cbegin(), std::move($1));
|
|
}
|
|
| constant_definition { $$.emplace_back(std::move($1)); }
|
|
constant_definition_part:
|
|
/* no constant definitions */ {}
|
|
| CONST constant_definitions SEMICOLON { std::swap($$, $2); };
|
|
%%
|
|
|
|
void yy::parser::error(const location_type& loc, const std::string& message)
|
|
{
|
|
driver.error(loc, message);
|
|
}
|