Antlr 4语法问题

时间:2017-02-12 05:13:05

标签: java parsing markdown antlr4

我正在尝试写一个降价语法。 但我对ANTLR语法有疑问。

我想忽略锐利,但我不知道该怎么做。

这是我的代码。

MarkdownLexer.g4

lexer grammar MarkdownLexer;

Title1
    : (SPACE|TAB)* ATX_START (SPACE|TAB) .*? NEWLINE
    ;

Codeblock
    : BACKQUOTE BACKQUOTE BACKQUOTE .*? BACKQUOTE BACKQUOTE BACKQUOTE
    ;


BACKQUOTE : '`' ;

ATX_START: ( '######' | '#####' | '####' | '###' | '##' | '#' ) {_tokenStartCharPositionInLine == 0}?;

SHARP: '#';

Text
    : ~[#`]*;

WS  : [ \t\r\n]+;

NEWLINE : '\n';

/* Single chars used by the parser.   */
SPACE: ' ';
TAB: '\t';

MarkdownParser.g4

parser grammar MarkdownParser;

options { tokenVocab=MarkdownLexer; }

@members {
    public void reportErrorsAsExceptions() {
        addErrorListener(new ExceptionThrowingErrorListener());
    }

    private static class ExceptionThrowingErrorListener extends BaseErrorListener {
        @Override
        public void syntaxError(Recognizer<?, ?> recognizer,
                Object offendingSymbol, int line, int charPositionInLine,
                String msg, RecognitionException e) {
            throw new RuntimeException(msg);
        }
    }
}

markDocument
    :
       ( Title1 | Codeblock | Text)* EOF
    ;

hello.txt的

# hello # ㅋㅋㅋㅋㅋ


hi  #hi
# bye

ㅋㅋㅋ

```
hello world 안녕 (korean)

```

zzz

MarkdownParserWalker.java

public class MarkdownParserWalker extends MarkdownParserBaseListener{
    @Override
    public void enterMarkDocument(MarkdownParser.MarkDocumentContext ctx) {
        System.out.println(ctx.Title1());
        System.out.println(ctx.Text());
        System.out.println(ctx.Codeblock());
        super.enterMarkDocument(ctx);
    }

    @Override
    public void exitMarkDocument(MarkdownParser.MarkDocumentContext ctx) {
        super.exitMarkDocument(ctx);
    }
}

Run.java

import org.antlr.v4.runtime.ANTLRFileStream;
import org.antlr.v4.runtime.CommonTokenStream;
import org.antlr.v4.runtime.tree.ParseTree;
import org.antlr.v4.runtime.tree.ParseTreeWalker;

import java.io.File;
import java.io.FileInputStream;

public class Run {
    public static void main(String[] args) throws Exception {
        MarkdownLexer lexer = new MarkdownLexer( new ANTLRFileStream("src/main/resources/hello.txt"));
        CommonTokenStream tokens = new CommonTokenStream( lexer );
        MarkdownParser parser = new MarkdownParser( tokens );
        ParseTree tree = parser.markDocument();
        ParseTreeWalker walker = new ParseTreeWalker();
        walker.walk( new MarkdownParserWalker(), tree );
    }
}

结果:

[    # hello # ㅋㅋㅋㅋㅋ
, # bye
]
[

hi  , hi
, 
ㅋㅋㅋ

, 

zzz]
[```
hello world !!! 한글 (korean)

```]

我希望结果为:

[# hello # ㅋㅋㅋㅋㅋ
, # bye
]
[


hi  #hi
, 

ㅋㅋㅋ

, 

zzz]
[```
hello world !!! 한글 (korean)

```]

对不起,我不会说英语。

感谢阅读。

0 个答案:

没有答案