Skip to content

Commit a7d5180

Browse files
committed
converting to more concrete ast
1 parent 07b8f9f commit a7d5180

File tree

6 files changed

+156
-24
lines changed

6 files changed

+156
-24
lines changed

Makefile.am

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ AM_LFLAGS = -olex.yy.c
1414

1515
bin_PROGRAMS += wiki_main
1616
wiki_main_SOURCES = lexer.ll parser.y wiki_main.cc expression.c
17+
wiki_main_LDADD = libproposition.la
1718

1819
clean-local:
1920
-rm -f lexer.c lexer.cc lexer.h lexer.hh parser.c parser.cc parser.h parser.hh

lexer.ll

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,16 @@
55
* To generate the lexical analyzer run: "flex Lexer.l"
66
*/
77

8-
#include "expression.h"
9-
#include "parser.hh"
10-
118
#include <cstdio>
129
#include <string>
1310

14-
/* #define YY_DECL extern "C" int yylex() */
11+
#include "norms.h"
12+
#include "expression.h"
13+
#include "parser.hh"
1514

15+
#define SAVE_TOKEN yylval.string = new std::string(yytext, yyleng)
16+
#define TOKEN(t) (yylval.token = t)
17+
/*extern "C" int yywrap() {} */
1618
%}
1719
%option outfile="lexer.cc" header-file="lexer.hh"
1820

@@ -37,7 +39,7 @@ STRING [a-zA-Z0-9]+
3739
{WS} { /* Skip blanks. */ }
3840
{NUMBER} { sscanf(yytext, "%d", &yylval->value); return TOKEN_NUMBER; }
3941

40-
{STRING} { yylval->string = strdup(yytext); return TOKEN_STRING; }
42+
{STRING} { yylval->string = new std::string(strdup(yytext)); return TOKEN_STRING; }
4143

4244
{MULTIPLY} { return TOKEN_MULTIPLY; }
4345
{PLUS} { return TOKEN_PLUS; }

norms.h

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
#pragma once
2+
3+
#include <iostream>
4+
#include <vector>
5+
#include <string>
6+
7+
class NStatement;
8+
class NExpression;
9+
class NVariableDeclaration;
10+
11+
typedef std::vector<NStatement*> StatementList;
12+
typedef std::vector<NExpression*> ExpressionList;
13+
typedef std::vector<NVariableDeclaration*> VariableList;
14+
15+
class Norm {
16+
public:
17+
virtual ~Norm() {}
18+
};
19+
20+
class NExpression : public Norm {
21+
};
22+
23+
class NStatement : public Norm {
24+
};
25+
26+
class NIdentifier : public NExpression {
27+
public:
28+
std::string name;
29+
NIdentifier(const std::string& name) : name(name) {}
30+
};
31+
32+
class NMethodCall : public NExpression {
33+
public:
34+
const NIdentifier& id;
35+
ExpressionList arguments;
36+
NMethodCall(const NIdentifier& id, ExpressionList& arguments) :
37+
id(id), arguments(arguments) {}
38+
NMethodCall(const NIdentifier& id) : id(id) {}
39+
};
40+
41+
class NBinaryOperator : public NExpression {
42+
public:
43+
NExpression& lhs;
44+
int op;
45+
NExpression& rhs;
46+
NBinaryOperator(NExpression& lhs, int op, NExpression& rhs) :
47+
lhs(lhs), op(op), rhs(rhs) {}
48+
};
49+
50+
class NBlock : public NExpression {
51+
public:
52+
StatementList statements;
53+
NBlock() {}
54+
};
55+
56+
class NExpressionStatement : public NStatement {
57+
public:
58+
NExpression& expression;
59+
NExpressionStatement(NExpression& expression) :
60+
expression(expression) {}
61+
};
62+
63+
class NVariableDeclaration : public NStatement {
64+
public:
65+
const NIdentifier& type;
66+
NIdentifier& id;
67+
NExpression *assignmentExpr;
68+
69+
NVariableDeclaration(const NIdentifier& type, NIdentifier& id) :
70+
type(type), id(id) {}
71+
72+
NVariableDeclaration(const NIdentifier& type, NIdentifier& id,
73+
NExpression *assignmentExpr) :
74+
type(type), id(id), assignmentExpr(assignmentExpr) {}
75+
};
76+
77+
class NFunctionDeclaration : public NStatement {
78+
public:
79+
const NIdentifier& type;
80+
const NIdentifier& id;
81+
VariableList arguments;
82+
NBlock& block;
83+
NFunctionDeclaration(const NIdentifier& type, const NIdentifier& id,
84+
const VariableList& arguments, NBlock& block) :
85+
type(type), id(id), arguments(arguments), block(block) {}
86+
};

parser.y

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,26 @@
11
%{
2-
3-
/*
4-
* Parser.y file
5-
* To generate the parser run: "bison Parser.y"
6-
*/
7-
82
#include <string>
93

4+
#include "norms.h"
105
#include "expression.h"
116
#include "parser.hh"
127
#include "lexer.hh"
138

149
namespace {
15-
int yyerror(yyscan_t scanner,
16-
SExpression **expression,
17-
const char *msg) {
18-
fprintf(stderr,"SPECIAL Error:%s %s\n",msg, (*expression)->string); return 0;
19-
}
20-
int yyerror(
21-
SExpression **expression,
22-
yyscan_t scanner, const char *msg) {
23-
fprintf(stderr,"SPECIAL Error:%s %s\n",msg, (*expression)->string); return 0;
10+
11+
int yyerror(yyscan_t scanner, SExpression **expression, const char *msg) {
12+
fprintf(stderr,"SPECIAL Error:%s %s\n",msg, (*expression)->string);
13+
return 0;
2414
}
15+
int yyerror(SExpression **expression, yyscan_t scanner, const char *msg) {
16+
fprintf(stderr,"SPECIAL Error:%s %s\n",msg, (*expression)->string);
17+
return 0;
2518
}
19+
20+
NBlock *normBlock;
21+
extern int yylex();
22+
23+
} /* namespace */
2624
%}
2725

2826
%code requires {
@@ -43,9 +41,19 @@ typedef void* yyscan_t;
4341
%parse-param { yyscan_t scanner }
4442

4543
%union {
46-
char *string;
4744
int value;
4845
SExpression *expression;
46+
47+
Norm *node;
48+
NBlock *block;
49+
NExpression *expr;
50+
NStatement *stmt;
51+
NIdentifier *ident;
52+
NVariableDeclaration *var_decl;
53+
std::vector<NVariableDeclaration*> *varvec;
54+
std::vector<NExpression*> *expvec;
55+
std::string *string;
56+
int token;
4957
}
5058

5159
%left '+' TOKEN_PLUS
@@ -78,6 +86,6 @@ expr
7886
| expr TOKEN_OR expr { $$ = createOperation( eOR, $1, $3 ); }
7987
| TOKEN_LPAREN expr TOKEN_RPAREN { $$ = $2; }
8088
| TOKEN_NUMBER { $$ = createNumber($1); }
81-
| TOKEN_STRING { $$ = createString($1); }
89+
| TOKEN_STRING { $$ = createString($1->c_str()); }
8290
;
8391
%%

proposition.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@ class Proposition {
6060
};
6161

6262
typedef shared_ptr<Base> pointer;
63-
6463
pointer value;
64+
6565
Proposition(const pointer& value_) : value(value_) {}
6666

6767
struct Variable : Base {

wiki_main.cc

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,19 @@
1+
#include <cassert>
12
#include <iostream>
23
#include <cstdio>
4+
#include <set>
5+
#include <string>
36

7+
#include "norms.h"
48
#include "expression.h"
59
#include "parser.hh"
610
#include "lexer.hh"
711

12+
#include "proposition.h"
13+
14+
using std::set;
15+
using std::string;
16+
817
int yyparse(SExpression **expression, yyscan_t scanner);
918

1019
SExpression *getAST(const char *expr) {
@@ -35,6 +44,7 @@ int evaluate(SExpression *e) {
3544
switch (e->type) {
3645
case eSTRING:
3746
printf("String: %s\n", e->string);
47+
assert(V(e->string).evaluate({e->string}));
3848
return 1;
3949
case eVALUE:
4050
return e->value;
@@ -52,10 +62,26 @@ int evaluate(SExpression *e) {
5262
}
5363
}
5464

65+
Proposition prop_evaluate(SExpression* e, set<string>* vars) {
66+
assert(vars != NULL);
67+
switch (e->type) {
68+
case eSTRING:
69+
printf("String: %s\n", e->string);
70+
vars->insert(e->string);
71+
return V(e->string);
72+
case eAND:
73+
return prop_evaluate(e->left, vars) && prop_evaluate(e->right, vars);
74+
case eOR:
75+
return prop_evaluate(e->left, vars) || prop_evaluate(e->right, vars);
76+
default:
77+
assert(false);
78+
}
79+
}
80+
5581
int main(int argc, char **argv) {
5682
SExpression *e = NULL;
5783
// char test[]=" 4 + 2*10 + 3*( 5 + 1 )";
58-
char test[] = "4 + makeItHappen + hello";
84+
char test[] = "makeItHappen || hello";
5985
int result = 0;
6086

6187
e = getAST(test);
@@ -64,6 +90,15 @@ int main(int argc, char **argv) {
6490

6591
printf("Result of '%s' is %d\n", test, result);
6692

93+
set<string> vars;
94+
Proposition prop = prop_evaluate(e, &vars);
95+
auto truth_assignments = prop.evaluate_all(vars);
96+
for (auto truth_assignment : truth_assignments) {
97+
for (auto variable : vars)
98+
cout << (truth_assignment.count(variable) ? "1" : "0") << " ";
99+
cout << endl;
100+
}
101+
67102
deleteExpression(e);
68103

69104
return 0;

0 commit comments

Comments
 (0)