0

When I have an abstract syntax tree in the Java Bison (.y) file with this:

string_op:
  SYMBOL_PLUS { $$ = $1; System.out.println("this should print + here: " + $$);}
  | SYMBOL_DOUBLEEQ
  | SYMBOL_NOTEQ;

I would expect it to print "this should print + here:+" whenever the parser comes across the + token (i.e. SYMBOL_PLUS). However, I always get "this should print + here:null".

I have already used the %type declarations and everything.

%nterm <String> string_op

%type<String> SYMBOL_PLUS
%type<String> RESERVED_BOOL
%type<String> RESERVED_STRING
%type<String> RESERVED_INT
%type<String> IDENTIFIER
%type<String> SYMBOL_DOUBLEEQ
%type<String> SYMBOL_NOTEQ

It still gives the error. There is a massive lack of good, understandable, and clear documentation for java bison so it's really frustrating. Please let me know if you can fix this issue. I've been on it for days.

rici
  • 234,347
  • 28
  • 237
  • 341
izash
  • 51
  • 5
  • I'm not very familiar with Bison's Java support or with JFlex, but it looks like your JFlex rule for `SYMBOL_PLUS` does not set whatever JFlex's equivalent of `yylval` is. – sepp2k Apr 16 '22 at 15:36
  • @sepp2k do you know how i can set that yylval value? I tried the stuff at https://jflex.de/manual.html under "JFlex and BYacc/J" but it doesn't seem to compile – izash Apr 16 '22 at 16:10
  • You need to implement [the lexer's `getLVal` method](https://www.gnu.org/software/bison/manual/bison.html#index-getLVal-on-Lexer), which returns the semantic value of the current token. – rici Apr 16 '22 at 17:45

0 Answers0