Indentation control while developing a small python like language

后端 未结 2 1862
一生所求
一生所求 2021-02-10 08:14

I\'m developing a small python like language using flex, byacc (for lexical and parsing) and C++, but i have a few questions regarding scope control.

just as python it u

相关标签:
2条回答
  • 2021-02-10 08:33

    I am currently implementing a programming language rather similar to this (including the multilevel break oddly enough). My solution was to have the tokenizer emit indent and dedent tokens based on indentation. Eg:

    while 1: # colons help :)
        print('foo')
        break 1
    

    becomes:

    ["while", "1", ":",
        indent,
        "print", "(", "'foo'", ")",
        "break", "1",
        dedent]
    

    It makes the tokenizer's handling of '\n' somewhat complicated though. Also, i wrote the tokenizer and parser from scratch, so i'm not sure whether this is feasable in lex and yacc.

    Edit:

    Semi-working pseudocode example:

    level = 0
    levels = []
    for c = getc():
        if c=='\n':
            emit('\n')
            n = 0
            while (c=getc())==' ':
                n += 1
            if n > level:
                emit(indent)
                push(levels,n)
            while n < level:
                emit(dedent)
                level = pop(levels)
                if level < n:
                    error tokenize
            # fall through
        emit(c) #lazy example
    
    0 讨论(0)
  • 2021-02-10 08:54

    Very interesting exercise. Can't you use the end keyword to check when the scope ends?

    On a different note, I have never seen a language that allows you to break out of several nested loops at once. There may be a good reason for that...

    0 讨论(0)
提交回复
热议问题