import re


def parse_links(txt: str, tokens: list, offset: int) -> list:
    """
    Parse links represented in postfix notation and return them as a list of tuples.

    :param txt:         Link list in postfix notation, obtained either from LG API or from link-parser postfix output.
    :param tokens:      List of tokens previously extracted from postfix notated output.
    :param offset:      Token index offset. Equals to 1 if tested grammar has no LEFT-WALL and the former was added
                        during postscript parsing. Offset is necessary for the links to have their indexes properly
                        updated.
    :return:            List of tuples representing token to token links.
    """
    links = []

    token_count = len(tokens)

    start_pos = 1
    end_pos = txt.find("]")

    q = re.compile('(\d+)\s(\d+)\s\d+\s\(.+\)')

    while end_pos - start_pos > 0:
        mm = q.match(txt[start_pos:end_pos:])

        if mm is not None:
            index1, index2 = int(mm.group(1))+offset, int(mm.group(2))+offset

            if index2 < token_count:
                links.append((index1, index2))

        start_pos = end_pos + 2
        end_pos = txt.find("]", start_pos)

    return links