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

The Archive Base Latest Questions

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

Given the gramar grammar T; options { k=4; language=CSharp3; TokenLabelType=CommonToken; output=AST; ASTLabelType=CommonTree; } tokens

  • 0

Given the gramar

grammar T;

options
{
    k=4;
  language=CSharp3;     
  TokenLabelType=CommonToken; 
  output=AST;   
  ASTLabelType=CommonTree;  
}

tokens 
{
  LPAREN = '(';
  RPAREN = ')';
  LBRACK = '{';
  RBRACK = '}';
}

fragment
ID  :   ('a'..'z'|'A'..'Z'|'_')('a'..'z'|'A'..'Z'|'0'..'9'|'_')*;
WS : (' ' | '\t' | '\n' |'\r' )+ { $channel = Hidden; } ;

public program: CLASSDEF+ EOF!   ;

CLASSDEF: 'class' ID LBRACK
       RBRACK     ;

This produces a lexer and a parser which I use as follows

using System;
using Antlr.Runtime;
using Antlr.Runtime.Tree;

namespace compiler
{
  internal class Program2
  {
    public static void Main(string[]arg)
    {
      ANTLRStringStream Input = new ANTLRStringStream(@"class foo 
{ 
}");
      TLexer lex = new TLexer(Input);
      Console.WriteLine("errors:" + lex.NumberOfSyntaxErrors);
      CommonTokenStream tokens = new CommonTokenStream(lex);
      TParser parser = new TParser(tokens);

      var parsed = parser.program();
      Console.WriteLine("errors: " + parser.NumberOfSyntaxErrors);
      CommonTree tree = parsed.Tree;


      Console.WriteLine("type:" + tree.Type);
      Console.WriteLine("text:" + tree.Text);
      Console.WriteLine("children:" +tree.ChildCount);
      Console.WriteLine(tree.ToString());
      Console.WriteLine(tree.ToStringTree());

      Console.ReadKey();
    }
  }
}

When running this code I get 0 lex errors and 1 parse error

result

errors:0
errors: 1
type:0
text:{
}
children:0
<error: {
}>
<error: {
}>

Questions!

  1. I thought ANTLR was supposed to give intelligent error messages, yet I fail to find out whats wrong

  2. Am I missing code to improve on the error messages?

  • 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:52:02+00:00Added an answer on June 9, 2026 at 10:52 am

    You made CLASSDEF a lexer rule (in other words: a single token), which is incorrect. When the lexer stumbles upon input like "class X", it cannot create a CLASSDEF token because there’s a space between "class" and "X" (and no, the WS token will not help you with this since CLASSDEF is a lexer rule).

    In other words: make CLASSDEF a parser rule instead (and remove fragment from ID!):

    grammar T;
    
    options
    {
      language=CSharp3;     
      output=AST; 
    }
    
    tokens 
    {
      CLASS  = 'class';
      LPAREN = '(';
      RPAREN = ')';
      LBRACK = '{';
      RBRACK = '}';
    }
    
    public program
     : class_def+ EOF!
     ;
    
    class_def
     : CLASS ID LBRACK RBRACK
     ;
    
    ID
     : ('a'..'z'|'A'..'Z'|'_') ('a'..'z'|'A'..'Z'|'0'..'9'|'_')*
     ;
    
    WS
     : (' ' | '\t' | '\n' |'\r' )+ { $channel = Hidden; } 
     ;
    

    Now parsing input like "class foo { }" will produce the following parse:

    enter image description here

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

Sidebar

Related Questions

Is there any specific methodology followed to specify a language for given grammar ??
I'm using ANTLR and the following grammar: grammar QuickBasic; options { language = 'CSharp2';
---> Consider the grammar below: S->SaS|bB B->AcB| ε A->dAd| ε For the grammar given
I am creating a program that will fill in a given grammar. Right now
Given a simple Parsekit grammar. @start = sentence+; sentence = 'beer' container; container =
Are there any APIs that check the grammar of a given string? If so
I am writhing a simple language with antlr, I defined a Lexer grammar in
I have a Lua grammar , (minor modifications to get it to output for
I am trying to implement Error Reporting and Recovery in JavaCC grammar as given
Given an ANTLR Java Grammar - what java source code would I write to

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.