ocamlyacc

Feed ocamlyacc parser from explicit token list?

主宰稳场 提交于 2020-01-03 16:54:55
问题 Is it possible to feed an OCamlYacc-generated parser an explicit token list for analysis? I'd like to use OCamlLex to explicitly generate a token list which I then analyze using a Yacc-generated parser later. However, the standard use case generates a parser that calls a lexer implicitly for the next token. Here tokens are computed during the yacc analysis rather than before. Conceptually a parser should only work on tokens but a Yacc-generated parser provides an interface that relies on a

Try the first rule if the second rule fails

二次信任 提交于 2019-12-13 06:26:57
问题 I have defined two sets of identifiers IDENTIFIER_ONE and IDENTIFIER_TWO which are both exculsive subsets of IDENTIFIER . I would like to write a parser such that: "i1(arg) EOS" can't be parsed (1) "i2(arg) EOS" can be parsed (2) "i1(arg) = value EOS" can be parsed (3) "i2(arg) = value EOS" can be parsed (4) where i1 (resp., i2 ) belongs to IDENTIFIER_ONE (resp., IDENTIFIER_TWO ); arg and value belong to IDENTIFIER . The following parser.mly has already realized all the points I am after,

Make a table containing tokens visible for both .mly an .mll

纵然是瞬间 提交于 2019-12-11 19:05:57
问题 I would like to define a keyword_table which maps some strings to some tokens, and I would like to make this table visible for both parser.mly and lexer.mll . It seems that the table has to be defined in parser.mly , %{ open Utility (* where hash_table is defined to make a table from a list *) let keyword_table = hash_table [ "Call", CALL; "Case", CASE; "Close", CLOSE; "Const", CONST; "Declare", DECLARE; "DefBool", DEFBOOL; "DefByte", DEFBYTE ] %} However, I could NOT use it in lexer.mll ,

multiple error reporting with menhir: which token?

ぃ、小莉子 提交于 2019-12-10 14:05:21
问题 I am writing a small parser with Menhir + Ocamllex and I have two requirements I cannot seem to meet at the same time I would like to keep parsing after an error (to report more errors). I would like to print the token at which the error ocurred. I can do only 1) easily, by using the error token. I can also do only 2) easily, using the approach suggested for this question. However, I don't know of an easy way to achieve both. The way I handle errors right now goes something like this: pair: |

ocamlyacc parse error: what token?

可紊 提交于 2019-12-09 02:22:12
问题 I'm using ocamlyacc and ocamllex. I have an error production in my grammar that signals a custom exception. So far, I can get it to report the error position: | error { raise (Parse_failure (string_of_position (symbol_start_pos ()))) } But, I also want to know which token was read. There must be a way---anyone know? Thanks. 回答1: Tokens are generated by lexer, hence you can use the current lexer token when error occurs : let parse_buf_exn lexbuf = try T.input T.rule lexbuf with exn -> begin

ocamlyacc parse error: what token?

别来无恙 提交于 2019-12-01 03:05:20
I'm using ocamlyacc and ocamllex. I have an error production in my grammar that signals a custom exception. So far, I can get it to report the error position: | error { raise (Parse_failure (string_of_position (symbol_start_pos ()))) } But, I also want to know which token was read. There must be a way---anyone know? Thanks. Tokens are generated by lexer, hence you can use the current lexer token when error occurs : let parse_buf_exn lexbuf = try T.input T.rule lexbuf with exn -> begin let curr = lexbuf.Lexing.lex_curr_p in let line = curr.Lexing.pos_lnum in let cnum = curr.Lexing.pos_cnum -