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;
/** 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
ExprStatement ->
@ -23,10 +23,10 @@ Expr ->
| T - Expr
T ->
| F
| F * T
| F / T
| F % T
| U
| U * T
| U / T
| U % T
U ->
| F
@ -40,8 +40,13 @@ F ->
| Identifier = Expr // Assignment
*/
/**
* Type de Noeuds
*/
enum class NodeType {
Prog, // -> Instruction; Prog
/* On ne créé pas de nouveau noeud -> ; Prog */
Prog, // -> Instruction Prog
Epsilon, // -> ;
AssignedDeclaration, // -> Type Identifier = Expr
Declaration, // -> Type Identifier
Plus, // -> T + Expr
@ -57,8 +62,15 @@ enum class NodeType {
struct InnerNode;
/**
* InnerNode: noeud interne
* Token: feuille
*/
using Node = variant<InnerNode, Token>;
/**
* Noeud interne
*/
struct InnerNode {
NodeType type;
vector<Node> children;
@ -66,4 +78,58 @@ struct InnerNode {
// 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