Code Monkey home page Code Monkey logo

Comments (4)

igordejanovic avatar igordejanovic commented on June 19, 2024

Hello adontz,

There are two issues you are having.

The first one is that you are using left-recursive grammar which is not possible with Arpeggio as it is recursive-descent (top-down) parser.
I just made a troubleshooting guide entry of how to handle left recursive rules.

The second issue is the infinite recursion in resolve which should not happen. It looks like you hit a bug in the support for textual PEG syntax. I will investigate the issue as soon as I find some time.

In the meantime you could try to use a cannonical Python notation.
Here is something to get you going:

from __future__ import unicode_literals
from arpeggio import *
from arpeggio import RegExMatch as _
from arpeggio.cleanpeg import ParserPEG


def number_token():                         return _(r'(\d+|\d+\.\d*|\d*\.\d+)')
def identifier_token():                     return _(r'[a-zA-Z_][a-zA-Z0-9_]*')
def unqualified_identifier_expression():    return identifier_token
def qualified_identifier_expression():      return identifier_token, ZeroOrMore(".", identifier_token)
def unary_expression():                     return [number_token, qualified_identifier_expression]

def lvalue_expression():                    return qualified_identifier_expression
def rvalue_expression():                    return expression

def expression():                           return [compound_expression, unary_expression]
def compound_expression():                  return qualified_identifier_expression,\
                                                   ZeroOrMore([method_call_par, index_par])
def method_call_par():                      return  "(", Optional( rvalue_expression, ZeroOrMore( ",", rvalue_expression)), ")"
def index_par():                            return "[", rvalue_expression, ZeroOrMore( ",", rvalue_expression ),    "]"

def belang():                               return ZeroOrMore(expression), EOF

parser = ParserPython(belang, debug=True)
parser.parse('a [0](1)[2]')

from arpeggio.

igordejanovic avatar igordejanovic commented on June 19, 2024

Check this test.
grammar2 will parse the expression you need.

You could also start with your left-recursive grammar and do a proper lef-recursion removal.

from arpeggio.

igordejanovic avatar igordejanovic commented on June 19, 2024

BTW, If you are building a language I highly recommend you to use textX. It is built on top of Arpeggio and is specifically crafted for language implementation. The docs with tutorials is here.

from arpeggio.

adontz avatar adontz commented on June 19, 2024

Thank you very much!

from arpeggio.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.