Indentation instead of parentheses for Antlr4

I am currently participating in the Compilers Construction course, and I am developing a simplified Java grammar in Antlr 4. The part that includes the Java grammar works fine, except that it is slightly different from the original Java.

I am told that I should not use parentheses and use indents instead. I tried some examples from the Internet, only examples for Antlr 3 that will not work in Antlr 4.

Can someone help me with this indentation problem or provide me with Python grammar for Antlr 4?

+4
source share
2 answers

What about the official GitHub ANTLR4 grammar repository ? You can find many common language grammars. There is even a python3 directory containing Python3.g4 .

This should help you.

+1
source

I wrote a small library that helps in this case: antlr-denter .

In short, you define the INDENT and DEDENT tokens in your grammar, do some template details to anchor the antlr-denter in the antlr created by lexer, and off you go. For example, the code is as follows:

 hello: world <eof> 

It is possible to create a WORD COLON INDENT WORD DEDENT .

0
source

Source: https://habr.com/ru/post/1493249/


All Articles