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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T05:00:14+00:00 2026-05-27T05:00:14+00:00

I’m trying to implement the && and || operators of the bash shell for

  • 0

I’m trying to implement the && and || operators of the bash shell for an assignment – in C. For an input like:

a && b && c

my parser makes a linked list with “symbol” and “connector” i.e.:

a - &&

b - &&

c - END

so I can evaluate the symbol to determine whether to check the next condition or not. I know the basic idea, but is there a general algorithm for how to implement the conditionals? Mainly, if somebody passes in something like a && (b | c) || d && e || f, then I just don’t know what’s a good way to attack that. I tried evaluating the first symbol and storing status to check what to read next, but it’s not a good approach.

Any suggestions?

  • 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-27T05:00:14+00:00Added an answer on May 27, 2026 at 5:00 am

    The general method for implementing this results in a syntax tree, not a list. You need to parse the list of tokens (which is what you’ve created) into an AST (Abstract Syntax Tree) describing the expression.

    There is no way to get the precedence right with the way you’re doing it now. And this is crucial for the short-circuit evaluation property the && and || operators have.

    Here is some sample code in Python that will build your tree structure. It’s kinda ugly, and I would write a prettier parser if I were really doing this. This is just to give you the general idea:

    def parsellst(lst):
        stack = []
        for tok in lst:
            if tok == '(':
                if (len(stack) > 0) and (stack[-1] not in ('&&', '||')):
                    raise RuntimeError("Malformed expression")
                stack.append(tok)
            elif tok == ')':
                while (len(stack) >= 4) and (stack[-2] != '('):
                    if stack[-2] not in ('&&', '||'):
                        raise RuntimeError("Malformed expression")
                    node = (stack[-2], stack[-3], stack[-1])
                    stack[-3:] = [node]
                if (len(stack) <= 1) or (stack[-2] != '(') or \
                        (stack[-1] in ('&&', '||')):
                    raise RuntimeError("Malformed expression")
                stack.pop(-2)
            elif tok == '||':
                if len(stack) <= 0:
                    raise RuntimeError("Malformed expression")
                elif stack[-1] in ('&&', '||', '('):
                    raise RuntimeError("Malformed expression")
                while (len(stack) >= 3) and (stack[-2] != '('):
                    node = (stack[-2], stack[-3], stack[-1])
                    stack[-3:] = [node]
                stack.append(tok)
            elif tok == '&&':
                if len(stack) <= 0:
                    raise RuntimeError("Malformed expression")
                elif stack[-1] in ('&&', '||', '('):
                    raise RuntimeError("Malformed expression")
                if (len(stack) > 1) and (len(stack) < 3):
                    raise RuntimeError("Malformed expression")
                while (len(stack) >= 3) and (stack[-2] not in ('(', '||')):
                    node = (stack[-2], stack[-3], stack[-1])
                    stack[-3:] = [node]
                stack.append(tok)
            else:
                stack.append(tok)
        while len(stack) > 1:
            if len(stack) < 3:
                raise RuntimeError("Malformed expression")
            if stack[-2] == '(':
                raise RuntimeError("Malformed expression: missing )")
            else:
                node = (stack[-2], stack[-3], stack[-1])
                stack[-3:] = [node]
        if len(stack) > 0:
            return stack[-1]
        else:
            return ()
    

    The tree structure this code builds is very lispish. Each node of your tree consists of a ‘tuple’ (sort of like a lisp list) in Python that looks like (operator, arg1, arg2). It’s up to you to figure out how to use it. As a hint, you would have to write an evaluator that processed the tree in a particular order.

    Also, this parser drops the parenthesis because it treats the parenthesis as merely a grouping construct. But for bash this isn’t wise because parenthesis have meaning beyond mere grouping. So the thing should be modified (which is relatively trivial) to put the ‘parenthesis operator’ back into the resulting tree.

    Here’s an example of how to use the parser and what it’s output looks like:

    >>> parsellst(['a', '&&', '(', 'b', '||', 'c', ')', '||', 'd', '&&', 'e', '||', 'f'])
    ('||', ('||', ('&&', 'a', ('||', 'b', 'c')), ('&&', 'd', 'e')), 'f')
    
    • 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
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
I am trying to render a haml file in a javascript response like so:
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
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString

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.