1

I wanna print out the value of the tokens called in parser (returned by the scanner) however it shows null for each.

For example when the input file is

tmp := X*X;

This grammar should print "tmp assigned" instead of "null assigned":

assignment      ::= IDENTIFIER:i ASSIGNMENT math_expression SEMI
          {: System.out.println(i + " assigned"); :}; // null assigned

Is there any other way to get the actual value (other than the written code :D) or shall I do any further step beyond this to get to them?

Parts of code that may be needed:

lexer.jflex

public Symbol symbol(String name, int code){
    return symbolFactory.newSymbol(name, code,
                    new Location(yyline+1,yycolumn+1, yychar), 
                    new Location(yyline+1,yycolumn+yylength(), yychar+yylength())
            );
}
public Symbol symbol(String name, int code, String lexem){
    return symbolFactory.newSymbol(name, code, 
                    new Location(yyline+1, yycolumn +1, yychar), 
                    new Location(yyline+1,yycolumn+yylength(), yychar+yylength()), lexem);
}

The way I have returned it in YYINITIAL state:

{identifier}        {return symbolFactory.newSymbol("IDENTIFIER", IDENTIFIER);}

parser.cup

terminal String     IDENTIFIER;
  • Could `Symbol`'s `toString()` method have returned `null`? I think that's the only reasonable explanation here, – Kevin Anderson Jul 03 '19 at 21:19
  • Actually there _is_ another reasonable explanation: the symbol's `toString()` could have returned not `null` itself but the string `"null"` (probably as a result of an internal field of the `Symbol` being `null`). – Kevin Anderson Jul 03 '19 at 23:23

0 Answers0