1

First of all, I know there are a lot of answers and resources already available regarding this topic. However, I'm having a really hard time understanding the grammar notation that is often used in these answers. So I was hoping someone could explain it in a more intuitive way.

I'm trying to write a simple recursive descent parser for some kind of C-type language. However, I am running into some issues when implementing a function call.

I am trying to support the following syntax:

a(b)
a(b, c)
a(b, c)()
a(b, c)(d)

Like I said, I have a hard time understanding the grammar notation often used, so I will show my implementation:

const functionCall = new Expression((parser) => {
    parser.expect(expression)
    parser.expect('(')
    if (parser.optional(expression)) {
        while (true) {
            if (parser.optional(',')) {
                parser.expect(expression)
            } else {
                break
            }
        }
    }
    parser.expect(')')    
})

const expression = new Expression((parser) => {
    parser.either([functionCall, literal])
})

const literal = new Expression((parser) => {
    parser.either([{name: 'string_literal'}, {name: 'number_literal'}, {name: 'identifier'}])
})

As I understand my grammar is (indirectly?) left recursive because the first thing that can be matched in a function call, can be a function call. If this assumption is right, I wouldn't know how to eliminate this recursion.

3
  • How do you do (repeated) addition? It's not that different. Commented Oct 17, 2020 at 3:56
  • @rici I gues i would run in to same issues indeed. I am not very familiar with parsing. I've read some articles on recursive descent, but cant figure out how to solve problems like these. Commented Oct 17, 2020 at 11:03
  • In the example code for a rd parser in Wikipedia, note the while loops in term and expression. Commented Oct 17, 2020 at 12:33

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.