Line Numbers in the Parser
I received a question regarding how one can get line number (and
column) information for tokens in the parser. Note that this is not
required for this phase of the project.
This is not straightforward because the parser extracts the "value"
field from the Symbol class when you label a terminal or non-terminal
in the RHS of a production. The other fields of ComplexSymbol are not
available in the action code of the parser.
The JFlex manual gives a solution to this problem. I find it a bit
distasteful but it works. I will detail it below:
In addition to the value field, the parser also extracts the left and
right fields from the Symbol in the RHS of a production. For example
if you have:
id ::= IDENTIFIER:Val
You can use Valleft and Valright in the action code. For example:
id ::= IDENTIFIER:Val {: System.out.println(Valleft + " " + Valright); :} ;
The default constructor for ComplexSymbol does not set the left and
right fields of its superclass, Symbol. We can use left and right to
store the line and column of the token. Our symbol(...) factory
method in the scanner can explicitly set symbol.left and symbol.right
(assuming symbol is the newly created ComplexSymbol is named symbol).
The implementation should be straightforward.
After the line number is available, we can use it while error reporting.
(This is distasteful because this is not the intended use for the left
and right fields.)
column) information for tokens in the parser. Note that this is not
required for this phase of the project.
This is not straightforward because the parser extracts the "value"
field from the Symbol class when you label a terminal or non-terminal
in the RHS of a production. The other fields of ComplexSymbol are not
available in the action code of the parser.
The JFlex manual gives a solution to this problem. I find it a bit
distasteful but it works. I will detail it below:
In addition to the value field, the parser also extracts the left and
right fields from the Symbol in the RHS of a production. For example
if you have:
id ::= IDENTIFIER:Val
You can use Valleft and Valright in the action code. For example:
id ::= IDENTIFIER:Val {: System.out.println(Valleft + " " + Valright); :} ;
The default constructor for ComplexSymbol does not set the left and
right fields of its superclass, Symbol. We can use left and right to
store the line and column of the token. Our symbol(...) factory
method in the scanner can explicitly set symbol.left and symbol.right
(assuming symbol is the newly created ComplexSymbol is named symbol).
The implementation should be straightforward.
After the line number is available, we can use it while error reporting.
(This is distasteful because this is not the intended use for the left
and right fields.)

0 Comments:
Post a Comment
<< Home