ANTLR4语法规则中的内部错误

时间:2016-11-08 13:40:00

标签: antlr4

我写了自己的语法:

grammar SimpleCode;

program: 'class Program' '{' field_decl* method_decl* '}' ;

field_decl: type id_int_list ;

id_int: id
      | id '[' int_literal ']'
      ;

id_int_list: id_int
           | id_int (',' id_int)*
           ;

method_decl: (type | 'void') id id_type_list? block ;

id_type_list: (type id)
            | (type id) (','(type id))*
            ;

block: '{' var_decl* statement* '}' ;

var_decl: type id_list ;

id_list: id
       | id (',' id)*
       ;

type: 'int'
    | 'boolean'
    ;

statement: location assign_op expr ';'
         | method_call ';'
         | 'if' expr block 'else' block
         | 'for' id '=' expr ',' expr block
         | 'return' expr? ';'
         | 'break' ';'
         | 'continue' ';'
         | block
         ;

assign_op: '='
         | '+='
         | '-='
         ;

method_call: method_name expr_list?
           | 'callout' (string_literal (',' callout_arg_list)?)
           ;

expr_list: expr
         | expr (',' expr)*
         ;

callout_arg_list: callout_arg
                | callout_arg (',' callout_arg)*
                ;

method_name: id ;

location: id
        | id '[' expr ']'
        ;

expr: location
    | method_call
    | literal
    | expr bin_op expr
    | '-' expr
    | '!' expr
    | '(' expr ')'
    ;

callout_arg: expr
           | string_literal
           ;

bin_op: arith_op
      | rel_op
      | eq_op
      | cond_op
      ;

arith_op: '+'
        | '-'
        | '*'
        | '\\'
        | '%'
        ;

rel_op: '>'
      | '<'
      | '>='
      | '<='
      ;

eq_op: '=='
     | '!='
     ;

cond_op: '&&'
       | '||'
       ;

literal: int_literal
       | char_literal
       | bool_literal
       ;

id: alpha alpha_num* ;

alpha_num: alpha
         | digit
         ;

alpha: ( 'a'..'z' | 'A'..'Z' ) ;

digit: '0'..'9' ;

hex_digit: digit
         | 'a'..'f'
         | 'A'..'F'
         ;

int_literal: decimal_literal
           | hex_literal
           ;

decimal_literal: digit digit* ;

hex_literal: '0x' hex_digit hex_digit* ;

bool_literal: 'true'
            | 'false'
            ;

char_literal: '\'' char '\'' ;


string_literal: '\"' char* '\"' ;

WS: [ \t\r\n]+ ->skip ;

我收到了这个错误:

error(20): SimpleCode.g4:8:12: internal error: Rule int_literal undefined

我不知道为什么会发生这种错误。 int_literal已定义。请向我解释为什么会发生此错误。我没有找到原因。

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

我查看了您的代码,主要问题是您没有从Lexer规则中分离出Parser规则。这是使用Capitals / Case sensitivity完成的。 Lexer规则以大写字母定义。一旦这被纠正,你的语法还有一个错误,因为规则&#34; char&#34;未定义。

这是一个修正版本:

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

grammar SimpleCode;


program: 'class Program' '{' field_decl* method_decl* '}' ;

field_decl: type id_int_list ;

id_int: id
      | id '[' int_literal ']'
      ;

id_int_list: id_int
           | id_int (',' id_int)*
           ;

method_decl: (type | 'void') id id_type_list? block ;

id_type_list: (type id)
            | (type id) (','(type id))*
            ;

block: '{' var_decl* statement* '}' ;

var_decl: type id_list ;

id_list: id
       | id (',' id)*
       ;

type: 'int'
    | 'boolean'
    ;

statement: location assign_op expr ';'
         | method_call ';'
         | 'if' expr block 'else' block
         | 'for' id '=' expr ',' expr block
         | 'return' expr? ';'
         | 'break' ';'
         | 'continue' ';'
         | block
         ;

assign_op: '='
         | '+='
         | '-='
         ;

method_call: method_name expr_list?
           | 'callout' (string_literal (',' callout_arg_list)?)
           ;

expr_list: expr
         | expr (',' expr)*
         ;

callout_arg_list: callout_arg
                | callout_arg (',' callout_arg)*
                ;

method_name: id ;

location: id
        | id '[' expr ']'
        ;

expr: location
    | method_call
    | literal
    | expr bin_op expr
    | '-' expr
    | '!' expr
    | '(' expr ')'
    ;

callout_arg: expr
           | string_literal
           ;

bin_op: arith_op
      | rel_op
      | eq_op
      | cond_op
      ;

arith_op: '+'
        | '-'
        | '*'
        | '\\'
        | '%'
        ;

rel_op: '>'
      | '<'
      | '>='
      | '<='
      ;

eq_op: '=='
     | '!='
     ;

cond_op: '&&'
       | '||'
       ;

literal: int_literal
       | char_literal
       | bool_literal
       ;

id: ALPHA alpha_num* ;

alpha_num: ALPHA
         | DIGIT
         ;

ALPHA: ( 'a'..'z' | 'A'..'Z' ) ;

DIGIT: '0'..'9' ;

HEX_DIGIT: DIGIT
         | 'a'..'f'
         | 'A'..'F'
         ;

int_literal: decimal_literal
           | hex_literal
           ;

decimal_literal: DIGIT DIGIT* ;

hex_literal: '0x' HEX_DIGIT HEX_DIGIT* ;

bool_literal: 'true'
            | 'false'
            ;

char_literal: '\'' ALPHA '\'' ;


string_literal: '\"' ALPHA* '\"' ;

WS: [ \t\r\n]+ ->skip ;

词法分析器规则用于创建令牌流和语义解析器,从而区分哪些规则在组合语法中很重要。

this论坛上有一篇很好的文章,其中详细说明了差异及其所需的原因。但除了缺少规则和缺乏词法分析器定义之外,这一切都很好。记住INT:DIGIT +; //词法分类中的词法规则。

祝你的项目好运!

相关问题