How can I modify the text of tokens in a CommonTokenStream with ANTLR?

前端 未结 4 1817
梦如初夏
梦如初夏 2021-02-09 13:38

I\'m trying to learn ANTLR and at the same time use it for a current project.

I\'ve gotten to the point where I can run the lexer on a chunk of code and output it to a C

4条回答
  •  北海茫月
    2021-02-09 14:03

    In ANTLR 4 there is a new facility using parse tree listeners and TokenStreamRewriter (note the name difference) that can be used to observe or transform trees. (The replies suggesting TokenRewriteStream apply to ANTLR 3 and will not work with ANTLR 4.)

    In ANTL4 an XXXBaseListener class is generated for you with callbacks for entering and exiting each non-terminal node in the grammar (e.g. enterClassDeclaration() ).

    You can use the Listener in two ways:

    1) As an observer - By simply overriding the methods to produce arbitrary output related to the input text - e.g. override enterClassDeclaration() and output a line for each class declared in your program.

    2) As a transformer using TokenRewriteStream to modify the original text as it passes through. To do this you use the rewriter to make modifications (add, delete, replace) tokens in the callback methods and you use the rewriter and the end to output the modified text.

    See the following examples from the ANTL4 book for an example of how to do transformations:

    https://github.com/mquinn/ANTLR4/blob/master/book_code/tour/InsertSerialIDListener.java

    and

    https://github.com/mquinn/ANTLR4/blob/master/book_code/tour/InsertSerialID.java

提交回复
热议问题