Basic parser grammar question

Denis Washington <>
20 Apr 2007 10:24:12 -0400

          From comp.compilers

Related articles
Basic parser grammar question (Denis Washington) (2007-04-20)
Basic parser grammar question (O JuciE) (2007-04-26)
| List of all articles for this month |

From: Denis Washington <>
Newsgroups: comp.compilers
Date: 20 Apr 2007 10:24:12 -0400
Organization: Compilers Central
Keywords: parse, question
Posted-Date: 20 Apr 2007 10:24:12 EDT


I have a small problem with a PLY [1] grammer, more particularly with
the part that parses arithmetic expressions, which looks like in the
following extract:

def p_additive_expression_pass(p):
        "additive-expression : multiplicative-expression"
        p[0] = p[1]

def p_additive_expression(p):
        "additive-expression : additive-expression additive-operator
        p[0] = operator_expression_to_call(p[2], p[1], [p[3]], filename,

def p_additive_operator_plus(p):
        "additive-operator : PLUS"
        p[0] = "__add__"

def p_additive_operator_minus(p):
        "additive-operator : MINUS"
        p[0] = "__sub__"

This piece of grammar works fine if I have simple additive expressions
like "a + b", but fails to function if having longer arrays of additive
expressions, e.g. "a + b + c". Why is that? Have I missed something? I
hope you can help me.

Denis Washington


Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.