6

I've heard if you parse something LL(1) it's faster so I was wondering if you want to parse a JSON string can this be done by using a LL(1) parser

Spidfire
  • 5,433
  • 6
  • 28
  • 36

5 Answers5

1

Yes, they are, since there's no ambiguity in the JSON grammar.

  • 1
    In addition, you can check out [my implementation](https://github.com/H2CO3/libjsonz/blob/master/src/jsonz.c) which is hopefully not too difficult to understand. –  Jul 08 '13 at 20:02
  • 7
    While it's true that JSON is LL(1), this answer implies that all non-ambiguous grammars are LL(1). This is not correct. LL(1) grammars are a subset of the non-ambiguous grammars. For example, left-recursive grammars like "A -> A a | a" are not LL(1). – Josh Haberman Jul 12 '13 at 11:45
  • @JoshHaberman It is my understanding that simple transformations on a left-recursive grammer will make it LL(1). – frodeborli Feb 20 '21 at 14:50
  • @JoshHaberman any reference tha JSON is LL(1)? – wlnirvana May 22 '22 at 02:10
1

Yes, it is. See to yourself that the implementation of a parser for JSON string can be done with a automaton consisting of no more than 1 token. In other words, there exists a Markov chain solution for it.

Chivalryman
  • 89
  • 1
  • 3
1

array:

[ ] | [ elements ]

elements:

value | value , elements

it seems not LL(1) to me. Clear cannot parse on "value"

What if this way?

array: [ array1 ]
array1: <eps> | elements
elements: value elements1
elements1: <eps> | , elements
FILEAHOLIC
  • 51
  • 1
  • 6
0

Yes, it is LL(1) parsable. It has a context-free grammar and no ambiguity.

sasha.sochka
  • 14,395
  • 10
  • 44
  • 68
  • That doesn't make it LL(1). – Doradus May 21 '14 at 18:29
  • 1
    A grammar is LL(1) if an LL(1) parser can be constructed for it. Not all unambiguous context-free grammars have LL(1) parsers. Sorry if I'm struggling a little here, but I'm not sure what else to say other than to direct you to the Wikipedia page for [LL_parser](https://en.wikipedia.org/wiki/LL_parser) – Doradus May 23 '14 at 04:00
0

array:

[] |
[ elements ]

elements:

value |
value , elements

it seems not LL(1) to me. Clear cannot parse on "value"

rafaelc
  • 57,686
  • 15
  • 58
  • 82