Skip to content

Commit f4d8cca

Browse files
committed
update the parser_cli
1 parent f10d924 commit f4d8cca

File tree

4 files changed

+545
-0
lines changed

4 files changed

+545
-0
lines changed

parser/parser/include/parser.hpp

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/**
2+
* @file parser.hpp
3+
* @brief Declares the Parser class for parsing TINY language tokens into a syntax tree.
4+
*
5+
* The Parser reads a sequence of tokens and constructs an Abstract Syntax Tree (AST)
6+
* according to the grammar rules of the TINY language.
7+
*/
8+
9+
#ifndef PARSER_HPP
10+
#define PARSER_HPP
11+
12+
#include <vector>
13+
#include <memory>
14+
15+
#include "token.hpp"
16+
#include "syntax_tree.hpp"
17+
18+
namespace TINY::PARSER
19+
{
20+
21+
/**
22+
* @class Parser
23+
* @brief Parses tokens into a syntax tree based on TINY language grammar.
24+
*
25+
* The Parser implements recursive descent parsing methods for each grammar rule.
26+
*/
27+
class Parser
28+
{
29+
public:
30+
/**
31+
* @brief Constructs a Parser with a given list of tokens.
32+
* @param tokens The list of tokens to parse.
33+
*/
34+
Parser(const std::vector<SCANNER::Token> &tokens);
35+
36+
/**
37+
* @brief Parses the tokens and returns the root of the syntax tree.
38+
* @return A unique pointer to the root SyntaxTreeNode.
39+
*/
40+
std::unique_ptr<SyntaxTreeNode> parse();
41+
42+
private:
43+
const std::vector<SCANNER::Token> &tokens; /**< The list of tokens to parse. */
44+
size_t currentTokenIndex; /**< Index of the current token being processed. */
45+
46+
// Parsing methods for grammar rules
47+
std::unique_ptr<SyntaxTreeNode> parseProgram();
48+
std::unique_ptr<SyntaxTreeNode> parseStatementSequence();
49+
std::unique_ptr<SyntaxTreeNode> parseStatement();
50+
std::unique_ptr<SyntaxTreeNode> parseIfStatement();
51+
std::unique_ptr<SyntaxTreeNode> parseRepeatStatement();
52+
std::unique_ptr<SyntaxTreeNode> parseAssignStatement();
53+
std::unique_ptr<SyntaxTreeNode> parseReadStatement();
54+
std::unique_ptr<SyntaxTreeNode> parseWriteStatement();
55+
std::unique_ptr<SyntaxTreeNode> parseExpression();
56+
std::unique_ptr<SyntaxTreeNode> parseSimpleExpression();
57+
std::unique_ptr<SyntaxTreeNode> parseTerm();
58+
std::unique_ptr<SyntaxTreeNode> parseFactor();
59+
60+
// Utility methods
61+
const SCANNER::Token &peekToken() const;
62+
const SCANNER::Token &getToken();
63+
bool matchToken(SCANNER::TokenType type);
64+
void expectToken(SCANNER::TokenType type);
65+
66+
// Error handling
67+
void error(const std::string &message);
68+
};
69+
70+
} // namespace TINY::PARSER
71+
72+
#endif // PARSER_HPP

parser/parser/include/syntax_tree.hpp

Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
/**
2+
* @file syntax_tree.hpp
3+
* @brief Defines the classes for syntax tree nodes used by the TINY language parser.
4+
*
5+
* The syntax tree represents the hierarchical structure of the parsed program.
6+
*/
7+
8+
#ifndef SYNTAX_TREE_HPP
9+
#define SYNTAX_TREE_HPP
10+
11+
#include <memory>
12+
#include <vector>
13+
#include <string>
14+
15+
namespace TINY::PARSER
16+
{
17+
18+
/**
19+
* @class SyntaxTreeNode
20+
* @brief Base class for all syntax tree nodes.
21+
*/
22+
class SyntaxTreeNode
23+
{
24+
public:
25+
virtual ~SyntaxTreeNode() = default;
26+
27+
/**
28+
* @brief Converts the syntax tree node to a string representation.
29+
* @param indentation The indentation level for pretty-printing.
30+
* @return A string representing the node.
31+
*/
32+
virtual std::string toString(int indentation = 0) const = 0;
33+
};
34+
35+
/**
36+
* @class StatementSequenceNode
37+
* @brief Represents a sequence of statements.
38+
*/
39+
class StatementSequenceNode : public SyntaxTreeNode
40+
{
41+
public:
42+
void addStatement(std::unique_ptr<SyntaxTreeNode> statement);
43+
std::string toString(int indentation = 0) const override;
44+
45+
private:
46+
std::vector<std::unique_ptr<SyntaxTreeNode>> statements;
47+
};
48+
49+
/**
50+
* @class IfStatementNode
51+
* @brief Represents an if-then-else statement.
52+
*/
53+
class IfStatementNode : public SyntaxTreeNode
54+
{
55+
public:
56+
IfStatementNode(std::unique_ptr<SyntaxTreeNode> condition,
57+
std::unique_ptr<SyntaxTreeNode> thenPart,
58+
std::unique_ptr<SyntaxTreeNode> elsePart);
59+
60+
std::string toString(int indentation = 0) const override;
61+
62+
private:
63+
std::unique_ptr<SyntaxTreeNode> condition;
64+
std::unique_ptr<SyntaxTreeNode> thenPart;
65+
std::unique_ptr<SyntaxTreeNode> elsePart;
66+
};
67+
68+
/**
69+
* @class RepeatStatementNode
70+
* @brief Represents a repeat-until loop.
71+
*/
72+
class RepeatStatementNode : public SyntaxTreeNode
73+
{
74+
public:
75+
RepeatStatementNode(std::unique_ptr<SyntaxTreeNode> body,
76+
std::unique_ptr<SyntaxTreeNode> condition);
77+
78+
std::string toString(int indentation = 0) const override;
79+
80+
private:
81+
std::unique_ptr<SyntaxTreeNode> body;
82+
std::unique_ptr<SyntaxTreeNode> condition;
83+
};
84+
85+
/**
86+
* @class AssignStatementNode
87+
* @brief Represents an assignment statement.
88+
*/
89+
class AssignStatementNode : public SyntaxTreeNode
90+
{
91+
public:
92+
AssignStatementNode(const std::string &variableName,
93+
std::unique_ptr<SyntaxTreeNode> expression);
94+
95+
std::string toString(int indentation = 0) const override;
96+
97+
private:
98+
std::string variableName;
99+
std::unique_ptr<SyntaxTreeNode> expression;
100+
};
101+
102+
/**
103+
* @class ReadStatementNode
104+
* @brief Represents a read statement.
105+
*/
106+
class ReadStatementNode : public SyntaxTreeNode
107+
{
108+
public:
109+
explicit ReadStatementNode(const std::string &variableName);
110+
111+
std::string toString(int indentation = 0) const override;
112+
113+
private:
114+
std::string variableName;
115+
};
116+
117+
/**
118+
* @class WriteStatementNode
119+
* @brief Represents a write statement.
120+
*/
121+
class WriteStatementNode : public SyntaxTreeNode
122+
{
123+
public:
124+
explicit WriteStatementNode(std::unique_ptr<SyntaxTreeNode> expression);
125+
126+
std::string toString(int indentation = 0) const override;
127+
128+
private:
129+
std::unique_ptr<SyntaxTreeNode> expression;
130+
};
131+
132+
/**
133+
* @class OpExpressionNode
134+
* @brief Represents an expression with an operator.
135+
*/
136+
class OpExpressionNode : public SyntaxTreeNode
137+
{
138+
public:
139+
OpExpressionNode(const std::string &op,
140+
std::unique_ptr<SyntaxTreeNode> left,
141+
std::unique_ptr<SyntaxTreeNode> right);
142+
143+
std::string toString(int indentation = 0) const override;
144+
145+
private:
146+
std::string op;
147+
std::unique_ptr<SyntaxTreeNode> left;
148+
std::unique_ptr<SyntaxTreeNode> right;
149+
};
150+
151+
/**
152+
* @class ConstExpressionNode
153+
* @brief Represents a constant (number) in an expression.
154+
*/
155+
class ConstExpressionNode : public SyntaxTreeNode
156+
{
157+
public:
158+
explicit ConstExpressionNode(const std::string &value);
159+
160+
std::string toString(int indentation = 0) const override;
161+
162+
private:
163+
std::string value;
164+
};
165+
166+
/**
167+
* @class VariableExpressionNode
168+
* @brief Represents a variable in an expression.
169+
*/
170+
class VariableExpressionNode : public SyntaxTreeNode
171+
{
172+
public:
173+
explicit VariableExpressionNode(const std::string &name);
174+
175+
std::string toString(int indentation = 0) const override;
176+
177+
private:
178+
std::string name;
179+
};
180+
181+
} // namespace TINY::PARSER
182+
183+
#endif // SYNTAX_TREE_HPP

0 commit comments

Comments
 (0)