4

I am using ocamlyacc for a small parser which also performs some semantic actions on most parsing rules.

I have defined a set of tokens in the beginning:

%token T_plus
%token T_minus
%token <int> T_int_const

%left T_plus T_minus

A parser rule which performs a semantic action is the following:

exp:  exp T_plus exp
      {
          checkType T_plus $1 $3
      }

where checkType is an external helper function. However, I'm getting this strange warning (which refers to a line in my Parser.mly file)

 warning: T_plus was selected from type Parser.token. 
 It is not visible in the current scope, 
 and will not be selected if the type becomes unknown.

I haven't found any relevant info in the ocamlyacc manual. Has anyone encountered a similar error? Why is the token not visible inside the scope of the semantic action?

VHarisop
  • 2,816
  • 1
  • 14
  • 28
  • Everytime that I saw this error, the developer missed an ``Open``. – alifirat Jul 29 '15 at 11:41
  • @alifirat the warning refers to `T_plus` *inside* Parser.mly and more specifically inside the semantic action's scope. I suspect that is has to do with `T_plus` being a constant constructor but I haven't encountered anything similar in the past. – VHarisop Jul 29 '15 at 11:43
  • Is it possible for you to post all the code ? – alifirat Jul 29 '15 at 12:19
  • Do you have any other `T_plus` definition that in the `%token` line ? – PatJ Jul 29 '15 at 12:23
  • @PatJ no, apart from the precedence annotation (see last edit). – VHarisop Jul 29 '15 at 12:25

1 Answers1

2

It is not possible to guess what goes wrong on your side, since you're not disclosing enough information. I can guess, that you somehow misread the error message, and the problem is in another file. For example, the following file:

%{
 let f PLUS _ =  ()
%}

%token PLUS
%left PLUS

%start exp
%type <unit> exp

%%

exp : exp PLUS exp {f PLUS $1}

compiles any problems or warnings with

ocamlbuild Parser.byte

I can only suggest, to look at the generated Parser.ml and see what's is happening there.

In general, this message means, that you're referring to a constructor, that was not brought to the scope. In Parser.mly tokens are always in the scope, so you can't see this error in that file. Usually, you may do this in your lexer. So make sure, that you have open Parser in the intro section of your lexer.

ivg
  • 34,431
  • 2
  • 35
  • 63