Skip to content

Commit

Permalink
Merge pull request #10 from sillydan1/symbol-table-extensions
Browse files Browse the repository at this point in the history
Symbol table extensions
  • Loading branch information
sillydan1 committed Aug 23, 2022
2 parents 8007052 + a64f589 commit 3cb3e53
Show file tree
Hide file tree
Showing 18 changed files with 446 additions and 387 deletions.
30 changes: 21 additions & 9 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
cmake_minimum_required(VERSION 3.21)
project(expr VERSION 1.5.0)
project(expr VERSION 1.6.0)
include(cmake/CPM.cmake)
configure_file(src/config.h.in config.h)
set(CMAKE_CXX_STANDARD 20)
Expand All @@ -30,8 +30,6 @@ set(CXX_STANDARD_REQUIRED ON)
option(ENABLE_Z3 "Enables the download and compilation of the expr::z3_driver driver. OFF by default" OFF)

# DEPENDENCIES
find_package(FLEX REQUIRED)
find_package(BISON REQUIRED)
CPMAddPackage("gh:yalibs/[email protected]")
CPMAddPackage("gh:yalibs/[email protected]")
CPMAddPackage("gh:yalibs/[email protected]")
Expand All @@ -41,14 +39,27 @@ if(ENABLE_Z3)
CPMAddPackage("gh:Z3Prover/z3#z3-4.8.17")
endif()

BISON_TARGET(expr_parser src/parser/parser.y ${CMAKE_CURRENT_BINARY_DIR}/parser.cpp)
FLEX_TARGET(expr_scanner src/parser/scanner.l ${CMAKE_CURRENT_BINARY_DIR}/scanner.cpp)
ADD_FLEX_BISON_DEPENDENCY(expr_scanner expr_parser)

set(${PROJECT_NAME}_BUILD_DIR ${CMAKE_CURRENT_BINARY_DIR} CACHE STRING "expr_BUILD_DIR" FORCE)
include_directories(${overload_SOURCE_DIR}/include)
find_package(FLEX REQUIRED)
find_package(BISON REQUIRED)

add_custom_command(OUTPUT
${CMAKE_CURRENT_BINARY_DIR}/lex.l
COMMAND m4
ARGS -I ${PROJECT_SOURCE_DIR}/src/parser/lex -P ${PROJECT_SOURCE_DIR}/src/parser/lex/scanner.l > ${CMAKE_CURRENT_BINARY_DIR}/lex.l
VERBATIM)
add_custom_command(OUTPUT
${CMAKE_CURRENT_BINARY_DIR}/yacc.y
COMMAND m4
ARGS -I ${PROJECT_SOURCE_DIR}/src/parser/yacc -P ${PROJECT_SOURCE_DIR}/src/parser/yacc/parser.y > ${CMAKE_CURRENT_BINARY_DIR}/yacc.y
VERBATIM)

BISON_TARGET(expr_parser ${CMAKE_CURRENT_BINARY_DIR}/yacc.y ${CMAKE_CURRENT_BINARY_DIR}/parser.cpp)
FLEX_TARGET(expr_lexer ${CMAKE_CURRENT_BINARY_DIR}/lex.l ${CMAKE_CURRENT_BINARY_DIR}/scanner.cpp)
ADD_FLEX_BISON_DEPENDENCY(expr_lexer expr_parser)
include_directories(
${CMAKE_CURRENT_BINARY_DIR}
${CMAKE_CURRENT_BINARY_DIR}/src
${yaoverload_SOURCE_DIR}/include
${yatree_SOURCE_DIR}/include
${yatimer_SOURCE_DIR}/include
Expand All @@ -59,7 +70,7 @@ include_directories(
src)
add_library(${PROJECT_NAME} SHARED
${BISON_expr_parser_OUTPUTS}
${FLEX_expr_scanner_OUTPUTS}
${FLEX_expr_lexer_OUTPUTS}
src/drivers/interpreter.cpp
src/drivers/compiler.cpp

Expand All @@ -71,6 +82,7 @@ add_library(${PROJECT_NAME} SHARED
src/operations/modulo.cpp
src/operations/pow.cpp
src/operations/boolean.cpp)

if(ENABLE_Z3)
target_sources(${PROJECT_NAME} PUBLIC src/drivers/z3_driver.cpp)
target_link_libraries(${PROJECT_NAME} libz3)
Expand Down
19 changes: 18 additions & 1 deletion include/symbol_table.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,13 @@ namespace expr {

using underlying_symbol_table_t = std::map<std::string, symbol_value_t>;
struct symbol_table_t : public underlying_symbol_table_t {
symbol_table_t &operator+=(const symbol_table_t &);
auto operator+=(const symbol_table_t &) -> symbol_table_t&;
auto operator*=(const symbol_table_t &) -> symbol_table_t&;
auto put(const symbol_table_t &) -> symbol_table_t&;
auto overwrite_elements(const symbol_table_t &) -> symbol_table_t&;
auto is_overlapping(const symbol_table_t& other) -> bool;
auto is_overlapping_and_not_idempotent(const symbol_table_t& other) -> bool;
auto is_completely_overlapping(const symbol_table_t& other) -> bool;
};

symbol_table_t operator+(const symbol_table_t &a, const symbol_table_t &b);
Expand Down Expand Up @@ -132,4 +136,17 @@ namespace std {
};
}

#ifndef BINOP_CTOR
#define BINOP_CTOR(op,arg1,arg2) expr::syntax_tree_t{expr::operator_t{expr::operator_type_t::op}}.concat(arg1).concat(arg2)
#endif
#ifndef IDENT_CTOR
#define IDENT_CTOR(arg1) drv->get_symbol(arg1);
#endif
#ifndef MONOOP_CTOR
#define MONOOP_CTOR(op,arg1) expr::syntax_tree_t{expr::operator_t{expr::operator_type_t::op}}.concat(arg1)
#endif
#ifndef LIT_CTOR
#define LIT_CTOR(arg1) expr::syntax_tree_t{arg1}
#endif

#endif
12 changes: 12 additions & 0 deletions src/parser/lex/expr_driver_footer.l
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
m4_changequote()

void expr::driver::scan_begin() {
yy_flex_debug = trace_scanning;
if(file.empty() || file == "-")
yyin = stdin;
else
yy_scan_string(file.c_str());
}

void expr::driver::scan_end() {
}
29 changes: 29 additions & 0 deletions src/parser/lex/footer.l
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
m4_changequote()

yy::parser::symbol_type make_NUMBER(const std::string &s, const yy::parser::location_type& loc) {
errno = 0;
long n = strtol (s.c_str(), NULL, 10);
if (! (INT_MIN <= n && n <= INT_MAX && errno != ERANGE))
throw yy::parser::syntax_error (loc, "integer is out of range: " + s);
return yy::parser::make_NUMBER ((int) n, loc);
}

yy::parser::symbol_type make_FLOAT(const std::string &s, const yy::parser::location_type& loc) {
try {
double n = std::stod(s.c_str());
return yy::parser::make_FLOAT((double)n, loc);
} catch(std::out_of_range& e) {
throw yy::parser::syntax_error (loc, "double is out of range: " + s);
}
}

yy::parser::symbol_type make_STRING(const std::string &s, const yy::parser::location_type& loc) {
return yy::parser::make_STRING(s.substr(1, s.size()-2), loc);
}

yy::parser::symbol_type make_BOOL(std::string s, const yy::parser::location_type& loc) {
std::transform(s.begin(), s.end(), s.begin(), [](unsigned char c){ return std::tolower(c); });
bool b;
std::istringstream(s) >> std::boolalpha >> b;
return yy::parser::make_BOOL(b, loc);
}
10 changes: 10 additions & 0 deletions src/parser/lex/includes.l
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
%{
#include <cerrno>
#include <climits>
#include <cstdlib>
#include <cstring> // strerror
#include <string>
#include <sstream>
#include "drivers/driver.h"
#include "parser.hpp"
%}
37 changes: 37 additions & 0 deletions src/parser/lex/lexer.l
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
m4_changequote()

{blank}+ loc.step();
\n+ { loc.lines(yyleng); loc.step(); }

"-" return yy::parser::make_MINUS (loc);
"+" return yy::parser::make_PLUS (loc);
"*" return yy::parser::make_STAR (loc);
"/" return yy::parser::make_SLASH (loc);
"%" return yy::parser::make_PERCENT(loc);
"^" return yy::parser::make_HAT (loc);
"&&" return yy::parser::make_AND (loc);
"||" return yy::parser::make_OR (loc);
"^^" return yy::parser::make_XOR (loc);
"=>" return yy::parser::make_IMPLIES(loc);
">" return yy::parser::make_GT (loc);
">=" return yy::parser::make_GE (loc);
"==" return yy::parser::make_EE (loc);
"!=" return yy::parser::make_NE (loc);
"<=" return yy::parser::make_LE (loc);
"<" return yy::parser::make_LT (loc);
"!" return yy::parser::make_NOT (loc);
"(" return yy::parser::make_LPAREN (loc);
")" return yy::parser::make_RPAREN (loc);
":=" return yy::parser::make_ASSIGN (loc);
";" return yy::parser::make_TERM (loc);

{accmod} return yy::parser::make_ACCMOD(loc);
{type} return yy::parser::make_TYPE(loc);

{int} return make_NUMBER(yytext, loc);
{flt} return make_FLOAT(yytext, loc);
{str} return make_STRING(yytext, loc);
{bool} return make_BOOL(yytext, loc);
{id} return yy::parser::make_IDENTIFIER (yytext, loc);
. { throw yy::parser::syntax_error(loc, "invalid character: " + std::string(yytext)); }
<<EOF>> return yy::parser::make_YYEOF (loc);
28 changes: 28 additions & 0 deletions src/parser/lex/scanner.l
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
m4_changequote()
/*
m4_include(../mit.license)
*/

m4_include(includes.l)
m4_include(skeleton.l)
m4_include(tokens.l)

%{
// Code run each time a pattern is matched.
#define YY_USER_ACTION loc.columns(yyleng);
%}

%%
%{
// A handy shortcut to the location held by the driver.
yy::location& loc = drv->location;
// Code run each time yylex is called.
loc.step();
%}

m4_include(lexer.l)

%%

m4_include(footer.l)
m4_include(expr_driver_footer.l)
82 changes: 82 additions & 0 deletions src/parser/lex/skeleton.l
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
m4_changequote()

%{
#if defined __clang__
# define CLANG_VERSION (__clang_major__ * 100 + __clang_minor__)
#endif

// Clang and ICC like to pretend they are GCC.
#if defined __GNUC__ && !defined __clang__ && !defined __ICC
# define GCC_VERSION (__GNUC__ * 100 + __GNUC_MINOR__)
#endif

// Pacify warnings in yy_init_buffer (observed with Flex 2.6.4)
// and GCC 6.4.0, 7.3.0 with -O3.
#if defined GCC_VERSION && 600 <= GCC_VERSION
# pragma GCC diagnostic ignored "-Wnull-dereference"
#endif

// This example uses Flex's C back end, yet compiles it as C++.
// So expect warnings about C style casts and NULL.
#if defined CLANG_VERSION && 500 <= CLANG_VERSION
# pragma clang diagnostic ignored "-Wold-style-cast"
# pragma clang diagnostic ignored "-Wzero-as-null-pointer-constant"
#elif defined GCC_VERSION && 407 <= GCC_VERSION
# pragma GCC diagnostic ignored "-Wold-style-cast"
# pragma GCC diagnostic ignored "-Wzero-as-null-pointer-constant"
#endif

#define FLEX_VERSION (YY_FLEX_MAJOR_VERSION * 100 + YY_FLEX_MINOR_VERSION)

// Old versions of Flex (2.5.35) generate an incomplete documentation comment.
//
// In file included from src/scan-code-c.c:3:
// src/scan-code.c:2198:21: error: empty paragraph passed to '@param' command
// [-Werror,-Wdocumentation]
// * @param line_number
// ~~~~~~~~~~~~~~~~~^
// 1 error generated.
#if FLEX_VERSION < 206 && defined CLANG_VERSION
# pragma clang diagnostic ignored "-Wdocumentation"
#endif

// Old versions of Flex (2.5.35) use 'register'. Warnings introduced in
// GCC 7 and Clang 6.
#if FLEX_VERSION < 206
# if defined CLANG_VERSION && 600 <= CLANG_VERSION
# pragma clang diagnostic ignored "-Wdeprecated-register"
# elif defined GCC_VERSION && 700 <= GCC_VERSION
# pragma GCC diagnostic ignored "-Wregister"
# endif
#endif

#if FLEX_VERSION < 206
# if defined CLANG_VERSION
# pragma clang diagnostic ignored "-Wconversion"
# pragma clang diagnostic ignored "-Wdocumentation"
# pragma clang diagnostic ignored "-Wshorten-64-to-32"
# pragma clang diagnostic ignored "-Wsign-conversion"
# elif defined GCC_VERSION
# pragma GCC diagnostic ignored "-Wconversion"
# pragma GCC diagnostic ignored "-Wsign-conversion"
# endif
#endif

// Flex 2.6.4, GCC 9
// warning: useless cast to type 'int' [-Wuseless-cast]
// 1361 | YY_CURRENT_BUFFER_LVALUE->yy_buf_size = (int) (new_size - 2);
// | ^
#if defined GCC_VERSION && 900 <= GCC_VERSION
# pragma GCC diagnostic ignored "-Wuseless-cast"
#endif
%}

%option noyywrap nounput noinput batch debug

%{
// A number symbol corresponding to the value in provided string.
yy::parser::symbol_type make_NUMBER(const std::string &s, const yy::parser::location_type& loc);
yy::parser::symbol_type make_FLOAT(const std::string &s, const yy::parser::location_type& loc);
yy::parser::symbol_type make_STRING(const std::string &s, const yy::parser::location_type& loc);
yy::parser::symbol_type make_BOOL(std::string s, const yy::parser::location_type& loc);
%}
13 changes: 13 additions & 0 deletions src/parser/lex/tokens.l
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
m4_changequote()

%{
// TODO: Remove [ðđ€\(\)]
%}
id [a-z_A-Z]([.ðđ€\(\)a-zA-Z_0-9]*[a-zA-Z_0-9]+)?
int [0-9]+[Ll]?
flt [0-9]+[.][0-9]+[fd]?
bool [Ff]alse|[Tt]rue
str \"(\\.|[^\\"])*\"
blank [ \t\r]
accmod [Pp](ublic|rivate|rotected)
type int|long|float|double|string|bool|var|auto
21 changes: 21 additions & 0 deletions src/parser/mit.license
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2022 Asger Gitz-Johansen

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
Loading

0 comments on commit 3cb3e53

Please sign in to comment.