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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T23:18:53+00:00 2026-05-25T23:18:53+00:00

Have found C++ BNF and there next lines selection-statement: if ( condition ) statement

  • 0

Have found C++ BNF and there next lines

selection-statement:
    if ( condition ) statement
    if ( condition ) statement else statement

Now trying to write parser. Need to build parse tree. On input i have BNF and source file. But i’m stucked in how i can point my parser what if condition evaluated to true, then it need to execute first statement otherwise else block? 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-05-25T23:18:53+00:00Added an answer on May 25, 2026 at 11:18 pm

    Conditional statements have a simple recursive structure. The corresponding recursive descent parser has a similarly simple recursive structure. Abstractly, the interior conditionals are parsed as follows:

    <cond> -> if <expression> then <statement> [else <statement>]
    
    cond :
      a = parse expression
      b = parse statement
      if is_else(token)
        then c = parse statement
             return conditional(a,b,c)
        else return conditional(a,b)
    

    In your example, conditional statements contain blocks of conditionals the last of which contains an else clause. Assuming that the tokenized input sequence has this form and syntactic errors were detected during lexical analysis, the outer conditional is parsed as follows:

    <conditional> -> selection_statement: {<cond>} <cond>
    
    conditional :
      b = new block
      while (iscond(next))
        s = parse cond
        b = insert(s,b)
      return b
    

    Of course, the actual implementation will be significantly more detailed and tedious. However, the preceding describes in outline the construction of a parse tree of a conditional statement having the required form from a tokenized input sequence.

    I just realized you were talking about evaluating the abstract syntax tree. The structure of the function that evaluations a conditional statement is similar to the function that parses a conditional statement. Abstractly,

    cond(input) :
      a = evaluate(if_part(input))
      if is_true(a)
        then evaluate(then_part(input))
        else if(is_else(input))
               then evaluate(else_part(input))
               else return
    

    In order to determine which portion of the conditional to evaluate, you must first evalute the “if” part of the conditional to a Boolean value. If the Boolean value is “true,” the “then” part of the conditional is evaluated. If the Boolean value is “false,” then the “else” part of the conditional is evaluated. If there is no “else” part, there is nothing to evaluate. Of course, the implementation will be more detailed than the above.

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

Sidebar

Related Questions

I have found that it is impossible to write ( file_put_contents or simple fwrite
I have found a few posts that seem similar to what I need, but
I have found a math parser Here . I have never added something like
I have found some similar questions but none gave me what I really need.
I have found that writing PHP code within classes can become rather long: $this->parser->parse_syntax($this->get_language_path($this->language),
I have found that there is generally a singe type or namespace that takes
I have found an example of something similar that I am trying to implement:
I have found Okapi Similarity measure can be used to calculated document similarity from
I have found that I have no problem using require to load something like
I have found many great answers to this from a year ago (when it

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.