I got this rule in parser.mly:
intervalue:
| c = CST(* True False 1 7 89 "sfr" *)
{ Ecst c }
| id = ident (* a-z [a-z]* *)
{ Eident id }
| iv = LSQ l = separated_list(TWOPoints, intervalue) RSQ /* [1..4]*/
{ Elist l }
;
I need to pass to list "l" the values of [start .. end]. Example ([1..4]). I search in manual and separated_list(TWOPoints, intervalue) only get values 1 and 4. But i need all values between 1 and 4 including, something like this [1..2..3..4], but without having to do it exhaustively.
separated_listdoes not reflect your desired syntax, as far as I can see. But then, neither does usingintervaluefor the limits of the interval.separated_listis not correct because it is used for an list of any positive number of elements separated by a delimiter. In particular,separated_list(TWOPoints, intervalue)will not just match1..4, but also1, and1..4..7, among other things. Those other things include nestedintervalues, such as2..[4..7], which seems unlikely to be a desired construct (although since I don't know what your language looks like, perhaps it is).You seem to be using
separated_listin the mistaken belief that it is the only way to turn the reduction into an OCanl list. That's not true, since you have the full power of OCaml available to you; you could write that production asOr even
although that won't work for all possible
CSTtokens (such as[1 .. "a"]). And, furthermore, it doesn't permit the use of non-constant limits, such as[1 .. limit].But mixing syntax with run-time semantics like that is almost certainly not what you want. How would you deal with program text like the example above (
[1 .. limit]), wherelimitis a variable which will be assigned a value during execution of the program? (Or even many values, as the program executes in a loop.) The parser should limit itself to producing a useful representation of the program to execute, and the most likely production rule will be something like this (whereValueneeds to be defined according to the actually desired syntax):+