2

I understand that ANTLR can accept an LL(*) grammar. But, is there any way to check a grammar whether it is an LL(1) or not using ANTLR?

Herry
  • 455
  • 3
  • 14

2 Answers2

6
options {
   k = 1;
}

It will give a warning if your grammer is not in LL(1).

jmihalicza
  • 2,083
  • 12
  • 20
1

For a grammer/language to be LL(1) we know that at any given position in the input there is only one production we can take to consume token(s) of the input. So, in order to determine if a grammar is LL(1), we need to:

1) check the FIRST set of all nonterminals and if there is any duplicates among a single nonterminal then the grammar is not LL(1).
EX: S->aba | abc (which production do we use if we are given the input "a", we can't determine, and need to peek at the next input token, and in fact we would need to peek at the third one to determine (so this would require LL(3).

2) Since we know which nonterminal we are finding a production for, we don't need to consider the FIRST set of other nonterminals.
EX. S->aba
T->acc
The intesection of FIRST(s) with FIRST(T) = {a}, but since we know we are producing for either S or T (only one at a time), we don't need to worry about multiple nonterminals having nonempty intersecting sets.

3) Finally, be carefull when a grammar goes to the empty string (S->ε). In this case, the FOLLOW set should be unioned with the FIRST to make sure their intersection is empty.
EX. S->aba | bSa | ε
T->cdd
Here, if we get the input "ba...", we would use the production S->bSa, but then we would see "a" in our input stream and would not know whether to produce S->aba (if input was b[aba]a) or to produce S->ε (if input was b[ε]a)

Counterfly
  • 29
  • 1
  • 4