[code title=”GrammarTree.hpp” lang=”cpp” line=”1″ collapse=”false”]
/**
The MIT License (MIT)
Copyright © 2019 John L. Mooney
Permission is hereby granted, free of charge, to any person obtaining a copy of this software
and associated documentation files (the “Software”), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial
portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/
#ifndef _PLAYPEN_GRAMMAR_NODE_
#define _PLAYPEN_GRAMMAR_NODE_
#include
#include
#include
namespace playpen
{
class GrammarTree
{
protected:
long intVal;
double floatVal;
std::string stringVal;
std::vector findChildren(GrammarTree* type);
std::vector> leaves;
public:
GrammarTree(){}
long getInt();
double getFloat();
void toCode(void);
virtual ~GrammarTree() = default;
};
extern std::unique_ptr tree;
class GrammarTreeInput : public GrammarTree
{
public:
GrammarTreeInput(GrammarTree* leaf1, GrammarTree* leaf2)
{
leaves.push_back(std::unique_ptr(leaf1));
leaves.push_back(std::unique_ptr(leaf2));
}
};
class GrammarTreeName : public GrammarTree
{
public:
GrammarTreeName(std::string leaf)
{
stringVal = leaf;
}
};
class GrammarTreeInteger : public GrammarTree
{
public:
GrammarTreeInteger(long leaf)
{
intVal = leaf;
}
};
class GrammarTreeFloat : public GrammarTree
{
public:
GrammarTreeFloat(double leaf)
{
floatVal = leaf;
}
};
class GrammarTreeString : public GrammarTree
{
public:
GrammarTreeString(std::string leaf)
{
stringVal = leaf;
}
};
class GrammarTreeTypeFloat : public GrammarTree
{
public:
GrammarTreeTypeFloat(){}
};
class GrammarTreeTypeInteger : public GrammarTree
{
public:
GrammarTreeTypeInteger(){}
};
class GrammarTreeTypeVoid : public GrammarTree
{
public:
GrammarTreeTypeVoid(){}
};
class GrammarTreeTypeString : public GrammarTree
{
public:
GrammarTreeTypeString(){}
};
class GrammarTreeTypeName: public GrammarTree
{
public:
GrammarTreeTypeName(){}
};
class GrammarTreeVariableDeclaration : public GrammarTree
{
public:
GrammarTreeVariableDeclaration(GrammarTree* leaf1, GrammarTree* leaf2)
{
leaves.push_back(std::unique_ptr(leaf1));
leaves.push_back(std::unique_ptr(leaf2));
}
};
class GrammarTreeVariableDeclarations : public GrammarTree
{
public:
GrammarTreeVariableDeclarations(GrammarTree* leaf1, GrammarTree* leaf2)
{
leaves.push_back(std::unique_ptr(leaf1));
leaves.push_back(std::unique_ptr(leaf2));
}
};
class GrammarTreeStatement : public GrammarTree
{
public:
GrammarTreeStatement(GrammarTree* leaf)
{
leaves.push_back(std::unique_ptr(leaf));
}
};
class GrammarTreeStatements : public GrammarTree
{
public:
GrammarTreeStatements(){}
GrammarTreeStatements(GrammarTree* leaf1, GrammarTree* leaf2)
{
leaves.push_back(std::unique_ptr(leaf1));
leaves.push_back(std::unique_ptr(leaf2));
}
};
class GrammarTreeFunctionPrototype : public GrammarTree
{
public:
GrammarTreeFunctionPrototype(GrammarTree* leaf1, GrammarTree* leaf2, GrammarTree* leav3)
{
leaves.push_back(std::unique_ptr(leaf1));
leaves.push_back(std::unique_ptr(leaf2));
leaves.push_back(std::unique_ptr(leaf3));
}
};
class GrammarTreeFunctionDefinition : public GrammarTree
{
public:
GrammarTreeFunctionDefinition(GrammarTree* leaf1, GrammarTree* leaf2)
{
leaves.push_back(std::unique_ptr(leaf1));
leaves.push_back(std::unique_ptr(leaf2));
}
};
class GrammarTreeFunctionDefinitions : public GrammarTree
{
public:
GrammarTreeFunctionDefinitions(GrammarTree* leaf1, GrammarTree* leaf2)
{
leaves.push_back(std::unique_ptr(leaf1));
leaves.push_back(std::unique_ptr(leaf2));
}
};
class GrammarTreeParams : public GrammarTree
{
public:
GrammarTreeParams(GrammarTree* leaf)
{
leaves.push_back(std::unique_ptr(leaf));
}
GrammarTreeParams(GrammarTree* leaf1, GrammarTree* leaf2)
{
leaves.push_back(std::unique_ptr(leaf1));
leaves.push_back(std::unique_ptr(leaf2));
}
};
class GrammarTreeBlock : public GrammarTree
{
public:
GrammarTreeBlock(GrammarTree* leaf)
{
leaves.push_back(std::unique_ptr(leaf));
}
};
}
#endif
[/code]
[code title=”grammar.y” line=”1″ collapse=”false”]
/**
The MIT License (MIT)
Copyright © 2019 John L. Mooney
Permission is hereby granted, free of charge, to any person obtaining a copy of this software
and associated documentation files (the “Software”), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial
portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/
%code requires {
#include “../includes/GrammarTree.hpp”
}
%{
#include
#include
#include
#include
#include “../includes/GrammarId.hpp”
#include “../includes/GrammarTree.hpp”
extern std::unique_ptr playpen::tree;
int yylex (void);
extern char* yytext;
extern int yylineno;
void yyerror (char const*);
%}
%define api.value.type {playpen::GrammarTree*}
%token COLON SEMICOLON COMMA RIGHTARROW LEFTBRACE RIGHTBRACE EQUAL
LEFTPAREN RIGHTPAREN STRING CHAR INT FLOAT NAME TYPEINT TYPEFLOAT TYPENAME
TYPEVOID TYPESTRING TYPEFUNCTION VOID RETURN
%start input
%%
input:
function_definition function_definitions { playpen::tree.reset(new playpen::GrammarTreeInput($1, $2)); }
function_prototype:
TYPEFUNCTION name COLON
LEFTPAREN params RIGHTPAREN RIGHTARROW vartype { $$ = new playpen::GrammarTreeFunctionPrototype($2, $5, $8); }
function_definition:
function_prototype block { $$ = new playpen::GrammarTreeFunctionDefinition($1, $2); }
function_definitions:
function_definitions function_definition { $$ = new playpen::GrammarTreeFunctionDefinitions($1, $2); }
| %empty { $$ = nullptr; }
statements:
statements statement { $$ = new playpen::GrammarTreeStatements( $1, $2); }
| %empty { $$ = new playpen::GrammarTreeStatements(); }
statement:
vardec SEMICOLON { $$ = new playpen::GrammarTreeStatement($1); }
block:
LEFTBRACE statements RIGHTBRACE { $$ = new playpen::GrammarTreeBlock($2); }
vartype:
typeint { $$ = $1; }
| typefloat { $$ = $1; }
| typevoid { $$ = $1; }
| typename { $$ = $1; }
| typestring { $$ = $1; }
name:
NAME { $$ = new playpen::GrammarTreeName(std::string(yytext)); }
typeint:
TYPEINT { $$ = new playpen::GrammarTreeTypeInteger(); }
typefloat:
TYPEFLOAT { $$ = new playpen::GrammarTreeTypeFloat(); }
typevoid:
TYPEVOID { $$ = new playpen::GrammarTreeTypeVoid(); }
typestring:
TYPESTRING { $$ = new playpen::GrammarTreeTypeString(); }
typename:
TYPENAME { $$ = new playpen::GrammarTreeTypeName(); }
vardec:
vartype name { $$ = new playpen::GrammarTreeVariableDeclaration($1, $2); }
vardecs:
vardec { $$ = $1;}
| vardecs COMMA vardec { $$ = new playpen::GrammarTreeVariableDeclarations($1, $2); }
params:
vardecs { $$ = $1; }
| typevoid { $$ = $1; }
%%
void yyerror(char const* errorCode)
{
printf(“Token: \”%s\” on line: %d\n”, yytext, yylineno);
printf(“Error: %s\n”, errorCode);
//exit(0);
}
[/code]