Add parser.h functions

This commit is contained in:
augustin64 2023-11-10 16:56:50 +01:00
parent 87f5ba4100
commit 97f82b7307

View File

@ -7,9 +7,9 @@
using namespace std; using namespace std;
/** Grammar: /** Grammar:
Prog -> Instruction; Prog | \epsilon Prog -> Instruction Prog | Instruction
Instruction -> Statement | ExprStatement | Expr Instruction -> Statement | ExprStatement; | Expr; | ;
Statement -> // Rien pour l'instant, mais "for", "if" etc Statement -> // Rien pour l'instant, mais "for", "if" etc
ExprStatement -> ExprStatement ->
@ -23,10 +23,10 @@ Expr ->
| T - Expr | T - Expr
T -> T ->
| F | U
| F * T | U * T
| F / T | U / T
| F % T | U % T
U -> U ->
| F | F
@ -40,8 +40,13 @@ F ->
| Identifier = Expr // Assignment | Identifier = Expr // Assignment
*/ */
/**
* Type de Noeuds
*/
enum class NodeType { enum class NodeType {
Prog, // -> Instruction; Prog /* On ne créé pas de nouveau noeud -> ; Prog */
Prog, // -> Instruction Prog
Epsilon, // -> ;
AssignedDeclaration, // -> Type Identifier = Expr AssignedDeclaration, // -> Type Identifier = Expr
Declaration, // -> Type Identifier Declaration, // -> Type Identifier
Plus, // -> T + Expr Plus, // -> T + Expr
@ -57,8 +62,15 @@ enum class NodeType {
struct InnerNode; struct InnerNode;
/**
* InnerNode: noeud interne
* Token: feuille
*/
using Node = variant<InnerNode, Token>; using Node = variant<InnerNode, Token>;
/**
* Noeud interne
*/
struct InnerNode { struct InnerNode {
NodeType type; NodeType type;
vector<Node> children; vector<Node> children;
@ -66,4 +78,58 @@ struct InnerNode {
// A Leaf is always corresponding to a Token // A Leaf is always corresponding to a Token
/**
* Node: AST
* tokens: tokens pas encore parsés
*/
struct ParseReturn {
Node node;
vector<Token> tokens;
};
/**
* Utilisé pour revenir en arrière quand quelque chose n'est pas reconnu
*/
class ParseException : public std::exception {};
/**
* Parse a list of tokens and return the associated AST
*/
Node parse(vector<Token> tokens);
/**
* Parse something derivated from Instruction
*/
ParseReturn parse_instruction(vector<Token> tokens);
/**
* Parse something derivated from Statement
*/
ParseReturn parse_statement(vector<Token> tokens);
/**
* Parse something derivated from ExprStatement
*/
ParseReturn parse_expr_statement(vector<Token> tokens);
/**
* Parse something derivated from Expr
*/
ParseReturn parse_expr(vector<Token> tokens);
/**
* Parse something derivated from T
*/
ParseReturn parse_t(vector<Token> tokens);
/**
* Parse something derivated from U
*/
ParseReturn parse_u(vector<Token> tokens);
/**
* Parse something derivated from F
*/
ParseReturn parse_f(vector<Token> tokens);
#endif #endif