Start echoing parse tree

This commit is contained in:
Dane Johnson 2021-12-08 14:36:22 -06:00
parent ecc773b774
commit 1c3233d272
5 changed files with 59 additions and 15 deletions

View File

@ -1,6 +1,7 @@
.PHONY: all clean .PHONY: all clean
all: deelang all: deelang
CC = g++ CC = g++
CPPFLAGS = -g
lexer.o lexer.h: parser.h lexer.l lexer.o lexer.h: parser.h lexer.l
flex -o lexer.cpp --header-file=lexer.h lexer.l flex -o lexer.cpp --header-file=lexer.h lexer.l
@ -13,7 +14,7 @@ parser.h parser.o: parser.y
rm parser.cpp rm parser.cpp
deelang: deelang.cpp lexer.o parser.o syntax.o deelang: deelang.cpp lexer.o parser.o syntax.o
$(CC) -o $@ $^ $(CC) $(CPPFLAGS) -o $@ $^
clean: clean:
rm -rf *.o parser.h lexer.h deelang rm -rf *.o parser.h lexer.h deelang

View File

@ -5,7 +5,6 @@
using namespace std; using namespace std;
int main() { int main() {
yydebug = 1;
yyparse(); yyparse();
return 0; return 0;

View File

@ -1,6 +1,8 @@
%{ %{
#include <stdio.h> #include <cstdio>
#include "syntax.h" #include "syntax.h"
using namespace std; using namespace std;
int yylex(); int yylex();
@ -12,7 +14,8 @@ void yyerror(const char* p) { fprintf(stderr, p); }
%union { %union {
float num; float num;
char *sym; char *sym;
Node *node; Node *expr;
NodeList *exprlist;
} }
%token <sym> ID %token <sym> ID
@ -26,10 +29,11 @@ void yyerror(const char* p) { fprintf(stderr, p); }
%right GETS %right GETS
%left CAT %left CAT
%left '+' '-' %left '+' '-'
%left '/' '*' %left '/' '*'n
%left MAPS %left MAPS
%nterm <node> expr %nterm <expr> expr
%nterm <exprlist> exprlist
%% %%
program: statements | statements statement; program: statements | statements statement;
@ -55,8 +59,10 @@ expr: funcdef
funcdef: param MAPS expr | param MAPS block; funcdef: param MAPS expr | param MAPS block;
param: ID | '_' | // null production; param: ID | '_' | // null production;
funcall: ID '(' exprlist ')' funcall: ID '(' exprlist ')' { print_expression_list($3); }
exprlist: exprlist ',' expr | expr | // null production; exprlist: exprlist ',' expr { $1 -> push_back($3); $$ = $1; }
| expr { $$ = new NodeList(); $$ -> push_back($1); }
| { $$ = NULL; } // null production;
conditional: IF expr block elifs conditional: IF expr block elifs
| IF expr block elifs ELSE block; | IF expr block elifs ELSE block;

View File

@ -1,18 +1,33 @@
#include <string.h> #include <iostream>
#include <stdlib.h> #include <cstring>
#include <cstdlib>
#include "syntax.h" #include "syntax.h"
using namespace std;
IdNode::IdNode(char * id) { IdNode::IdNode(char * id) {
this->id = id; this->id = string(id);
}
void IdNode::display() {
cout << id;
} }
NumNode::NumNode(float num) { NumNode::NumNode(float num) {
this->num = num; this->num = num;
} }
void NumNode::display() {
}
StringNode::StringNode(char *str) { StringNode::StringNode(char *str) {
this->string = string; this->stringVal = str;
}
void StringNode::display() {
} }
OpNode::OpNode(int op, Node *first, Node *second) { OpNode::OpNode(int op, Node *first, Node *second) {
@ -20,3 +35,13 @@ OpNode::OpNode(int op, Node *first, Node *second) {
this->first = first; this->first = first;
this->second = second; this->second = second;
} }
void OpNode::display() {
}
void print_expression_list(NodeList *list) {
for (Node *expr : *list) {
expr->display();
}
}

View File

@ -1,13 +1,20 @@
#ifndef SYNTAX_H #ifndef SYNTAX_H
#define SYNTAX_H #define SYNTAX_H
class Node {}; #include <vector>
#include <string>
class Node {
public:
virtual void display() = 0;
};
class IdNode : public Node { class IdNode : public Node {
private: private:
char* id; std::string id;
public: public:
IdNode(char*); IdNode(char*);
virtual void display();
}; };
class NumNode : public Node { class NumNode : public Node {
@ -15,13 +22,15 @@ private:
float num; float num;
public: public:
NumNode(float); NumNode(float);
virtual void display();
}; };
class StringNode : public Node { class StringNode : public Node {
private: private:
char* string; char* stringVal;
public: public:
StringNode(char*); StringNode(char*);
virtual void display();
}; };
class OpNode : public Node { class OpNode : public Node {
@ -31,7 +40,11 @@ private:
Node *second; Node *second;
public: public:
OpNode(int, Node*, Node*); OpNode(int, Node*, Node*);
virtual void display();
}; };
class NodeList : public std::vector<Node*>{};
void print_expression_list(NodeList *);
#endif /* SYNTAX_H */ #endif /* SYNTAX_H */