Thursday 15 July 2010

Line Number Information in BYacc/J

In order to know until which line parsing has been done, we can utilize %line directive in JFlex specification and add relevant methods.

%byaccj
%line
%column

%{
  private Parser yyparser;

  public Yylex (java.io.Reader r, Parser yyparser) {
    this (r);
    this.yyparser = yyparser;
  }

  int getLineNum () {
    return (yyline+1);
  }

  int getColNum () {
    return (yycolumn+1);
  }
%}

"yyline+1" expression is needed because JFlex starts counting from 0.

To get the position information in BYacc/J, add several lines in the specification:

private Yylex lexer;

public void yyerror (String strError) {
  System.err.println ("Error at line: " + lexer.getLineNum() + ": " + strError);
}

2 comments:

dloven said...

hello,,blog walking..btw scrip itu untuk di blog ya mas??
salam.

Anung said...

BYacc/J bukan script untuk blog.