Closed ikeas68 closed 7 years ago
please simplify this example. I have no idea what's going on here. For example, get rid of the subclass. I need to see something that is specifically wrong with ANTLR.
i have find a solution adding protected boolean checkMnemonic(){ return false; } protected boolean checkPilot(){ return false; }
and change predicate for MNEMONIQUE : [A-Za-z.0-9_]+ { checkMnemonic() }? ; PILOT : [A-Za-z.0-9_]+ { checkPilot() }? ;
/**
* Define a grammar called TEST
*/
grammar Test;
@header
{
package x.calcul.antlr;
}
@lexer::members {
public static final int EXTRA = 1;
public static final int COMMENTS = 2;
protected boolean checkMnemonic(){ return false; }
protected boolean checkPilot(){ return false; }
}
compute
: expr EOF # line
;
// Whitespace
//NEWLINE : '\r\n' | 'r' | '\n';
WS : [\t ]+ -> channel(EXTRA);
A : 'abc';
B : 'de' ;
C : 'abcde' ;
MNEMONIQUE : [A-Za-z\.0-9_]+ { checkMnemonic() }? ;
PILOT : [A-Za-z\.0-9_]+ { checkPilot() }? ;
//SK : ~[' ' | ',' | '=' ]+ ;
SK : ~[ ]+ ;
expr : A '=' # fa
| A ',' B # fb
| C # fc
| MNEMONIQUE # mnemonic
| PILOT # pilot
;
and finaly change extended class
public class TestExtendLexer extends TestLexer {
public TestExtendLexer(final CharStream input) {
super(input);
}
List<String> mnemonics = new ArrayList<>();
public List<String> getMnemonics() {
return mnemonics;
}
List<String> pilots = new ArrayList<>();
public List<String> getPilots() {
return pilots;
}
//
@Override
protected boolean checkMnemonic() {
final String name = getText();
System.out.println(
String.format("MNEMONIC %-20.20s = %s", name, mnemonics.contains(name)));
return mnemonics.contains(name);
}
@Override
protected boolean checkPilot() {
final String name = getText();
System.out.println(
String.format("PILOTES %-20.20s = %s", name, pilots.contains(name)));
return pilots.contains(name);
}
}
and it's work now
with a simple grammar
and override class
and test
the token is not correctly reconized
sample :