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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T03:05:58+00:00 2026-06-14T03:05:58+00:00

I’m trying to parse user-inputted prefix notation logical expressions using a context-free-grammar with the

  • 0

I’m trying to parse user-inputted prefix notation logical expressions using a context-free-grammar with the Irony library. This is for a class assignment so if anybody happens to be an expert on this, I would love to know more.

I need to accept a user-inputted logical expression of the following format:

and P Q -- (meaning P ^ Q)
or P Q -- (meaning P v Q)
not P  -- (meaning ~P)
imp P Q -- (meaning P -> Q)

I’m attempting to parse these into expression trees using a context free grammar I’m implementing in Irony. The context-free grammar I’m using is here, in BNF:

<Expression> ::= <Not> | <And> | <Or> | <Implies> | <Identifier>
<Not>        ::= "not" <Expression>
<And>        ::= "and" <Expression> <Expression>
<Or>         ::= "or" <Expression> <Expression>
<Implies>    ::= "imp" <Expression> <Expression>

(<Identifier> is implemented as an IdentifierTerminal object).

I’ve used Irony to parse expressions before but for some reason, I cannot get it to work. When I type the expression and P Q it seems to be identifying “and” as an Identifier terminal, not part of the And nonterminal. I may be doing something obvious but I simply can’t figure it out. Here is the language class I extended:

class LogicPrefix : Grammar
{
    public LogicPrefix()
        : base(false)
    {
        NonTerminal Expression = new NonTerminal("expression");
        NonTerminal Implies = new NonTerminal("implies");
        NonTerminal And = new NonTerminal("and");
        NonTerminal Or = new NonTerminal("or");
        NonTerminal Not = new NonTerminal("not");
        Terminal Identifier = new IdentifierTerminal("identifier");
        Root = Expression;

        Expression.Rule = And | Or | Not | Identifier;

        Not.Rule = "not" + Expression;
        Implies.Rule = "imp" + Expression + Expression;
        And.Rule = "and" + Expression + Expression;
        Or.Rule = "or" + Expression + Expression;

    }
}

And here is my driver class:

class Program
{
    static void Main(string[] args)
    {
        LogicPrefix grammar = new LogicPrefix();
        Parser p = new Parser(grammar);
        ParseTree pt = p.Parse("and P Q");
        //pt has thrown an error flag.
    }
}

Please let me know if I’m doing something wrong, I’d love some advice on this.

  • 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-14T03:05:59+00:00Added an answer on June 14, 2026 at 3:05 am

    Your identifier lexing seems to allow your other terminals to be lexed as identifiers because by definition IdentifierTerminal recognizes anything that “starts with an underscore or letter and contains only letters, numbers, and underscores” (source). This means that when your program reads the and, it can read it as the identifier and or the keyword and.

    It looks to me like you can fix this by declaring your operators to be punctuation with the following line:

    MarkPunctuation("imp", "and", "or", "not")
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

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'm new to using the Perl treebuilder module for HTML parsing and can't figure
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 a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I am trying to render a haml file in a javascript response like so:
I have a French site that I want to parse, but am running into
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... 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.