parsing - How can I execute an ANTLR parser action for each item in a rule that can match more than one item? -


I am trying to write an ANLR parser rule that matches a list of things, and I write parser action

  • > This is the result of an evaluator in which three MyIdentEvaluator objects - A1, A2 , And a list for each of the A3's.

    Here's a snippet of my grammar:

      returns my_list [IEvaluator e]: {$ e = New MyListEvaluator (); } '$' From LPAREN = my_ident + {/ * wants to do something here for each 'my_ident' * / / * The following sees only 'A3' my_ident * / $ e.Add ($ op.e); } RPAREN; My_ident [IEvaluator E] Returns: IDent {$ e = New Menteent Evil ($ IDENT.text); };  

    I think that my_ident is correctly defined, because I have given these three MyIdentEvaluators to my input string Expected to be prepared, but only after my_ident is added to the list (A3 in my example input).

    How can I treat each of these elements independently, either by changing the grammar or by changing the action through the parser?

    It also happened to me that my terminology for these concepts is not what it should be, so if it seems that I am misusing any word, then maybe I am.


    In response to Wayne's comment, edit:

    I tried to use op + = my_ident + . In that case, my action becomes $ op a IList (in C #), in which Antial. Runtime. Tree.CommonTree are examples, it gives me an entry for the token matched in $ op , so I look at my three matches, but I have MyIdentEvaluator are not the examples I want. I was hoping that I can find a rule feature in the ANLL documents, which can help, but I did not feel anything to get rid of the iliis .


    Result ...

    Based on Clodada's answer, I ended up with that which works well:

      My_list returns [iiVillulator A]: {$ e = new MyListEvaluator (); } '$' LPAREN (op = my_ident {$ e.Add ($ op.e);}) + RPAREN;  

    The Add method for each match of my_ident is called.

    If I were writing this, I would split personalized matches into a list pattern:

      my_list [IEvaluator e] returns: {$ e = new MyListEvaluator (); } '$' From LPAREN = my_ident {$ e.Add ($ op.e)); } (OpNext = my_ident {$ e.Add ($ opNext.e);}) * RPAREN; My_ident [IEvaluator E] Returns: IDent {$ e = New Menteent Evil ($ IDENT.text); };  

    Here we use the code of + in exchange for the use of anil. If we match the first item and add it to the list, Match and store them.


  • Comments

    Popular posts from this blog

    sql - dynamically varied number of conditions in the 'where' statement using LINQ -

    asp.net mvc - Dynamically Generated Ajax.BeginForm -

    Debug on symbian -