0

Are character classes supported in ANTLR 4 lexers? I saw some examples that looked like this is OK:

LITERAL: [a-zA-z]+;

but what I found is that it matches the string "OR[" with the opening bracket. Using ranges worked:

LITERAL: ('a'..'z' | 'A'..'Z')+;

and only identified "OR" as the LITERAL. Here is an example:

grammar Test;

@members {
  private void log(String msg) {
    System.out.println(msg);
  }
}

parse
 : expr EOF
 ;

expr
 : atom                   {log("atom(" + $atom.text + ")");}
 | l=expr OR r=expr       {log("IOR:left(" + $l.text + ") right(" + $r.text + "}");}
 | (OR '[' la=atom ra=atom ']') {log("POR:left(" + $la.text + ") right(" + $ra.text + "}");}
 ;

atom
 : LITERAL
 ;

OR      : O R ;

LITERAL: [a-zA-z]+;
//LITERAL: ('a'..'z' | 'A'..'Z')+;

SPACE
 : [ \t\r\n] -> skip
 ;

fragment O: ('o'|'O');
fragment R: ('r'|'R');

When given the input "OR [ cat dog ]" it parses correctly, but "OR[ cat dog ]" does not.

4

1 回答 1

1

您可以在 ANTLR 4 词法分析器中使用字符集,但范围区分大小写。你用[a-zA-z]了我相信你的意思[a-zA-Z]

于 2013-09-21T16:37:49.993 回答