elna/source/parser.yy

345 lines
13 KiB
Plaintext
Raw Normal View History

2024-12-27 10:51:46 +01:00
/*
* This Source Code Form is subject to the terms of the Mozilla Public License
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
* obtain one at http://mozilla.org/MPL/2.0/.
*/
2024-12-21 00:08:48 +01:00
%require "3.2"
%language "c++"
%code requires {
#include <cstdint>
#include <iostream>
2024-12-23 13:54:11 +01:00
#include "elna/source/driver.h"
2024-12-21 00:08:48 +01:00
2024-12-21 14:05:27 +01:00
#if !defined(yyFlexLexerOnce)
2024-12-21 00:08:48 +01:00
#include <FlexLexer.h>
#endif
2024-12-21 14:05:27 +01:00
namespace elna::source
2024-12-21 00:08:48 +01:00
{
2024-12-21 14:05:27 +01:00
class lexer;
2024-12-21 00:08:48 +01:00
}
}
%code provides {
2024-12-21 14:05:27 +01:00
namespace elna::source
2024-12-21 00:08:48 +01:00
{
2024-12-21 14:05:27 +01:00
class lexer: public yyFlexLexer
2024-12-21 00:08:48 +01:00
{
public:
yy::location location;
2024-12-21 14:05:27 +01:00
lexer(std::istream& arg_yyin)
2024-12-21 00:08:48 +01:00
: yyFlexLexer(&arg_yyin)
{
}
2024-12-23 13:54:11 +01:00
yy::parser::symbol_type lex(elna::source::driver& driver);
2024-12-21 00:08:48 +01:00
};
}
}
%define api.token.raw
%define api.token.constructor
%define api.value.type variant
2024-12-21 14:05:27 +01:00
%parse-param {elna::source::lexer& lexer}
2024-12-23 13:54:11 +01:00
%param {elna::source::driver& driver}
2024-12-21 00:08:48 +01:00
%locations
%header
%code {
#define yylex lexer.lex
}
%start program;
%token <std::string> IDENTIFIER "identifier"
2025-01-01 23:02:19 +01:00
%token <std::int32_t> INTEGER "integer"
2024-12-31 18:10:34 +01:00
%token <float> FLOAT "float"
2025-01-01 23:02:19 +01:00
%token <std::string> CHARACTER "character"
2024-12-21 00:08:48 +01:00
%token <bool> BOOLEAN
2024-12-30 23:12:47 +01:00
%token IF WHILE DO
2024-12-21 00:08:48 +01:00
%token CONST VAR PROCEDURE
%token BEGIN_BLOCK END_BLOCK
%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
2024-12-30 23:12:47 +01:00
%precedence THEN
%precedence ELSE
2024-12-31 18:10:34 +01:00
%type <std::unique_ptr<elna::source::number_literal<std::int32_t>>> integer_literal;
%type <std::unique_ptr<elna::source::number_literal<double>>> float_literal;
2025-01-01 23:02:19 +01:00
%type <std::unique_ptr<elna::source::number_literal<bool>>> boolean_literal;
%type <std::unique_ptr<elna::source::char_literal>> character_literal;
2024-12-21 00:08:48 +01:00
%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;
2024-12-21 14:05:27 +01:00
%type <std::unique_ptr<elna::source::declaration>> variable_declaration;
%type <std::vector<std::unique_ptr<elna::source::declaration>>> variable_declarations variable_declaration_part
formal_parameter_list;
2024-12-21 00:08:48 +01:00
%type <std::unique_ptr<elna::source::type_expression>> type_expression;
2024-12-27 10:51:46 +01:00
%type <std::unique_ptr<elna::source::expression>> expression pointer summand factor address comparand;
2024-12-21 14:05:27 +01:00
%type <std::vector<std::unique_ptr<elna::source::expression>>> expressions actual_parameter_list;
%type <std::unique_ptr<elna::source::variable_expression>> variable_expression;
%type <std::unique_ptr<elna::source::compound_statement>> compound_statement;
%type <std::unique_ptr<elna::source::assign_statement>> assign_statement;
%type <std::unique_ptr<elna::source::call_statement>> call_statement;
%type <std::unique_ptr<elna::source::while_statement>> while_statement;
%type <std::unique_ptr<elna::source::if_statement>> if_statement;
%type <std::unique_ptr<elna::source::statement>> statement;
%type <std::vector<std::unique_ptr<elna::source::statement>>> statements optional_statements;
%type <std::unique_ptr<elna::source::procedure_definition>> procedure_definition;
2024-12-27 10:51:46 +01:00
%type <std::vector<std::unique_ptr<elna::source::procedure_definition>>> procedure_definitions
procedure_definition_part;
2024-12-21 14:05:27 +01:00
%type <std::unique_ptr<elna::source::block>> block;
2024-12-21 00:08:48 +01:00
%%
2024-12-27 10:51:46 +01:00
program: constant_definition_part procedure_definition_part variable_declaration_part statement DOT
2024-12-21 14:05:27 +01:00
{
2024-12-27 10:51:46 +01:00
std::vector<std::unique_ptr<elna::source::definition>> definitions($1.size() + $2.size());
2024-12-21 14:05:27 +01:00
std::vector<std::unique_ptr<elna::source::definition>>::iterator definition = definitions.begin();
for (auto& constant : $1)
{
*definition++ = std::move(constant);
}
2024-12-27 10:51:46 +01:00
for (auto& procedure : $2)
2024-12-21 14:05:27 +01:00
{
2024-12-27 10:51:46 +01:00
*definition++ = std::move(procedure);
2024-12-21 14:05:27 +01:00
}
driver.tree = std::make_unique<elna::source::program>(elna::source::position{},
2024-12-27 10:51:46 +01:00
std::move(definitions), std::move($3),
2024-12-21 14:05:27 +01:00
std::move($4));
}
block: constant_definition_part variable_declaration_part statement
2024-12-21 00:08:48 +01:00
{
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);
}
2024-12-21 14:05:27 +01:00
$$ = std::make_unique<elna::source::block>(elna::source::position{},
std::move(definitions), std::move($2), std::move($3));
};
procedure_definition:
PROCEDURE IDENTIFIER formal_parameter_list SEMICOLON block SEMICOLON
{
$$ = std::make_unique<elna::source::procedure_definition>(elna::source::position{},
std::move($2), std::move($5));
std::swap($$->parameters(), $3);
};
procedure_definitions:
procedure_definition procedure_definitions
{
std::swap($$, $2);
$$.emplace($$.cbegin(), std::move($1));
2024-12-21 00:08:48 +01:00
}
2024-12-21 14:05:27 +01:00
| procedure_definition { $$.emplace_back(std::move($1)); }
2024-12-27 10:51:46 +01:00
procedure_definition_part:
/* no procedure definitions */ {}
| procedure_definitions { std::swap($$, $1); }
2025-01-01 23:02:19 +01:00
integer_literal: INTEGER
2024-12-21 00:08:48 +01:00
{
2024-12-31 18:10:34 +01:00
$$ = std::make_unique<elna::source::number_literal<std::int32_t>>(elna::source::make_position(@1), $1);
};
float_literal: FLOAT
{
$$ = std::make_unique<elna::source::number_literal<double>>(elna::source::make_position(@1), $1);
2024-12-21 14:05:27 +01:00
};
2025-01-01 23:02:19 +01:00
character_literal: CHARACTER
{
$$ = std::make_unique<elna::source::char_literal>(elna::source::make_position(@1), $1[0]);
};
2024-12-21 14:05:27 +01:00
boolean_literal: BOOLEAN
{
2025-01-01 23:02:19 +01:00
$$ = std::make_unique<elna::source::number_literal<bool>>(elna::source::make_position(@1), $1);
2024-12-21 00:08:48 +01:00
};
2024-12-21 14:05:27 +01:00
compound_statement: BEGIN_BLOCK optional_statements END_BLOCK
{
$$ = std::make_unique<elna::source::compound_statement>(elna::source::make_position(@1));
std::swap($$->statements(), $2);
}
assign_statement: IDENTIFIER ASSIGNMENT expression
{
$$ = std::make_unique<elna::source::assign_statement>(elna::source::make_position(@1), $1, std::move($3));
}
call_statement: IDENTIFIER actual_parameter_list
{
$$ = std::make_unique<elna::source::call_statement>(elna::source::make_position(@1), $1);
std::swap($$->arguments(), $2);
}
while_statement: WHILE expression DO statement
{
$$ = std::make_unique<elna::source::while_statement>(elna::source::make_position(@1),
std::move($2), std::move($4));
}
if_statement:
IF expression THEN statement
{
$$ = std::make_unique<elna::source::if_statement>(elna::source::make_position(@1),
std::move($2), std::move($4));
}
2024-12-30 23:12:47 +01:00
| IF expression THEN statement ELSE statement
{
$$ = std::make_unique<elna::source::if_statement>(elna::source::make_position(@1),
std::move($2), std::move($4), std::move($6));
}
2024-12-21 14:05:27 +01:00
pointer:
integer_literal { $$ = std::move($1); }
2024-12-31 18:10:34 +01:00
| float_literal { $$ = std::move($1); }
2024-12-21 14:05:27 +01:00
| boolean_literal { $$ = std::move($1); }
2025-01-01 23:02:19 +01:00
| character_literal { $$ = std::move($1); }
2024-12-21 14:05:27 +01:00
| variable_expression { $$ = std::move($1); }
| LEFT_PAREN expression RIGHT_PAREN { $$ = std::move($2); }
summand:
factor { $$ = std::move($1); }
| factor MULTIPLICATION factor
{
$$ = std::make_unique<elna::source::binary_expression>(elna::source::make_position(@1),
std::move($1), std::move($3), '*');
}
| factor DIVISION factor
{
$$ = std::make_unique<elna::source::binary_expression>(elna::source::make_position(@1),
std::move($1), std::move($3), '/');
}
address:
pointer HAT
{
$$ = std::make_unique<elna::source::unary_expression>(elna::source::make_position(@1),
std::move($1), '^');
}
| pointer { $$ = std::move($1); }
factor:
AT address
{
$$ = std::make_unique<elna::source::unary_expression>(elna::source::make_position(@1),
std::move($2), '@');
}
| address { $$ = std::move($1); }
2024-12-27 10:51:46 +01:00
comparand:
summand PLUS summand
{
$$ = std::make_unique<elna::source::binary_expression>(elna::source::make_position(@1),
std::move($1), std::move($3), '+');
}
| summand MINUS summand
{
$$ = std::make_unique<elna::source::binary_expression>(elna::source::make_position(@1),
std::move($1), std::move($3), '-');
}
| summand { $$ = std::move($1); }
2024-12-21 14:05:27 +01:00
expression:
2024-12-27 10:51:46 +01:00
comparand EQUALS comparand
2024-12-21 14:05:27 +01:00
{
$$ = std::make_unique<elna::source::binary_expression>(elna::source::make_position(@1),
std::move($1), std::move($3), '=');
}
2024-12-27 10:51:46 +01:00
| comparand NOT_EQUAL comparand
2024-12-21 14:05:27 +01:00
{
$$ = std::make_unique<elna::source::binary_expression>(elna::source::make_position(@1),
std::move($1), std::move($3), 'n');
}
2024-12-27 10:51:46 +01:00
| comparand LESS_THAN comparand
2024-12-21 14:05:27 +01:00
{
$$ = std::make_unique<elna::source::binary_expression>(elna::source::make_position(@1),
std::move($1), std::move($3), '<');
}
2024-12-27 10:51:46 +01:00
| comparand GREATER_THAN comparand
2024-12-21 14:05:27 +01:00
{
$$ = std::make_unique<elna::source::binary_expression>(elna::source::make_position(@1),
std::move($1), std::move($3), '>');
}
2024-12-27 10:51:46 +01:00
| comparand LESS_EQUAL comparand
2024-12-21 14:05:27 +01:00
{
$$ = std::make_unique<elna::source::binary_expression>(elna::source::make_position(@1),
std::move($1), std::move($3), '<');
}
2024-12-27 10:51:46 +01:00
| comparand GREATER_EQUAL comparand
2024-12-21 14:05:27 +01:00
{
$$ = std::make_unique<elna::source::binary_expression>(elna::source::make_position(@1),
std::move($1), std::move($3), '>');
}
2024-12-27 10:51:46 +01:00
| comparand { $$ = std::move($1); }
2024-12-21 14:05:27 +01:00
expressions:
expression COMMA expressions
{
std::swap($$, $3);
$$.emplace($$.cbegin(), std::move($1));
}
| expression { $$.emplace_back(std::move($1)); }
variable_expression: IDENTIFIER
{ $$ = std::make_unique<elna::source::variable_expression>(elna::source::make_position(@1), $1); }
statement:
compound_statement { $$ = std::move($1); }
| assign_statement { $$ = std::move($1); }
| call_statement { $$ = std::move($1); }
| while_statement { $$ = std::move($1); }
| if_statement { $$ = std::move($1); }
statements:
statement SEMICOLON statements
{
std::swap($$, $3);
$$.emplace($$.cbegin(), std::move($1));
}
| statement { $$.emplace_back(std::move($1)); }
optional_statements:
statements { std::swap($$, $1); }
| /* no statements */ {}
2024-12-21 00:08:48 +01:00
type_expression:
2024-12-21 14:05:27 +01:00
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);
}
2024-12-21 00:08:48 +01:00
variable_declaration: IDENTIFIER COLON type_expression
2024-12-21 14:05:27 +01:00
{
$$ = std::make_unique<elna::source::declaration>(elna::source::make_position(@1),
$1, std::move($3));
};
2024-12-21 00:08:48 +01:00
variable_declarations:
2024-12-21 14:05:27 +01:00
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); }
2024-12-21 00:08:48 +01:00
constant_definition: IDENTIFIER EQUALS integer_literal
{
2024-12-21 14:05:27 +01:00
$$ = std::make_unique<elna::source::constant_definition>(elna::source::make_position(@1),
2024-12-21 00:08:48 +01:00
$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); };
2024-12-21 14:05:27 +01:00
formal_parameter_list:
LEFT_PAREN RIGHT_PAREN {}
| LEFT_PAREN variable_declarations RIGHT_PAREN { std::swap($$, $2); }
actual_parameter_list:
LEFT_PAREN RIGHT_PAREN {}
| LEFT_PAREN expressions RIGHT_PAREN { std::swap($$, $2); }
2024-12-21 00:08:48 +01:00
%%
2024-12-21 14:05:27 +01:00
void yy::parser::error(const location_type& loc, const std::string& message)
2024-12-21 00:08:48 +01:00
{
2024-12-21 14:05:27 +01:00
driver.error(loc, message);
2024-12-21 00:08:48 +01:00
}