Beispiel #1
0
    def __init__(self, **kw):
        self.debug = kw.get('debug', 0)
        self.names = {}
        try:
            modname = os.path.split(os.path.splitext(__file__)
                                    [0])[1] + "_" + self.__class__.__name__
        except:
            modname = "parser" + "_" + self.__class__.__name__
        self.debugfile = modname + ".dbg"
        self.tabmodule = modname + "_" + "parsetab"

        # Build lexer/parser
        lex.lex(module=self, debug=self.debug)
        yacc.yacc(module=self,
                  debug=self.debug,
                  debugfile=self.debugfile,
                  tabmodule=self.tabmodule)
Beispiel #2
0
t_ignore = " \t"


def t_newline(t):
    r'\n+'
    t.lexer.lineno += t.value.count("\n")


def t_error(t):
    print("Illegal character '%s'" % t.value[0])
    t.lexer.skip(1)


# Build the lexer

lex.lex()

# Precedence rules for the arithmetic operators
precedence = (
    ('left', 'PLUS', 'MINUS'),
    ('left', 'TIMES', 'DIVIDE'),
    ('right', 'UMINUS'),
)

# dictionary of names (for storing variables)
names = {}


def p_statement_assign(p):
    'statement : NAME EQUALS expression'
    names[p[1]] = p[3]
Beispiel #3
0
lexpos = 0  # Para armazenar número de caracteres consumidos até ao momento


def find_column(t):
    """ Identificar a coluna em que ocorreu o erro """
    # token.lexpos devolve o número de caracteres desde o inicio
    # Nós queremos o número de caracteres desde o princípio desta linha
    return t.lexpos - lexpos


if __name__ == "__main__":
    # Build the lexer
    from ply import lex
    import sys

    lex.lex()

    if len(sys.argv) > 1:
        f = open(sys.argv[1], "r")
        data = f.read()
        f.close()
    else:
        data = ""
        while 1:
            try:
                data += raw_input() + "\n"
            except:
                break

    lex.input(data)
Beispiel #4
0
    else:
        t.type = 'IDEN'
        return t


def t_error(t):
    global error
    global st
    error = True
    st = "\n--> ERROR LÉXICO ~~~ Se encontraron una expresion no válida: " + t.value + " , en la linea " + str(
        get_line_error()) + "!"
    while t.lexer.lexpos < t.lexer.lexlen:
        t.lexer.skip(1)


lexer = lex.lex()

precedence = (('left', 'PLUS', 'MINUS'), ('left', 'MULTIPLY', 'DIVIDE'))
''' ------------------------------ ANALISIS SINTACTICO ------------------------------ '''


def p_parse(p):
    '''
    parse : comparative
          | sentence
          | var_declare
          | proc
          | IDEN
          | empty
    '''
    #print(p[1])