Commit e967b58a authored by Armillon Damien's avatar Armillon Damien
Browse files

working gramar

parent 767417d2
......@@ -10,6 +10,7 @@ non-terminals ADD_EXPRS ADD_EXPR
non-terminals MUL_EXPRS MUL_EXPR
non-terminals CMP_EXPRS CMP_EXPR
non-terminals EQ_EXPRS EQ_EXPR
non-terminals BLOC
axiom S
{
......@@ -20,13 +21,53 @@ axiom S
open Batteries
open Utils
(* TODO *)
let resolve_associativity term other =
(* TODO *)
term
List.fold_left (fun acc (tag, tree) -> Node(tag,acc::tree)) term other
}
rules
S -> FUNDEFS SYM_EOF { Node (Tlistglobdef, []) }
S -> FUNDEFS SYM_EOF { Node (Tlistglobdef, $1) }
BLOC -> SYM_LBRACE INSTRS SYM_RBRACE {Node(Tblock, $2)}
FUNDEFS -> {[]}
FUNDEFS -> FUNDEF FUNDEFS { (Node (Tfundef, $1)) :: $2 }
FUNDEF -> SYM_IDENTIFIER SYM_LPARENTHESIS LPARAMS SYM_RPARENTHESIS BLOC { [(Node (Tfunname, [StringLeaf($1)])); (Node(Tfunargs, $3)); $5] }
LPARAMS -> {[]}
LPARAMS -> SYM_IDENTIFIER REST_PARAMS {(Node(Targ, [StringLeaf($1)])) :: $2}
REST_PARAMS -> SYM_COMMA SYM_IDENTIFIER REST_PARAMS {(Node(Targ, [StringLeaf($2)])):: $3}
REST_PARAMS -> {[]}
INSTRS -> {[]}
INSTRS -> INSTR INSTRS {$1::$2}
INSTR -> BLOC {$1}
INSTR -> SYM_IF SYM_LPARENTHESIS EXPR SYM_RPARENTHESIS BLOC ELSE {Node(Tif, $3::$5::$6)}
INSTR -> SYM_WHILE SYM_LPARENTHESIS EXPR SYM_RPARENTHESIS BLOC {Node(Twhile, [$3;$5])}
INSTR -> SYM_RETURN EXPR SYM_SEMICOLON {Node(Treturn, [$2])}
INSTR -> SYM_PRINT SYM_LPARENTHESIS EXPR SYM_RPARENTHESIS SYM_SEMICOLON {Node(Tprint, [$3])}
INSTR -> SYM_IDENTIFIER SYM_ASSIGN EXPR SYM_SEMICOLON {Node(Tassign, [Node(Tassignvar,[StringLeaf($1);$3])])}
ELSE -> SYM_ELSE BLOC {[$2]}
ELSE -> {[]}
EXPR -> EQ_EXPR EQ_EXPRS {resolve_associativity $1 $2}
EQ_EXPRS -> SYM_EQUALITY EQ_EXPR {[(Tceq, [$2])]}
EQ_EXPRS -> SYM_NOTEQ EQ_EXPR {[(Tne, [$2])]}
EQ_EXPRS ->{[]}
EQ_EXPR -> CMP_EXPR CMP_EXPRS {resolve_associativity $1 $2}
CMP_EXPRS -> SYM_LT CMP_EXPR {[(Tclt, [$2])]}
CMP_EXPRS -> SYM_GT CMP_EXPR {[(Tcgt, [$2])]}
CMP_EXPRS -> SYM_LEQ CMP_EXPR {[(Tcle, [$2])]}
CMP_EXPRS -> SYM_GEQ CMP_EXPR {[(Tcge, [$2])]}
CMP_EXPRS -> {[]}
CMP_EXPR -> ADD_EXPR ADD_EXPRS {resolve_associativity $1 $2}
ADD_EXPRS -> SYM_PLUS ADD_EXPR ADD_EXPRS {(Tadd, [$2])::$3}
ADD_EXPRS -> SYM_MINUS ADD_EXPR ADD_EXPRS {(Tsub, [$2])::$3}
ADD_EXPRS -> {[]}
ADD_EXPR -> MUL_EXPR MUL_EXPRS {resolve_associativity $1 $2}
MUL_EXPRS -> SYM_ASTERISK MUL_EXPR MUL_EXPRS {(Tmul, [$2])::$3}
MUL_EXPRS -> SYM_DIV MUL_EXPR MUL_EXPRS {(Tdiv, [$2])::$3}
MUL_EXPRS -> SYM_MOD MUL_EXPR MUL_EXPRS {(Tmod, [$2])::$3}
MUL_EXPRS -> {[]}
MUL_EXPR -> FACTOR {$1}
MUL_EXPR -> SYM_MINUS FACTOR {Node(Tneg,[$2])}
FACTOR -> SYM_INTEGER {Node(Tint,[IntLeaf($1)])}
FACTOR -> SYM_IDENTIFIER {StringLeaf($1)}
FACTOR -> SYM_LPARENTHESIS EXPR SYM_RPARENTHESIS {$2}
......@@ -361,10 +361,8 @@ type lexer_result =
*)
let to_lexer_result (d:dfa) (q: dfa_state) (w: char list): lexer_result =
let result = List.find_opt (fun (state,_) -> Set.equal q state) d.dfa_final in
match result with
| None -> LRerror
| Some (_,f) -> match f (string_of_char_list w) with
let (_,f) = List.find (fun (state,_) -> Set.equal q state) d.dfa_final in
match f (string_of_char_list w) with
| None -> LRskip
| Some token -> LRtoken token
......@@ -373,16 +371,16 @@ let tokenize_one (d : dfa) (w: char list) : lexer_result * char list =
let rec recognize (q: dfa_state) (w: char list)
(current_word: char list) (last_accepted: lexer_result * char list)
: lexer_result * char list =
let last_accepted' = (to_lexer_result d q current_word, w) in
match w with
| [] -> last_accepted
| [] -> last_accepted'
| lettre::r ->
match d.dfa_step q lettre with
| None -> (match (fst last_accepted) with | LRtoken(t) -> (match t with | Symbols.SYM_DIV -> Printf.printf "%s\n\n" (string_of_char_list w); last_accepted | _ -> last_accepted) | _ -> last_accepted)
| None -> last_accepted'
| Some q' -> let new_current_word = current_word @ [lettre] in match to_lexer_result d q' new_current_word with
| LRerror -> recognize q' r (new_current_word) last_accepted
| lr -> recognize q' r (new_current_word) (lr, r)
| lr -> recognize q' r (new_current_word) last_accepted'
in
recognize d.dfa_initial w [] (LRtoken (Symbols.SYM_EOF), w)
recognize d.dfa_initial w [] (LRerror, w)
(* La fonction [tokenize_all d w] répète l'application de [tokenize_one] tant qu'on
n'est pas arrivé à la fin du fichier (token [SYM_EOF]). Encore une fois,
......@@ -526,9 +524,6 @@ let nfa_of_list_regexp l =
let dfa_of_list_regexp l =
let n = nfa_of_list_regexp l in
let oc = open_out "/tmp/nfa.dot" in
nfa_to_dot oc n;
close_out oc;
dfa_of_nfa n
let tokenize_list_regexp l s =
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment