Skip to content

Commit

Permalink
specify c++17, and fix a bug that we don't have to put expression in …
Browse files Browse the repository at this point in the history
…statements
  • Loading branch information
ytsao committed Dec 1, 2024
1 parent 8a3ca79 commit fdab176
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 53 deletions.
2 changes: 2 additions & 0 deletions abstract_interpreter/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ project(abstract_interpreter_parser
HOMEPAGE_URL "https://github.com/ytsao/lattice-gym"
LANGUAGES CXX)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++17")

# dependencies
include(FetchContent)
FetchContent_Declare(
Expand Down
57 changes: 36 additions & 21 deletions abstract_interpreter/example/abstract_interpreter/parser.cpp
Original file line number Diff line number Diff line change
@@ -1,30 +1,45 @@
#include <fstream>
#include <sstream>

#include "../../include/parser.hpp"
#include "../../include/ast.hpp"


int main(){
std::string input = R"(
int x, y ;
int x = 4;
int y = 3;
x = 2 * (y - 10);
if (x == 10) {
if (y == 10){
y = 3;
x = 5;
}
}
while(x != 0){
int z = 10;
// std::string input = R"(
// int x, y ;
// int x = 4;
// int y = 3;
// x = 2 * (y - 10);
// if (x == 10) {
// if (y == 10){
// y = 3;
// x = 5;
// }
// }
// while(x != 0){
// int z = 10;
// }
// )";
for (int i = 0; i < 1; ++i){
std::string filename = (i < 10 ? "0" : "") + std::to_string(i); // Add "0" prefix if i < 10
std::ifstream f("./tests/" + filename + ".c");
if (!f.is_open()){
std::cerr << "[ERROR] file cannot open!" << std::endl;
return -1;
}
)";

AbstractInterpreterParser AIParser;
auto ast = AIParser.parse(input);
printAST(ast);
// AIParser.parse("-1+(1+2)*3- -1");


// std::cout << "Result: " << root->evaluate() << std::endl;
std::ostringstream buffer;
buffer << f.rdbuf();
std::string input = buffer.str();
f.close();

std::cout << "Program content:\n" << input << std::endl;

AbstractInterpreterParser AIParser;
auto ast = AIParser.parse(input);
printAST(ast);
}

return 0;
}
55 changes: 35 additions & 20 deletions abstract_interpreter/include/parser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ class AbstractInterpreterParser{
ASTNode parse(const std::string& input){
peg::parser parser(R"(
Program <- Statements*
Statements <- DeclareVar / Assignment / Expression / IfElse / WhileLoop / Block / Comment
Statements <- DeclareVar / Assignment / IfElse / WhileLoop / Block / Comment
Integer <- < [+-]? [0-9]+ >
Identifier <- < [a-zA-Z_][a-zA-Z0-9_]* >
SeqOp <- '+' / '-'
PreOp <- '*' / '/'
LogicOp <- '<' / '>' / '<=' / '>=' / '==' / '!='
DeclareVar <- 'int' Identifier ('=' Integer / ',' Identifier)* ';'
Assignment <- Identifier '=' Expression ';'
Block <- '{' Statements* '}'
Block <- ('void' 'main' '(' ')')? '{' Statements* '}'
IfElse <- 'if' '(' Expression ')' (Block / Statements) ('else' (Block / Statements))?
WhileLoop <- 'while' '(' Expression ')' (Block / Statements)
Expand All @@ -43,7 +43,9 @@ class AbstractInterpreterParser{
// // setup actions
parser["Program"] = [this](const SV& sv){return make_program(sv);};
parser["Integer"] = [](const SV& sv){return ASTNode(sv.token_to_number<int>());};
parser["Identifier"] = [](const SV& sv){return ASTNode(sv.token_to_string());};
parser["Identifier"] = [](const SV& sv){
std::cout << sv.token_to_string() << std::endl;
return ASTNode(sv.token_to_string());};
parser["SeqOp"] = [this](const SV& sv){return make_seq_op(sv);};
parser["PreOp"] = [this](const SV& sv){return make_pre_op(sv);};
parser["LogicOp"] = [this](const SV& sv){return make_logic_op(sv);};
Expand All @@ -69,7 +71,7 @@ class AbstractInterpreterParser{

private:
ASTNode make_program(const SV& sv){
std::cout << sv.token_to_string() << std::endl;
std::cout << "program section" << std::endl;
std::cout << sv.size() << std::endl;
if (sv.size() == 1){
return std::any_cast<ASTNode>(sv[0]);
Expand All @@ -84,7 +86,7 @@ class AbstractInterpreterParser{
}

ASTNode make_decl_var(const SV& sv){
ASTNode decl_node(NodeType::DECLARATION);
ASTNode decl_node(NodeType::DECLARATION, std::string("int"));
for (size_t i = 0; i < sv.size(); ++i){
decl_node.children.push_back(std::any_cast<ASTNode>(sv[i]));
}
Expand Down Expand Up @@ -120,25 +122,36 @@ class AbstractInterpreterParser{
}

ASTNode make_expr(const SV& sv){
ASTNode expr(NodeType::ASSIGNMENT);
for (size_t i = 0; i < sv.size(); ++i){
ASTNode node = std::any_cast<ASTNode>(sv[i]);
expr.children.push_back(node);
if (sv.size() == 1){
return std::any_cast<ASTNode>(sv[0]);
}
else{
assert(sv.size()>=3);

ASTNode expr(NodeType::BINARY_OP, std::any_cast<ASTNode>(sv[1]).value);
expr.children.push_back(std::any_cast<ASTNode>(sv[0]));
expr.children.push_back(std::any_cast<ASTNode>(sv[2]));
return expr;
}
return expr;
}

ASTNode make_term(const SV& sv){
ASTNode term(NodeType::BINARY_OP);
for (size_t i = 0; i < sv.size(); ++i){
ASTNode node = std::any_cast<ASTNode>(sv[i]);
term.children.push_back(node);
if (sv.size() == 1){
return std::any_cast<ASTNode>(sv[0]);
}
return term;
else{
assert(sv.size()>=3);

ASTNode term(NodeType::BINARY_OP, std::any_cast<ASTNode>(sv[1]).value);
term.children.push_back(std::any_cast<ASTNode>(sv[0]));
term.children.push_back(std::any_cast<ASTNode>(sv[2]));
return term;
}

}

ASTNode make_assign(const SV& sv){
ASTNode assign_node(NodeType::ASSIGNMENT);
ASTNode assign_node(NodeType::ASSIGNMENT, std::string("="));
ASTNode var = std::any_cast<ASTNode>(sv[0]);
ASTNode expr = std::any_cast<ASTNode>(sv[1]);
assign_node.children.push_back(var);
Expand All @@ -147,11 +160,14 @@ class AbstractInterpreterParser{
}

ASTNode make_block(const SV& sv){
std::cout << sv.token_to_string() << std::endl;
std::cout << "this is block section" << std::endl;
std::cout << "size = " << sv.size() << std::endl;
if (sv.size() == 1){
return std::any_cast<ASTNode>(sv[0]);
}
else{
ASTNode block_node(NodeType::BODY);
ASTNode block_node(NodeType::BODY, std::string("Block-Body"));
for (size_t i = 0; i < sv.size(); ++i){
block_node.children.push_back(std::any_cast<ASTNode>(sv[i]));
}
Expand All @@ -160,17 +176,16 @@ class AbstractInterpreterParser{
}

ASTNode make_ifelse(const SV& sv){
ASTNode ifelse_node(NodeType::IFELSE);
ASTNode ifelse_node(NodeType::IFELSE, std::string("IfElse"));
for (size_t i = 0; i < sv.size(); ++i){
std::cout << "if-else ...." << std::endl;
ASTNode node = std::any_cast<ASTNode>(sv[i]);
ifelse_node.children.push_back(node);
}
return ifelse_node;
}

ASTNode make_whileloop(const SV& sv){
ASTNode whileloop_node(NodeType::WHILELOOP);
ASTNode whileloop_node(NodeType::WHILELOOP, std::string("WhileLoop"));
for (size_t i = 0; i < sv.size(); ++i){
ASTNode node = std::any_cast<ASTNode>(sv[i]);
whileloop_node.children.push_back(node);
Expand Down
16 changes: 4 additions & 12 deletions abstract_interpreter/tests/00.c
Original file line number Diff line number Diff line change
@@ -1,22 +1,14 @@
/*******************************************************************/
/* Cas d'etudes pour le projet du cours d'interpratation abstraite */
/* Ecrit par Olivier Bouissou ([email protected]) */
/* Le but de ces cas d'etudes est de vous permettre de tester */
/* votre projet sur des exemples de programmes contenant chacun */
/* une difficulte que vous devriez rencontrer. */
/*******************************************************************/
/* Caclul arithmetique 1. */
/* On cherche ici a tester les fonctions arithmetiques des domaines */
/* abstraits. */
/*******************************************************************/


int a, b, c, d, e, f;

void main() {
void main () {

a = 23;
b = 5;

if (a == 1) b = 4;

c = a + b;
d = a + c;
e = d + c + f;
Expand Down

0 comments on commit fdab176

Please sign in to comment.