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
all: deelang
CC = g++
CPPFLAGS = -g
lexer.o lexer.h: parser.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
deelang: deelang.cpp lexer.o parser.o syntax.o
$(CC) -o $@ $^
$(CC) $(CPPFLAGS) -o $@ $^
clean:
rm -rf *.o parser.h lexer.h deelang

View File

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

View File

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

View File

@ -1,18 +1,33 @@
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include "syntax.h"
using namespace std;
IdNode::IdNode(char * id) {
this->id = id;
this->id = string(id);
}
void IdNode::display() {
cout << id;
}
NumNode::NumNode(float num) {
this->num = num;
}
void NumNode::display() {
}
StringNode::StringNode(char *str) {
this->string = string;
this->stringVal = str;
}
void StringNode::display() {
}
OpNode::OpNode(int op, Node *first, Node *second) {
@ -20,3 +35,13 @@ OpNode::OpNode(int op, Node *first, Node *second) {
this->first = first;
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
#define SYNTAX_H
class Node {};
#include <vector>
#include <string>
class Node {
public:
virtual void display() = 0;
};
class IdNode : public Node {
private:
char* id;
std::string id;
public:
IdNode(char*);
virtual void display();
};
class NumNode : public Node {
@ -15,13 +22,15 @@ private:
float num;
public:
NumNode(float);
virtual void display();
};
class StringNode : public Node {
private:
char* string;
char* stringVal;
public:
StringNode(char*);
virtual void display();
};
class OpNode : public Node {
@ -31,7 +40,11 @@ private:
Node *second;
public:
OpNode(int, Node*, Node*);
virtual void display();
};
class NodeList : public std::vector<Node*>{};
void print_expression_list(NodeList *);
#endif /* SYNTAX_H */