PLY not matching input file

This is the place for queries that don't fit in any of the other categories.

PLY not matching input file

Postby kirbyfan64sos » Sat Sep 07, 2013 10:19 pm

I have this code:

lexer.py:
Code: Select all
#!/usr/bin/env python

import ply.lex as lex

reserved = {
    'begin': 'BEGIN',
    'end;' : 'END',
    'end\.': 'MEND'
    }

tokens = reserved.values() + [
    'IDEN',
    'LPAREN',
    'RPAREN',
    'STRING',
    'SEMIC',
    'COMMA'
    ]



t_IDEN = r'[a-zA-Z][a-zA-Z0-9_]*'

t_LPAREN = r'\('
t_RPAREN = r'\)'

t_STRING = r"'[^']*'"

t_SEMIC = r';'
t_COMMA = r','

t_ignore = ' \t'

def t_error(t):
    t.lexer.skip(1)

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


lex = lex.lex()


parser.py:
Code: Select all
#!/usr/bin/env python

from lexer import tokens, reserved
import ply.yacc as yacc

class ASTNode():
    pass

class ProgramNode():
    def __init__(self, content):
        self.content = content
    def CodeGen(self):
        return ''.join(self.content)

class MainNode():
    def __init__(self, content):
        self.content = content
    def CodeGen(self):
        return 'int main(){\n' + ';\n'.join(self.content) + ';\n}\n'

class IdentifierNode(ASTNode):
    def __init__(self, iden):
        self.iden = iden
    def CodeGen(self):
        return self.iden

class StringNode():
    def __init__(self, string):
        self.string = string.strip("'").rstrip("'")
    def CodeGen(self):
        return '"' + self.string + '"'

class CallNode():
    def ___init__(self, fname, arglist):
        self.fname = fname
        self.arglist = arglist
    def CodeGen(self):
        return self.fname + '(' + ', '.join([arg.CodeGen() for arg in arglist]) + ')'



def p_string(p):
    'expression : STRING'
    p[0] = StringNode(p[1])

def p_fcall(p):
    'expression : IDEN LPAREN arglist RPAREN'
    p[0] = CallNode(p[1], p[3])

def p_iden(p):
    'expression : IDEN'
    p[0] = IdentifierNode(p[1])

def p_main(p):
    'expression : BEGIN multi_exp MEND'
    p[0] = MainNode(p[2])

def p_arglist(p):
    '''arglist :
    | expression
    | arglist COMMA expression'''
    if len(p) == 1:
        p[0] = []
    elif len(p) == 2:
        p[0] = [p[1]]
    else:
        p[1].append(p[2])
        p[0] = p[1]

def p_multi(p):
    '''multi_exp :
    | expression
    | multi_exp SEMIC expression'''
    if len(p) == 1:
        p[0] = []
    elif len(p) == 2:
        p[0] = [p[1]]
    else:
        p[1].append(p[2])
        p[0] = p[1]



def p_error(e):
    print('error: %s'%e)



def parse(data):
    parser = yacc.yacc()
    parser.parse(data)

def main(argv):
    with open(argv[1], 'r') as f:
        parse(f.read())

if __name__ == '__main__':
    main([None, 'C:\\Users\\Ryan\\Documents\\rpascal\\test.pas'])


with this input file:
Code: Select all
begin
   writeln('Hello world!')
end


PLY is failing:
Code: Select all
error: LexToken(IDEN,'writeln',2,7)


I keep reviewing the code and am not sure what's wrong.
This program is bug-free. If you see a bug, it's actually a feature.
kirbyfan64sos
 
Posts: 7
Joined: Sun Aug 25, 2013 10:45 pm

Return to General Coding Help

Who is online

Users browsing this forum: buran, Google [Bot] and 5 guests