PLAYPEN Chapter 3: GrammarTree

GrammarTree is a superclass that allows for derived classes to interface with the bison parser and grammar engine. Classes derived from GrammarTree create the constructor signature needed for their specific class type, and are able to use the code generation method of the base class polymorphically.

Here is the base class: GrammarTree.

GrammarTree.hpp
[code line=”1″ lang=”cpp” collapse=”false”] 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;
[/code]

There is a simple pattern to adding to the grammar tree.

Select a rule from the PlayPen grammar.

Input:

For it’s action generate a new pointer to a GrammarTree object. Choose the subclass matching the rule in this case, GrammarTreeInput.

The GrammarTreeInput rule consists of two components: function_definition and function_definitions. This means that the GrammarTreeInput must have two parameters to process the rule in its action when this pattern is detected.

The rule now looks like this:

input:
  function_definition function_definitions	{ $$ = new GrammarTreeInput($1, $2); }

Next make a corresponding class in the GrammarTree.hpp file:

	class GrammarTreeInput : public GrammarTree
	{
	 	public:
		GrammarTreeInput(GrammarTree* leaf1, GrammarTree* leaf2)
		{
			leaves.push_back(std::unique_ptr<grammartree>(leaf1));
			leaves.push_back(std::unique_ptr<grammartree>(leaf2));
		}
	}

Then move to the next rules.

Integer and String:

In the case of a rule like integer or string, you must use the value in yytext and convert it before placing it in the GrammarTree object and primitive variables must exist in the class to store them.

Integer:
  LONG			{ $$ = new GrammarTreeInteger(std::strtol(yytext, NULL, 10)); }
String:
  STRING		{ $$ = new GrammarTreeString(std::string(yytext)); }

Which requires the following constructors:

GrammarTreeInteger(long leaf)
{
	intVal = leaf;
}

GrammarTreeString(std::string leaf)
{
	stringVal = leaf;
}

The final results should look like this:

GrammarTree.h and grammar.y
[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]