From 28ca85b94e936383e8f391bebed3a74cadf7bc54 Mon Sep 17 00:00:00 2001 From: Markus Hauschild Date: Fri, 31 May 2013 23:51:44 +0200 Subject: [PATCH] Progress! --- CMakeLists.txt | 17 +++++++++++------ grammar/grammar.y | 43 +++++++++++++++++++++++++++++++----------- inc/AST/ASTElement.h | 14 ++++++++++++++ inc/AST/ASTVisitor.h | 10 ++++++++++ inc/Token.h | 20 ++++++++++++++++++++ src/AST/ASTElement.cpp | 10 ++++++++++ src/AST/ASTVisitor.cpp | 10 ++++++++++ src/Token.cpp | 15 +++++++++++++++ src/test.cpp | 7 +++++++ 9 files changed, 129 insertions(+), 17 deletions(-) create mode 100644 inc/AST/ASTElement.h create mode 100644 inc/AST/ASTVisitor.h create mode 100644 inc/Token.h create mode 100644 src/AST/ASTElement.cpp create mode 100644 src/AST/ASTVisitor.cpp create mode 100644 src/Token.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 7f6e77e..291c54f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -18,7 +18,7 @@ INCLUDE_DIRECTORIES (${CMAKE_SOURCE_DIR}/inc) ADD_DEFINITIONS("-std=c++0x") ADD_EXECUTABLE (lemon grammar/lemon.c) -#ADD_EXECUTABLE (makeheaders grammar/makeheaders.c) +ADD_EXECUTABLE (makeheaders grammar/makeheaders.c) ADD_CUSTOM_COMMAND( COMMAND cp @@ -26,18 +26,18 @@ ADD_CUSTOM_COMMAND( COMMAND cp ARGS ${CMAKE_CURRENT_SOURCE_DIR}/grammar/lempar.c ${CMAKE_CURRENT_BINARY_DIR} COMMAND ${CMAKE_CURRENT_BINARY_DIR}/lemon -# ARGS -q -m ${CMAKE_CURRENT_BINARY_DIR}/grammar.y - ARGS -q ${CMAKE_CURRENT_BINARY_DIR}/grammar.y + ARGS -q -m ${CMAKE_CURRENT_BINARY_DIR}/grammar.y +# ARGS -q ${CMAKE_CURRENT_BINARY_DIR}/grammar.y COMMAND rm ARGS ${CMAKE_CURRENT_BINARY_DIR}/lempar.c COMMAND rm ARGS ${CMAKE_CURRENT_BINARY_DIR}/grammar.y -# COMMAND ${CMAKE_CURRENT_BINARY_DIR}/makeheaders -# ARGS ${CMAKE_CURRENT_BINARY_DIR}/grammar.c + COMMAND ${CMAKE_CURRENT_BINARY_DIR}/makeheaders + ARGS ${CMAKE_CURRENT_BINARY_DIR}/grammar.c COMMAND mv ARGS ${CMAKE_CURRENT_BINARY_DIR}/grammar.c ${CMAKE_CURRENT_BINARY_DIR}/grammar.cpp DEPENDS lemon -# DEPENDS makeheaders + DEPENDS makeheaders DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/grammar/grammar.y OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/grammar.cpp OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/grammar.h @@ -56,6 +56,11 @@ ENDIF (SCULLY_DEBUG) SET(SCULLY_SOURCE src/test.cpp + src/Token.cpp + + src/AST/ASTElement.cpp + src/AST/ASTVisitor.cpp + ${CMAKE_CURRENT_BINARY_DIR}/grammar.cpp ) diff --git a/grammar/grammar.y b/grammar/grammar.y index 2453f46..7432053 100644 --- a/grammar/grammar.y +++ b/grammar/grammar.y @@ -5,9 +5,15 @@ #include #include +#include "Token.h" + +#include "AST/ASTElement.h" + } -%token_type {int} +%name scullyParser + +%token_type {Token*} // whitespace and comments %type T_WHITESPACE {int} @@ -19,40 +25,53 @@ %left T_TIMES T_DIV. %syntax_error { - std::cout << "Syntax error!" << std::endl; + std::cerr << "Syntax error!" << std::endl; } -programm(A) ::= fundefs(B). { A = B; } +%parse_failure { + std::cerr << "Parser Failed!" << std::endl; +} +%type program {int} +program ::= fundefs(F). { std::cout << F << std::endl; } +program ::= expr(E). { std::cout << E << std::endl; } + +%type fundefs {int} fundefs(A) ::= . { A = 0; } fundefs(A) ::= fundefs fundef(B). { A = A + B; } -fundef(A) ::= type(T) T_IDENTIFIER(ID) params(P) T_BEGIN statements(S) T_END. { A = T + ID + P + S; } +%type fundef {int} +fundef(A) ::= type(T) T_IDENTIFIER(ID) params(P) T_BEGIN statements(S) T_END. { A = T + 1 + P + S; /* ID */ } +%type type {int} type(A) ::= T_BOOL. { A = 1; } type(A) ::= T_INT. { A = 1; } type(A) ::= T_STRING. { A = 1; } type(A) ::= T_VOID. { A = 1; } +%type params {int} params(A) ::= . { A = 0; } -params(A) ::= type(T) T_IDENTIFIER(ID). { A = T + ID; } -params(A) ::= params(B) T_COMMA type(T) T_IDENTIFIER(ID). { A = B + T + ID; } +params(A) ::= type(T) T_IDENTIFIER(ID). { A = T + 1; /* ID */ } +params(A) ::= params(B) T_COMMA type(T) T_IDENTIFIER(ID). { A = B + T + 1; /* ID */ } +%type statement {int} statement(A) ::= T_IF T_LPAREN expr(E) T_RPAREN statement(S). { A = E + S; } -statement(A) ::= T_RIF T_LPAREN T_CINT(P) T_RPAREN statement(S). { A = P + S; } +statement(A) ::= T_RIF T_LPAREN T_CINT(P) T_RPAREN statement(S). { A = 1 + S; /* P */ } statement(A) ::= T_FOR T_LPAREN expr(INIT) T_SEMICOLON expr(COND) T_SEMICOLON expr(STEP) T_RPAREN statement(S). { A = INIT + COND + STEP + S; } statement(A) ::= T_RFOR T_LPAREN expr(INIT) T_SEMICOLON T_CINT(P) T_SEMICOLON expr(STEP) T_RPAREN statement(S). - { A = INIT + P + STEP + S; } + { A = INIT + 1 + STEP + S; /* P */ } statement(A) ::= T_RETURN expr(E) T_SEMICOLON. { A = E; } statement(A) ::= T_BEGIN statements(S) T_END. { A = S; } statement(A) ::= vardef(V) T_SEMICOLON. { A = V; } statement(A) ::= expr(E) T_SEMICOLON. { A = E; } +%type statements {int} statements(A) ::= . { A = 0; } statements(A) ::= statements(B) statement(C). { A = B + C; } -expr(A) ::= T_IDENTIFIER(ID) T_ASSIGN expr(E). { A = ID + E; } +%type expr {int} +expr(A) ::= T_IDENTIFIER(ID) T_ASSIGN expr(E). { A = 1 + E; /* ID */ } expr(A) ::= expr(B) T_EQUALS expr(C). { A = B + C; } expr(A) ::= expr(B) T_LESS expr(C). { A = B + C; } expr(A) ::= expr(B) T_PLUS expr(C). { A = B + C; } @@ -62,10 +81,12 @@ expr(A) ::= expr(B) T_DIV expr(C). { A = B + C; } expr(A) ::= T_CINT. { A = 1; } expr(A) ::= T_TRUE. { A = 1; } expr(A) ::= T_FALSE. { A = 1; } -expr(A) ::= T_IDENTIFIER(ID) T_LPAREN values(V) T_RPAREN. { A = ID + V; } +expr(A) ::= T_IDENTIFIER(ID) T_LPAREN values(V) T_RPAREN. { A = 1 + V; /* ID */ } -vardef(A) ::= type(T) T_IDENTIFIER(ID). { A = T + ID; } +%type vardef {int} +vardef(A) ::= type(T) T_IDENTIFIER(ID). { A = T + 1; /* ID */ } +%type values {int} values(A) ::= . { A = 0; } values(A) ::= expr(E). { A = E; } values(A) ::= values(B) T_COMMA expr(E). { A = B + E; } diff --git a/inc/AST/ASTElement.h b/inc/AST/ASTElement.h new file mode 100644 index 0000000..f88b627 --- /dev/null +++ b/inc/AST/ASTElement.h @@ -0,0 +1,14 @@ +#ifndef ASTELEMENT_H +#define ASTELEMENT_H + +#include "AST/ASTVisitor.h" + +class ASTElement { +public: + ASTElement(); + virtual ~ASTElement(); + + virtual void accept(ASTVisitor* visitor) = 0; +}; + +#endif // ASTELEMENT_H diff --git a/inc/AST/ASTVisitor.h b/inc/AST/ASTVisitor.h new file mode 100644 index 0000000..6bb3b17 --- /dev/null +++ b/inc/AST/ASTVisitor.h @@ -0,0 +1,10 @@ +#ifndef ASTVISITOR_H +#define ASTVISITOR_H + +class ASTVisitor { +public: + ASTVisitor(); + virtual ~ASTVisitor(); +}; + +#endif // ASTVISITOR_H diff --git a/inc/Token.h b/inc/Token.h new file mode 100644 index 0000000..a2da3fb --- /dev/null +++ b/inc/Token.h @@ -0,0 +1,20 @@ +/* The scully programming language. + * + * Copyright (c) Peter Dahlberg, Markus Hauschild and Florian Sattler, 2013. + * Licensed under the GNU GPL v2. + */ + +#ifndef TOKEN_H +#define TOKEN_H + +#include + +class Token { +public: + Token(std::string text); + std::string getText(); +private: + std::string text_; +}; + +#endif // TOKEN_H diff --git a/src/AST/ASTElement.cpp b/src/AST/ASTElement.cpp new file mode 100644 index 0000000..a2987fd --- /dev/null +++ b/src/AST/ASTElement.cpp @@ -0,0 +1,10 @@ +#include "AST/ASTElement.h" +#include "AST/ASTVisitor.h" + +ASTElement::ASTElement() { + // +} + +ASTElement::~ASTElement() { + // +} diff --git a/src/AST/ASTVisitor.cpp b/src/AST/ASTVisitor.cpp new file mode 100644 index 0000000..347ffc8 --- /dev/null +++ b/src/AST/ASTVisitor.cpp @@ -0,0 +1,10 @@ +#include "AST/ASTVisitor.h" + +ASTVisitor::ASTVisitor() { + // +} + +ASTVisitor::~ASTVisitor() { + // +} + diff --git a/src/Token.cpp b/src/Token.cpp new file mode 100644 index 0000000..2438cd5 --- /dev/null +++ b/src/Token.cpp @@ -0,0 +1,15 @@ +/* The scully programming language. + * + * Copyright (c) Peter Dahlberg, Markus Hauschild and Florian Sattler, 2013. + * Licensed under the GNU GPL v2. + */ + +#include "Token.h" + +Token::Token(std::string text) : text_(text) { + // +} + +std::string Token::getText() { + return text_; +} diff --git a/src/test.cpp b/src/test.cpp index 0dac0d0..9f3a759 100644 --- a/src/test.cpp +++ b/src/test.cpp @@ -4,6 +4,8 @@ * Licensed under the GNU GPL v2. */ +#include "Token.h" + // this file is auto generated from grammar/grammar.y #include "grammar.h" @@ -59,6 +61,8 @@ int main() { std::cout << "The scully programming languae v0.1" << std::endl; + void* parser = scullyParserAlloc(malloc); + while (true) { std::cout << "> "; std::string input; @@ -77,8 +81,11 @@ int main() { std::string s(results.start, results.end); if (results.id != T_WHITESPACE) { std::cout << "Id: " << results.id << ", Token: " << s << std::endl; + scullyParser(parser, results.id, new Token(s)); } } while (results.id != 0); } + + scullyParserFree(parser, free); return 0; }