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

  • Home
  • SEARCH
  • 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 5839607
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T11:36:29+00:00 2026-05-22T11:36:29+00:00

I’m trying to write a language parser and build a nice AST. In the

  • 0

I’m trying to write a language parser and build a nice AST. In the language, a function is essentially a variable with a callable value. For example:

int f(int arg) {...};
#int(int) f: int(int arg) {...};

both are equal, and I want to transform the first into the second. As you can see, the variable’s type contains parameters, but without name. The function value needs the parameter name.

So the question is: Is it possible to get both (int arg) and (int) back from my rule that matches a parameter list, or is it alternatively possible to transform the first into the second on the right of the ->?

I’ll add an example source and result tree below

Input:
^(FUN_DEF
  ^(TYPE_SIMP 'int')
  'f'
  ^(PARAM_LIST
    ^(PARAM 'int' 'arg')
   )
  ^(BLOCK ...)
 )

Result:
^(VAR_DEF
  ^(TYPE_FUN
    ^(TYPE_SIMP 'int')
    ^(PARAM_LIST
      ^(PARAM 'int')
     )
   )
  'f'
  ^(FUN
    ^(TYPE_SIMP 'int')
    ^(PARAM_LIST
      ^(PARAM 'int' 'arg')
     )
    ^(BLOCK ...)
   )
 )
  • 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-05-22T11:36:30+00:00Added an answer on May 22, 2026 at 11:36 am

    A possibility would be invoke a custom method in your shortFunction rule that, given a paramList, would strip all identifiers from them leaving only the types and insert that tree in the proper place:

    shortFunction
      :  type ID '(' paramList ')' block 
         -> ^( ... {customMethod($paramList.tree)} ... )
      ;
    

    A demo:

    file: Fun.g

    grammar Fun;
    
    options { 
      output=AST;
      ASTLabelType=CommonTree;
    }
    
    tokens {
      ROOT;
      PARAM;
      PARAM_LIST;
      BLOCK;
      VAR_DEF;
      FUN;
      TYPE_FUN;
      TYPE_SIMP;
    }
    
    @parser::members {
      private CommonTree stripIDs(CommonTree tree) {
        CommonTree copy = new CommonTree(new CommonToken(PARAM_LIST, "PARAM_LIST"));
        for(int i = 0; i < tree.getChildCount(); i++) {
          CommonTree temp = (CommonTree)tree.getChild(i);
          CommonTree child = new CommonTree(temp);
          child.addChild(new CommonTree((CommonTree)temp.getChild(0)));
          copy.addChild(child);
        }
        return copy;
      }
    }
    
    parse
      :  function+ EOF -> ^(ROOT function+)
      ;
    
    function
      :  shortFunction
      |  longFunction
      ;
    
    shortFunction
      :  type ID '(' paramList ')' block
         -> ^(VAR_DEF ^(TYPE_FUN ^(TYPE_SIMP type) {stripIDs($paramList.tree)}) ID ^(FUN ^(TYPE_SIMP type) paramList block))
      ;
    
    longFunction
      :  '#' t1=type '(' typeList ')' ID ':' t2=type '(' paramList ')' block
         -> ^(VAR_DEF ^(TYPE_FUN ^(TYPE_SIMP $t1) typeList) ID ^(FUN ^(TYPE_SIMP $t2) paramList block))
      ;
    
    paramList
      :  (param (',' param)*)? -> ^(PARAM_LIST param*)
      ;
    
    param
      :  type ID -> ^(PARAM type ID)
      ;
    
    typeList
      :  (type (',' type)*)? -> ^(PARAM_LIST ^(PARAM type)*)
      ;
    
    type
      :  INT
      |  SHORT
      |  BYTE
      ;
    
    block
      :  '{' '...' '}' -> ^(BLOCK '...')
      ;
    
    SHORT : 'short';
    BYTE  : 'byte';
    INT   : 'int';
    ID    : ('a'..'z' | 'A'..'Z') ('a'..'z' | 'A'..'Z' | '0'..'9')*;
    SPACE : (' ' | '\t' | '\r' | '\n') {$channel=HIDDEN;};
    

    file: Main.java

    import org.antlr.runtime.*;
    import org.antlr.runtime.tree.*;
    import org.antlr.stringtemplate.*;
    
    public class Main {
      public static void main(String[] args) throws Exception {
        String source = "#short(byte, int) f: short(byte a, int b) { ... } short f(byte a, int b) { ... }";
        FunLexer lexer = new FunLexer(new ANTLRStringStream(source));
        FunParser parser = new FunParser(new CommonTokenStream(lexer));
        CommonTree tree = (CommonTree)parser.parse().getTree();
        DOTTreeGenerator gen = new DOTTreeGenerator();
        StringTemplate st = gen.toDOT(tree);
        System.out.println(st);
      }
    }
    

    If you run the main class, you will see that the input:

    #short(byte, int) f: short(byte a, int b) { ... } 
    short f(byte a, int b) { ... }
    

    produces two identical trees:

    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 have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
i got an object with contents of html markup in it, for example: string
I need a function that will clean a strings' special characters. I do NOT

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.