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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T17:24:00+00:00 2026-06-01T17:24:00+00:00

I am working on a simple mathematical parser. Something that just reads number =

  • 0

I am working on a simple mathematical parser. Something that just reads number = 1 + 2;

I have a vector containing these tokens. They store a type and string value of the character. I am trying to step through the vector to build an AST of these tokens, and I keep getting segmentation faults, even when I am under the impression my code should prevent this from happening.

Here is the bit of code that builds the AST:

struct ASTGen
{
    const vector<Token>            &Tokens;
    unsigned int                   size,
                                   pointer;

    ASTGen(const vector<Token> &t) : Tokens(t), pointer(0) 
    {
        size = Tokens.size() - 1;
    }

    unsigned int next()
    {
        return pointer + 1;
    }

    Node* Statement()
    {
        if(next() <= size)
        {
            switch(Tokens[next()].type)
            {
                case EQUALS
                :
                    Node* n = Assignment_Expr();
                    return n;
            }
        }

        advance();
    }

    void advance()
    {
        if(next() <= size) ++pointer;
    }

    Node* Assignment_Expr()
    {
        Node* lnode = new Node(Tokens[pointer], NULL, NULL);
        advance();
        Node* n = new Node(Tokens[pointer], lnode, Expression());
        return n;
    }

    Node* Expression()
    {
        if(next() <= size)
        {                        
            advance();
            if(Tokens[next()].type == SEMICOLON)
            {
                Node* n = new Node(Tokens[pointer], NULL, NULL);
                return n;
            }

            if(Tokens[next()].type == PLUS)
            {
                Node* lnode = new Node(Tokens[pointer], NULL, NULL);
                advance();
                Node* n = new Node(Tokens[pointer], lnode, Expression());
                return n;
            }
        }
    }
};

...

ASTGen AST(Tokens);
Node* Tree = AST.Statement();
cout << Tree->Right->Data.svalue << endl;

I can access Tree->Data.svalue and get the = Node’s token info, so I know that node is getting spawned, and I can also get Tree->Left->Data.svalue and get the variable to the left of the =

I have re-written it many times trying out different methods for stepping through the vector, but I always get a segmentation fault when I try to access the = right node (which should be the + node)

Any help would be greatly appreciated.

  • 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-01T17:24:01+00:00Added an answer on June 1, 2026 at 5:24 pm

    There’s plenty more code that we haven’t seen, so I can’t tell you precisely what’s going on, but I see a few things that are reasons for concern. One is that the Statement() method doesn’t always return a value. If the first if test doesn’t pass, then we call advance() and fall off the bottom of the routine without an explicit return. The caller will try to get the return value of the function but it’ll get garbage. This could lead to all sorts of problems, including things like double free() calls, etc, which can easily cause segfaults.

    Expression() has the same problem.

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

Sidebar

Related Questions

I had a simple working (ASP.NET) Scenario here: 3 asp:RadioButtons that each have OnCheckedChanged
The simple working query, list the number of licenses for each license type: Query:
I'm working with my simple PHP captcha algorithm ( http://www.source.ofitall.com/devel/captcha.php ) and I have
I have a simple working PHP script to write an HTML table from a
I'm working on a project that performs physiological simulations using mathematical models. We currently
I'm currently working on a small project with OCaml; a simple mathematical expression simplifier.
I am working in a company that develops computing kernels. Their design is simple
I am working on simple facebook integration that allows users to post high scores
I'm working on simple counter. My problem is that drawString() method draws new string
I have been working for two years in software industry. Some things that have

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.