I currently started working on a project in Cpp and using Antlr to parse input files.I just want to access all parsed fields all at the same place.
I will like to do something like this - build a parse tree and traverse that tree to get my identifiers values.
I tried doing this:CompilerParser parser(&tokens);CompilerParser::ProgramContext *nf = parser.program();cout << nf->IDENT()->getText() << endl; //Like I can access one identifier here.But I can not do this for something like this:expression : item #atom| op expression #single;CompilerParser::ExpressionContext *expr = parser.expression(); //I can not do this because I can't parse twice as this gives seg fault.
CompilerParser::AtomContext atom(expr); //I thought this will work to construct AtomContext class and access it members but is doesn't work.
Also, I can't find how to build parsetree from parser.
Hi Harsha,I currently started working on a project in Cpp and using Antlr to parse input files.I just want to access all parsed fields all at the same place.I will like to do something like this - build a parse tree and traverse that tree to get my identifiers values.Not sure I understand it correctly: traversing the tree means *not* to access all fields in the same place. You are walking the tree and take the values from the individual sub contexts.I tried doing this:CompilerParser parser(&tokens);CompilerParser::ProgramContext *nf = parser.program();cout << nf->IDENT()->getText() << endl; //Like I can access one identifier here.But I can not do this for something like this:expression : item #atom| op expression #single;CompilerParser::ExpressionContext *expr = parser.expression(); //I can not do this because I can't parse twice as this gives seg fault.What do you mean by "parse twice"? Do you trigger 2 parse runs and if so why? Also, it would be interesting to see what causes the seg fault.
CompilerParser::AtomContext atom(expr); //I thought this will work to construct AtomContext class and access it members but is doesn't work.You are not supposed to create the contexts yourself. They are created while building the parse tree.Also, I can't find how to build parsetree from parser.You neither build the parse tree yourself. It is created automatically by the parser (if enabled).
OK. I understand this.But my doubt is :I have this input:rule ALLOW = sip:192.168.120.1;my grammar for parsing this rule is :declare_entry :type IDENT (LB granu=expression RB)? (ASSIGN value=expression)? SEMICOLON;expression : item #atom| op expression #single;item: function_call #func| flow_or_rule_entry #rule_flow| constant;flow_or_rule_entry: fields COLON constant;In my visitor file:antlrcpp::Any visitDeclare_entry func, I can access rule name through Declare_entryContext->IDENT()->getText(), but how do I access its subcontext field values , for e.g. the constant field of flow_or_rule_entryContext.
The reason why I want to access the subcontext fields in base context is that if I want to add this symbol in my symboltable, I want its name as well as its constant value.
I was earlier trying to access all these subcontexts in my main file, but couldn't get it working as I mentioned in previous mail. But, even if I try to access these fields in each subcontexts's visitor function, I need to access fields from a child subcontexts in parent subcontext. Is there a way to do so from antlrcpp::Any object returned from visitor functions ?