How to know the grammar order with 2 vectors? (writing the parser/translator)

18 views
Skip to first unread message

Bandark

unread,
May 11, 2018, 1:59:18 PM5/11/18
to antlr-discussion

Hello everyone,
it's my first time using antlr4 for a school project, i've written the grammar, and now i'm writing a sort of "parser/translator" in c++
I have this case:
A : ID op (NUMBER | ID);
B: (A|C)((other_op)(A|C))+;
C:'(' (B|A) ')';

Basically antlr4 generate the cpp code that i should use, so translating B i've two vectors: one contains all the A and one all the C.
How do i know the order? I mean, both the vectors are 0-n, i can't understand if the order is A .. A .. C or C .. A .. C.
How do i discorver the order? I read something about tokens but i've no idea how to use them...
I'd like to get the order so i can get the contexts in order, so i know i have to read AContext* - AContext* - CContext*
Any suggestion?
Thanks guys

Mike Lischke

unread,
May 11, 2018, 2:13:33 PM5/11/18
to antlr-discussion
Each parser rule context has a child array. The children appear in the found order in the input, so this should give that information.


Bandark

unread,
May 11, 2018, 2:55:28 PM5/11/18
to antlr-discussion
Thanks Mike, i've seen the child array, but it doesn't seems really useful: i can get the text for each children, but i'd like to know what kind of Context each child is or to get his Context (A or C Context) anyay.
Could you please be more specific about what should i do with the childrens? Because i'd like to not manually parse a string... 

Mike Lischke

unread,
May 11, 2018, 4:34:41 PM5/11/18
to antlr-discussion
Thanks Mike, i've seen the child array, but it doesn't seems really useful: i can get the text for each children, but i'd like to know what kind of Context each child is or to get his Context (A or C Context) anyay.
Could you please be more specific about what should i do with the childrens? Because i'd like to not manually parse a string… 

Each parser rule context comes with a rule index value, which is one of the RULE_xxx values in your generated parser. Use that to determine what kind of context it is, which directly corresponds to invoked rule. This way you can exactly determine which rule comes in which position.


Reply all
Reply to author
Forward
0 new messages