Java 中使用 antlr4 的解析器

问题描述 投票:0回答:1

我想用 Java 和 ANTRL4 来解析它:

; comment
[WORLD]

[game] ; is a chilf of WORLD
Name=The Game ; needs a string

[/game] ; end of game

[/WORLD] ; end of WORLD

grammar GameData;

// Whitespace and comments (ignored)
WS  : [ \t\r\n]+ -> skip;
COMMENT     : ';' .*? ('\r'? '\n' | EOF) -> skip;

WORLD   : '[Bb] [Dd] [Cc] [Ff] [Ff]';
GAME    : '[Gg] [Aa] [Mm] [Ee]';


// Header and closing
header     : '[' WORLD ']' ;
closing    : '[' '/' WORLD ']' ;

// Section (game in this case)
game_section    : '[' GAME ']' ( keyValuePair )* ;

// Key-value pair
keyValuePair : ID '=' value ';' ;

// Value (string in this case)
value       : STRING ;

// Terminals
DOT      : '.';
ID       : [a-zA-Z]+;
STRING    : '"' .*? '"' ;

file        : (header|game_section)*;

但我不知道名字。

这是我的Java代码:

import org.antlr.v4.runtime.*;
import org.antlr.v4.runtime.tree.*;
import parser.*;
public class Main {
    public static void main(String[] args) throws Exception {
        // Create an input stream from your GameData file

        CharStream input = CharStreams.fromFileName("test.bd");

        // Create a lexer that feeds off of input CharStream

        GameDataLexer lexer = new GameDataLexer(input);
        // Create a buffer of tokens pulled from the lexer
        CommonTokenStream tokens = new CommonTokenStream(lexer);

        // Create a parser that feeds off the tokens buffer
        GameDataParser parser = new GameDataParser(tokens);
        // Start parsing at the 'file' rule
        ParseTree tree = parser.file();

        System.out.println("getChildCount "+tree.getChildCount());
        for(int i=0;i<tree.getChildCount();i++) 
            System.out.println("getChild "+tree.getChild(i).toString());
        System.out.println(tree.toStringTree(parser));
        
    }

这是输出: 第 2:1 行在输入“[WORLD”处没有可行的替代方案 获取子项计数 18 获取子项[ 获取儿童世界 获取子对象] 获取子项[ 获取孩子游戏 获取子对象] 获取孩子的名字 获取子对象 = 获取子对象 获取孩子游戏 获取子项[ 获取孩子/ 获取孩子游戏 获取子对象] 获取子项[ 获取孩子/ 获取儿童世界 获取子对象] (文件[世界][游戏]名称=游戏[/游戏][/世界])

java parsing antlr4
1个回答
0
投票
WORLD   : '[Bb] [Dd] [Cc] [Ff] [Ff]';
GAME    : '[Gg] [Aa] [Mm] [Ee]';

这是不正确的:它应该不带引号,并且

WORLD
应该使用正确的字符:

WORLD   : [Ww] [Oo] [Rr] [Ll] [Dd];
GAME    : [Gg] [Aa] [Mm] [Ee];

查看您的规则:

// Key-value pair
keyValuePair : ID '=' value ';' ;

// Value (string in this case)
value       : STRING ;

并输入:

Name=The Game ; needs a string

keyValuePair
应该没有
;
,因为它被视为词法分析器中注释的一部分。此外,输入预计有引号:
Name="The Game"

最后,你似乎从来没有使用过

closing
。我希望看到
file
包括这个:

file : header game_section* closing EOF;

编辑

类似这样的:

grammar GameData;

file         : header game_section* closing EOF;
header       : '[' WORLD ']' ;
closing      : '[/' WORLD ']' ;
game_section : '[' GAME ']' keyValuePair* '[/' GAME ']';
keyValuePair : ID '=' value ;
value        : STRING ;

WS      : [ \t\r\n]+ -> skip;
COMMENT : ';' ~[\r\n]* -> skip;
WORLD   : [Ww] [Oo] [Rr] [Ll] [Dd];
GAME    : [Gg] [Aa] [Mm] [Ee];
DOT     : '.';
ID      : [a-zA-Z]+;
STRING  : '"' .*? '"';

似乎适用于输入:

; comment
[WORLD]

[game] ; is a chilf of WORLD
Name="The Game" ; needs a string

[/game] ; end of game

[/WORLD] ; end of WORLD
© www.soinside.com 2019 - 2024. All rights reserved.