简体   繁体   中英

Get column number information of multiple IDs in same line in antlr4 python target

I am writing a parser in python with antlr4.

In short, Input line is:

concept foo bar

The grammar rule which parse above input line is:

start_rule: 'concept' identifier
identifier: ID {ID}

To get column number and line number of all IDs in input line I am adding code in enteridentifier(self, ctx) function.

enteridentifier(self, ctx):
    context = ctx.start
    line_number = context.line
    column_number = conext.column

The above snippet of code returns column number of first ID ie foo. If multiple IDs present in same line number (belongs to same rule) ie foo bar then how can I get column number for both IDs?

On enter you will not have the entire (sub) parse tree ready for evaluation. Instead use the exit variant or (better yet) do a post-parse stage (often called the semantic phase) to extract this kind of information.

Once you have a complete (sub) tree, you can access the ID member in the identifier context, which is an array, if more than one occurences of a specific rule or token can appear. Iterate over that to get the individual child elements.

To get the column information for an ID use the fact that the for lexer tokens the getChild() call returns a TerminalNode instance (you have to cast the result to that). From there call getSymbol() which gives you a Token instance, which in turn has all the info for a specific token like text, channel, column, line, type etc.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM