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 8650209
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T13:45:43+00:00 2026-06-12T13:45:43+00:00

For a grammar like, how to preserver the order in which productions appear. class:

  • 0

For a grammar like, how to preserver the order in which productions appear.

class: 'class' ID 
        '{' (fields 
         | methods) * '}'         -> ^(CLASS ID ^(FIELD fields*) ^(METHOD methods) 
         ;

The production works as I expect, but if there’s a class like

class abc {
   field 1 
   field 2
   method 1
   method 2
   field 3
   method 3
   field 4
}

All of the fields end up in single list and the methods in second list. What is the correct way of preserving their order? I tried doing..

class: 'class' ID 
        '{' (fields               -> ^(FIELD fields)
         | methods                -> ^(METHOD methods)
         )* '}'                   -> ^(CLASS ID $class)
         ;

I even tried inserting a dummy head in between with no luck.

class: 'class' ID 
        '{' (fields 
         | methods) * '}'         -> ^(CLASS ID ^(FIELD fields*) ^NODE ^(METHOD methods) 
         ;

but this didn’t work.

  • 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-12T13:45:44+00:00Added an answer on June 12, 2026 at 1:45 pm

    In this first case, the CLASS node contains a series of FIELD nodes (FIELD fields*) followed by a series of METHOD nodes (METHOD methods*) because the production explicitly states that field expressions be processed together and then method expressions be processed together:

    -> ^(CLASS ID ^(FIELD fields*) ^(METHOD methods)
    

    You didn’t mention what the second and third approaches produced, but it was probably less ideal than the first.

    Try the following approach with a body expression instead:

    grammar temp;
    options {output=AST;}
    tokens { KLASS; FIELD; METHOD; }
    
    klass   :   'class' ID body -> ^(KLASS ID body)
            ;
    
    body    :   '{'!
                    (field | method) *
                '}'! 
            ;
    
    field   : 'field' INT -> ^(FIELD INT)
            ;
    
    method  : 'method' INT -> ^(METHOD INT)
            ;
    
    ID      : ('a'..'z')+;
    INT     : ('0'..'9')+;
    WS      : (' '|'\r'|'\n')+ {$channel = HIDDEN;} ;
    

    You’ll get the following AST result:

    -KLASS
       -abc
       -FIELD
          -1
       -FIELD
          -2
       -METHOD
          -1
       -METHOD
          -2
       -FIELD
          -3
       -METHOD
          -3
       -FIELD
          -4
    

    If you’d like to test the grammar with ANTLRWorks, open (or create) the grammar with it:

    enter image description here

    and press CTRL+D to start the debugger (don’t use the interpreter, it’s buggy!):

    enter image description here

    Paste your input in the window that pops up and make sure you select the correct start production rule (klass, in this case). Then press OK.

    When the debugger is launched, press the end-arrow button >| to parse the input and then click the AST button to see what the AST the parser created looks like:

    enter image description here

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

Sidebar

Related Questions

I've defined a simple Xtext grammar which looks like this (simplified): grammar org.xtext.example.mydsl.MyDsl with
perhaps I'm asking the wrong question. I have code like this: class ExpressionGrammar(Grammar): def
It is often convenient to express grammar productions in BNF like A ::= car
I have this simple grammar for a C# like syntax. I can't figure out
Let's say I have a grammar defined to something like: some_rule := a b
friends. I have a 'make'-like style file needed to be parsed. The grammar is
Here's the grammar, which is supposed to describe a language of nested braces with
Say I have a Boost Spirit grammar like this, where a parent rule passes
I am trying realize python like indent-depending grammar. Source example: ABC QWE CDE EFG
I'm OCaml newbie and I'm trying to write a simple OCaml-like grammar, and I

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.