Upper recursive analysis

I need to enable the parseS, parseL and parseE functions. These functions will be taken as the already designated "program", presented in the form of a list of tokens.

type token = TK_IF | TK_THEN | TK_ELSE | TK_BEGIN | TK_END | TK_PRINT | TK_SEMIC | TK_ID of string;; let program = [ TK_IF; TK_ID("a"); TK_THEN; TK_BEGIN; TK_PRINT; TK_ID("b"); TK_SEMIC; TK_PRINT; TK_ID("c"); TK_END; TK_ELSE; TK_PRINT; TK_ID("d")];; 

and output something like this:

 parseS program;; IF2(ID "a",STATLIST(PRINT (ID "b"),LISTCONT(PRINT (ID "c"),END)),PRINT (ID "d")) 

ParseL should look something like this:

 let rec parseL = function | TK_END::xs -> (xs,END) | TK_SEMIC::xs -> let (rest,stat) = parseS xs let (rest,lst) = parseL rest (rest, LISTCONT(stat,lst)) | l -> failwith ("error parsing L (expecting TK_END or TK_SEMIC):" + (List.map debug l).ToString()) 

reference

+5
source share

Source: https://habr.com/ru/post/1245489/


All Articles