-1

I would like to parse a set of expressions, for instance:X[3], X[-3], XY[-2], X[4]Y[2], etc.

In my parser.mly, index (which is inside []) is defined as follows:

index:
| INTEGER { $1 }
| MINUS INTEGER { 0 - $2 }

The token INTEGER, MINUS etc. are defined in lexer as normal.

I try to parse an example, it fails. However, if I comment | MINUS INTEGER { 0 - $2 }, it works well. So the problem is certainly related to that. To debug, I want to get more information, in other words I want to know what is considered to be MINUS INTEGER. I tried to add print:

index:
| INTEGER { $1 }
| MINUS INTEGER { Printf.printf "%n" $2; 0 - $2 }

But nothing is printed while parsing.

Could anyone tell me how to print information or debug that?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
SoftTimur
  • 5,630
  • 38
  • 140
  • 292
  • Include `--verbose --debug` in the command line of `yacc`. –  Jul 23 '13 at 20:07
  • sorry, I just realized I am using `menhir`... – SoftTimur Jul 23 '13 at 20:12
  • Your question lacks important details. You wrote INTEGER and MINUS are defined "as normal" but we have no idea what you think normal. You wrote "it fails" but you wrote nothing about how it failed. You got exceptions, or program terminates but with strange result? – camlspotter Jul 24 '13 at 01:47

1 Answers1

0

I tried coming up with an example of what you describe and was able to get output of 8 with what I show below. [This example is completely stripped down so that it only works for [1] and [- 1 ], but I believe it's equivalent logically to what you said you did.]

However, I also notice that your example's debug string in your example does not have an explicit flush with %! at the end, so that the debugging output might not be flushed to the terminal until later than you expect.

Here's what I used:

Test.mll:

{
   open Ytest
   open Lexing
}
rule test =
parse
"-" { MINUS }
| "1" { ONE 1 }
| "[" { LB }
| "]" { RB }
| [ ' ' '\t' '\r' '\n'  ] { test lexbuf } 
| eof { EOFTOKEN } 

Ytest.mly:

%{
%}
%token <int> ONE 
%token MINUS LB RB EOFTOKEN
%start item
%type <int> index item
%%
index:
    ONE { 2 }
    | MINUS ONE { Printf.printf "%n" 8; $2 }
item : LB index RB EOFTOKEN { $2 } 

Parse.ml

open Test;;
open Ytest;;
open Lexing;;
let lexbuf = Lexing.from_channel stdin in
ignore (Ytest.item Test.test lexbuf)
sesquized
  • 128
  • 5
  • Thanks for your comment. "your example's debug string in your example does not have a new line at the end" ==> I don't see the `new line at the end` in your code either... – SoftTimur Jul 23 '13 at 21:38
  • 1
    I believe a new line does not force flushing in Printf.printf. Use "%!" for explicit flushing. – camlspotter Jul 24 '13 at 01:44
  • The \n isn't required for the output to be produced. I was suggesting that it would make the output appear when the Printf.printf was executed instead of later. However, I checked and I was wrong. As @camlspotter says, it is %! that would cause the flushing, not \n – sesquized Jul 24 '13 at 02:38