简体   繁体   中英

Fixed number format in ANTLR

How to specify a fixed digit number in antlr grammar?

I want to parse a line which contains fields of fixed number of characters. Each field is a number.

0034|9056|4567|0987|-2340| +345|1000

The above line is a sample line. | indicates field boundaries (which will not be in the actual file. shown here just to indicate the boundary).

The fields can include blank characters +/-

I'd keep the lexer grammar as simple as possible and just match zero or more spaces followed by an optional sign followed by a number in your parser grammar. After matching that, check (in your parser grammar) if the "width" of the field is correct.

An example grammar:

line
  :  field ('|' field)*
  ;

field
  :  Spaces? ('+' | '-')? Number // validate if 'field' is correct in this rule  
  ;

Number
  :  '0'..'9'+
  ;

Spaces
  :  ' '+
  ;

And a possible validation scheme could look like:

line
  :  field ('|' field)*
  ;

field
@init{int length = 0;}
  :  (Spaces {length += $Spaces.text.length();})? 
     ('+' | '-')? Number {length += $Number.text.length(); if(length != 4) {/* do something */}}
  ;

Number
  :  '0'..'9'+
  ;

Spaces
  :  ' '+
  ;

关于以下内容:

INT : ('+'|'-')? ('0'..'9')+;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM