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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T16:06:19+00:00 2026-06-03T16:06:19+00:00

Below is a cut down version of a grammar that is parsing an input

  • 0

Below is a cut down version of a grammar that is parsing an input assembly file. Everything in my grammar is fine until i use labels that have 3 characters (i.e. same length as an OPCODE in my grammar), so I’m assuming Antlr is matching it as an OPCODE rather than a LABEL, but how do I say “in this position, it should be a LABEL, not an OPCODE”?

Trial input:

set a, label1
set b, abc

Output from a standard rig gives:

line 2:5 missing EOF at ','
(OP_BAS set a (REF label1)) (OP_SPE set b)

When I step debug through ANTLRWorks, I see it start down instruction rule 2, but at the reference to “abc” jumps to rule 3 and then fail at the “,”.

I can solve this with massive left factoring, but it makes the grammar incredibly unreadable. I’m trying to find a compromise (there isn’t so much input that the global backtrack is a hit on performance) between readability and functionality.

grammar TestLabel;

options {
    language = Java;
    output = AST;
    ASTLabelType = CommonTree;
    backtrack = true;
}

tokens {
    NEGATION;
    OP_BAS;
    OP_SPE;
    OP_CMD;
    REF;
    DEF;
}

program
    : instruction* EOF!
    ;

instruction
    : LABELDEF                  -> ^(DEF LABELDEF)
    | OPCODE dst_op ',' src_op  -> ^(OP_BAS OPCODE dst_op src_op)
    | OPCODE src_op             -> ^(OP_SPE OPCODE src_op)
    | OPCODE                    -> ^(OP_CMD OPCODE)
    ;

operand
    : REG
    | LABEL                     -> ^(REF LABEL)
    | expr
    ;

dst_op
    : PUSH
    | operand
    ;

src_op
    : POP
    | operand
    ;

term
    : '('! expr ')'!
    | literal
    ;

unary
    : ('+'! | negation^ )* term
    ;

negation
    : '-' -> NEGATION
    ;

mult
    : unary ( ( '*'^ | '/'^ ) unary )*
    ;

expr
    :  mult ( ( '+'^ | '-'^ ) mult )*
    ;

literal
    :   number
    |   CHAR
    ;

number
    :   HEX
    |   BIN
    |   DECIMAL
    ;

REG: ('A'..'C'|'I'..'J'|'X'..'Z'|'a'..'c'|'i'..'j'|'x'..'z') ;
OPCODE: LETTER LETTER LETTER;

HEX: '0x' ( 'a'..'f' | 'A'..'F' | DIGIT )+ ;
BIN: '0b' ('0'|'1')+;
DECIMAL: DIGIT+ ;

LABEL: ( '.' | LETTER | DIGIT | '_' )+ ;
LABELDEF: ':' ( '.' | LETTER | DIGIT | '_' )+ {setText(getText().substring(1));} ;

STRING: '\"' .* '\"' {setText(getText().substring(1, getText().length()-1));} ;
CHAR: '\'' . '\'' {setText(getText().substring(1, 2));} ;
WS: (' ' | '\n' | '\r' | '\t' | '\f')+ { $channel = HIDDEN; } ;

fragment LETTER: ('a'..'z'|'A'..'Z') ;
fragment DIGIT: '0'..'9' ;
fragment PUSH: ('P'|'p')('U'|'u')('S'|'s')('H'|'h');
fragment POP: ('P'|'p')('O'|'o')('P'|'p');
  • 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-03T16:06:21+00:00Added an answer on June 3, 2026 at 4:06 pm

    The parser has no influence on what tokens the lexer produces. So, the input "abc" will always be tokenized as a OPCODE, no matter what the parser tries to match.

    What you can do is create a label parser rules that matches either a LABEL or OPCODE and then use this label rule in your operand rule:

    label
     : LABEL
     | OPCODE
     ;
    
    operand
     : REG
     | label -> ^(REF label)
     | expr
     ;
    

    resulting in the following AST for your example input:

    enter image description here

    This will only match OPCODE, but will not change the type of the token. If you want the type to be changed as well, add a bit of custom code to the rule that changes it to type LABEL:

    label
     : LABEL
     | t=OPCODE {$t.setType(LABEL);}
     ;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Below is a heavily cut down version of some code I have public class
Below is a cut down example of some c# code I can't get to
A cut down version of my code sort of looks like this: string=test test
I have a file name foo. That file contains some text (shown below). Can
Below is a file iterarot unction I have working on that iterates over folders
i am testing with the shell script below: #!/bin/ksh -x instance=`echo $1 | cut
Below is code to an inherited ComboBox. The issue is that the ComboBox is
below is the code to download a txt file from internet approx 9000 lines
Below is my main activity file. I am stuck with the bottom area (commented
Below is the code from a plugin I use for sitemaps. I would like

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.