2010-11-10 6 views
0

J'utilise ANTLR pour remplacer un (petit) parseur existant que j'ai actuellement. Voici un extrait du fichier que je suis en train d'analyser:Pourquoi ANTLR ne reconnaît-il pas cette règle comme je le souhaite?

Lurker 915236167 10 2 Bk cc b  b   1000 70 200 Jc Qs 
Lurker 915236237 10 1 Bc kf -  -   1130 10 0 
Lurker 915236302 10 10 c c  rc b   1120 110 305 6d Kd 
Lurker 915236381 10 9 c f  -  -   1315 20 0 
Lurker 915236425 10 8 cc f  -  -   1295 30 0 

Voici Shared.g:

lexer grammar Shared; 


NICK 
    : LETTER (LETTER | NUMBER | SPECIAL)* 
    ; 

fragment 
LETTER 
    : 'A'..'Z' 
    | 'a'..'z' 
    | '_' 
    ; 

NUMBER 
    : ('0'|'1'|'2'|'3'|'4'|'5'|'6'|'7'|'8'|'9')+ 
    ; 

fragment 
SPECIAL 
    : ('-'|'^'|'{'|'}'|'|'|'['|']'|'`'|'\\') 
    ; 

WS 
    : (' ' 
     | '\t' 
     | '\r' 
     | '\n' 
     )+ 
    ; 

Et Pdb.g:

grammar Pdb; 
import Shared; 

@header{ 
import java.util.ArrayList; 
import java.sql.Connection; 
} 


@members{ 
    private Connection conn; 

    private StringBuilder currentExpr = new StringBuilder(500); 

    ArrayList<String> players = new ArrayList<String>(10); 

    public void setConn(Connection conn){ 
     this.conn = conn; 
    } 
} 

pdb 
    : line+ 
    ; 

line 
    @after{ 
     currentExpr.append("execute player_handplan("); 
     currentExpr.append($nick.text); 
     currentExpr.append(", to_timestamp("); 
     currentExpr.append(Integer.parseInt($timestamp.text)); 
     currentExpr.append("), "); 
     currentExpr.append(Integer.parseInt($n_players.text)); 
     currentExpr.append(", "); 
     currentExpr.append(Integer.parseInt($position.text)); 
     currentExpr.append(", "); 
     currentExpr.append($action_p.text); 
     currentExpr.append(", "); 
     currentExpr.append($action_f.text); 
     currentExpr.append(", "); 
     currentExpr.append($action_t.text); 
     currentExpr.append(", "); 
     currentExpr.append($action_r.text); 
     currentExpr.append(", "); 
     currentExpr.append(Integer.parseInt($bankroll.text)); 
     currentExpr.append(", "); 
     currentExpr.append(Integer.parseInt($total_action.text)); 
     currentExpr.append(", "); 
     currentExpr.append(Integer.parseInt($amount_won.text)); 
     currentExpr.append(", "); 
     currentExpr.append("CARDS"); 
     currentExpr.append(");"); 
     System.out.println(currentExpr.toString()); 
     currentExpr = new StringBuilder(500); 
    } 
    : nick=NICK WS 
     timestamp=NUMBER WS 
     n_players=NUMBER WS 
     position=NUMBER WS 
     action_p=action WS 
     action_f=action WS 
     action_t=action WS 
     action_r=action WS 
     bankroll=NUMBER WS 
     total_action=NUMBER WS 
     amount_won=NUMBER WS 
     (NICK WS NICK WS)? // ignore this 
    ; 

action 
    : '-' 
    | ('B'|'f'|'k'|'b'|'c'|'r'|'A'|'Q'|'K')+ 
    ; 

Mon problème est, quand Je cours l'analyseur, j'obtiens l'erreur suivante:

[email protected]:~/src/DecisionTrees/grammar/output$ cat example | java Test 
line 1:26 no viable alternative at input 'Bk' 
line 1:30 no viable alternative at input 'cc' 
execute player_handplan(Lurker, to_timestamp(915236167), 10, 2, null, null, b, b, 1000, 70, 200, CARDS); 

Pourquoi ma grammaire n'acceptera-t-elle pas "Bk", même si elle acceptera "b"? J'ai l'impression qu'il y a quelque chose d'évident que je néglige. Merci d'avance

+0

Essayez de changer d'entrée et trouver ce que d'autres solutions de rechange à l'analyseur résultant n'acceptera pas. –

+0

Il semble rejeter toutes les chaînes comportant plus d'un caractère à ce stade. "c" fonctionne, "cc" ne fonctionne pas, etc –

+0

Pourquoi "action" est-il défini dans l'analyseur et non dans le lexeur? –

Répondre

1

Pourquoi n'utilisez-vous pas {$channel=HIDDEN} dans la règle WS et laissez-les hors de la règle de ligne.

De cette façon au moins, vous n'aurez pas de problèmes pour en mettre un de trop WS par accident.

Et si l'action ne peut avoir 2 caractères max essayant peut-être cela vous aidera:

action 
    : '-' 
    | ('B'|'f'|'k'|'b'|'c'|'r'|'A'|'Q'|'K')('B'|'f'|'k'|'b'|'c'|'r'|'A'|'Q'|'K')? 
    ; 
Questions connexes