From dcf7320c0d0fd603aa7d068588c460a0832f521c Mon Sep 17 00:00:00 2001 From: Benjamin Loison Date: Sat, 10 Dec 2022 01:58:09 +0100 Subject: [PATCH] Add one-line comment support and make some semi-column optional Make possible the parsing of the counting example of *Clock-directed Modular Code Generation for Synchronous Data-flow Languages* (https://www.di.ens.fr/~pouzet/bib/lctes08a.pdf). ``` -- count the number of top between two tick node counting (tick:bool; top:bool) returns (o: int) var v: int; let o = if tick then v else 0 -> pre o + v; v = if top then 1 else 0; tel; ``` The one-line comment rule was inspired from https://mukulrathi.com/create-your-own-programming-language/parsing-ocamllex-menhir/. Note their typo using `single_line_comment` instead of `read_single_line_comment`. --- src/lexer.mll | 5 +++++ src/parser.mly | 9 +++++++-- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/src/lexer.mll b/src/lexer.mll index a2c7d7d..3a4175c 100644 --- a/src/lexer.mll +++ b/src/lexer.mll @@ -62,6 +62,11 @@ rule token = parse | '/' { BO_div } | '%' { BO_mod } | "->" { BO_arrow } + | "--" { read_single_line_comment lexbuf } | eof { EOF } | _ { raise (Lexing_error (Format.sprintf "Error when seeing %s" (lexeme lexbuf)))} +and read_single_line_comment = parse + | '\n' { token lexbuf } + | eof { EOF } + | _ { read_single_line_comment lexbuf } diff --git a/src/parser.mly b/src/parser.mly index e6ac41a..8dba723 100644 --- a/src/parser.mly +++ b/src/parser.mly @@ -187,9 +187,9 @@ node: node_content: IDENT LPAREN in_params RPAREN - RETURNS LPAREN out_params RPAREN SEMICOL + RETURNS LPAREN out_params RPAREN OPTIONAL_SEMICOL local_params - LET equations TEL + LET equations TEL OPTIONAL_SEMICOL { let node_name = $1 in let (t_in, e_in) = $3 in let (t_out, e_out) = $7 in @@ -202,6 +202,11 @@ node_content: n_type = FTArr (t_in, t_out); } in Hashtbl.add defined_nodes node_name n; n }; +OPTIONAL_SEMICOL: + | /* empty */ {} + | SEMICOL {} +; + in_params: | /* empty */ { (FTList [], []) } | param_list { $1 }