5

文法について少し質問があります。次のように文字列を解析したい:

 "(ICOM LIKE '%bridge%' or ICOM LIKE '%Munich%')"

私は次の文法になりました(私が知っている必要よりも少し複雑です):

//完全なBQS形式のクエリの解析を目指します

grammar Logic;

options {
    output=AST;
}

tokens {
  NOT_LIKE;
}

/*------------------------------------------------------------------
 * PARSER RULES
 *------------------------------------------------------------------*/
 // precedence order is (low to high): or, and, not, [comp_op, geo_op, rel_geo_op, like, not like, exists], ()
 parse  
    : expression EOF -> expression
    ; // ommit the EOF token

 expression
    : query
    ;       

 query  
    : term (OR^ term)*    // make `or` the root
    ;

 term   
    : factor (AND^ factor)*
    ;

 factor
  :  (notexp -> notexp) ( NOT LIKE e=notexp  -> ^(NOT_LIKE $factor $e))?
  ;

 notexp
  :  NOT^ like
  |  like
  ;

 like // this one has to be completed (a lot)
    : atom (LIKE^ atom)*
    ;


 atom   
    : ID 
    | | '(' expression ')' -> expression
    ;

/*------------------------------------------------------------------
 * LEXER RULES
 *------------------------------------------------------------------*/
// GENERAL OPERATORS: 
//NOTLIKE   :   'notlike' | 'NOTLIKE'; // whitespaces have been removed
LIKE    :   'like' | 'LIKE';

OR          :   'or' | 'OR';
AND         :   'and' | 'AND';
NOT         :   'not' | 'NOT';

//ELEMENTS 
CONSTANT_EXPRESSION : DATE | NUMBER | QUOTED_STRING;    
ID          :   (CHARACTER|DIGIT)+; 

WHITESPACE : ( '\t' | ' ' | '\r' | '\n'| '\u000C' )+    { $channel = HIDDEN; } ;

fragment DATE       :   '\'' YEAR '/' MONTH '/' DAY (' ' HOUR ':' MINUTE ':' SECOND)? '\'';

fragment QUOTED_STRING :    '\'' (CHARACTER)+ '\'' ; 

//UNITS
fragment CHARACTER :    ('a'..'z' | 'A'..'Z'|'.'|'\''|'%'); // FIXME: Careful, should be all ASCII
fragment DIGIT  :   '0'..'9' ;
fragment DIGIT_SEQ  :(DIGIT)+;
fragment DEL    :   SPACE ',' SPACE ; //Delimiter + may be space behind
fragment NUMBER :   (SIGN)? DIGIT_SEQ ('.' (DIGIT_SEQ)?)?; // should be given in decimal degrees, North is 0 and direction is clockwise, range is 0 to 360
fragment SIGN   :   '+' | '-';
fragment YEAR   :   DIGIT DIGIT DIGIT DIGIT;
fragment MONTH  :   DIGIT DIGIT;
fragment DAY    :   DIGIT DIGIT;
fragment HOUR   :   DIGIT DIGIT;
fragment MINUTE :   DIGIT DIGIT;
fragment SECOND :   DIGIT (DIGIT)? ('.' (DIGIT)+)?;

fragment SPACE : (' ')?;// used to increase compatibility

ASTを作成するときにこのメッセージが表示されます。

line 1:11 no viable alternative at input ''%bridge%''
line 1:35 no viable alternative at input ''%Munich%''

生成されたツリーは正しいですが(少なくとも私に関する限り):

antlr実行可能なastツリー

だから、誰かが私にそこの何が悪いのかについてのヒントを与えることができますか?文字には、この式を正しく解析するために必要なすべての余分な文字が含まれていると思います。。。

ありがとう !

いつものように、文法をすばやくテストするためのJavaコード:

import org.antlr.runtime.*;
import org.antlr.runtime.tree.*;
import org.antlr.stringtemplate.*;

public class Main {
  public static void main(String[] args) throws Exception {

    // the expression
    String src = "(ICOM LIKE '%bridge%' or ICOM LIKE '%Munich%')";

    // create a lexer & parser
    //LogicLexer lexer = new LogicLexer(new ANTLRStringStream(src));
    //LogicParser parser = new LogicParser(new CommonTokenStream(lexer));

    LogicLexer lexer = new LogicLexer(new ANTLRStringStream(src));
    LogicParser parser = new LogicParser(new CommonTokenStream(lexer));

    // invoke the entry point of the parser (the parse() method) and get the AST
    CommonTree tree = (CommonTree)parser.parse().getTree();

    // print the DOT representation of the AST 
    DOTTreeGenerator gen = new DOTTreeGenerator();
    StringTemplate st = gen.toDOT(tree);
    System.out.println(st);
  }
}
4

1 に答える 1

7

3つの問題があります。

1

atomルールがイプシロンに一致します(なし):

atom   
 : ID 
 | | '(' expression ')' -> expression
 ;

(内部の「無」に注意してください| |

文法が曖昧になります。私はそれがすべきだと思います:

atom   
 : ID 
 | '(' expression ')' -> expression
 ;

2

あなたfragment CHARACTERは一重引用符に一致しますが、この一重引用符はの終わりも示しますfragment QUOTED_STRING

私はCHARACTER代わりにこれであるべきだと思います:

fragment CHARACTER : ('a'..'z' | 'A'..'Z' | '.' | '%'); 

3

パーサールールのどこにもトークンと一致しCONSTANT_EXPRESSIONないため、投稿したASTは、投稿した文法から生成されたパーサーによって作成された可能性はありません。atom私はあなたがこのようなルールでそれを一致させたいと思うと思います:

atom   
 : ID 
 | CONSTANT_EXPRESSION
 | '(' expression ')' -> expression
 ;

上記の変更により、コンソールにエラーが出力されることなく、次のASTが得られます。

ここに画像の説明を入力してください

于 2012-05-16T18:07:00.283 に答える