Determining k of LR(k) from this example?

前端 未结 2 1779
南旧
南旧 2021-01-18 21:16

I have prepared the following grammar that generates a subset of C logical and integer arithmetic expressions:

Expression:
    LogicalOrExpression
    Logica         


        
2条回答
  •  太阳男子
    2021-01-18 21:51

    From Donald Knuths On the Translation of Languages from Left to Right, in the abstract,

    It is shown that the problem of whether or not a grammar is LR(k) for some k is undecidable,

    In otherwords,

    Given a grammar G, "∃k. G ∊ LR(k)" is undecidable.

    Therefore, the best we can do in general is try constructing a parser for LR(0), then LR(1), LR(2), etc. At some point you will succeed, or you may at some point give up when k becomes large.

    This specific grammar

    In this specific case, I happen to know that the grammar you give is LALR(1), which means it must therefore be LR(1). I know this because I have written LALR parsers for similar languages. It can't be LR(0) for obvious reasons (the grammar {A -> x, A -> A + x} is not LR(0)).

提交回复
热议问题