Question

I am trying to write a grammar for a python-like language in antlr, the language looks like this:

if condition:
    if condition:
    pass;
        if condtion:
            pass;
        if condtion:
            pass;
pass;
if condtion:
    pass;

My problem is I can't find the blocks with spaces. So I want to know if there is a way to implement this without writing any code in the grammar?

Was it helpful?

Solution

If I remember correctly, what I did was to create a stack of white space in the lexer but of course that requires actions in the lexer.

Another way to handle this would be to create a character stream processor that sends Unicode 16 characters not just 8 bit to the lexer. Then, use a special bit patterns to indicate the character is actually a nesting depth. The lexer can then send INDENT and DEDENT tokens to the parser.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top