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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T02:23:40+00:00 2026-05-24T02:23:40+00:00

Okay, I’m really confused about this error. I know in the past having a

  • 0

Okay, I’m really confused about this error. I know in the past having a ‘/’ as a token in a rule hasn’t produced any errors. However, this is simply baffling. Here is my grammar:

grammar LilWildC;

options {
    language = Java;
}

@header
{
    package com.matthewkimber.lilwildc;
}

@lexer::header
{
    package com.matthewkimber.lilwildc;
}

program
    :   global_variables procedure+
    ;

global_variables
    :   variable_definition*
    ;

variable_definition
    :   'number' IDENT ';'
    |   'number' '[' A_NUMBER ']' IDENT ';'
    ;

procedure
    :   'procedure' IDENT '{' block '}'
    ;

block
    :   local_variables statement+
    ;

local_variables
    :   variable_definition*
    ;

statement
    :   variable_reference '=' numeric_expression ';'
    ;

variable_reference
    :   IDENT
    |   IDENT '[' numeric_expression ']'
    ;

numeric_expression
    :   multiply_expression
        ( '+' multiply_expression
        | '-' multiply_expression
        )*
    ;

multiply_expression
    :   negative_factor
        ( '*' negative_factor
        | '/' negative_factor
        | '%' negative_factor
        )*
    ;

negative_factor
    :   '-'? factor
    ;

factor
    :   A_NUMBER
    |   variable_reference
    |   '(' numeric_expression ')'
    ;

A_NUMBER:   (('0'..'9')+'.'?) | (('0'..'9')*'.'('0'..'9')+) ;
IDENT:  ('a'..'z' | 'A'..'Z')('a'..'z' | 'A'..'Z' | '0'..'9' | '_')* ;
WS: (' ' | '\t' | ('\r'?'\n'))+ { $channel = HIDDEN; } ;

When I run a test on the grammar with the following input:

procedure main
{
    var = 10 / 1;
}

I get the following parse tree in the ANTLR eclipse plug-in:

ANTLR Parse Tree - NoViableAltException

What I don’t get is that multiplication and modulo work fine, only divide throws this error. Is ANTLR skipping right over the ‘/’ and not seeing it as a token or have I missed something? Any help is greatly appreciated.

  • 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-24T02:23:41+00:00Added an answer on May 24, 2026 at 2:23 am

    There’s nothing wrong with your grammar, the problem must be the Eclipse plugin. ANTLRWorks’ debugger produces the tree:

    enter image description here

    And creating a little test myself (after fixing the typo grammary LilWildC; to grammar LilWildC;, and removing the packages) with a main class and ANTLR 3.3:

    LilWildC.g

    grammar LilWildC;
    
    options {
        language = Java;
    }
    
    program
        :   global_variables procedure+
        ;
    
    global_variables
        :   variable_definition*
        ;
    
    variable_definition
        :   'number' IDENT ';'
        |   'number' '[' A_NUMBER ']' IDENT ';'
        ;
    
    p    rocedure
        :   'procedure' IDENT '{' block '}'
        ;
    
    block
        :   local_variables statement+
        ;
    
    local_variables
        :   variable_definition*
        ;
    
    statement
        :   variable_reference '=' numeric_expression ';'
        ;
    
    variable_reference
        :   IDENT
        |   IDENT '[' numeric_expression ']'
        ;
    
    numeric_expression
        :   multiply_expression
            ( '+' multiply_expression
            | '-' multiply_expression
            )*
        ;
    
    multiply_expression
        :   negative_factor
            ( '*' negative_factor
            | '/' negative_factor
            | '%' negative_factor
            )*
        ;
    
    negative_factor
        :   '-'? factor
        ;
    
    factor
        :   A_NUMBER
        |   variable_reference
        |   '(' numeric_expression ')'
        ;
    
    A_NUMBER:   (('0'..'9')+'.'?) | (('0'..'9')*'.'('0'..'9')+) ;
    IDENT:  ('a'..'z' | 'A'..'Z')('a'..'z' | 'A'..'Z' | '0'..'9' | '_')* ;
    WS: (' ' | '\t' | ('\r'?'\n'))+ { $channel = HIDDEN; } ;
    

    Main.java

    import org.antlr.runtime.*;
    
    public class Main {
      public static void main(String[] args) throws Exception {
        String src = 
            "procedure main     \n" +
            "{                  \n" +
            "    var = 10 / 1;  \n" +
            "}                  \n";
        LilWildCLexer lexer = new LilWildCLexer(new ANTLRStringStream(src));
        LilWildCParser parser = new LilWildCParser(new CommonTokenStream(lexer));
        parser.program();
      }
    }
    
    bart@hades:~/Programming/ANTLR/Demos/LilWildC$ java -cp antlr-3.3.jar org.antlr.Tool LilWildC.g
    bart@hades:~/Programming/ANTLR/Demos/LilWildC$ javac -cp antlr-3.3.jar *.java
    bart@hades:~/Programming/ANTLR/Demos/LilWildC$ java -cp .:antlr-3.3.jar Main
    

    produces no errors or warnings.

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

Sidebar

Related Questions

Okay, I really know this has GOT to be the long way around doing
Okay, I am having a hard time with this. I've searched for the past
Okay, I feel a bit foolish for having to ask this but I guess
Okay I know right off the bat this is going to be a stupid
Okay, this should be really simple, but I have searched all over for the
Okay i have seen TouchXML, parseXML, NSXMLDocument, NSXMLParser but i am really confused with
Okay, Maybe this is by design of c# or maybe I am going about
Okay. So I'm creating this app and I'm really happy with it apart from
Okay, bear with me on this, I know it's going to look horribly convoluted,
Okay, first some background, I can't use any javascript library except YUI for this

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.