From 4b3b7899f4503b9af1cb955f4c2315fa430d92d5 Mon Sep 17 00:00:00 2001 From: TYoko Date: Fri, 5 Feb 2016 13:17:31 +0900 Subject: [PATCH] Reserved symbols have a higher precedence than predefined tokens as in Haskell version. -- The following file produces different results in Haskell and OCaml versions. {- % bnfc -m -ocaml A.cf % make % echo ":a" | ./TestA [Abstract syntax] MyId (MyIdent ":a") [Linearized tree] :a % bnfc -m -haskell A.cf % make % echo ":a" | ./TestA Parse Successful! [Abstract Syntax] Keyword [Linearized tree] :a -} Keyword. Exp ::= ":a" ; MyId. Exp ::= MyIdent ; token MyIdent (':' letter) --- source/src/BNFC/Backend/OCaml/CFtoOCamlLex.hs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/source/src/BNFC/Backend/OCaml/CFtoOCamlLex.hs b/source/src/BNFC/Backend/OCaml/CFtoOCamlLex.hs index a5bdbcf8..c78eae41 100644 --- a/source/src/BNFC/Backend/OCaml/CFtoOCamlLex.hs +++ b/source/src/BNFC/Backend/OCaml/CFtoOCamlLex.hs @@ -193,16 +193,17 @@ rules cf = mkRule "token" $ ++ [ (mkRegexMultilineComment b e, "token lexbuf") | (b,e) <- multilineC] ++ + -- reserved keywords + [ ( "rsyms" + , "let id = lexeme lexbuf in try Hashtbl.find symbol_table id with Not_found -> failwith (\"internal lexer error: reserved symbol \" ^ id ^ \" not found in hashtable\")" ) + | not (null (cfgSymbols cf))] + ++ -- user tokens [ (text n , tokenAction (text t)) | (n,_,t) <- userTokens cf] ++ -- predefined tokens [ ( "l i*", tokenAction "Ident" ) ] ++ - [ ( "rsyms" - , "let id = lexeme lexbuf in try Hashtbl.find symbol_table id with Not_found -> failwith (\"internal lexer error: reserved symbol \" ^ id ^ \" not found in hashtable\")" ) - | not (null (cfgSymbols cf))] - ++ -- integers [ ( "d+", "let i = lexeme lexbuf in TOK_Integer (int_of_string i)" ) -- doubles