سؤال

I am wondering how shall I get the line number of a token inside a grammar. Suppose I have the following grammar:

S : expr MINUS expr { $$ = $1 -$3; }
  ;

How to get the line number for MINUS token? I am not using the lexer inside jison but rather pass it from a lex file by overriding the lexer:

parser.lexer = {
  lex: function() {
    var token = "MINUS";
    parser.lexer.yytext = "...";
    parser.lexer.yylineno = xx;
    return token;
  }
}

I realized that I could call yylineno but what if different tokens in a grammar have different line numbers and I only want line number of a specific token in the jison file. Maybe something like $1.yylineno? Thank you very much!

هل كانت مفيدة؟

المحلول

Jison gives you access to locations using the @ notation. See the "Tracking Locations" section here. The line number of your minus symbol above would be @2.first_line.

Then it is up to your lexer to provide the information that Jison expects.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top