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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T10:57:26+00:00 2026-06-09T10:57:26+00:00

I created a grammar for a Scheme-like language interpreter. Initially, I had one semantic

  • 0

I created a grammar for a Scheme-like language interpreter. Initially, I had one semantic predicate for the if-then-else statement to control evaluation i.e. when the conditional is true only the ‘then’ is evaluated; when it’s false, only the ‘else’ is evaluated. To add type-checking functionality, I added a second semantic predicate enclosing the first one. It’s a hack, though, because to type-check or evaluate I have to manually change the global boolean typeCheck to either true or false.

The AST for the IF statement now has two branches. The first (IFT) is for type-checking the IF statement’s arguments. [this is why of two predicates; to type-check, all arguments must be evaluated] The second branch (IFE) is for evaluating the short-circuiting if-then-else. The problem started when I added a second semantic predicate that enclosed the first, getting the infamous “no viable alternative at input” error. After getting nowhere slowly, I created new grammars with just the essentials. Same issue. Here is the stripped down AST:

IF AST

Though I have never experienced one, I’ve seen issues reported on SO with the ANTLR IDE in Eclipse. So I fired up ANTLRWorks, debugged the parser grammar, then tried to debug the tree grammar. Versions 1.4.3 and 1.4.2 both popup this box, “warning: the grammar used by the remote parser is not the same“. I click OK then in the debugger click Step Forward just once and the java.exe*32 process dies. As a final test, I manually compiled from the command line with antlr-3.3 and antlr-3.4 complete jars, no change.

The parser:

grammar NestedSemPreds;

options {
    output = AST;
    ASTLabelType = CommonTree;
}

tokens {
    IF;
    IFT;
    IFE;
}   

/** parser rules **/
ifstmt  : '(' 'if' COND THEN ELSE ')' NEWLN
            -> ^(IF ^(IFT COND THEN ELSE)
                    ^(IFE COND THEN ELSE)
                )
        ;

/** lexer rules **/
COND    : 'true' ; 
THEN    : 'then' ;
ELSE    : 'else' ;
COMMENT :   ('//' .* NEWLN) { skip(); } ; //for lines in datafile I don't want processed
NEWLN   :   '\r'? '\n' ;
WS      :   (' '|'\t')+ { skip(); } ;

The tree grammar:

tree grammar treeEval;

options {
    tokenVocab = NestedSemPreds;
    ASTLabelType = CommonTree;
}

@members {
    boolean typeCheck = false;
}

ifstmt 
@init {
    boolean condition = true;
}
 : ^(IF (  
           {typeCheck}? => //{System.out.println("typeCheck is true");}
                            ^(IFT COND THEN ELSE) {System.out.println("IFT COND THEN ELSE");}
                            ^(IFE . . .) {System.out.println("    SKIP IFE WITH . WILDCARD");}
        | {!typeCheck}? => //{System.out.println("typeCheck is false");} 
                            ^(IFT . . .) {System.out.println("skip ift with . wildcard");}
                            ^(IFE COND
                                    (  {condition}? => ({System.out.println("    condition is true");}
                                                       THEN . {System.out.println("        evaluate THEN");})
                                    | {!condition}? => ({System.out.println("    condition is false");}
                                                      . ELSE {System.out.println("        evaluate else");})
                                    )//close inner predicate
                             )//close ^IFE
        )//close outer predicate
    )//close ^IF
 ;

I couldn’t find any particular issues on nested semantic predicates, but I didn’t find any examples either. Why is this code failing? Any ideas on the issue with ANTLRWorks debugger?

  • 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-09T10:57:27+00:00Added an answer on June 9, 2026 at 10:57 am

    Using Bart’s comment about not needing to “add a predicate before the second alternative”, I did some additional testing. All tested options were valid syntax so I think this is an ANTLR bug. To cut to the chase, here is what works:

    ifstmt 
    @init {
        boolean condition = true;
    }
     : ^(IF (  
               {typeCheck}? => ^(IFT COND THEN ELSE) {System.out.println("IFT COND THEN ELSE");}
                               . //MUST use only one dot
            | {!typeCheck}? => . //also works with ^(IFT . . .) here
                                ^(IFE COND
                                        (  {condition}? => ({System.out.println("    condition is true");}
                                                           THEN . {System.out.println("        evaluate THEN");})
                                        | {!condition}? => //this inner 2nd predicate not required
                                                          . ELSE {System.out.println("        evaluate else");})
                                        )//close inner predicates
                                 )//close ^IFE
            )//close outer predicates
        )//close ^IF
     ;
    

    These two steps are required:

    1. Both outer predicates, {typeCheck}? and {!typeCheck}?, are necessary.
    2. The first outer predicate, a single . operator to skip the whole subtree

    As noted in the code sample, it still works with either of these two syntax changes:

    1. The second outer predicate, {!typeCheck}?, can use a single . or the original ^(IFT . . .) syntax.
    2. The second inner predicate is optional, as Bart mentioned.

    Another solution that worked was to manually process the node stream and add appropriate code, but that’s not as clean as letting ANTLR do its thing.

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

Sidebar

Related Questions

I'm attemping to learn language parsing for fun... I've created a ANTLR grammar which
I have an grammar for a template language. I created this for Antlr 3.2
I created a simple grammar in AntlWorks. Then I generated code and I have
I have a custom made grammar for an interpreted language and I am looking
I've created a new major mode derived from cc-mode, because I'm using a meta-language
I've created a grammar to parse simple ldap query syntax. The grammer is: expression
I'm trying to compile a simple grammar which I created with ANTLR but I
I've created my 'new programming language' using the ANTLR framework. The language is defined
sorry in advance for my poor grammar. I have created a pipeline with GATE
I'm trying to create a small english-like language for specifying tasks. The basic idea

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.