Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found

Target

Select target project
No results found
Show changes
Commits on Source (2)
Showing
with 1291 additions and 397 deletions
...@@ -28,13 +28,57 @@ SyntaxTree* Compiler::extract(SyntaxTree* ptr, int index, std::string warning) ...@@ -28,13 +28,57 @@ SyntaxTree* Compiler::extract(SyntaxTree* ptr, int index, std::string warning)
return ptr->children[index]; return ptr->children[index];
} }
SyntaxTree* Compiler::getElement(SyntaxTree* ptr, Type from, Type get)
{
std::string warning = (std::string)getTypeName(get) + " from " + (std::string)getTypeName(from);
if (!nullCheck(ptr, warning))
return NULL;
if (ptr->type != from)
{
std::cout << "Warnig: Node type is " << getTypeName(ptr->type) << ", and should be " << getTypeName(from) << '\n';
return NULL;
}
for (int i = 0; i < ptr->children_num; i++)
if (ptr->children[i]->type == get)
return ptr->children[i];
std::cout << "Warnig: Not found " << getTypeName(get) << " in " << getTypeName(from) << '\n';
return NULL;
}
SyntaxTree* Compiler::getSingleElement(SyntaxTree* ptr, Type from)
{
std::string warning = "single from " + (std::string)getTypeName(from);
if (!nullCheck(ptr, warning))
return NULL;
if (ptr->type != from)
{
std::cout << "Warnig: Node type is " << getTypeName(ptr->type) << ", and should be " << getTypeName(from) << '\n';
return NULL;
}
if (ptr->children_num != 1)
{
std::cout << "Warnig: While extracting sinle element " << getTypeName(ptr->type) << " has " << getTypeName(from) << " children and should have 1\n";
return NULL;
}
return ptr->children[0];
}
SyntaxTree* Compiler::get_STATE_from_GAME(SyntaxTree* game) SyntaxTree* Compiler::get_STATE_from_GAME(SyntaxTree* game)
{ {
return extract(game, 1, "STATE from GAME"); return extract(game, 1, "STATE from GAME");
} }
SyntaxTree* Compiler::get_DATA_SET_from_STATE(SyntaxTree* state) SyntaxTree* Compiler::get_DATA_SET_from_STATE(SyntaxTree* state)
{ {
return extract(state, 0, "DATA_SET from GAME"); return extract(state, 0, "DATA_SET from STATE");
}
SyntaxTree* Compiler::get_INSTRUCTION_BLOCK_from_STATE(SyntaxTree* state)
{
return extract(state, 1, "INSTRUCTION_BLOCK from STATE");
} }
SyntaxTree* Compiler::get_VAR_LIST_from_DATA_SET(SyntaxTree* data_set) SyntaxTree* Compiler::get_VAR_LIST_from_DATA_SET(SyntaxTree* data_set)
{ {
...@@ -91,29 +135,31 @@ void Compiler::setVariable(DataSet* data_set, std::string type, std::string name ...@@ -91,29 +135,31 @@ void Compiler::setVariable(DataSet* data_set, std::string type, std::string name
Game* Compiler::createGame(SyntaxTree* input_game) Game* Compiler::createGame(SyntaxTree* input_game)
{ {
DataSet* state = createDataSet(get_STATE_from_GAME(input_game)); SyntaxTree* state_st = get_STATE_from_GAME(input_game);
DataSet* state = createDataSet(get_DATA_SET_from_STATE(state_st));
InstructionBlock* IB = createInstructionBlock(get_INSTRUCTION_BLOCK_from_STATE(state_st), state, NULL);
void* ptr = state->getValuePtr("a");
*(int*)ptr = 7;
IB->RunBlock();
state->print(); state->print();
Game* game = new Game(state); Game* game = new Game(state);
return game; return game;
} }
DataSet* Compiler::createDataSet(SyntaxTree* input_state) DataSet* Compiler::createDataSet(SyntaxTree* input_data_set)
{ {
SyntaxTree* data_set_st = get_DATA_SET_from_STATE(input_state); SyntaxTree* var_list = get_VAR_LIST_from_DATA_SET(input_data_set);
SyntaxTree* var_list = get_VAR_LIST_from_DATA_SET(data_set_st);
std::list<SyntaxTree*> declaration_list; std::list<SyntaxTree*> declaration_list;
while (true) while (var_list->children_num == 2)
{ {
declaration_list.push_back(get_VAR_DECLARATION_from_VAR_LIST(var_list)); declaration_list.push_back(get_VAR_DECLARATION_from_VAR_LIST(var_list));
var_list = get_VAR_LIST_from_VAR_LIST(var_list);
if (var_list->children_num == 2)
var_list = get_VAR_LIST_from_VAR_LIST(var_list);
else
break;
} }
int size = 0; int size = 0;
...@@ -142,3 +188,167 @@ DataSet* Compiler::createDataSet(SyntaxTree* input_state) ...@@ -142,3 +188,167 @@ DataSet* Compiler::createDataSet(SyntaxTree* input_state)
return data_set; return data_set;
} }
InstructionBlock* Compiler::createInstructionBlock(SyntaxTree* input_instruction_block, DataSet* state, DataSet* move)
{
SyntaxTree* local_data = getElement(input_instruction_block, Type::instruction_block, Type::data_set);
SyntaxTree* instruction_list = extract(input_instruction_block, 1, "INSTRUCTION_LIST from INSTRUCTION_BLOCK"); // REFACTOR
//getElement(input_instruction_block, Type::instruction_block, Type::instruction_list);
// Create local DataSet
DataSet* local = createDataSet(local_data);
// Create Instruction graph
Instruction* entry = NULL;
Instruction* previous_instr = NULL;
while (instruction_list->type == Type::instruction_list)
{
SyntaxTree* instruction_st = getElement(instruction_list, Type::instruction_list, Type::instruction);
instruction_list = extract(instruction_list, 1, "INSTRUCTION_LIST from INSTRUCTION_LIST"); // REFACTOR
//getElement(instruction_list, Type::instruction_list, Type::instruction_list);
Instruction* next_instr = createInstruction(instruction_st, local, state, move);
if (entry == NULL)
{
entry = next_instr;
previous_instr = next_instr;
}
else
{
previous_instr->setNext(next_instr);
previous_instr = next_instr;
}
}
return new InstructionBlock(entry);
}
Instruction* Compiler::createInstruction(SyntaxTree* input_instruction, DataSet* local, DataSet* state, DataSet* move)
{
SyntaxTree* typed_instruction = getSingleElement(input_instruction, Type::instruction);
switch (typed_instruction->type)
{
case Type::assign_instr: return createAssignInstruction(typed_instruction, local, state, move);
case Type::return_instr: break;
default: break;
}
return nullptr;
}
Instruction* Compiler::createAssignInstruction(SyntaxTree* input_instruction, DataSet* local, DataSet* state, DataSet* move)
{
SyntaxTree* variable_reference_st = getElement(input_instruction, Type::assign_instr, Type::var_reference);
SyntaxTree* expression_st = extract(input_instruction, 1, "EXPR from ASSIGN_INSTR"); // REFACTOR
SyntaxTree* scope_st = extract(variable_reference_st, 0, "SCOPE from VARIABLE_REFERENCE"); // REFACTOR
SyntaxTree* identifier_st = extract(variable_reference_st, 1, "IDENTIFIER from VARIABLE_REFERENCE"); // REFACTOR
DataSet* data_set_to = getScope(scope_st->type, local, state, move);
if (!data_set_to) return NULL;
void* to = data_set_to->getValuePtr(identifier_st->text);
VAR_TYPE type_to = data_set_to->getValueType(identifier_st->text);
if (!to) std::cout << "Warnig: Not found identifier \"" << identifier_st->text << "\" in scope: " << getTypeName(scope_st->type) << '\n';
switch (type_to)
{
case VAR_TYPE::INT:
{
ExpressionInt* expr = createIntExpression(expression_st, local, state, move);
return new InstructionAssignInt(to, expr);
}
case VAR_TYPE::BOOL: {
std::cout << "Warnig: Bool value type not implemented\n";
return NULL;
}
default: {
std::cout << "Warnig: Unknown value type\n";
return NULL;
}
}
return nullptr;
}
ExpressionInt* Compiler::createIntExpression(SyntaxTree* input_expression, DataSet* local, DataSet* state, DataSet* move)
{
switch (input_expression->type)
{
case Type::expr:
{
SyntaxTree* next_expr = extract(input_expression, 0, "EXPR from EXPR"); // REFACTOR
return createIntExpression(next_expr, local, state, move);
}
case Type::expr_ref: // REFACTOR
{
SyntaxTree* variable_reference_st = extract(input_expression, 0, "VARIABLE_REFERENCE from EXPR"); // REFACTOR
SyntaxTree* scope_st = extract(variable_reference_st, 0, "SCOPE from VARIABLE_REFERENCE"); // REFACTOR
SyntaxTree* identifier_st = extract(variable_reference_st, 1, "IDENTIFIER from VARIABLE_REFERENCE"); // REFACTOR
DataSet* data_set_from = getScope(scope_st->type, local, state, move);
if (!data_set_from) return NULL;
void* from = data_set_from->getValuePtr(identifier_st->text);
VAR_TYPE type_from = data_set_from->getValueType(identifier_st->text);
if (!from) std::cout << "Warnig: Not found identifier \"" << identifier_st->text << "\" in scope: " << getTypeName(scope_st->type) << '\n';
if (type_from != VAR_TYPE::INT) {
std::cout << "Warnig: Incorrect variable type, should be INT\n";
return NULL;
}
return new ExpressionInt_Id((int*)from);
}
case Type::expr_literal:
{
SyntaxTree* var_def = getElement(input_expression, Type::expr_literal, Type::var_definition);
std::string string_val = extract(var_def, 0, "type from VAR_DECLARATION")->text;
return new ExpressionInt_Value(stoi(string_val));
}
case Type::expr_add:
case Type::expr_sub:
case Type::expr_mul:
case Type::expr_div:
case Type::expr_mod:
{
SyntaxTree* arg_a_st = extract(input_expression, 0, "EXPR A from EXPR");
SyntaxTree* arg_b_st = extract(input_expression, 1, "EXPR B from EXPR");
ExpressionInt* a = createIntExpression(arg_a_st, local, state, move);
ExpressionInt* b = createIntExpression(arg_b_st, local, state, move);
switch (input_expression->type)
{
case Type::expr_add: return new ExpressionInt_Add(a, b);
case Type::expr_sub: return new ExpressionInt_Sub(a, b);
case Type::expr_mul: return new ExpressionInt_Mul(a, b);
case Type::expr_div: return new ExpressionInt_Div(a, b);
case Type::expr_mod: return new ExpressionInt_Mod(a, b);
}
}
default: break;
}
return nullptr;
}
DataSet* Compiler::getScope(Type type, DataSet* local, DataSet* state, DataSet* move)
{
switch (type)
{
case Type::local_scope: return local;
case Type::state_scope: return state;
case Type::move_scope: return move;
default: {
std::cout << "Warnig: Unknown scope: " << (std::string)getTypeName(type) << '\n';
return NULL;
}
}
}
#pragma once #pragma once
#include <list> #include <list>
#include <string>
#include "../Grammar/Common.h" #include "../Grammar/Common.h"
#include "Game.h" #include "Game.h"
...@@ -12,10 +13,21 @@ private: ...@@ -12,10 +13,21 @@ private:
// Extrctors // Extrctors
SyntaxTree* extract(SyntaxTree* ptr, int index, std::string warning); SyntaxTree* extract(SyntaxTree* ptr, int index, std::string warning);
SyntaxTree* getElement(SyntaxTree* ptr, Type from, Type get);
SyntaxTree* getSingleElement(SyntaxTree* ptr, Type from);
// Extract GAME
SyntaxTree* get_STATE_from_GAME(SyntaxTree* game); SyntaxTree* get_STATE_from_GAME(SyntaxTree* game);
// Extract STATE
SyntaxTree* get_DATA_SET_from_STATE(SyntaxTree* state); SyntaxTree* get_DATA_SET_from_STATE(SyntaxTree* state);
SyntaxTree* get_INSTRUCTION_BLOCK_from_STATE(SyntaxTree* state);
// Extract DATA_SET
SyntaxTree* get_VAR_LIST_from_DATA_SET(SyntaxTree* data_set); SyntaxTree* get_VAR_LIST_from_DATA_SET(SyntaxTree* data_set);
// Extract VAR_LIST
SyntaxTree* get_VAR_DECLARATION_from_VAR_LIST(SyntaxTree* var_list); SyntaxTree* get_VAR_DECLARATION_from_VAR_LIST(SyntaxTree* var_list);
SyntaxTree* get_VAR_LIST_from_VAR_LIST(SyntaxTree* var_list); SyntaxTree* get_VAR_LIST_from_VAR_LIST(SyntaxTree* var_list);
...@@ -24,13 +36,40 @@ private: ...@@ -24,13 +36,40 @@ private:
std::string get_identifier_from_VAR_DECLARATION(SyntaxTree* var_declaration); std::string get_identifier_from_VAR_DECLARATION(SyntaxTree* var_declaration);
std::string get_value_from_VAR_DECLARATION(SyntaxTree* var_declaration); std::string get_value_from_VAR_DECLARATION(SyntaxTree* var_declaration);
// Extract INSTRUCTION_BLOCK
SyntaxTree* get_DATA_SET_from_INSTRUCTION_BLOCK(SyntaxTree* state);
SyntaxTree* get_INSTRUCTION_LIST_from_INSTRUCTION_BLOCK(SyntaxTree* state);
// Extract INSTRUCTION_LIST
SyntaxTree* get_INSTRUCTION_LIST_from_INSTRUCTION_LIST(SyntaxTree* state);
SyntaxTree* get_INSTRUCTION_from_INSTRUCTION_LIST(SyntaxTree* state);
// Extract INSTRUCTION
SyntaxTree* get_INSTRUCTION_type_from_INSTRUCTION(SyntaxTree* state);
// Extract ASSIGN_INSTR
SyntaxTree* get_VAR_REFERENCE_from_ASSIGN_INSTR(SyntaxTree* state);
SyntaxTree* get_EXPR_from_ASSIGN_INSTR(SyntaxTree* state);
// Extract VAR_REFERENCE
SyntaxTree* get_SCOPE_from_VAR_REFERENCE(SyntaxTree* state);
SyntaxTree* get_IDENTIFIER_from_VAR_REFERENCE(SyntaxTree* state);
// Extract EXPR
SyntaxTree* get_VAR_REFERENCE_from_EXPR(SyntaxTree* state);
SyntaxTree* get_VAR_DEFINITION_from_EXPR(SyntaxTree* state);
SyntaxTree* get_EXPR_from_EXPR(SyntaxTree* state);
// Data functions // Data functions
void setVariable(DataSet* data_set, std::string type, std::string name, std::string value); void setVariable(DataSet* data_set, std::string type, std::string name, std::string value);
// Main components // Main components //
// Takes STATE part of syntax tree as an input // Takes STATE part of syntax tree as an input
// Structure of STATE is: // Structure of STATE is:
...@@ -39,7 +78,16 @@ private: ...@@ -39,7 +78,16 @@ private:
// | // |
// -> VAR_DECLARATION // -> VAR_DECLARATION
// VAR_LIST // VAR_LIST
DataSet* createDataSet(SyntaxTree* input_state); DataSet* createDataSet(SyntaxTree* input_data_set);
// Instructions //
InstructionBlock* createInstructionBlock(SyntaxTree* input_instruction_block, DataSet* state, DataSet* move);
Instruction* createInstruction(SyntaxTree* input_instruction, DataSet* local, DataSet* state, DataSet* move);
Instruction* createAssignInstruction(SyntaxTree* input_instruction, DataSet* local, DataSet* state, DataSet* move);
ExpressionInt* createIntExpression(SyntaxTree* input_expression, DataSet* local, DataSet* state, DataSet* move);
DataSet* getScope(Type type, DataSet* local, DataSet* state, DataSet* move);
public: public:
// Takes as an input all syntax tree // Takes as an input all syntax tree
......
...@@ -48,6 +48,17 @@ void* DataSet::getValuePtr(std::string name) ...@@ -48,6 +48,17 @@ void* DataSet::getValuePtr(std::string name)
return (char*)data + data_index; return (char*)data + data_index;
} }
VAR_TYPE DataSet::getValueType(std::string name)
{
if (!exist(name))
{
std::cout << "not found\n";
return VAR_TYPE::UNKNOWN;
}
return map.at(name).type;
}
void DataSet::print() void DataSet::print()
{ {
for (std::pair<const std::string, variable>& kv : map) for (std::pair<const std::string, variable>& kv : map)
......
...@@ -44,6 +44,7 @@ public: ...@@ -44,6 +44,7 @@ public:
bool defineVariable(std::string name, VAR_TYPE type, int size); bool defineVariable(std::string name, VAR_TYPE type, int size);
void* getValuePtr(std::string name); void* getValuePtr(std::string name);
VAR_TYPE getValueType(std::string name);
void print(); void print();
......
#pragma once #pragma once
#include "DataSet/DataSet.h" #include "DataSet/DataSet.h"
#include "Instructions/InstructionBlock.h"
class Game class Game
{ {
......
#include "ExpressionInt.h"
// Expression constructors
ExpressionInt_Id::ExpressionInt_Id(int* ptr)
{
value_ptr = ptr;
}
ExpressionInt_Value::ExpressionInt_Value(int val)
{
value = val;
}
ExpressionInt_TwoArgumentsOperation::ExpressionInt_TwoArgumentsOperation(ExpressionInt* a, ExpressionInt* b)
{
expr_a = a;
expr_b = b;
}
// Evaluators
int ExpressionInt::evaluate()
{
return 0;
}
int ExpressionInt_Id::evaluate()
{
return *value_ptr;
}
int ExpressionInt_Value::evaluate()
{
return value;
}
int ExpressionInt_Add::evaluate()
{
return expr_a->evaluate() + expr_b->evaluate();
}
int ExpressionInt_Sub::evaluate()
{
return expr_a->evaluate() - expr_b->evaluate();
}
int ExpressionInt_Mul::evaluate()
{
return expr_a->evaluate() * expr_b->evaluate();
}
int ExpressionInt_Div::evaluate()
{
return expr_a->evaluate() / expr_b->evaluate();
}
int ExpressionInt_Mod::evaluate()
{
return expr_a->evaluate() % expr_b->evaluate();
}
#pragma once
class ExpressionInt
{
public:
virtual int evaluate();
};
// Subclasses for Int Expression
// Expression represents a refference
// to DataSet by identifier
class ExpressionInt_Id : public ExpressionInt
{
private:
int* value_ptr;
public:
ExpressionInt_Id(int* ptr);
int evaluate() override;
};
// Expression represents a
// constant value
class ExpressionInt_Value : public ExpressionInt
{
private:
int value;
public:
ExpressionInt_Value(int val);
int evaluate() override;
};
// Class agregates two argument operations
class ExpressionInt_TwoArgumentsOperation : public ExpressionInt
{
protected:
ExpressionInt* expr_a;
ExpressionInt* expr_b;
public:
ExpressionInt_TwoArgumentsOperation(ExpressionInt* a, ExpressionInt* b);
};
// Expression represents
// sum
// of two expressions
class ExpressionInt_Add : public ExpressionInt_TwoArgumentsOperation
{
public:
ExpressionInt_Add(ExpressionInt* a, ExpressionInt* b) : ExpressionInt_TwoArgumentsOperation(a, b) {}
int evaluate() override;
};
// Expression represents
// difference
// of two expressions
class ExpressionInt_Sub : public ExpressionInt_TwoArgumentsOperation
{
public:
ExpressionInt_Sub(ExpressionInt* a, ExpressionInt* b) : ExpressionInt_TwoArgumentsOperation(a, b) {}
int evaluate() override;
};
// Expression represents
// multiplication
// of two expressions
class ExpressionInt_Mul : public ExpressionInt_TwoArgumentsOperation
{
public:
ExpressionInt_Mul (ExpressionInt* a, ExpressionInt* b) : ExpressionInt_TwoArgumentsOperation(a, b) {}
int evaluate() override;
};
// Expression represents
// division
// of two expressions
class ExpressionInt_Div : public ExpressionInt_TwoArgumentsOperation
{
public:
ExpressionInt_Div(ExpressionInt* a, ExpressionInt* b) : ExpressionInt_TwoArgumentsOperation(a, b) {}
int evaluate() override;
};
// Expression represents
// mod
// of two expressions
class ExpressionInt_Mod : public ExpressionInt_TwoArgumentsOperation
{
public:
ExpressionInt_Mod(ExpressionInt* a, ExpressionInt* b) : ExpressionInt_TwoArgumentsOperation(a, b) {}
int evaluate() override;
};
#include "Instruction.h"
Instruction::Instruction()
//:next(NULL)
{}
Instruction::Instruction(Instruction* next)
: next(next)
{}
void Instruction::setNext(Instruction* next)
{
this->next = next;
}
Instruction* Instruction::Run()
{
return next;
}
#pragma once
#include <iostream>
#include <string>
class Instruction
{
protected:
Instruction* next;
public:
Instruction();
Instruction(Instruction* next);
void setNext(Instruction* next);
virtual Instruction* Run();
};
#include "InstructionAssign.h"
InstructionAssign::InstructionAssign(void* to)
: to(to)
{}
InstructionAssignInt::InstructionAssignInt(void* to, ExpressionInt* expr)
: InstructionAssign(to), expression(expr)
{}
Instruction* InstructionAssignInt::Run()
{
*(int*)to = expression->evaluate();
return next;
}
#pragma once
#include "Instruction.h"
#include "Expression/ExpressionInt.h"
class InstructionAssign : public Instruction
{
protected:
void* to;
InstructionAssign(void* to);
};
class InstructionAssignInt : public InstructionAssign
{
private:
ExpressionInt* expression;
public:
InstructionAssignInt(void* to, ExpressionInt* expr);
Instruction* Run() override;
};
#include "InstructionBlock.h"
InstructionBlock::InstructionBlock(Instruction* entry)
: entry_point(entry)
{}
void InstructionBlock::RunBlock()
{
Instruction* instr = entry_point;
while (instr)
instr = instr->Run();
// TODO: get return value
}
#pragma once
#include <iostream>
#include "Instruction/Instruction.h"
#include "Instruction/InstructionAssign.h"
#include "../DataSet/DataSet.h"
class InstructionBlock
{
private:
//DataSet local;
//DataSet state;
Instruction* entry_point;
public:
InstructionBlock(Instruction* entry);
void RunBlock();
};
#include "Common.h" #include "Common.h"
void SyntaxTree_print(SyntaxTree* st, int depth) const char* getTypeName(Type type)
{ {
const char* symbols[] = { switch (type)
"kw_main_rule", "tmp_state", "tmp_moves", {
"kw_players", "tmp_players_list", case kw_main_rule: return "kw_main_rule";
"kw_state", "kw_int", "kw_bool", " kw_return", case tmp_state: return "tmp_state";
"identifier", "integer", "boolean", "syntax_char", case tmp_moves: return "tmp_moves";
"tmp", case kw_players: return "kw_players";
case tmp_players_list: return "tmp_players_list";
case kw_state: return "kw_state";
case kw_int: return "kw_int";
case kw_bool: return "kw_bool";
case kw_return: return "kw_return";
case identifier: return "identifier";
case integer: return "integer";
case boolean: return "boolean";
case syntax_char: return "syntax_char";
case tmp: return "tmp";
case game: return "game";
case main_rule: return "main_rule";
case players: return "players";
case players_list: return "players_list";
case player: return "player";
case state: return "state";
case moves: return "moves";
case data_set: return "data_set";
case var_list: return "var_list";
case var_list_tail: return "var_list_tail";
case var_declaration: return "var_declaration";
case var_type: return "var_type";
case var_definition: return "var_definition";
case instruction_block: return "instruction_block";
case instruction_list: return "instruction_list";
case instruction_list_tail: return "instruction_list_tail";
case instruction: return "instruction";
case assign_instr: return "assign_instr";
case return_instr: return "return_instr";
case expr: return "expr";
case expr_ref: return "expr_ref";
case expr_literal: return "expr_literal";
case expr_add: return "expr_add";
case expr_sub: return "expr_sub";
case expr_mul: return "expr_mul";
case expr_div: return "expr_div";
case expr_mod: return "expr_mod";
case expr_equal: return "expr_equal";
case expr_not_equal: return "expr_not_equal";
case expr_less_equal: return "expr_less_equal";
case expr_greater_equal: return "expr_greater_equal";
case expr_greater: return "expr_greater";
case expr_less: return "expr_less";
case expr_and: return "expr_and";
case expr_or: return "expr_or";
case var_reference: return "var_reference";
case local_scope: return "local_scope";
case state_scope: return "state_scope";
case move_scope: return "move_scope";
case m_rule_list: return "m_rule_list";
case m_rule: return "m_rule";
case payoff_list: return "payoff_list";
case payoff: return "payoff";
case move_list: return "move_list";
case move: return "move";
case players_scope: return "players_scope";
case identifier_list: return "identifier_list";
"game", "main_rule", "players", "players_list", "player", "state", "moves", default: return "Name not assigned";
"data_set", "var_list", "var_declaration", "var_type", "var_definition", }
"instruction_block", "instruction_list", }
"instruction", "assign_instr", "return_instr",
"expr", "expr_literal", "expr_add", "expr_sub", "expr_mul", "expr_div", "expr_mod",
"m_rule_list", "m_rule",
"payoff_list", "payoff",
"move_list", "move", "players_scope",
"identifier_list" };
void SyntaxTree_print(SyntaxTree* st, int depth)
{
for (int i = 0; i < depth; i++) printf(" "); for (int i = 0; i < depth; i++) printf(" ");
printf("%s: ", symbols[st->type]); printf("%s: ", getTypeName(st->type));
if (st->children_num == 0) if (st->children_num == 0)
{ {
......
...@@ -16,14 +16,16 @@ typedef enum Type ...@@ -16,14 +16,16 @@ typedef enum Type
game, main_rule, players, players_list, player, state, moves, game, main_rule, players, players_list, player, state, moves,
data_set, var_list, var_declaration, var_type, var_definition, data_set, var_list, var_list_tail, var_declaration, var_type, var_definition,
instruction_block, instruction_list, instruction_block, instruction_list,
instruction, assign_instr, return_instr, instruction, assign_instr, return_instr,
expr, expr_literal, expr_add, expr_sub, expr_mul, expr_div, expr_mod, expr, expr_ref, expr_literal, expr_add, expr_sub, expr_mul, expr_div, expr_mod,
expr_equal, expr_not_equal, expr_less_equal, expr_greater_equal, expr_greater, expr_less, expr_and, expr_or,
var_reference, local_scope, state_scope, move_scope,
m_rule_list, m_rule, m_rule_list, m_rule,
payoff_list, payoff, payoff_list, payoff,
move_list, move, players_scope, move_list, move, players_scope,
identifier_list identifier_list, instruction_list_tail
} Type; } Type;
...@@ -37,5 +39,6 @@ struct SyntaxTree{ ...@@ -37,5 +39,6 @@ struct SyntaxTree{
}; };
void SyntaxTree_print(SyntaxTree* st, int depth); void SyntaxTree_print(SyntaxTree* st, int depth);
const char* getTypeName(Type type);
#endif // ifndef __COMMON_H__ #endif // ifndef __COMMON_H__
\ No newline at end of file
...@@ -286,17 +286,18 @@ static void yy_fatal_error YY_PROTO(( yyconst char msg[] )); ...@@ -286,17 +286,18 @@ static void yy_fatal_error YY_PROTO(( yyconst char msg[] ));
*yy_cp = '\0'; \ *yy_cp = '\0'; \
yy_c_buf_p = yy_cp; yy_c_buf_p = yy_cp;
#define YY_NUM_RULES 15 #define YY_NUM_RULES 21
#define YY_END_OF_BUFFER 16 #define YY_END_OF_BUFFER 22
static yyconst short int yy_accept[67] = static yyconst short int yy_accept[79] =
{ 0, { 0,
0, 0, 16, 15, 14, 13, 8, 12, 12, 12, 0, 0, 22, 21, 20, 21, 19, 21, 14, 19,
12, 12, 12, 12, 12, 12, 12, 8, 12, 12, 19, 19, 18, 18, 18, 18, 18, 18, 18, 18,
12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 18, 18, 21, 9, 12, 14, 10, 8, 11, 18,
5, 12, 12, 12, 12, 12, 11, 12, 12, 6, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18,
12, 12, 12, 12, 12, 12, 12, 9, 12, 4, 13, 18, 5, 18, 18, 18, 18, 18, 17, 18,
12, 12, 2, 12, 12, 12, 7, 12, 12, 1, 18, 6, 18, 18, 18, 18, 18, 18, 18, 15,
12, 12, 12, 3, 10, 0 18, 4, 18, 18, 2, 18, 18, 18, 7, 18,
18, 1, 18, 18, 18, 3, 16, 0
} ; } ;
static yyconst int yy_ec[256] = static yyconst int yy_ec[256] =
...@@ -304,17 +305,17 @@ static yyconst int yy_ec[256] = ...@@ -304,17 +305,17 @@ static yyconst int yy_ec[256] =
1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 2, 1, 1, 1, 1, 3, 1, 1, 3, 1, 2, 3, 1, 4, 4, 4, 5, 1, 4,
3, 3, 3, 3, 3, 1, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 6, 6, 6,
4, 4, 4, 4, 4, 4, 4, 1, 3, 3, 6, 6, 6, 6, 6, 6, 6, 1, 4, 7,
3, 3, 1, 1, 5, 6, 7, 7, 8, 7, 8, 9, 1, 1, 10, 11, 12, 12, 13, 12,
7, 7, 9, 7, 7, 10, 11, 12, 13, 14, 12, 12, 14, 12, 12, 15, 16, 17, 18, 19,
7, 15, 16, 17, 18, 19, 7, 7, 20, 7, 12, 20, 21, 22, 23, 24, 12, 12, 25, 12,
3, 1, 3, 1, 21, 1, 22, 7, 7, 7, 4, 1, 4, 1, 26, 1, 27, 12, 12, 12,
23, 24, 7, 7, 7, 7, 7, 25, 7, 7, 28, 29, 12, 12, 12, 12, 12, 30, 12, 12,
7, 7, 7, 26, 27, 28, 29, 7, 7, 7, 12, 12, 12, 31, 32, 33, 34, 12, 12, 12,
7, 7, 3, 1, 3, 1, 1, 1, 1, 1, 12, 12, 4, 35, 4, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
...@@ -331,65 +332,72 @@ static yyconst int yy_ec[256] = ...@@ -331,65 +332,72 @@ static yyconst int yy_ec[256] =
1, 1, 1, 1, 1 1, 1, 1, 1, 1
} ; } ;
static yyconst int yy_meta[30] = static yyconst int yy_meta[36] =
{ 0, { 0,
1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 2, 1, 1, 1, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 1
} ; } ;
static yyconst short int yy_base[68] = static yyconst short int yy_base[80] =
{ 0, { 0,
0, 0, 80, 81, 81, 81, 75, 0, 65, 65, 0, 0, 92, 93, 93, 83, 93, 85, 83, 80,
25, 66, 67, 57, 62, 50, 45, 66, 0, 56, 79, 78, 0, 67, 67, 26, 68, 69, 59, 64,
51, 58, 47, 60, 47, 58, 48, 36, 31, 49, 52, 47, 42, 93, 93, 70, 93, 93, 93, 0,
0, 46, 49, 36, 37, 37, 32, 25, 28, 0, 57, 52, 59, 48, 61, 48, 59, 49, 37, 32,
29, 33, 40, 32, 38, 29, 21, 0, 28, 0, 93, 50, 0, 47, 50, 37, 38, 38, 33, 26,
27, 29, 0, 23, 21, 21, 0, 31, 25, 0, 29, 0, 30, 34, 41, 33, 39, 30, 22, 0,
17, 25, 24, 0, 0, 81, 29 29, 0, 28, 30, 0, 24, 22, 22, 0, 32,
26, 0, 18, 26, 25, 0, 0, 93, 35
} ; } ;
static yyconst short int yy_def[68] = static yyconst short int yy_def[80] =
{ 0, { 0,
66, 1, 66, 66, 66, 66, 66, 67, 67, 67, 78, 1, 78, 78, 78, 78, 78, 78, 78, 78,
67, 67, 67, 67, 67, 67, 67, 66, 67, 67, 78, 78, 79, 79, 79, 79, 79, 79, 79, 79,
67, 67, 67, 67, 67, 67, 67, 67, 67, 67, 79, 79, 78, 78, 78, 78, 78, 78, 78, 79,
67, 67, 67, 67, 67, 67, 67, 67, 67, 67, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79,
67, 67, 67, 67, 67, 67, 67, 67, 67, 67, 78, 79, 79, 79, 79, 79, 79, 79, 79, 79,
67, 67, 67, 67, 67, 67, 67, 67, 67, 67, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79,
67, 67, 67, 67, 67, 0, 66 79, 79, 79, 79, 79, 79, 79, 79, 79, 79,
79, 79, 79, 79, 79, 79, 79, 0, 78
} ; } ;
static yyconst short int yy_nxt[111] = static yyconst short int yy_nxt[129] =
{ 0, { 0,
4, 5, 6, 7, 8, 9, 8, 8, 10, 8, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,
11, 8, 8, 12, 13, 14, 15, 8, 8, 8, 14, 13, 13, 15, 13, 16, 13, 13, 17, 18,
8, 8, 8, 16, 8, 8, 8, 17, 8, 22, 19, 20, 13, 13, 13, 13, 13, 13, 21, 13,
19, 65, 64, 63, 62, 61, 60, 23, 59, 58, 13, 13, 22, 13, 23, 33, 30, 77, 76, 75,
57, 56, 55, 48, 54, 53, 52, 51, 50, 49, 74, 73, 72, 34, 71, 70, 69, 68, 67, 60,
48, 47, 46, 45, 44, 43, 42, 41, 40, 39, 66, 65, 64, 63, 62, 61, 60, 59, 58, 57,
38, 37, 36, 35, 34, 33, 32, 31, 30, 18, 56, 55, 54, 53, 52, 51, 50, 49, 48, 47,
29, 28, 27, 26, 25, 24, 21, 20, 18, 66, 46, 45, 44, 43, 42, 26, 41, 40, 39, 38,
3, 66, 66, 66, 66, 66, 66, 66, 66, 66, 37, 36, 35, 32, 31, 29, 28, 27, 26, 25,
66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 24, 78, 3, 78, 78, 78, 78, 78, 78, 78,
66, 66, 66, 66, 66, 66, 66, 66, 66, 66 78, 78, 78, 78, 78, 78, 78, 78, 78, 78,
78, 78, 78, 78, 78, 78, 78, 78, 78, 78,
78, 78, 78, 78, 78, 78, 78, 78
} ; } ;
static yyconst short int yy_chk[111] = static yyconst short int yy_chk[129] =
{ 0, { 0,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 11, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
67, 63, 62, 61, 59, 58, 56, 11, 55, 54, 1, 1, 1, 1, 1, 16, 79, 75, 74, 73,
52, 51, 49, 47, 46, 45, 44, 43, 42, 41, 71, 70, 68, 16, 67, 66, 64, 63, 61, 59,
39, 38, 37, 36, 35, 34, 33, 32, 30, 29, 58, 57, 56, 55, 54, 53, 51, 50, 49, 48,
28, 27, 26, 25, 24, 23, 22, 21, 20, 18, 47, 46, 45, 44, 42, 40, 39, 38, 37, 36,
17, 16, 15, 14, 13, 12, 10, 9, 7, 3, 35, 34, 33, 32, 31, 26, 23, 22, 21, 20,
66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 19, 18, 17, 15, 14, 12, 11, 10, 9, 8,
66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 6, 3, 78, 78, 78, 78, 78, 78, 78, 78,
66, 66, 66, 66, 66, 66, 66, 66, 66, 66 78, 78, 78, 78, 78, 78, 78, 78, 78, 78,
78, 78, 78, 78, 78, 78, 78, 78, 78, 78,
78, 78, 78, 78, 78, 78, 78, 78
} ; } ;
static yy_state_type yy_last_accepting_state; static yy_state_type yy_last_accepting_state;
...@@ -416,7 +424,7 @@ char *yytext; ...@@ -416,7 +424,7 @@ char *yytext;
#define fileno _fileno #define fileno _fileno
#line 420 "Lexer.c" #line 428 "Lexer.c"
/* Macros after this point can all be overridden by user definitions in /* Macros after this point can all be overridden by user definitions in
* section 1. * section 1.
...@@ -571,7 +579,7 @@ YY_DECL ...@@ -571,7 +579,7 @@ YY_DECL
#line 575 "Lexer.c" #line 583 "Lexer.c"
if ( yy_init ) if ( yy_init )
{ {
...@@ -622,13 +630,13 @@ yy_match: ...@@ -622,13 +630,13 @@ yy_match:
while ( yy_chk[yy_base[yy_current_state] + yy_c] != yy_current_state ) while ( yy_chk[yy_base[yy_current_state] + yy_c] != yy_current_state )
{ {
yy_current_state = (int) yy_def[yy_current_state]; yy_current_state = (int) yy_def[yy_current_state];
if ( yy_current_state >= 67 ) if ( yy_current_state >= 79 )
yy_c = yy_meta[(unsigned int) yy_c]; yy_c = yy_meta[(unsigned int) yy_c];
} }
yy_current_state = yy_nxt[yy_base[yy_current_state] + (unsigned int) yy_c]; yy_current_state = yy_nxt[yy_base[yy_current_state] + (unsigned int) yy_c];
++yy_cp; ++yy_cp;
} }
while ( yy_base[yy_current_state] != 81 ); while ( yy_base[yy_current_state] != 93 );
yy_find_action: yy_find_action:
yy_act = yy_accept[yy_current_state]; yy_act = yy_accept[yy_current_state];
...@@ -720,68 +728,98 @@ YY_RULE_SETUP ...@@ -720,68 +728,98 @@ YY_RULE_SETUP
case 8: case 8:
YY_RULE_SETUP YY_RULE_SETUP
#line 63 "Lexer.l" #line 63 "Lexer.l"
{ return OPERATOR_EQUAL; }
YY_BREAK
case 9:
YY_RULE_SETUP
#line 65 "Lexer.l"
{ return OPERATOR_NOT_EQUAL; }
YY_BREAK
case 10:
YY_RULE_SETUP
#line 67 "Lexer.l"
{ return OPERATOR_LESS_EQUAL; }
YY_BREAK
case 11:
YY_RULE_SETUP
#line 69 "Lexer.l"
{ return OPERATOR_GREATER_EQUAL; }
YY_BREAK
case 12:
YY_RULE_SETUP
#line 71 "Lexer.l"
{ return OPERATOR_AND; }
YY_BREAK
case 13:
YY_RULE_SETUP
#line 73 "Lexer.l"
{ return OPERATOR_OR; }
YY_BREAK
case 14:
YY_RULE_SETUP
#line 76 "Lexer.l"
{ {
yylval.t = SyntaxTree_init(integer, yytext, 0); yylval.t = SyntaxTree_init(integer, yytext, 0);
return INTEGER; return INTEGER;
} }
YY_BREAK YY_BREAK
case 9: case 15:
YY_RULE_SETUP YY_RULE_SETUP
#line 70 "Lexer.l" #line 83 "Lexer.l"
{ {
yylval.t = SyntaxTree_init(boolean, yytext, 0); yylval.t = SyntaxTree_init(boolean, yytext, 0);
return BOOLEAN; return BOOLEAN;
} }
YY_BREAK YY_BREAK
case 10: case 16:
YY_RULE_SETUP YY_RULE_SETUP
#line 77 "Lexer.l" #line 90 "Lexer.l"
{ {
yylval.t = SyntaxTree_init(state, yytext, 0); yylval.t = SyntaxTree_init(state, yytext, 0);
return TMP_STATE; return TMP_STATE;
} }
YY_BREAK YY_BREAK
case 11: case 17:
YY_RULE_SETUP YY_RULE_SETUP
#line 85 "Lexer.l" #line 98 "Lexer.l"
{ {
yylval.t = SyntaxTree_init(tmp, yytext, 0); yylval.t = SyntaxTree_init(tmp, yytext, 0);
return TMP; return TMP;
} }
YY_BREAK YY_BREAK
case 12: case 18:
YY_RULE_SETUP YY_RULE_SETUP
#line 93 "Lexer.l" #line 106 "Lexer.l"
{ {
yylval.t = SyntaxTree_init(identifier, yytext, 0); yylval.t = SyntaxTree_init(identifier, yytext, 0);
return IDENTIFIER; return IDENTIFIER;
} }
YY_BREAK YY_BREAK
case 13: case 19:
YY_RULE_SETUP YY_RULE_SETUP
#line 100 "Lexer.l" #line 113 "Lexer.l"
{ {
yylval.t = SyntaxTree_init(syntax_char, yytext, 0); yylval.t = SyntaxTree_init(syntax_char, yytext, 0);
return yytext[0]; return yytext[0];
} }
YY_BREAK YY_BREAK
case 14: case 20:
YY_RULE_SETUP YY_RULE_SETUP
#line 107 "Lexer.l" #line 120 "Lexer.l"
{ } { }
YY_BREAK YY_BREAK
case 15: case 21:
YY_RULE_SETUP YY_RULE_SETUP
#line 109 "Lexer.l" #line 122 "Lexer.l"
ECHO; ECHO;
YY_BREAK YY_BREAK
#line 785 "Lexer.c" #line 823 "Lexer.c"
case YY_STATE_EOF(INITIAL): case YY_STATE_EOF(INITIAL):
yyterminate(); yyterminate();
...@@ -1073,7 +1111,7 @@ static yy_state_type yy_get_previous_state() ...@@ -1073,7 +1111,7 @@ static yy_state_type yy_get_previous_state()
while ( yy_chk[yy_base[yy_current_state] + yy_c] != yy_current_state ) while ( yy_chk[yy_base[yy_current_state] + yy_c] != yy_current_state )
{ {
yy_current_state = (int) yy_def[yy_current_state]; yy_current_state = (int) yy_def[yy_current_state];
if ( yy_current_state >= 67 ) if ( yy_current_state >= 79 )
yy_c = yy_meta[(unsigned int) yy_c]; yy_c = yy_meta[(unsigned int) yy_c];
} }
yy_current_state = yy_nxt[yy_base[yy_current_state] + (unsigned int) yy_c]; yy_current_state = yy_nxt[yy_base[yy_current_state] + (unsigned int) yy_c];
...@@ -1108,11 +1146,11 @@ yy_state_type yy_current_state; ...@@ -1108,11 +1146,11 @@ yy_state_type yy_current_state;
while ( yy_chk[yy_base[yy_current_state] + yy_c] != yy_current_state ) while ( yy_chk[yy_base[yy_current_state] + yy_c] != yy_current_state )
{ {
yy_current_state = (int) yy_def[yy_current_state]; yy_current_state = (int) yy_def[yy_current_state];
if ( yy_current_state >= 67 ) if ( yy_current_state >= 79 )
yy_c = yy_meta[(unsigned int) yy_c]; yy_c = yy_meta[(unsigned int) yy_c];
} }
yy_current_state = yy_nxt[yy_base[yy_current_state] + (unsigned int) yy_c]; yy_current_state = yy_nxt[yy_base[yy_current_state] + (unsigned int) yy_c];
yy_is_jam = (yy_current_state == 66); yy_is_jam = (yy_current_state == 78);
return yy_is_jam ? 0 : yy_current_state; return yy_is_jam ? 0 : yy_current_state;
} }
...@@ -1667,5 +1705,5 @@ int main() ...@@ -1667,5 +1705,5 @@ int main()
return 0; return 0;
} }
#endif #endif
#line 109 "Lexer.l" #line 122 "Lexer.l"
...@@ -60,6 +60,19 @@ RETURN { ...@@ -60,6 +60,19 @@ RETURN {
} }
"==" { return OPERATOR_EQUAL; }
"!=" { return OPERATOR_NOT_EQUAL; }
"<=" { return OPERATOR_LESS_EQUAL; }
">=" { return OPERATOR_GREATER_EQUAL; }
"&&" { return OPERATOR_AND; }
"||" { return OPERATOR_OR; }
[0-9]+ { [0-9]+ {
yylval.t = SyntaxTree_init(integer, yytext, 0); yylval.t = SyntaxTree_init(integer, yytext, 0);
...@@ -97,7 +110,7 @@ TMP { ...@@ -97,7 +110,7 @@ TMP {
} }
[\[\]{}<>()+\-*/=,;%] { [\[\]{}<>()+\-*/=,;%$#\.] {
yylval.t = SyntaxTree_init(syntax_char, yytext, 0); yylval.t = SyntaxTree_init(syntax_char, yytext, 0);
return yytext[0]; return yytext[0];
......
This diff is collapsed.
...@@ -50,7 +50,13 @@ ...@@ -50,7 +50,13 @@
IDENTIFIER = 266, IDENTIFIER = 266,
INTEGER = 267, INTEGER = 267,
BOOLEAN = 268, BOOLEAN = 268,
TMP = 269 OPERATOR_EQUAL = 269,
OPERATOR_NOT_EQUAL = 270,
OPERATOR_LESS_EQUAL = 271,
OPERATOR_GREATER_EQUAL = 272,
OPERATOR_AND = 273,
OPERATOR_OR = 274,
TMP = 275
}; };
#endif #endif
...@@ -71,7 +77,7 @@ symval ...@@ -71,7 +77,7 @@ symval
/* Line 1676 of yacc.c */ /* Line 1676 of yacc.c */
#line 75 "Parser.tab.h" #line 81 "Parser.tab.h"
} YYSTYPE; } YYSTYPE;
# define YYSTYPE_IS_TRIVIAL 1 # define YYSTYPE_IS_TRIVIAL 1
# define yystype YYSTYPE /* obsolescent; will be withdrawn */ # define yystype YYSTYPE /* obsolescent; will be withdrawn */
......
...@@ -24,6 +24,7 @@ SyntaxTree* root; ...@@ -24,6 +24,7 @@ SyntaxTree* root;
KW_PLAYERS KW_PLAYERS
KW_STATE KW_INT KW_BOOL KW_RETURN KW_STATE KW_INT KW_BOOL KW_RETURN
IDENTIFIER INTEGER BOOLEAN IDENTIFIER INTEGER BOOLEAN
OPERATOR_EQUAL OPERATOR_NOT_EQUAL OPERATOR_LESS_EQUAL OPERATOR_GREATER_EQUAL OPERATOR_AND OPERATOR_OR
TMP TMP
%type <t> GAME MAIN_RULE PLAYERS STATE MOVES %type <t> GAME MAIN_RULE PLAYERS STATE MOVES
...@@ -31,11 +32,16 @@ SyntaxTree* root; ...@@ -31,11 +32,16 @@ SyntaxTree* root;
DATA_SET VAR_LIST VAR_DECLARATION VAR_TYPE VAR_DEFINITION DATA_SET VAR_LIST VAR_DECLARATION VAR_TYPE VAR_DEFINITION
INSTRUCTION_BLOCK INSTRUCTION_LIST INSTRUCTION_BLOCK INSTRUCTION_LIST
INSTRUCTION ASSIGN_INSTR RETURN_INSTR EXPR INSTRUCTION ASSIGN_INSTR RETURN_INSTR EXPR
VAR_REFERENCE SCOPE
M_RULE_LIST M_RULE M_RULE_LIST M_RULE
PAYOFF_LIST PAYOFF PAYOFF_LIST PAYOFF
MOVE_LIST MOVE PLAYERS_SCOPE MOVE_LIST MOVE PLAYERS_SCOPE
IDENTIFIER_LIST IDENTIFIER_LIST
%left OPERATOR_OR
%left OPERATOR_AND
%left OPERATOR_EQUAL OPERATOR_NOT_EQUAL
%left OPERATOR_GREATER_EQUAL OPERATOR_LESS_EQUAL '<' '>'
%left '+' '-' %left '+' '-'
%left '*' '/' '%' %left '*' '/' '%'
...@@ -64,11 +70,12 @@ PLAYERS: KW_PLAYERS '[' PLAYERS_LIST ']'{ ...@@ -64,11 +70,12 @@ PLAYERS: KW_PLAYERS '[' PLAYERS_LIST ']'{
st->children[0] = $3; st->children[0] = $3;
} }
STATE: KW_STATE DATA_SET { STATE: KW_STATE DATA_SET '(' INSTRUCTION_BLOCK ')' {
SyntaxTree* st = SyntaxTree_init(state, "", 1); SyntaxTree* st = SyntaxTree_init(state, "", 2);
$$ = st; $$ = st;
st->children[0] = $2; st->children[0] = $2;
st->children[1] = $4;
} }
MAIN_RULE: KW_MAIN_RULE '[' M_RULE_LIST ']' { MAIN_RULE: KW_MAIN_RULE '[' M_RULE_LIST ']' {
...@@ -120,11 +127,10 @@ DATA_SET: '[' VAR_LIST ']' { ...@@ -120,11 +127,10 @@ DATA_SET: '[' VAR_LIST ']' {
st->children[0] = $2; st->children[0] = $2;
} }
VAR_LIST: VAR_DECLARATION {
SyntaxTree* st = SyntaxTree_init(var_list, "", 1); VAR_LIST: {
SyntaxTree* st = SyntaxTree_init(var_list_tail, "", 0);
$$ = st; $$ = st;
st->children[0] = $1;
} }
| VAR_DECLARATION VAR_LIST { | VAR_DECLARATION VAR_LIST {
SyntaxTree* st = SyntaxTree_init(var_list, "", 2); SyntaxTree* st = SyntaxTree_init(var_list, "", 2);
...@@ -172,13 +178,7 @@ VAR_DEFINITION: INTEGER { ...@@ -172,13 +178,7 @@ VAR_DEFINITION: INTEGER {
// INSTRUCTIONS // INSTRUCTIONS
INSTRUCTION_BLOCK: '{' INSTRUCTION_LIST '}' { INSTRUCTION_BLOCK: DATA_SET '{' INSTRUCTION_LIST '}' {
SyntaxTree* st = SyntaxTree_init(instruction_block, "", 1);
$$ = st;
st->children[0] = $2;
}
|DATA_SET '{' INSTRUCTION_LIST '}' {
SyntaxTree* st = SyntaxTree_init(instruction_block, "", 2); SyntaxTree* st = SyntaxTree_init(instruction_block, "", 2);
$$ = st; $$ = st;
...@@ -186,11 +186,9 @@ INSTRUCTION_BLOCK: '{' INSTRUCTION_LIST '}' { ...@@ -186,11 +186,9 @@ INSTRUCTION_BLOCK: '{' INSTRUCTION_LIST '}' {
st->children[1] = $3; st->children[1] = $3;
} }
INSTRUCTION_LIST: INSTRUCTION { INSTRUCTION_LIST: {
SyntaxTree* st = SyntaxTree_init(instruction_list, "", 1); SyntaxTree* st = SyntaxTree_init(instruction_list_tail, "", 0);
$$ = st; $$ = st;
st->children[0] = $1;
} }
| INSTRUCTION INSTRUCTION_LIST { | INSTRUCTION INSTRUCTION_LIST {
SyntaxTree* st = SyntaxTree_init(instruction_list, "", 2); SyntaxTree* st = SyntaxTree_init(instruction_list, "", 2);
...@@ -208,13 +206,13 @@ INSTRUCTION: ASSIGN_INSTR { ...@@ -208,13 +206,13 @@ INSTRUCTION: ASSIGN_INSTR {
st->children[0] = $1; st->children[0] = $1;
} }
| RETURN_INSTR { | RETURN_INSTR {
SyntaxTree* st = SyntaxTree_init(return_instr, "", 1); SyntaxTree* st = SyntaxTree_init(instruction, "", 1);
$$ = st; $$ = st;
st->children[0] = $1; st->children[0] = $1;
} }
ASSIGN_INSTR: IDENTIFIER '=' EXPR ';' { ASSIGN_INSTR: VAR_REFERENCE '=' EXPR ';' {
SyntaxTree* st = SyntaxTree_init(assign_instr, "", 2); SyntaxTree* st = SyntaxTree_init(assign_instr, "", 2);
$$ = st; $$ = st;
...@@ -227,15 +225,15 @@ RETURN_INSTR: KW_RETURN ';' { ...@@ -227,15 +225,15 @@ RETURN_INSTR: KW_RETURN ';' {
$$ = st; $$ = st;
} }
| KW_RETURN EXPR ';' { | KW_RETURN EXPR ';' {
SyntaxTree* st = SyntaxTree_init(assign_instr, "", 1); SyntaxTree* st = SyntaxTree_init(return_instr, "", 1);
$$ = st; $$ = st;
st->children[0] = $2; st->children[0] = $2;
} }
EXPR: IDENTIFIER { EXPR: VAR_REFERENCE {
SyntaxTree* st = SyntaxTree_init(expr, "", 1); SyntaxTree* st = SyntaxTree_init(expr_ref, "", 1);
$$ = st; $$ = st;
st->children[0] = $1; st->children[0] = $1;
...@@ -288,6 +286,85 @@ EXPR: IDENTIFIER { ...@@ -288,6 +286,85 @@ EXPR: IDENTIFIER {
st->children[1] = $3; st->children[1] = $3;
} }
| EXPR OPERATOR_EQUAL EXPR{
SyntaxTree* st = SyntaxTree_init(expr_equal, "", 2);
$$ = st;
st->children[0] = $1;
st->children[1] = $3;
}
| EXPR OPERATOR_NOT_EQUAL EXPR{
SyntaxTree* st = SyntaxTree_init(expr_not_equal, "", 2);
$$ = st;
st->children[0] = $1;
st->children[1] = $3;
}
| EXPR OPERATOR_GREATER_EQUAL EXPR{
SyntaxTree* st = SyntaxTree_init(expr_greater_equal, "", 2);
$$ = st;
st->children[0] = $1;
st->children[1] = $3;
}
| EXPR OPERATOR_LESS_EQUAL EXPR{
SyntaxTree* st = SyntaxTree_init(expr_greater_equal, "", 2);
$$ = st;
st->children[0] = $1;
st->children[1] = $3;
}
| EXPR '>' EXPR{
SyntaxTree* st = SyntaxTree_init(expr_greater, "", 2);
$$ = st;
st->children[0] = $1;
st->children[1] = $3;
}
| EXPR '<' EXPR{
SyntaxTree* st = SyntaxTree_init(expr_less, "", 2);
$$ = st;
st->children[0] = $1;
st->children[1] = $3;
}
| EXPR OPERATOR_AND EXPR{
SyntaxTree* st = SyntaxTree_init(expr_and, "", 2);
$$ = st;
st->children[0] = $1;
st->children[1] = $3;
}
| EXPR OPERATOR_OR EXPR{
SyntaxTree* st = SyntaxTree_init(expr_or, "", 2);
$$ = st;
st->children[0] = $1;
st->children[1] = $3;
}
VAR_REFERENCE: SCOPE IDENTIFIER {
SyntaxTree* st = SyntaxTree_init(var_reference, "", 2);
$$ = st;
st->children[0] = $1;
st->children[1] = $2;
}
SCOPE: {
SyntaxTree* st = SyntaxTree_init(local_scope, "", 0);
$$ = st;
}
| '$' '.' {
SyntaxTree* st = SyntaxTree_init(state_scope, "", 0);
$$ = st;
}
| '#' '.' {
SyntaxTree* st = SyntaxTree_init(move_scope, "", 0);
$$ = st;
}
// MAIN_RULE // MAIN_RULE
M_RULE_LIST: M_RULE { M_RULE_LIST: M_RULE {
...@@ -394,22 +471,20 @@ IDENTIFIER_LIST: IDENTIFIER { ...@@ -394,22 +471,20 @@ IDENTIFIER_LIST: IDENTIFIER {
%% %%
SyntaxTree* parser_main(int argc, char *argv[]) SyntaxTree* parser_main(char *file_path)
{ {
FILE *fp = NULL; FILE *fp = NULL;
if (argc == 2)
fopen_s(&fp, file_path, "rb");
if (fp == NULL)
{
perror("Failed to open file");
return NULL;
}
else
{ {
fopen_s(&fp, argv[1], "rb"); yyin = fp;
if (fp == NULL)
{
perror("Failed to open file");
return NULL;
}
else
{
yyin = fp;
}
} }
yyparse(); yyparse();
......