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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T03:09:32+00:00 2026-05-19T03:09:32+00:00

I’ve written the below grammar for ANTLR parser and lexer for building trees for

  • 0

I’ve written the below grammar for ANTLR parser and lexer for building trees for logical formulae and had a couple of questions if someone could help:

class AntlrFormulaParser extends Parser;

options {
    buildAST = true;
}

biconexpr : impexpr (BICONDITIONAL^ impexpr)*;

impexpr : orexpr (IMPLICATION^ orexpr)*;

orexpr : andexpr (DISJUNCTION^ andexpr)*;

andexpr : notexpr (CONJUNCTION^ notexpr)*;

notexpr : (NEGATION^)? formula;

formula 
    : atom
    | LEFT_PAREN! biconexpr RIGHT_PAREN!
    ;

atom
    : CHAR
    | TRUTH
    | FALSITY
    ;


class AntlrFormulaLexer extends Lexer;

// Atoms
CHAR: 'a'..'z';
TRUTH: ('\u22A4' | 'T');
FALSITY: ('\u22A5' | 'F');

// Grouping
LEFT_PAREN: '(';
RIGHT_PAREN: ')';
NEGATION: ('\u00AC' | '~' | '!');
CONJUNCTION: ('\u2227' | '&' | '^');
DISJUNCTION: ('\u2228' | '|' | 'V');
IMPLICATION: ('\u2192' | "->");
BICONDITIONAL: ('\u2194' | "<->");

WHITESPACE : (' ' | '\t' | '\r' | '\n') { $setType(Token.SKIP); };

The tree grammar:

tree grammar AntlrFormulaTreeParser;

options {
    tokenVocab=AntlrFormula;
    ASTLabelType=CommonTree;
}

expr returns [Formula f]
    : ^(BICONDITIONAL f1=expr f2=expr) {
        $f = new Biconditional(f1, f2);
    }
    | ^(IMPLICATION f1=expr f2=expr) {
        $f = new Implication(f1, f2);
    }
    | ^(DISJUNCTION f1=expr f2=expr) {
        $f = new Disjunction(f1, f2);
    }
    | ^(CONJUNCTION f1=expr f2=expr) {
        $f = new Conjunction(f1, f2);
    }
    | ^(NEGATION f1=expr) {
        $f = new Negation(f1);
    }
    | CHAR {
        $f = new Atom($CHAR.getText());
    }
    | TRUTH {
        $f = Atom.TRUTH;
    }
    | FALSITY {
        $f = Atom.FALSITY;
    }
    ;

The problems I’m having with the above grammar are these:

  1. The tokens, IMPLICATION and BICONDITIONAL, in the java code for AntlrFormulaLexer only seem to be checking for their respective first character (i.e. ‘-‘ and ‘<‘) to match the token, instead of the whole string, as specified in the grammar.

  2. When testing the java code for AntlrFormulaParser, if I pass a string such as “~ab”, it returns a tree of “(~ a)” (and a string “ab&c” returns just “a”), when it should really be returning an error/exception, since an atom can only have one letter according to the above grammar. It doesn’t give any error/exception at all with these sample strings.

I’d really appreciate if someone could help me solve these couple of problems. Thank you 🙂

  • 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-19T03:09:33+00:00Added an answer on May 19, 2026 at 3:09 am

    I would change the following definitions as:

    IMPLICATION: ('\u2192' | '->');
    BICONDITIONAL: ('\u2194' | '<->');
    

    note “->” vs ‘->’

    And to solve the error issue:

    formula 
        : (
             atom
           | LEFT_PAREN! biconexpr RIGHT_PAREN! 
          ) EOF
        ;
    

    from here:
    http://www.antlr.org/wiki/pages/viewpage.action?pageId=4554943

    Fixed grammar to compile against antlr 3.3 (save as AntlrFormula.g):

    grammar AntlrFormula;
    
    options {
        output = AST; 
    }
    
    
    program : formula ;
    
    formula : atom | LEFT_PAREN! biconexpr RIGHT_PAREN! ;
    
    biconexpr : impexpr (BICONDITIONAL^ impexpr)*;
    
    impexpr : orexpr (IMPLICATION^ orexpr)*;
    
    orexpr : andexpr (DISJUNCTION^ andexpr)*;
    
    andexpr : notexpr (CONJUNCTION^ notexpr)*;
    
    notexpr : (NEGATION^)? formula;
    
    
    atom
        : CHAR
        | TRUTH
        | FALSITY
        ;
    
    
    // Atoms
    CHAR: 'a'..'z';
    TRUTH: ('\u22A4' | 'T');
    FALSITY: ('\u22A5' | 'F');
    
    // Grouping
    LEFT_PAREN: '(';
    RIGHT_PAREN: ')';
    NEGATION: ('\u00AC' | '~' | '!');
    CONJUNCTION: ('\u2227' | '&' | '^');
    DISJUNCTION: ('\u2228' | '|' | 'V');
    IMPLICATION: ('\u2192' | '->');
    BICONDITIONAL: ('\u2194' | '<->');
    
    WHITESPACE : (' ' | '\t' | '\r' | '\n') { $channel = HIDDEN; };
    

    Link to antlr 3.3 binary: http://www.antlr.org/download/antlr-3.3-complete.jar

    you will need to try to match the program rule in order to match the complete file.

    testable with this class:

    import org.antlr.runtime.*;
    
    public class Main {
        public static void main(String[] args) {
            AntlrFormulaLexer lexer = new AntlrFormulaLexer(new ANTLRStringStream("(~ab)"));
            AntlrFormulaParser p = new AntlrFormulaParser(new CommonTokenStream(lexer));
    
            try {
                p.program();
                if ( p.failed() || p.getNumberOfSyntaxErrors() != 0) {
                    System.out.println("failed");
                }
            } catch (RecognitionException e) {
                e.printStackTrace();
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
Does anyone know how can I replace this 2 symbol below from the string
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
Seemingly simple, but I cannot find anything relevant on the web. What is the
this is what i have right now Drawing an RSS feed into the php,
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
That's pretty much it. I'm using Nokogiri to scrape a web page what has

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.