diff --git a/src/include/parser.h b/src/include/parser.h index 8172730..bc37065 100644 --- a/src/include/parser.h +++ b/src/include/parser.h @@ -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; +/** + * Noeud interne +*/ struct InnerNode { NodeType type; vector 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 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 tokens); + +/** + * Parse something derivated from Instruction +*/ +ParseReturn parse_instruction(vector tokens); + +/** + * Parse something derivated from Statement +*/ +ParseReturn parse_statement(vector tokens); + +/** + * Parse something derivated from ExprStatement +*/ +ParseReturn parse_expr_statement(vector tokens); + +/** + * Parse something derivated from Expr +*/ +ParseReturn parse_expr(vector tokens); + +/** + * Parse something derivated from T +*/ +ParseReturn parse_t(vector tokens); + +/** + * Parse something derivated from U +*/ +ParseReturn parse_u(vector tokens); + +/** + * Parse something derivated from F +*/ +ParseReturn parse_f(vector tokens); + #endif \ No newline at end of file