Verify LL(1) grammar with ANTLR

不想你离开。 提交于 2019-12-10 22:13:56

问题


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?


回答1:


options {
   k = 1;
}

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




回答2:


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)



来源:https://stackoverflow.com/questions/16745218/verify-ll1-grammar-with-antlr

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!