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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T07:29:21+00:00 2026-05-29T07:29:21+00:00

So I have generated a parser via JISON: // mygenerator.js var Parser = require(jison).Parser;

  • 0

So I have generated a parser via JISON:

// mygenerator.js
var Parser = require("jison").Parser;

// a grammar in JSON
var grammar = {
    "lex": {
        "rules": [
           ["\\s+", "/* skip whitespace */"],
           ["[a-f0-9]+", "return 'HEX';"]
        ]
    },

    "bnf": {
        "hex_strings" :[ "hex_strings HEX",
                         "HEX" ]
    }
};

// `grammar` can also be a string that uses jison's grammar format
var parser = new Parser(grammar);

// generate source, ready to be written to disk
var parserSource = parser.generate();

// you can also use the parser directly from memory

// returns true
parser.parse("adfe34bc e82a");

// throws lexical error
parser.parse("adfe34bc zxg");

My question is, how do I retrieve the AST now? I can see that I can run the parser against input, but it just returns true if it works or fails if not.

For the record, I am using JISON: http://zaach.github.com/jison/docs/

  • 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-29T07:29:22+00:00Added an answer on May 29, 2026 at 7:29 am

    I’m not too familiar with Jison’s inner workings, so I don’t know any method that would do it.

    But in case you’re interested in a little bruteforce to solve this problem, try this:

    First, create an object to hold the AST

    function jisonAST(name, x) { this.name = name; this.x = x; }
    
    // return the indented AST
    jisonAST.prototype.get = function(indent){
      // create an indentation for level l
      function indentString(l) { var r=""; for(var i=0;i<l;i++){r+="  "}; return r }
    
      var r = indentString(indent) + "["+this.name+": ";
      var rem = this.x;
      if( rem.length == 1 && !(rem[0] instanceof jisonAST) ) r += "'"+rem[0]+"'"; 
      else for( i in rem ){ 
          if( rem[i] instanceof jisonAST ) r += "\n" + rem[i].get(indent+1);
          else { r += "\n" + indentString(indent+1); r += "'"+rem[i]+"'"; }
        }
      return r + "]";
    }
    

    Add a little helper function for Jison’s BNF

    function o( s ){
        r = "$$ = new yy.jisonAST('"+s+"',[";
        for( i = 1; i <= s.split(" ").length; i++ ){ r += "$"+i+"," }
        r = r.slice(0,-1) + "]);";
        return [s,r];
    }
    

    With this, continue to the example code (slight modification):

    var Parser = require("jison").Parser;
    
    // a grammar in JSON
    var grammar = {
        "lex": {
            "rules": [
               ["\\s+", "/* skip whitespace */"],
               ["[a-f0-9]+", "return 'HEX';"]
            ]
        },
        "bnf": {
            // had to add a start/end, see below
            "start" : [ [ "hex_strings", "return $1" ] ],
            "hex_strings" :[ 
                o("hex_strings HEX"), 
                o("HEX") 
            ]
        }
    };
    
    var parser = new Parser(grammar);
    // expose the AST object to Jison
    parser.yy.jisonAST = jisonAST
    

    Now you can try parsing:

    console.log( parser.parse("adfe34bc e82a 43af").get(0) );
    

    This will give you:

    [hex_strings HEX: 
      [hex_strings HEX: 
        [HEX: 'adfe34bc']  
        'e82a']  
      '43af']
    

    Small note: I had to add a “start” rule, in order to only have one statement that returns the result. It is not clean (since the BNF works fine without it). Set it as an entry point to be sure…

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

Sidebar

Related Questions

By default, a parser generated using ANTLR-3 extends from org.antlr.runtime.Parser. How do I have
I am trying to invoke method via webservice (SOAP) I have generated Web service
I have a parser generated using Flex/Bison - it parses each line of a
I have several Textboxes generated via a foreach in the view. To make each
I have an AST derived from the ANTLR Parser Generator for Java. What I
I have generated a collection of images. Some of them are blank as in
I have generated a C# SharePoint Sequential Workflow project using the very handy STSDEV
I have generated inputs with price values. Example: <input type=text value=59,00/> Now I should
I have generated using openssl mycert.pem which contents the certificate. And I converted the
I have generated a bunch of png files to use as markers on my

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.