Antlr输入不匹配'('期待'('例外

时间:2017-06-26 21:05:10

标签: antlr

我正在尝试为以下声明编写Antlr Grammer:

data (DROP=hello);

我的Antlr Grammer是:

DATA '(' DROP EQUAL 'hello' ')' ';'

不确定我在这里缺少什么。我的测试声明是:

Data File1(compress=char REUSE=YES);
  1. 主要语法文件:

    语法ParseSAS; 导入CommonGrammar,DataStmt;

       /*NOTE: When i execute my grammar on data_stmt it works fine but on Parse level it fails*/
    
        parse
         : (data_stmt
          )*EOF
        ; 
        /*** End of Parse Grammar
    
  2. 以下DataStmt语法是直接解析数据处理

    1. DataStatement Grammer文件:

          grammar DataStmt;
          import CommonGrammar;
      
          parse
           : (data_stmt)* EOF
           ;
      
          data_stmt
           : DATA ';'
           | DATA Tk_NULL datastmt_cmd? NOLIST? ';' 
           | DATA dataset_name_opt+ datastmt_cmd? NOLIST? ';'
           | DATA view_dsname_opt+ '/' VIEW '=' view_name passwd_opt? source_opt? NESTING? NOLIST? ';'
           | DATA dataset_name '/' PGM '=' program_name passwd_opt? source_opt? NESTING? NOLIST? ';'
           | DATA VIEW '=' view_name passwd_opt? NOLIST? ';'
           | DATA PGM '=' program_name passwd_opt? NOLIST? ';'
           ;
      
          dataset_name_opt
           : dataset_name ('(' variables '=' (~('('|')'))*? ')')?
           ;
      
          datastmt_cmd
           : '/' DEBUG? NESTING? (STACK '=' INT)?
           ;
      
           // view-name <data-set-name-1 <(data-set-options-1)>>
          view_dsname_opt
           : variables variables? ('(' variables '=' (~('('|')'))*? ')')?
           ;
      
          view_name : variables  ;
          dataset_name : variables;
          program_name : variables;
          passwd_opt  : '(' (ALTER|READ|PW) '=' (~('('|')'))*? ')' ;
          source_opt : ('(' SOURCE '=' (SAVE | ENCRYPT | NOSAVE) ')') ;
      
    2. 具有所有常用功能的通用语法文件:

          CommonGrammar.g4:
      
          grammar CommonGrammar;
      
          expression
           : literal
           | Identifier
           | DOT
           | OUTPUT Identifier?
           | '(' expression ')'
           | expression '(' expressionList? ')'
           | expression '{' expression '}'
           | expression '[' expression ']'
           | NOT expression
           | expression MIN expression
           | expression MAX expression
           | expression ('*'|'/'|'%') expression
           | expression ('+'|'-') expression
           | expression ('||' | '!!' ) expression
           | expression (EQ | NE | GT | LT | GE | LE | EQUAL ) expression
           | expression (EQC | NEC | GTC | LTC | GEC | LEC ) expression
           | expression (IN | INColon| NOT IN) in_var_list
           | expression (AND | OR ) expression
           | LIST
           ;
          expressionList 
           : (expression | of_var_list) (',' (expression|of_var_list))*
           | expressionList ','+ expressionList
           ;
      
          of_var_list 
           : 'of' (Identifier '-' Identifier) ((COMMA 'of')? (Identifier '-' Identifier) )*
           | 'of' Identifier Identifier*
           | 'of' Identifier '[' '*' ']'
           ;
          identifiers_list
           : (Identifier | Identifier '-' Identifier |Identifier ',' Identifier ','Identifier)+
           ;
      
          in_var_list
           : Identifier
           | '(' (literal|colonInts) (',' (literal|colonInts))* ')'
           ;
          colonInts
           : INT ':' INT
           ;
      
          literal
           : INT
           | FloatingPointLiteral
           | STRINGLITERAL
           | DateLiteral
           | TimeLiteral
           | DateTimeLiteral
           | BitLiteral
           | NameLiteral
           | HexLiteral
           | DOT
           ;
      
          variables
           : variables DOT variables
           | Identifier
           // place all the keywords here to resolve "keywords can also be identifiers" problem   
           ;
          /**
           * Tokens 
           * */
      
          // Drop
          DROP: D R O P;
          DATA: D A T A;
      
          Identifier  : [a-zA-Z_] ([a-zA-Z_0-9])*  ;
      
    3. 当我在DataStatement级别执行语法时,它工作文件,但是当我在Parse Level上执行它时,它说:

        

      ParseSAS :: parse:1:10:输入&#39;数据文件1没有可行的选择(&#39;

1 个答案:

答案 0 :(得分:0)

如果您同时拥有与此输入匹配的词法规则,并且在解析器规则中对同一输入使用字符串文字,则通常会出现此错误。不要这样做。始终在词法分析器规则中定义词法分析器输入,并且不要在解析器规则中使用字符串文字。

更新:在您上传语法后,我发现您的文字几乎没有词法规则。你当然应该重构你的语法并将所有文字转换成一个普通的词法分析器。