Search code examples
parsingcontext-free-grammarll-grammarcontext-free-language

LL(1) grammar interpretation


How do I interpret this grammar? I can't understand why some words are bold.

enter image description here

If I was writing a parse tree/table for this, would I include the bold words? I'm trying to figure out if the grammar in the pic is LL(1), but do not understand how to read it. Any help would be appreciated!


Solution

  • The bold items are tokens (in <variable>, a b and c should also be bold; your teacher's mistake). The items in angle brackets are production rules.

    The grammar is LL(1) if, for every place an alternative exists (separated by |), a parser can decide which alternative to pursue by looking at only the one token (the next one).

    So take for example the <stmt> rule. It has four alternatives. <ifstmt> must start with the token if. <whilestmt> must start with the token while. <block> must start with the token begin. That leaves only <assign>, which must start with a variable, that is, exactly one of a, b or c. So as far as <stmt> is concerned, an LL(1) parser can process that rule, because it can decide between the four possibilities by examining just one token.

    If any rule requires (for example) examining two tokens, then an LL(1) parser cannot process that grammar - it requires an LL(2) parser.

    I hope this helps.