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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T18:26:04+00:00 2026-06-01T18:26:04+00:00

I’m currently learning about parsing but i’m a bit confused as how to generate

  • 0

I’m currently learning about parsing but i’m a bit confused as how to generate an AST. I have written a parser that correctly verifies whether an expressions conforms to a grammar (it is silent when the expression conforms and raises an exception when it is not). Where do i go from here to build an AST? I found plenty of information on building my LL(1) parser, but very little on then going on to build the AST.

My current code (written in very simple Ruby, and including a lexer and a parser) is found here on github: https://gist.github.com/e9d4081b7d3409e30a57

Can someone explain how i go from what i have currently to an AST?

Alternatively, if you are unfamiliar with Ruby, but know C, could you tell me how i build an AST for the C code in the recursive descent parsing wikipedia article.

Please note, i do not want to use a parser generator like yacc or antlr to do the work for me, i want to do everything from scratch.

Thanks!

  • 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-01T18:26:06+00:00Added an answer on June 1, 2026 at 6:26 pm

    You need to associate each symbol that you match with a callback that constructs that little part of the tree. For example, let’s take a fairly common construct: nested function calls.

    a(b())
    

    Your terminal tokens here are something like:

    • L_PAREN = ‘(‘
    • R_PAREN = ‘)’
    • IDENTIFIER = [a-z]+

    And your nonterminal symbols are something like:

    • FUNCTION_CALL = IDENTIFIER, L_PAREN, R_PAREN
    • or;
    • FUNCTION_CALL = IDENTIFIER, L_PAREN, FUNCTION_CALL, R_PAREN

    Obviously the second alternative above for the rule FUNCTION_CALL is recursive.

    You already have a parser that knows it has found a valid symbol. The bit you’re missing is to attach a callback to the rule, which receives its components as inputs and returns a value (usually) representing that node in the AST.

    Imagine if the first alternative from our FUNCTION_CALL rule above had a callback:

    Proc.new do |id_tok, l_paren_tok, r_paren_tok|
      { item: :function_call, name: id_tok, args: [] }
    end
    

    That would mean that the AST resulting from matching:

    a()
    

    Would be:

    {
      item: :function_call,
      name: "a",
      args: []
    }
    

    Now to extrapolate that to the more complex a(b()). Because the parser is recursive, it will recognize the b() first, the callback from which returns what we have above, but with “b” instead of “a”.

    Now let’s define the callback attached to the rule that matches the second alternative. It’s very similar, except it also deals with the argument it was passed:

    Proc.new do |id_tok, l_paren_tok, func_call_item, r_paren_tok|
      { item: :function_call, name: id_tok, args: [ func_call_item ] }
    end
    

    Because the parser has already recognized b() and that part of the AST was returned from your callback, the resulting tree is now:

    {
      item: :function_call,
      name: "a",
      args: [
        {
          item: :function_call,
          name: "b",
          args: []
        }
      ]
    }
    

    Hopefully this gives you some food for thought. Pass all the tokens you match into a routine that constructs very small parts of your AST.

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

Sidebar

Related Questions

I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
This could be a duplicate question, but I have no idea what search terms
I don't have much knowledge about the IPv6 protocol, so sorry if the question
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and

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.