Hello comsatcat,
You defined parser rules but did not specify the start symbol, meaning
it gets to be 'statement', because that's the first one (I think).
Statement matches the part '$foo = 1;' after which the statement has
been matched. The grammar doesn't expect anything after that. You could
add something like this, before any other parsing rules:
def p_statements_1(p):
'''
statements : statement
'''
pass # or something else...
def p_statements_2(p):
'''
statements : statements statement
'''
pass # or something else...
Or you could at it after/between the other rules, and explicitly define
the start symbol.
Dennis
comsatcat wrote:
> I'm playing with Ply, my input file is as follows:
>
> $foo = 1;
> $bar = 3;
>
> My problem is, when I pass multiple lines to the parser, it errors out
> after the first statement at $bar...
>
> <code>
>
> #!/usr/bin/env python
>
> import os, sys
> import ply.lex as lex
> import ply.yacc as yacc
>
> tables = {}
>
> reserved = {
> 'if' : 'IF',
> 'else' : 'ELSE',
> 'elsif' : 'ELSIF',
> 'while' : 'WHILE',
> 'for' : 'FOR',
> 'print' : 'PRINT'
> }
>
> tokens = [
> 'VARIABLE', 'NUMBER',
> 'PLUS', 'MINUS', 'TIMES', 'DIVIDE', 'EQUALS',
> 'LPAREN', 'RPAREN', 'SEMI', 'COMMENT',
> 'LT', 'GT', 'ET', 'LE', 'GE'
> ]
> tokens += list(reserved.values())
> t_LT = r'<'
> t_GT = r'>'
> t_ET = r'=='
> t_LE = r'<='
> t_GE = r'>='
> t_SEMI = r';'
> t_PLUS = r'\+'
> t_MINUS = r'-'
> t_TIMES = r'\*'
> t_DIVIDE = r'/'
> t_EQUALS = r'='
> t_LPAREN = r'\('
> t_RPAREN = r'\)'
>
> t_ignore = ' \t'
>
> def t_COMMENT(t):
> r'\#.*'
> pass
>
> def t_VARIABLE(t):
> r'\$[a-zA-Z]{1,}'
> t.type = reserved.get(t.value, 'VARIABLE')
> return t
>
> def t_NUMBER(t):
> r'[0-9]+'
> t.value = int(t.value)
> return t
>
> def t_newline(t):
> r'\n'
> t.lexer.lineno += 1
>
> def t_error(t):
> print "Illegal character '%s'" % t.value[0]
> t.lexer.skip(1)
>
> lexer = lex.lex()
>
> precedence = (
> ('left', 'PLUS','MINUS'),
> ('left', 'TIMES','DIVIDE')
> )
>
> def p_statement_assignment(p):
> '''
> statement : VARIABLE EQUALS expression SEMI
> '''
> tables[p[1].replace("$", "")] = p[3]
> print "statement_assignment"
>
> def p_statement_expression(p):
> '''
> statement : expression
> '''
> print "statement_expression"
> p[0] = p[1]
>
> def p_expression(p):
> '''
> expression : expression PLUS expression
> | expression MINUS expression
> | expression TIMES expression
> | expression DIVIDE expression
> '''
> print "expression"
> if p[2] == '+':
> p[0] = p[1] + p[3]
> elif p[2] == '-':
> p[0] = p[1] - p[3]
> elif p[2] == '*':
> p[0] = p[1] * p[3]
> elif p[2] == '/':
> p[0] = p[1] / p[3]
>
> def p_expression_number(p):
> '''
> expression : NUMBER
> '''
> print "expression_number"
> p[0] = p[1]
>
> def p_expression_variable(p):
> '''
> expression : VARIABLE
> '''
> print "expression_variable"
> try:
> p[0] = tables[p[1].replace("$", "")]
> except IndexError:
> print "Cannot find variable"
> pass
>
> def p_error(p):
> if not p:
> print "Syntax error: premature end of file"
> else:
> print "Syntax error on line %d: %s" % (p.lineno, p.value)
>
> parser = yacc.yacc()
>
> def run():
> data = open(sys.argv[1]).read()
> parser.parse(data)
>
> if __name__ == "__main__":
> if len(sys.argv) != 2:
> sys.exit(0)
>
> run()
> print str(tables)
>
> </code>
>
> Does anyone see what I'm doing wrong here?
> >
>
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"ply-hack" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/ply-hack?hl=en
-~----------~----~----~----~------~----~------~--~---