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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T01:59:17+00:00 2026-05-26T01:59:17+00:00

In my grammar with antlrworks, I can get noviablealtexception for rules like if, while

  • 0

In my grammar with antlrworks, I can get noviablealtexception for rules like if, while which need corresponding right and left brackets. However, in java, i cannot get noviablealtexception.

loop_statement: (WHILE LPAREN expr RPAREN statement)
        | (DO statement WHILE LPAREN expr RPAREN);

condition_statement 
      : IF LPAREN expr RPAREN statement (options {greedy=true;}: ELSE statement)?

In statement rule I have a block rule which is,

statement_blocks
  :   (LBRACE statement* RBRACE) 
  ;

And statement rule is below,

statement
  :  var_dec
    | statement_blocks 
    | condition_statement  
    | loop_statement
    | expr_statement 
;

Before posting this I’ve checked some examples. I think i need to add EOF at the end of each rule. When I add EOF for those rules, I get different errors. For example,

loop_statement: ((WHILE LPAREN expr RPAREN statement)
    | (DO statement WHILE LPAREN expr RPAREN)) EOF;

condition_statement 
  : (
     (IF LPAREN expr RPAREN statement (options {greedy=true;}: ELSE statement)?
    )EOF

These are what I get for the following inputs;

if(s==d){
d=s;
if(a=n){
s=h;
}
a=g;
}

line 6:0 missing EOF at ‘a’
When I remove the first left bracket from the first “if”

if(s==d)
    d=s;
    if(a=n){
    s=h;
    }
    a=g;
    } 

testcases/new file line 3:0 missing EOF at ‘if’,
testcases/new file line 6:0 missing EOF at ‘a’

 while(s==d){
    d=s;
    while(a=n){
    s=h;
    }
    a=g;
    }

line 6:0 missing EOF at ‘a’
When I remove the first left bracket from the first “while”

 while(s==d)
    d=s;
    while(a=n){
    s=h;
    }
    a=g;
    }

testcases/new file line 3:0 missing EOF at ‘while’
testcases/new file line 6:0 missing EOF at ‘a’

  • 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-26T01:59:17+00:00Added an answer on May 26, 2026 at 1:59 am

    No, you need to place EOF at the end of your “main” parser rule, not after more than one statement. By doing so, the parser expects the end of the file after such statements (which is not correct, of course).

    My guess is that your entry point does not contain EOF causing the parser to stop prematurely instead of throwing an error/exception when it stumbles upon invalid input.

    Here’s a demo (note the EOF after the parse rule):

    T.g

    grammar T;
    
    parse
      :  statement+ EOF
      ;
    
    statement
      :  var_dec
      |  statement_blocks
      |  c=condition_statement {System.out.println("parsed :: " + $c.text);}
      ;
    
    var_dec
      :  ID '=' ID ';'
      ;
    
    statement_blocks
      :  LBRACE statement* RBRACE
      ;
    
    condition_statement 
      :  IF LPAREN expr RPAREN statement (options {greedy=true;}: ELSE statement)?
      ;
    
    expr
      :  ID '==' ID
      ;
    
    IF     : 'if';
    ELSE   : 'else';
    ID     : 'a'..'z'+;
    LBRACE : '{';
    RBRACE : '}';
    LPAREN : '(';
    RPAREN : ')';
    SPACE  : (' ' | '\t' | '\r' | '\n')+ {skip();};
    

    which can be tested with the class:

    Main.java

    import org.antlr.runtime.*;
    
    public class Main {
      public static void main(String[] args) throws Exception {
        TLexer lexer = new TLexer(new ANTLRFileStream("in.txt"));
        TParser parser = new TParser(new CommonTokenStream(lexer));
        parser.parse();
      }
    }
    

    Testing it all

    If you now parse the input file (in.txt):

    if(s==d) {
      d=s;
      if(a==n){
        s=h;
      }
      a=g;
    }
    

    there’s no problem, as you can see:

    java -cp antlr-3.3.jar org.antlr.Tool T.g
    javac -cp antlr-3.3.jar *.java
    java -cp .:antlr-3.3.jar Main
    
    parsed :: if(a==n){s=h;}
    parsed :: if(s==d){d=s;if(a==n){s=h;}a=g;}
    

    And if you remove a ( or ) from the file in.txt, you will get the following (similar) error:

    in.txt line 1:8 missing RPAREN at '{'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In Antlrworks I get this error: [18:21:03] Checking Grammar Grammar.g... [18:21:26] Grammar.java:12: code too
I have this simple grammar for a C# like syntax. I can't figure out
For a grammar like, how to preserver the order in which productions appear. class:
The grammar below parses ( left part = right part # comment ) ,
I have a grammar which parses dot notion expressions like this: a.b.c memberExpression returns
I have this grammar and need to modify it to allow parenthesis like: (-1)
Grammar: http://pastebin.com/ef2jt8Rg y.output: http://pastebin.com/AEKXrrRG I don't know where is those conflicts, someone can help
Here's the grammar, which is supposed to describe a language of nested braces with
Throughout a Bison grammar I am using right recursion, and I have read that
The ANSI C grammar from -link- give me the following rules for array declarations:

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.