LL Parsing
def. Parsing. Left-to-Right, Left-most derivation Parser with lookaheads.
alg. Given CFG , parsing occurs in the following steps:
- Create the sets for each of the each of the variables in the grammar.
- is the set of terminals (including ) that can lead the string derived.
- Created by checking the variable on the left side of the production: . Common sense will be enough.
- Don’t forget the check variables that go to i.e. disappear. is only included in the FIRST set if the whole variable can disappear to .
- is created by checking the variable on the right side of the production: .
- The start variable always has the $$$ symbol in its follow set.
- Use the first sets to make life simpler. is always removed.
- is the set of terminals (including ) that can lead the string derived.
- Create the parse table.
- For each variable, check the rules.
- There must be an entry for every element of the first set. check which rule makes the most sense [= FIRST set of the RHS of the rule is the lookahead symbol]
- If FIRST set includes a (and the variable can goto) , ⇒ There must be a entry for every element of the follows set…
- …however, if the FIRST set includes a but the variable cannot goto , find another rule that makes sense to put there.
- Use the table to do the necessary steps for parsing the string.
LR Parsing
def. Parsing. Left-to-Right, Right-most Reverse derivation parser with lookaheads.
alg. Given CFG , parsing occurs in the following steps:
- Set up the grammar so that it’s ready to parse:
- Change the start symbol to . This is rule #0.
- Number the rules through .
- Calculate the FIRST and FOLLOW sets of variables.
- Construct a parsing DFA in the following steps:
- The start state has rule number 0 and rules for . Place markers () in the beginning.
- For each rule, “process” one symbol at a time. Processing means that to transition to another state… …with arc labeled *symbol …*to create a new state with the marker jumped over that symbol.
- You have a new state with one rule that has the jumped marker. Now, add to the state the closure of that rule. The closure of a rule is calculated as such:
- If the marker is in front of a variable, the derivation rules for that variables is included
- if the market is in front of a terminal, don’t do anything
- Construct a Parsing table from the DFA to use during parsing:
- Do the following for every state of the DFA [=every row of the parse table]:
- For every transition arc, put an entry in the table, for that state number and arc label:
- If it’s just a transition arc, put a “shift to state #” or sN
- If it’s a final state, only the entries for the elements of FOLLOW should be populated; put a “reduce by rule number #” or rN and the rule number is the rule of the state (since it’s a final state, there should be only one rule)
- If it’s a final state but its rule is , it’s special; only populate the “$” entry. put a “accept” or acc in that place.
- If the state contains a -rule,** then always put a “reduce by rule #” or rN, where the rule number is that lambda rule ← this should be in the **“$” column**
- Do the following for every state of the DFA [=every row of the parse table]: