elna/source/parser.yy

424 lines
15 KiB
Plaintext
Raw Permalink 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"
2025-01-03 22:18:35 +01:00
%token <std::string> STRING "string"
2024-12-21 00:08:48 +01:00
%token <bool> BOOLEAN
2025-01-18 21:30:11 +01:00
%token IF WHILE DO THEN ELSE RETURN
%token CONST VAR PROCEDURE ARRAY OF TYPE RECORD POINTER TO UNION
2025-01-16 15:09:58 +01:00
%token BEGIN_BLOCK END_BLOCK EXTERN
2025-01-05 00:06:51 +01:00
%token LEFT_PAREN RIGHT_PAREN LEFT_SQUARE RIGHT_SQUARE SEMICOLON DOT COMMA
2025-01-13 11:55:19 +01:00
%token AND OR NOT
2024-12-21 00:08:48 +01:00
%token GREATER_EQUAL LESS_EQUAL LESS_THAN GREATER_THAN NOT_EQUAL EQUALS
%token PLUS MINUS MULTIPLICATION DIVISION
%token ASSIGNMENT COLON HAT AT
2025-01-15 01:48:09 +01:00
%type <elna::source::literal *> literal;
%type <elna::source::constant_definition *> constant_definition;
2025-01-09 22:40:39 +01:00
%type <std::vector<elna::source::constant_definition *>> constant_part constant_definitions;
2025-01-11 13:32:37 +01:00
%type <elna::source::variable_declaration *> variable_declaration;
%type <std::vector<elna::source::variable_declaration *>> variable_declarations variable_part
2024-12-21 14:05:27 +01:00
formal_parameter_list;
2025-01-06 15:08:23 +01:00
%type <elna::source::type_expression *> type_expression;
2025-01-13 11:55:19 +01:00
%type <elna::source::expression *> expression pointer summand factor comparand logical_operand;
%type <std::vector<elna::source::expression *>> expressions actual_parameter_list;
2025-01-07 14:37:30 +01:00
%type <elna::source::designator_expression *> designator_expression;
%type <elna::source::assign_statement *> assign_statement;
2025-01-18 21:30:11 +01:00
%type <elna::source::call_expression *> call_expression;
%type <elna::source::while_statement *> while_statement;
%type <elna::source::if_statement *> if_statement;
2025-01-18 21:30:11 +01:00
%type <elna::source::return_statement *> return_statement;
%type <elna::source::statement *> statement;
%type <std::vector<elna::source::statement *>> statements optional_statements;
%type <elna::source::procedure_definition *> procedure_definition;
2025-01-09 22:40:39 +01:00
%type <std::vector<elna::source::procedure_definition *>> procedure_definitions procedure_part;
2025-01-07 14:37:30 +01:00
%type <elna::source::type_definition *> type_definition;
2025-01-09 22:40:39 +01:00
%type <std::vector<elna::source::type_definition *>> type_definitions type_part;
%type <elna::source::block *> block;
2025-01-10 23:17:18 +01:00
%type <std::pair<std::string, elna::source::type_expression *>> field_declaration;
%type <std::vector<std::pair<std::string, elna::source::type_expression *>>> field_list;
2024-12-21 00:08:48 +01:00
%%
2025-01-07 14:37:30 +01:00
program:
2025-01-16 15:09:58 +01:00
type_part constant_part procedure_part variable_part BEGIN_BLOCK optional_statements END_BLOCK DOT
2024-12-21 14:05:27 +01:00
{
2025-01-11 13:32:37 +01:00
std::vector<elna::source::definition *> definitions($1.size() + $3.size());
std::vector<elna::source::definition *>::iterator definition = definitions.begin();
2025-01-11 13:32:37 +01:00
std::vector<elna::source::definition *> value_definitions($2.size() + $4.size());
std::vector<elna::source::definition *>::iterator value_definition = value_definitions.begin();
2024-12-21 14:05:27 +01:00
2025-01-11 13:32:37 +01:00
for (auto type : $1)
2025-01-07 14:37:30 +01:00
{
*definition++ = type;
}
2025-01-11 13:32:37 +01:00
for (auto constant : $2)
2024-12-21 14:05:27 +01:00
{
2025-01-11 13:32:37 +01:00
*value_definition++ = constant;
2024-12-21 14:05:27 +01:00
}
2025-01-11 13:32:37 +01:00
for (auto procedure : $3)
2024-12-21 14:05:27 +01:00
{
*definition++ = procedure;
2024-12-21 14:05:27 +01:00
}
2025-01-11 13:32:37 +01:00
for (auto variable : $4)
{
*value_definition++ = variable;
}
auto tree = new elna::source::program(elna::source::position{},
2025-01-16 15:09:58 +01:00
std::move(definitions), std::move(value_definitions), std::move($6));
driver.tree.reset(tree);
2024-12-21 14:05:27 +01:00
}
2025-01-16 15:09:58 +01:00
block: constant_part variable_part BEGIN_BLOCK optional_statements END_BLOCK
2024-12-21 00:08:48 +01:00
{
2025-01-11 13:32:37 +01:00
std::vector<elna::source::definition *> definitions($1.size() + $2.size());
std::vector<elna::source::definition *>::iterator definition = definitions.begin();
2024-12-21 00:08:48 +01:00
2025-01-11 13:32:37 +01:00
for (auto constant : $1)
2024-12-21 00:08:48 +01:00
{
*definition++ = constant;
2024-12-21 00:08:48 +01:00
}
2025-01-11 13:32:37 +01:00
for (auto variable : $2)
{
*definition++ = variable;
}
$$ = new elna::source::block(elna::source::position{},
2025-01-16 15:09:58 +01:00
std::move(definitions), std::move($4));
}
2024-12-21 14:05:27 +01:00
procedure_definition:
PROCEDURE IDENTIFIER formal_parameter_list SEMICOLON block SEMICOLON
{
$$ = new elna::source::procedure_definition(elna::source::position{},
2025-01-18 21:30:11 +01:00
$2, std::move($3), nullptr, $5);
2025-01-16 15:09:58 +01:00
}
| PROCEDURE IDENTIFIER formal_parameter_list SEMICOLON EXTERN SEMICOLON
{
$$ = new elna::source::procedure_definition(elna::source::position{},
2025-01-18 21:30:11 +01:00
$2, std::move($3), nullptr, nullptr);
}
| PROCEDURE IDENTIFIER formal_parameter_list COLON type_expression SEMICOLON block SEMICOLON
{
$$ = new elna::source::procedure_definition(elna::source::position{},
$2, std::move($3), $5, $7);
}
| PROCEDURE IDENTIFIER formal_parameter_list COLON type_expression SEMICOLON EXTERN SEMICOLON
{
$$ = new elna::source::procedure_definition(elna::source::position{},
$2, std::move($3), $5, nullptr);
2025-01-16 15:09:58 +01:00
}
2024-12-21 14:05:27 +01:00
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)); }
2025-01-09 22:40:39 +01:00
procedure_part:
2024-12-27 10:51:46 +01:00
/* no procedure definitions */ {}
| procedure_definitions { std::swap($$, $1); }
2025-01-07 14:37:30 +01:00
assign_statement: designator_expression ASSIGNMENT expression
2024-12-21 14:05:27 +01:00
{
$$ = new elna::source::assign_statement(elna::source::make_position(@1), $1, $3);
2024-12-21 14:05:27 +01:00
}
2025-01-18 21:30:11 +01:00
call_expression: IDENTIFIER actual_parameter_list
2024-12-21 14:05:27 +01:00
{
2025-01-18 21:30:11 +01:00
$$ = new elna::source::call_expression(elna::source::make_position(@1), $1);
2024-12-21 14:05:27 +01:00
std::swap($$->arguments(), $2);
}
while_statement: WHILE expression DO optional_statements END_BLOCK
2024-12-21 14:05:27 +01:00
{
auto body = new elna::source::compound_statement(elna::source::make_position(@3), std::move($4));
$$ = new elna::source::while_statement(elna::source::make_position(@1), $2, body);
2024-12-21 14:05:27 +01:00
}
if_statement:
IF expression THEN optional_statements END_BLOCK
2024-12-21 14:05:27 +01:00
{
auto then = new elna::source::compound_statement(elna::source::make_position(@3), std::move($4));
$$ = new elna::source::if_statement(elna::source::make_position(@1), $2, then);
2024-12-21 14:05:27 +01:00
}
| IF expression THEN optional_statements ELSE optional_statements END_BLOCK
2024-12-30 23:12:47 +01:00
{
auto then = new elna::source::compound_statement(elna::source::make_position(@3), std::move($4));
auto _else = new elna::source::compound_statement(elna::source::make_position(@5), std::move($6));
$$ = new elna::source::if_statement(elna::source::make_position(@1), $2, then, _else);
2024-12-30 23:12:47 +01:00
}
2025-01-18 21:30:11 +01:00
return_statement:
RETURN expression
{
$$ = new elna::source::return_statement(elna::source::make_position(@1), $2);
}
2025-01-15 01:48:09 +01:00
literal:
INTEGER
{
$$ = new elna::source::number_literal<std::int32_t>(elna::source::make_position(@1), $1);
}
| FLOAT
{
$$ = new elna::source::number_literal<double>(elna::source::make_position(@1), $1);
}
| BOOLEAN
{
$$ = new elna::source::number_literal<bool>(elna::source::make_position(@1), $1);
}
| CHARACTER
{
$$ = new elna::source::number_literal<unsigned char>(elna::source::make_position(@1), $1.at(0));
}
| STRING
{
$$ = new elna::source::string_literal(elna::source::make_position(@1), $1);
}
2024-12-21 14:05:27 +01:00
pointer:
2025-01-15 01:48:09 +01:00
literal { $$ = $1; }
2025-01-07 14:37:30 +01:00
| designator_expression { $$ = $1; }
2024-12-21 14:05:27 +01:00
| LEFT_PAREN expression RIGHT_PAREN { $$ = std::move($2); }
summand:
factor { $$ = std::move($1); }
| factor MULTIPLICATION factor
{
$$ = new elna::source::binary_expression(elna::source::make_position(@1),
$1, $3, '*');
2024-12-21 14:05:27 +01:00
}
| factor DIVISION factor
{
$$ = new elna::source::binary_expression(elna::source::make_position(@1),
$1, $3, '/');
2024-12-21 14:05:27 +01:00
}
factor:
2025-01-09 22:40:39 +01:00
AT pointer
2024-12-21 14:05:27 +01:00
{
2025-01-10 23:17:18 +01:00
$$ = new elna::source::unary_expression(elna::source::make_position(@1), $2, '@');
2024-12-21 14:05:27 +01:00
}
2025-01-13 11:55:19 +01:00
| NOT pointer
{
$$ = new elna::source::unary_expression(elna::source::make_position(@1), $2, '!');
}
2025-01-09 22:40:39 +01:00
| pointer { $$ = $1; }
2024-12-27 10:51:46 +01:00
comparand:
summand PLUS summand
{
2025-01-10 23:17:18 +01:00
$$ = new elna::source::binary_expression(elna::source::make_position(@1), $1, $3, '+');
2024-12-27 10:51:46 +01:00
}
| summand MINUS summand
{
2025-01-10 23:17:18 +01:00
$$ = new elna::source::binary_expression(elna::source::make_position(@1), $1, $3, '-');
2024-12-27 10:51:46 +01:00
}
| summand { $$ = std::move($1); }
2025-01-13 11:55:19 +01:00
logical_operand:
2024-12-27 10:51:46 +01:00
comparand EQUALS comparand
2024-12-21 14:05:27 +01:00
{
2025-01-10 23:17:18 +01:00
$$ = new elna::source::binary_expression(elna::source::make_position(@1), $1, $3, '=');
2024-12-21 14:05:27 +01:00
}
2024-12-27 10:51:46 +01:00
| comparand NOT_EQUAL comparand
2024-12-21 14:05:27 +01:00
{
2025-01-10 23:17:18 +01:00
$$ = new elna::source::binary_expression(elna::source::make_position(@1), $1, $3, 'n');
2024-12-21 14:05:27 +01:00
}
2024-12-27 10:51:46 +01:00
| comparand LESS_THAN comparand
2024-12-21 14:05:27 +01:00
{
2025-01-10 23:17:18 +01:00
$$ = new elna::source::binary_expression(elna::source::make_position(@1), $1, $3, '<');
2024-12-21 14:05:27 +01:00
}
2024-12-27 10:51:46 +01:00
| comparand GREATER_THAN comparand
2024-12-21 14:05:27 +01:00
{
2025-01-13 11:55:19 +01:00
$$ = new elna::source::binary_expression(elna::source::make_position(@1), $1, $3, '>');
2024-12-21 14:05:27 +01:00
}
2024-12-27 10:51:46 +01:00
| comparand LESS_EQUAL comparand
2024-12-21 14:05:27 +01:00
{
2025-01-13 11:55:19 +01:00
$$ = new elna::source::binary_expression(elna::source::make_position(@1), $1, $3, '<');
2024-12-21 14:05:27 +01:00
}
2024-12-27 10:51:46 +01:00
| comparand GREATER_EQUAL comparand
2024-12-21 14:05:27 +01:00
{
2025-01-13 11:55:19 +01:00
$$ = new elna::source::binary_expression(elna::source::make_position(@1), $1, $3, '>');
}
| comparand { $$ = $1; }
expression:
logical_operand AND logical_operand
{
$$ = new elna::source::binary_expression(elna::source::make_position(@1), $1, $3, 'a');
}
| logical_operand OR logical_operand
{
$$ = new elna::source::binary_expression(elna::source::make_position(@1), $1, $3, 'o');
2024-12-21 14:05:27 +01:00
}
2025-01-13 11:55:19 +01:00
| logical_operand { $$ = $1; }
2025-01-18 21:30:11 +01:00
| call_expression { $$ = $1; }
2024-12-21 14:05:27 +01:00
expressions:
expression COMMA expressions
{
std::swap($$, $3);
$$.emplace($$.cbegin(), $1);
2024-12-21 14:05:27 +01:00
}
| expression { $$.emplace_back(std::move($1)); }
2025-01-07 14:37:30 +01:00
designator_expression:
designator_expression LEFT_SQUARE expression RIGHT_SQUARE
2025-01-07 14:37:30 +01:00
{
$$ = new elna::source::array_access_expression(elna::source::make_position(@1), $1, $3);
}
2025-01-09 22:40:39 +01:00
| designator_expression DOT IDENTIFIER
{
$$ = new elna::source::field_access_expression(elna::source::make_position(@1), $1, $3);
}
| designator_expression HAT
{
$$ = new elna::source::dereference_expression(elna::source::make_position(@1), $1);
}
| IDENTIFIER
{
$$ = new elna::source::variable_expression(elna::source::make_position(@1), $1);
}
2024-12-21 14:05:27 +01:00
statement:
assign_statement { $$ = $1; }
| while_statement { $$ = $1; }
| if_statement { $$ = $1; }
2025-01-18 21:30:11 +01:00
| return_statement { $$ = $1; }
| expression { $$ = new elna::source::expression_statement(elna::source::make_position(@1), $1); }
2024-12-21 14:05:27 +01:00
statements:
statement SEMICOLON statements
{
std::swap($$, $3);
2025-01-10 23:17:18 +01:00
$$.emplace($$.cbegin(), $1);
2024-12-21 14:05:27 +01:00
}
2025-01-10 23:17:18 +01:00
| statement { $$.push_back($1); }
2024-12-21 14:05:27 +01:00
optional_statements:
statements { std::swap($$, $1); }
| /* no statements */ {}
2025-01-10 23:17:18 +01:00
field_declaration:
IDENTIFIER COLON type_expression { $$ = std::make_pair($1, $3); }
field_list:
field_declaration SEMICOLON field_list
{
std::swap($$, $3);
$$.emplace($$.cbegin(), $1);
}
| field_declaration { $$.emplace_back($1); }
2024-12-21 00:08:48 +01:00
type_expression:
2025-01-06 15:08:23 +01:00
ARRAY INTEGER OF type_expression
{
$$ = new elna::source::array_type_expression(elna::source::make_position(@1), $4, $2);
}
| POINTER TO type_expression
2025-01-08 23:23:27 +01:00
{
$$ = new elna::source::pointer_type_expression(elna::source::make_position(@1), $3);
2025-01-08 23:23:27 +01:00
}
2025-01-10 23:17:18 +01:00
| RECORD field_list END_BLOCK
{
$$ = new elna::source::record_type_expression(elna::source::make_position(@1), std::move($2));
}
| UNION field_list END_BLOCK
{
$$ = new elna::source::union_type_expression(elna::source::make_position(@1), std::move($2));
}
2025-01-06 15:08:23 +01:00
| IDENTIFIER
2024-12-21 14:05:27 +01:00
{
$$ = new elna::source::basic_type_expression(elna::source::make_position(@1), $1);
2024-12-21 14:05:27 +01:00
}
2024-12-21 00:08:48 +01:00
variable_declaration: IDENTIFIER COLON type_expression
2024-12-21 14:05:27 +01:00
{
2025-01-15 01:48:09 +01:00
$$ = new elna::source::variable_declaration(elna::source::make_position(@1), $1, $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(), $1);
2024-12-21 14:05:27 +01:00
}
| variable_declaration { $$.emplace_back(std::move($1)); }
2025-01-09 22:40:39 +01:00
variable_part:
2024-12-21 14:05:27 +01:00
/* no variable declarations */ {}
| VAR variable_declarations SEMICOLON { std::swap($$, $2); }
2025-01-15 01:48:09 +01:00
constant_definition: IDENTIFIER EQUALS literal
2024-12-21 00:08:48 +01:00
{
2025-01-15 01:48:09 +01:00
$$ = new elna::source::constant_definition(elna::source::make_position(@1), $1, $3);
2025-01-07 14:37:30 +01:00
}
2024-12-21 00:08:48 +01:00
constant_definitions:
constant_definition COMMA constant_definitions
{
std::swap($$, $3);
$$.emplace($$.cbegin(), std::move($1));
}
| constant_definition { $$.emplace_back(std::move($1)); }
2025-01-09 22:40:39 +01:00
constant_part:
2024-12-21 00:08:48 +01:00
/* no constant definitions */ {}
2025-01-07 14:37:30 +01:00
| CONST constant_definitions SEMICOLON { std::swap($$, $2); }
type_definition: IDENTIFIER EQUALS type_expression
{
$$ = new elna::source::type_definition(elna::source::make_position(@1), $1, $3);
}
type_definitions:
type_definition COMMA type_definitions
{
std::swap($$, $3);
$$.emplace($$.cbegin(), std::move($1));
}
| type_definition { $$.emplace_back(std::move($1)); }
2025-01-09 22:40:39 +01:00
type_part:
2025-01-07 14:37:30 +01:00
/* no type definitions */ {}
| TYPE type_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
}