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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T00:32:02+00:00 2026-05-30T00:32:02+00:00

I am writhing a simple language with antlr, I defined a Lexer grammar in

  • 0

I am writhing a simple language with antlr, I defined a Lexer grammar in AntlrWorks, but when I want to generate the java code, it gives me the error:

Antlr error : the following token definition can never be matched because prior tokens match the same input:
FLOAT_OR_INT, OPEN_PAR, CLOSE_PAR, …. (almost for all the rules!)

I am new to antlr, I assume it is because of the order of rule locations, but I don’t know how should they have to be, what is my mistake?

here is the grammar:

lexer grammar OurCompiler;
options
{
    k=5;

} 


ID                      : ('a'..'z'|'A'..'Z'|'_') ('a'..'z'|'A'..'Z'|'0'..'9'|'_')*
                        ;



protected
INT                     : ('0'..'9')+
                        ;

protected
FLOAT                   : INT '.' INT
                        ;


FLOAT_OR_INT            : ( INT '.' ) => FLOAT { $setType(FLOAT); }
                        | INT { $setType(INT); }
                        ;       


OPENPAR_OR_OUTPUT_OPERATOR  :   '(' { $setType(OPEN_PAR); } | '(' '(' { $setType(OUTPUT_OPERATOR); }
                            ;

CLOSEPAR_OR_INPUT_OPERATOR  :   ')' { $setType(CLOSE_PAR); } | ')' ')' { $setType(INPUT_OPERATOR); }
                            ;

protected   
OPEN_PAR                :  '('  ;

protected
CLOSE_PAR               :  ')'  ;

protected
INPUT_OPERATOR          :   ')' ')' ;

protected
OUTPUT_OPERATOR         :   '(' '(' ;



BOOLEAN                 :   't' 'r' 'u' 'e' | 'f' 'a' 'l' 's' 'e'   ;    

LOWER                   :   '<'     ;

LOWER_EQUAL             :   LOWER '='   ;

UPPER                   :  '>'  ;

UPPER_EQUAL             :    UPPER '='  ;

ASSIGN                  : '='  ;

EQUAL                   :  '=' '='  ;

NOT                     :   '!'  ;

NOT_EQUAL               :  NOT '='  ;

ADD                     :   '+'  ;

ADD_TO_PREVIOUS         :  ADD '='  ;

INCREMENT               :   ADD ADD  ;

MINUS                   : '-'  ;

MINUS_FROM_PREVIOUS     :  MINUS '='  ;

DECREMENT               :  MINUS MINUS  ;

MULTIPLY                :  '*'  ;

MULTIPLY_TO_PREVIOUS    :  MULTIPLY '='  ;

DIVIDE                  :  '/'  ;

DIVIDE_FROM_PREVIOUS    :  DIVIDE '='  ;

MODE                    :  '%'  ;

OPEN_BRAKET             :  '['  ;

CLOSE_BRAKET            :  ']'  ;

OPEN_BRACE              :  '{'  ;

CLOSE_BRACE             :  '}'  ;

COLON                   : ':'   ;

SEMICOLON               :  ';'  ;

COMMA                   :  ','  ;


SINGLE_LINE_COMMENT     : 
                        '#' '#' ( ~ ('\n'|'\r') )*  ( '\n' | '\r' ('\n')? )? { $setType(Token.SKIP); newline(); }
                        ; 


MULTIPLE_LINE_COMMENT   :   '#' ( options {greedy=false;} : . )* '#' { $setType(Token.SKIP); }
                        ;



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


protected
ESC_SEQ                 :   '\\' ('b'|'t'|'n'|'f'|'r'|'\"'|'\''|'\\')
                    ;

STRING                  :  
                        '"' ( ESC_SEQ | ~('\\'|'"') )* '"'
                        ;

CHAR                    :
                        '\'' ( ESC_SEQ | ~('\''|'\\') ) '\''
                        ;





INT_KEYWORD             :   'i' 'n' 't'         ;

FLOAT_KEYWORD           :   'f' 'l' 'o' 'a' 't'     ;

CHAR_KEYWORD            :   'c' 'h' 'a' 'r'         ;

STRING_KEYWORD          :   's' 't' 'r' 'i' 'n' 'g'     ;

BOOLEAN_KEYWORD         :   'b' 'o' 'o' 'l' 'e' 'a' 'n'     ;

INPUT_KEYWORD           :   'i' 'n' ID  { $setType(ID); }
                        |   'i' 'n'     
                        ;

OUTPUT_KEYWORD          :   'o' 'u' 't' ID  { $setType(ID); }
                        |   'o' 'u' 't'     ;

IF_KEYWORD              :   'i' 'f'             ;

FOR_KEYWORD             :   'f' 'o' 'r'         ;

SWITCH_KEYWORD          :   's' 'w' 'i' 't' 'c' 'h'     ;

CASE_KEYWORD            :   'c' 'a' 's' 'e'         ;

BREAK_KEYWORD           :   'b' 'r' 'e' 'a' 'k'     ;

DEFAULT_KEYWORD         :   'd' 'e' 'f' 'a' 'u' 'l' 't' ;

WHILE_KEYWORD           :   'w' 'h' 'i' 'l' 'e'     ;

ELSE_KEYWORD            :   'e' 'l' 's' 'e'         ;

ELSEIF_KEYWORD          :   'e' 'l' 's' 'e' 'i' 'f'     ;

AND_KEYWORD             :   'a' 'n' 'd'         ;

OR_KEYWORD              :   'o' 'r'             ;

NOT_KEYWORD             :   'n' 'o' 't'         ;

CONSTANT_KEYWORD        :   'c' 'o' 'n' 's' 't' 'a' 'n' 't' ;
  • 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-30T00:32:03+00:00Added an answer on May 30, 2026 at 12:32 am

    I have 7 remarks about your grammar after glancing over it:

    1

    k=? denotes the look-ahead for parser rules and since yours is a lexer grammar, remove it;

    2

    Although not wrong, BOOLEAN_KEYWORD : 'b' 'o' 'o' 'l' 'e' 'a' 'n'; is rather verbose. Do BOOLEAN_KEYWORD : 'boolean'; instead.

    3

    The keyword protected has changed in ANTLR 3 to fragment. But you’re doing odd things. Take the following rules:

    fragment
    INT 
     : ('0'..'9')+
     ;
    
    fragment
    FLOAT 
     : INT '.' INT
     ;
    
    FLOAT_OR_INT 
     : ( INT '.' ) => FLOAT { $setType(FLOAT); }
     | INT { $setType(INT); }
     ;
    

    You create two fragments, and then have FLOAT_OR_INT check through a predicate if it “sees” an INT followed by a '.' and then change it into a FLOAT. The following does the same and is far more readable/better/preferred:

    FLOAT 
     : DIGIT+ '.' DIGIT+
     ;
    
    INT 
     : DIGIT+
     ;
    
    fragment DIGIT 
     : '0'..'9'
     ;
    

    4

    .* is ungreedy by default, so change:

    '#' ( options {greedy=false;} : . )* '#'
    

    into

    '#' .* '#'
    

    or even better:

    '#' ~'#'+ '#'
    

    5

    The rule:

    OPENPAR_OR_OUTPUT_OPERATOR
     : '('     { $setType(OPEN_PAR); } 
     | '(' '(' { $setType(OUTPUT_OPERATOR); }
     ;
    

    should simply be:

    OUTPUT_OPERATOR
     : '(('
     ;
    
    OPEN_PAR
     : '('
     ;
    

    6

    ANTLR’s lexer tries to match as much characters as possible. Whenever two rules match the same amount of characters, the rule defined firs will “win”. That is why you should define all your *_KEYWORD rules before the ID rule.

    7

    Lastly, you don’t need to check if "in" or "out" is followed by an ID (and then change the type of the token). Whenever the lexer “sees” input like "inside", it will always create a single ID token, and not an INPUT_KEYWORD followed by an ID, since the lexer matches as much as possible (see remark #6).


    It appears you’re trying to learn ANTLR by trial and error, or are using out-dated documentation. This is not the way to learn ANTLR. Try to get a hold of Parr’s The Definitive ANTLR Reference to learn it properly.

    Good luck!

    EDIT

    Well, in case you don’t manage to get it working, here’s a working version of your grammar:

    lexer grammar OurCompiler; // A bit of an odd name for a lexer...
    
    K_INT      : 'int';
    K_FLOAT    : 'float';
    K_CHAR     : 'char';
    K_STRING   : 'string';
    K_BOOLEAN  : 'boolean';
    K_INPUT    : 'in';
    K_OUTPUT   : 'out';
    K_IF       : 'if';
    K_FOR      : 'for';
    K_SWITCH   : 'switch';
    K_CASE     : 'case';
    K_BREAK    : 'break';
    K_DEFAULT  : 'default';
    K_WHILE    : 'while';
    K_ELSE     : 'else';
    K_ELSEIF   : 'elseif';
    K_AND      : 'and';
    K_OR       : 'or';
    K_NOT      : 'not';
    K_CONSTANT : 'constant';
    
    BOOLEAN : 'true' | 'false';
    FLOAT   : DIGIT+ '.' DIGIT+; 
    INT     : DIGIT+;
    STRING  : '"'  ( ESC_SEQ | ~('\\'|'"') )* '"';
    CHAR    : '\'' ( ESC_SEQ | ~('\''|'\\') ) '\'';
    
    ID : ('a'..'z'|'A'..'Z'|'_') ('a'..'z'|'A'..'Z'|'0'..'9'|'_')*;
    
    INPUT_OPERATOR  : '))';
    OUTPUT_OPERATOR : '((';
    OPEN_PAR        : '(';
    CLOSE_PAR       : ')';
    
    LOWER                : '<';
    LOWER_EQUAL          : '<=';
    UPPER                : '>';
    UPPER_EQUAL          : '>=';
    ASSIGN               : '=';
    EQUAL                : '==';
    NOT                  : '!';
    NOT_EQUAL            : '!=';
    ADD                  : '+';
    ADD_TO_PREVIOUS      : '+=';
    INCREMENT            : '++';
    MINUS                : '-';
    MINUS_FROM_PREVIOUS  : '-=';
    DECREMENT            : '--';
    MULTIPLY             : '*';
    MULTIPLY_TO_PREVIOUS : '*=';
    DIVIDE               : '/';
    DIVIDE_FROM_PREVIOUS : '/=';
    MODE                 : '%';
    OPEN_BRAKET          : '[';
    CLOSE_BRAKET         : ']';
    OPEN_BRACE           : '{';
    CLOSE_BRACE          : '}';
    COLON                : ':';
    SEMICOLON            : ';';
    COMMA                : ',';
    
    SINGLE_LINE_COMMENT   : '##' ~('\r' | '\n')*        {skip();}; 
    MULTIPLE_LINE_COMMENT : '#' ~'#'+ '#'               {skip();};
    WS                    : ( ' ' | '\t' | '\r' | '\n') {skip();};
    
    fragment ESC_SEQ : '\\' ('b' | 't' | 'n' | 'f' | 'r' | '\"' | '\'' | '\\');
    fragment DIGIT   : '0'..'9';
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We want to design a simple domain specific language for writing test scripts to
I'm writing a simple scripting language on top of Java/JVM, where you can also
I have made a simple experimental language of my own. I want make Eclipse
I'm writing a compiler for a simple imperative language in Haskell, outputting Java bytecode.
I'm trying to write a simple interactive (using System.in as source) language using antlr,
I'm writing a simple piece of code in ruby, but it's not working the
I'm writing an interpreter in Python for a very simple language grammar. I have
I'm writing a small interpreter for a simple BASIC like language as an exercise
I'm looking into writing simple graphics code in Android and I've noticed some synchronized()
I'm writing simple filter in Android and want to use ExpandableListAdapter with check boxes.

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.