From a282cd0c7ec925eeb86be9056362d6567fb612dd Mon Sep 17 00:00:00 2001 From: Asger Gitz-Johansen Date: Thu, 7 Apr 2022 16:03:45 +0200 Subject: [PATCH 1/7] Removed reduce-reduce conflicts --- src/parser/parser.y | 39 +++++++++++++++++---------------------- 1 file changed, 17 insertions(+), 22 deletions(-) diff --git a/src/parser/parser.y b/src/parser/parser.y index 63a29f7..1665b5a 100644 --- a/src/parser/parser.y +++ b/src/parser/parser.y @@ -62,14 +62,14 @@ %token FLOAT "float" %token BOOL "bool" %token STRING "string" -%nterm exp cmp op +%nterm lit exp %printer { yyo << $$; } <*>; %% %start unit; unit: statements { } -| cmp { drv.result["expression_result"] = $1; } +| exp { drv.result["expression_result"] = $1; } ; statements: @@ -79,25 +79,16 @@ statements: ; statement: - "identifier" ":=" cmp { drv.result[$1] = $3; } -| "type" "identifier" ":=" cmp { drv.result[$2] = $4; } -| "access_modifier" "type" "identifier" ":=" cmp { drv.result[$3] = $5; } + "identifier" ":=" exp { drv.result[$1] = $3; } +| "type" "identifier" ":=" exp { drv.result[$2] = $4; } +| "access_modifier" "type" "identifier" ":=" exp { drv.result[$3] = $5; } ; -%left "+" "-"; -%left "*" "/"; -%precedence "||" "&&"; +%precedence "||" "&&" ">" ">=" "==" "!=" "<=" "<" "*" "/" "+" "-"; -cmp: - op "||" op { $$ = or_($1,$3); } -| op "&&" op { $$ = and_($1,$3); } -| "!" op { $$ = not_($2); } -| op { $$ = $1; } -| "(" op ")" { $$ = $2; } -; - -op: - exp "+" exp { $$ = $1 + $3; } +exp: + lit { $$ = $1; } +| exp "+" exp { $$ = $1 + $3; } | exp "-" exp { $$ = $1 - $3; } | exp "*" exp { $$ = $1 * $3; } | exp "/" exp { $$ = $1 / $3; } @@ -107,13 +98,17 @@ op: | exp "!=" exp { $$ = ne_($1,$3); } | exp "<=" exp { $$ = le_($1,$3); } | exp "<" exp { $$ = lt_($1,$3); } -| exp { $$ = $1; } +| exp "||" exp { $$ = or_($1,$3); } +| exp "&&" exp { $$ = and_($1,$3); } +| "!" exp { $$ = not_($2); } +| "(" exp ")" { $$ = $2; } ; -exp: - cmp { $$ = $1; } -| "number" { $$ = $1; } +lit: + "number" { $$ = $1; } +| "-" "number" { $$ = -$2; } | "float" { $$ = $1; } +| "-" "float" { $$ = -$2; } | "string" { $$ = $1; } | "bool" { $$ = $1; } | "identifier" { $$ = drv.environment.at($1); } From 5c8218b8faadf04b41b8672a986991ce9048f3e0 Mon Sep 17 00:00:00 2001 From: Asger Gitz-Johansen Date: Mon, 18 Apr 2022 07:46:25 +0200 Subject: [PATCH 2/7] Removed the rest of the shift-reduce conflicts --- src/parser/parser.y | 58 +++++++++++++++++++++++--------------------- src/parser/scanner.l | 2 +- 2 files changed, 31 insertions(+), 29 deletions(-) diff --git a/src/parser/parser.y b/src/parser/parser.y index 1665b5a..8c2a7d4 100644 --- a/src/parser/parser.y +++ b/src/parser/parser.y @@ -65,6 +65,9 @@ %nterm lit exp %printer { yyo << $$; } <*>; +%left PLUS MINUS STAR SLASH GT GE EE NE LE LT OR AND +%precedence "bool" "id" +%precedence LPAREN NOT %% %start unit; unit: @@ -75,43 +78,42 @@ unit: statements: %empty {} | statement {} -| statement ";" statements {} +| statement TERM statements {} ; + statement: - "identifier" ":=" exp { drv.result[$1] = $3; } -| "type" "identifier" ":=" exp { drv.result[$2] = $4; } -| "access_modifier" "type" "identifier" ":=" exp { drv.result[$3] = $5; } + "identifier" ASSIGN exp { drv.result[$1] = $3; } +| "type" "identifier" ASSIGN exp { drv.result[$2] = $4; } +| "access_modifier" "type" "identifier" ASSIGN exp { drv.result[$3] = $5; } ; -%precedence "||" "&&" ">" ">=" "==" "!=" "<=" "<" "*" "/" "+" "-"; - exp: - lit { $$ = $1; } -| exp "+" exp { $$ = $1 + $3; } -| exp "-" exp { $$ = $1 - $3; } -| exp "*" exp { $$ = $1 * $3; } -| exp "/" exp { $$ = $1 / $3; } -| exp ">" exp { $$ = gt_($1,$3); } -| exp ">=" exp { $$ = ge_($1,$3); } -| exp "==" exp { $$ = ee_($1,$3); } -| exp "!=" exp { $$ = ne_($1,$3); } -| exp "<=" exp { $$ = le_($1,$3); } -| exp "<" exp { $$ = lt_($1,$3); } -| exp "||" exp { $$ = or_($1,$3); } -| exp "&&" exp { $$ = and_($1,$3); } -| "!" exp { $$ = not_($2); } -| "(" exp ")" { $$ = $2; } + lit { $$ = $1; } +| exp PLUS exp { $$ = $1 + $3; } +| exp MINUS exp { $$ = $1 - $3; } +| exp STAR exp { $$ = $1 * $3; } +| exp SLASH exp { $$ = $1 / $3; } +| exp GT exp { $$ = gt_($1,$3); } +| exp GE exp { $$ = ge_($1,$3); } +| exp EE exp { $$ = ee_($1,$3); } +| exp NE exp { $$ = ne_($1,$3); } +| exp LE exp { $$ = le_($1,$3); } +| exp LT exp { $$ = lt_($1,$3); } +| exp OR exp { $$ = or_($1,$3); } +| exp AND exp { $$ = and_($1,$3); } +| NOT exp { $$ = not_($2); } +| LPAREN exp RPAREN { $$ = $2; } ; lit: - "number" { $$ = $1; } -| "-" "number" { $$ = -$2; } -| "float" { $$ = $1; } -| "-" "float" { $$ = -$2; } -| "string" { $$ = $1; } -| "bool" { $$ = $1; } -| "identifier" { $$ = drv.environment.at($1); } + "number" { $$ = $1; } +| MINUS "number" { $$ = -$2; } +| "float" { $$ = $1; } +| MINUS "float" { $$ = -$2; } +| "string" { $$ = $1; } +| "bool" { $$ = $1; } +| "identifier" { $$ = drv.environment.at($1); } ; %% diff --git a/src/parser/scanner.l b/src/parser/scanner.l index ffe75ab..e37d765 100644 --- a/src/parser/scanner.l +++ b/src/parser/scanner.l @@ -93,7 +93,7 @@ %{ // TODO: Remove [ðđ€\(\)] %} -id [a-z_A-Z][a-zA-Z_.0-9ðđ€\(\)]* +id [a-z_A-Z]([.ðđ€\(\)a-zA-Z_0-9]*[a-zA-Z_0-9]+)? int [0-9]+ flt [0-9]+[.][0-9]+f bool [Ff]alse|[Tt]rue From 38763bb1900f6ef2adda903e625ff4e21693be83 Mon Sep 17 00:00:00 2001 From: Asger Gitz-Johansen Date: Mon, 18 Apr 2022 08:09:58 +0200 Subject: [PATCH 3/7] Restored support for 3 == 3 && 0 == 0 expressions --- src/parser/parser.y | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/parser/parser.y b/src/parser/parser.y index 8c2a7d4..844d5e7 100644 --- a/src/parser/parser.y +++ b/src/parser/parser.y @@ -65,8 +65,10 @@ %nterm lit exp %printer { yyo << $$; } <*>; -%left PLUS MINUS STAR SLASH GT GE EE NE LE LT OR AND -%precedence "bool" "id" +%left OR +%left AND +%left GT GE EE NE LE LT +%left PLUS MINUS STAR SLASH %precedence LPAREN NOT %% %start unit; From ec158c1cf2aac3a8252532ba56c1cde1763a9ca4 Mon Sep 17 00:00:00 2001 From: Asger Gitz-Johansen Date: Mon, 18 Apr 2022 08:38:33 +0200 Subject: [PATCH 4/7] wip --- include/parser/driver.h | 2 ++ src/parser/driver.cpp | 13 +++++++++---- src/parser/parser.y | 5 +++-- src/parser/scanner.l | 6 +++--- 4 files changed, 17 insertions(+), 9 deletions(-) diff --git a/include/parser/driver.h b/include/parser/driver.h index a4e5e73..d6c8144 100644 --- a/include/parser/driver.h +++ b/include/parser/driver.h @@ -10,6 +10,8 @@ struct driver { explicit driver(const symbol_table_t& env); const symbol_table_t& environment{}; symbol_table_t result{}; + symbol_value_t error{}; + symbol_value_t expression_result{}; int parse(const std::string& f); std::string file; diff --git a/src/parser/driver.cpp b/src/parser/driver.cpp index 429bcd9..9bdfdb8 100644 --- a/src/parser/driver.cpp +++ b/src/parser/driver.cpp @@ -8,7 +8,7 @@ driver::driver(const symbol_table_t& map) : trace_parsing (false), trace_scannin int driver::parse(const std::string &f) { if(f.empty()) { #ifdef DEFAULT_EXPRESSION_VALUE - result["expression_result"] = DEFAULT_EXPRESSION_VALUE; + expression_result = DEFAULT_EXPRESSION_VALUE; #endif return 0; } @@ -17,7 +17,12 @@ int driver::parse(const std::string &f) { scan_begin(); yy::parser parse(*this); parse.set_debug_level(trace_parsing); - int res = parse(); - scan_end(); - return res; + try { + int res = parse(); + scan_end(); + return res; + } catch(std::exception& e) { + error = e.what(); + return 1; + } } diff --git a/src/parser/parser.y b/src/parser/parser.y index 844d5e7..a78e8ba 100644 --- a/src/parser/parser.y +++ b/src/parser/parser.y @@ -54,6 +54,7 @@ ACCMOD "access_modifier" TYPE "type" TERM ";" + NEWLINE "\n" ; // Identifiers are strings @@ -74,16 +75,16 @@ %start unit; unit: statements { } -| exp { drv.result["expression_result"] = $1; } +| exp { drv.expression_result = $1; } ; statements: %empty {} | statement {} | statement TERM statements {} +| statement NEWLINE statements {} ; - statement: "identifier" ASSIGN exp { drv.result[$1] = $3; } | "type" "identifier" ASSIGN exp { drv.result[$2] = $4; } diff --git a/src/parser/scanner.l b/src/parser/scanner.l index e37d765..3c44663 100644 --- a/src/parser/scanner.l +++ b/src/parser/scanner.l @@ -94,8 +94,8 @@ // TODO: Remove [ðđ€\(\)] %} id [a-z_A-Z]([.ðđ€\(\)a-zA-Z_0-9]*[a-zA-Z_0-9]+)? -int [0-9]+ -flt [0-9]+[.][0-9]+f +int [0-9]+[Ll]? +flt [0-9]+[.][0-9]+[fd]? bool [Ff]alse|[Tt]rue str \"(\\.|[^"\\])*\" blank [ \t\r] @@ -114,7 +114,7 @@ type int|long|float|double|string|bool|var|auto loc.step(); %} {blank}+ loc.step(); -\n+ loc.lines(yyleng); loc.step(); +\n+ { loc.lines(yyleng); loc.step(); return yy::parser::make_NEWLINE(loc); } "-" return yy::parser::make_MINUS (loc); "+" return yy::parser::make_PLUS (loc); From 29fb6f3e628b8094023b79c31a61ba459e9d6292 Mon Sep 17 00:00:00 2001 From: Asger Gitz-Johansen Date: Tue, 19 Apr 2022 20:15:44 +0200 Subject: [PATCH 5/7] Perform check if debug before lookup --- include/parser/driver.h | 1 + src/parser/driver.cpp | 8 ++++++++ src/parser/parser.y | 2 +- 3 files changed, 10 insertions(+), 1 deletion(-) diff --git a/include/parser/driver.h b/include/parser/driver.h index d6c8144..9d1d816 100644 --- a/include/parser/driver.h +++ b/include/parser/driver.h @@ -14,6 +14,7 @@ struct driver { symbol_value_t expression_result{}; int parse(const std::string& f); + auto get_symbol(const std::string& identifier) -> symbol_value_t; std::string file; bool trace_parsing; diff --git a/src/parser/driver.cpp b/src/parser/driver.cpp index 9bdfdb8..48c3fde 100644 --- a/src/parser/driver.cpp +++ b/src/parser/driver.cpp @@ -26,3 +26,11 @@ int driver::parse(const std::string &f) { return 1; } } + +auto driver::get_symbol(const std::string &identifier) -> symbol_value_t { +#ifndef NDEBUG + if(!environment.contains(identifier)) + throw std::out_of_range(identifier + " not found"); +#endif + return environment.at(identifier); +} diff --git a/src/parser/parser.y b/src/parser/parser.y index a78e8ba..d1f91a0 100644 --- a/src/parser/parser.y +++ b/src/parser/parser.y @@ -116,7 +116,7 @@ lit: | MINUS "float" { $$ = -$2; } | "string" { $$ = $1; } | "bool" { $$ = $1; } -| "identifier" { $$ = drv.environment.at($1); } +| "identifier" { $$ = drv.get_symbol($1); } ; %% From f8af8fb839f5e5399d050781dad843b92e8ffac2 Mon Sep 17 00:00:00 2001 From: Asger Gitz-Johansen Date: Fri, 22 Apr 2022 07:39:45 +0200 Subject: [PATCH 6/7] Remove the newline stuff Simply make the TERM optional --- src/parser/parser.y | 8 +++----- src/parser/scanner.l | 2 +- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/src/parser/parser.y b/src/parser/parser.y index d1f91a0..b7deb82 100644 --- a/src/parser/parser.y +++ b/src/parser/parser.y @@ -54,7 +54,6 @@ ACCMOD "access_modifier" TYPE "type" TERM ";" - NEWLINE "\n" ; // Identifiers are strings @@ -79,16 +78,15 @@ unit: ; statements: - %empty {} -| statement {} -| statement TERM statements {} -| statement NEWLINE statements {} + %empty {} +| statement statements {} ; statement: "identifier" ASSIGN exp { drv.result[$1] = $3; } | "type" "identifier" ASSIGN exp { drv.result[$2] = $4; } | "access_modifier" "type" "identifier" ASSIGN exp { drv.result[$3] = $5; } +| statement TERM { } ; exp: diff --git a/src/parser/scanner.l b/src/parser/scanner.l index 3c44663..10956b8 100644 --- a/src/parser/scanner.l +++ b/src/parser/scanner.l @@ -114,7 +114,7 @@ type int|long|float|double|string|bool|var|auto loc.step(); %} {blank}+ loc.step(); -\n+ { loc.lines(yyleng); loc.step(); return yy::parser::make_NEWLINE(loc); } +\n+ { loc.lines(yyleng); loc.step(); } "-" return yy::parser::make_MINUS (loc); "+" return yy::parser::make_PLUS (loc); From 6e90fe0b3c2f642fe0881f529de1a930a43b1897 Mon Sep 17 00:00:00 2001 From: Asger Gitz-Johansen Date: Sun, 24 Apr 2022 10:40:58 +0200 Subject: [PATCH 7/7] Update version --- CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 3a1e9d8..86397fa 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,5 +1,5 @@ cmake_minimum_required(VERSION 3.0) -project(expr VERSION 1.3.0) +project(expr VERSION 1.3.1) include(cmake/CPM.cmake) configure_file(src/config.h.in config.h) set(CMAKE_CXX_STANDARD 20)