Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8765953
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T16:25:27+00:00 2026-06-13T16:25:27+00:00

PARSER GRAMMAR protocol.g grammar protocol; options { language = Java; output = AST; ASTLabelType=CommonTree;

  • 0

PARSER GRAMMAR
protocol.g

grammar protocol; 

options {
  language = Java;
  output = AST;
  ASTLabelType=CommonTree;
}

tokens{ 
TRANSITIONS;
PAIR;
}

@header {
package com.javadude.antlr3.x.tutorial;
}

@lexer::header {
  package com.javadude.antlr3.x.tutorial;
}

parse
 : transitions EOF!
   {
     CommonTree root = $transitions.tree;

     int count = root.getChildCount();

     Tree child1 = root.getChild(0);
     Tree child2 = root.getChild(1);
     Tree child3 = root.getChild(2);
     Tree child4 = root.getChild(3);

     System.out.println("root=" + root.getToken().getText() + " has " + count + " child nodes:");
     System.out.println(" - child1=" + child1.toStringTree());
     System.out.println(" - child2=" + child2.toStringTree());
     System.out.println(" - child3=" + child3.toStringTree());
     System.out.println(" - child4=" + child4.toStringTree());
   }
 ;
transitions
 : 'transitions' '=' INT pair+ ';' -> ^(TRANSITIONS INT pair+)
 ;
pair
 : '(' INT ',' INT ')' -> ^(PAIR INT INT)
 ;

INT 
    : ('0'..'9')+;
WHITESPACE
    : ('\t' | ' ' | '\r' | '\n' | '\u000C')+ {$channel = HIDDEN;};

TREE GRAMMAR
protocolWalker.g

tree grammar protocolWalker;

options {
  language = Java;
  tokenVocab = protocol;   
  ASTLabelType = CommonTree;
}


@header {
package com.javadude.antlr3.x.tutorial;
}

transitions
 : ^(TRANSITIONS INT pair+) 
 {
 System.out.println("transitions=" + $INT.text);
 }
 ;

pair
 : ^(PAIR a=INT b=INT) 
 {
 System.out.println("pair=" + $a.text + ", " + $b.text);

 }
 ;

JAVA TEST RIG
Protocoltest.java

package com.javadude.antlr3.x.tutorial;
import org.antlr.runtime.*;
import org.antlr.runtime.tree.CommonTree;
import org.antlr.runtime.tree.CommonTreeNodeStream;
public class Protocoltest {

    /**
     * @param args
     */
    public static void main(String[] args) throws Exception {
        //create input stream from standard input
        ANTLRInputStream input = new ANTLRInputStream(System.in);
        //create a lexer attached to that input stream
        protocolLexer lexer = new protocolLexer(input);
        //create a stream of tokens pulled from the lexer
        CommonTokenStream tokens = new CommonTokenStream(lexer);

        //create a parser attached to teh token stream
        protocolParser parser = new protocolParser(tokens);
        //invoke the program rule in get return value
        protocolParser.parse_return r =parser.parse();

        CommonTree t = (CommonTree)r.getTree();
        //output the extracted tree to the console
        System.out.println("\nAST is: " + t.toStringTree());

        //walk resulting tree; create treenode stream first
        CommonTreeNodeStream nodes = new CommonTreeNodeStream(t);
        //AST nodes have payloads that point into token stream
        nodes.setTokenStream(tokens);

        //create a tree walker attached to the nodes stream
        protocolWalker walker = new protocolWalker(nodes);

        //invoke the start symbol, rule parse
        walker.transitions();
        }
}

INPUT

transitions = 3(5,0) (5,1) (5,2);

OUTPUT

root=TRANSITIONS has 4 child nodes:
 - child1=3
 - child2=(PAIR 5 0)
 - child3=(PAIR 5 1)
 - child4=(PAIR 5 2)

AST is: (TRANSITIONS 3 (PAIR 5 0) (PAIR 5 1) (PAIR 5 2))
pair=5, 0
pair=5, 1
pair=5, 2
transitions=3  

PROBLEM
You can see above, in the parser grammar (protocol.g) I can store all the children of the transition root as child1, child2, child3 and child4. Also, I have printed these.
In the tree grammar, how can I store these and can perform operations upon these?
Thank you

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T16:25:28+00:00Added an answer on June 13, 2026 at 4:25 pm

    I’ll instantiate java classes (will create java objects) e.g, The very first number in the tree will determine how many objects will be created, then, PAIR 5 0 will create an object with 2 arguments(5,0), PAIR 5 1 will create 2nd object with 2 arguments (5,1) and PAIR 5 2 will create 3rd object with 2 arguments (5,2).

    Here is a simple way to create transitions and add pairs to them, and it requires only a small change to protocolWalker.g. First, though, here are the dummy Transitions and Pair classes that I’ll use:

    Transitions.java

    import java.util.ArrayList;
    
    
    public class Transitions {
        private ArrayList<Pair> pairs = new ArrayList<Pair>();
    
        public void addPair(Pair pair){
            System.out.println(String.format("Added pair %s to transitions", pair));
            pairs.add(pair);
        }
    
        @Override
        public String toString() {
            return "Pairs: " + pairs;
        }
    }
    

    Pair.java

    public class Pair {
        private int a;
        private int b;
    
        public Pair(int a, int b){
            this.a = a;
            this.b = b;
        }
    
        @Override
        public String toString() {
            return String.format("(%d, %d)", a, b);
        }
    }
    

    Here is the modified protocolWalker.g.

    protocolWalker.g (modified)

    tree grammar protocolWalker;
    
    options {
      language = Java;
      tokenVocab = protocol;   
      ASTLabelType = CommonTree;
    }    
    
    
    @header {
        package com.javadude.antlr3.x.tutorial;
        import java.util.List;
        import java.util.ArrayList;
    }
    
    @members { 
      //stores all the transitions objects as they get processed
      private ArrayList<Transitions> allTransitions = new ArrayList<Transitions>();
    
      //returns all the transitions
      public List<Transitions> getAllTransitions() { 
        return allTransitions;
      }
    }
    
    
    transitions
    @init { 
            //create a Transitions object when the rule is hit
            Transitions transitions = new Transitions();
    
            //store it to be accessed later.
            allTransitions.add(transitions);
          } 
     : ^(TRANSITIONS INT transitions_pair[transitions]+) //pass the object to transitions_pair for each PAIR encountered
     {
         System.out.println("transitions=" + $INT.text);
     }
     ;
    
    transitions_pair[Transitions transitions]
     : ^(PAIR a=INT b=INT) 
     {
         System.out.println("pair=" + $a.text + ", " + $b.text);
         //make a call to the Transitions object that was passed to this rule.
         transitions.addPair(new Pair($a.int, $b.int));
     }
     ;
    

    (I renamed pair to transitions_pair because the rule is now tied to transitions-building.) The rule transitions calls transitions_pair, passing a new Transitions object at the same time. transitions_pair adds a new Pair object to the received Transitions object.

    Rules in tree parsers and token parsers can be written to accept objects using the [ArgType argname,...] way. In this case, it makes it easier to visit the child PAIR trees.

    I added a small change to Protocoltest.java to print out the stored transitions:

            ...
            //invoke the start symbol, rule parse
            walker.transitions();
    
            //get the stored transitions and print them out.            
            List<Transitions> transitions = walker.getAllTransitions();
            System.out.println(transitions);
            ...
    

    Here is the new output for the walker:

    pair=5, 0
    Added pair (5, 0) to transitions
    pair=5, 1
    Added pair (5, 1) to transitions
    pair=5, 2
    Added pair (5, 2) to transitions
    transitions=3
    [Pairs: [(5, 0), (5, 1), (5, 2)]]
    

    Here’s a recap of the major changes I made:

    • Added a way to store and return transitions from the walker.
    • Added code to create a Transitions object in the rule transitions.
    • Added code to pass the object to transitions_pair.
    • Added code in the tester to retrieve the transitions from the walker and print them out.

    I think you’ll be all set once you implement your own Transitions class.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a script language based on Antlr: A parser and a tree grammar
If I have an ANTLR grammar as follows: grammar Test; options { language =
I have another problem with my boost::spirit parser. template<typename Iterator> struct expression: qi::grammar<Iterator, ast::expression(),
Given the following lexer: lexer grammar CodeTableLexer; @header { package ch.bsource.ice.parsers; } CodeTabHeader :
I have the following parser grammar (this is a small sample): expr: ident assignop
i am developing parser using bison...in my grammar i am getting this error Here
I'm writing a parser for a very simple grammar in javacc. It's beginning to
I currently have a working, simple language implemented in Java using ANTLR. What I
Is it adequate to use the link grammar parser to do POS tagging? How
I'm currently looking for a lexer/parser that generates Scala code from a BNF grammar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.