Role of inference rules
Given grammar, we need to compute certain sets that will be used by the parser. These sets are usually specified recursively (e.g. if is a member of the set, then is also a member of the set).
From these rules, it is easy to write down code.
Heavily reliant on the definition of Rules of Inference and Well-Formed Formulae.
Parsing SLL(1) Grammars
Java uses this.
Compute relations NULLABLE
, FIRST
, and FOLLOW
.
NULLABLE
- set of non-terminals that can be rewritten into an empty string
FIRST(A)
- if can be rewritten to a string starting with terminal , then is in
FIRST(A)
- if can be rewritten to a string starting with terminal , then is in
FOLLOW(A)
- set of terminals that can follow A in a sentential form
FOLLOW(A)
if we can derive a string .
These relations are defined for any context-free grammar, but if the grammar is SLL(1), they can be used to implement a recursive-descent parser.
Nullable
production
- A production whose righthand side is the empty string
Nullable non-terminal
- Non-terminal that can be rewritten to
NULLABLE
: Set of nullable non-terminals
- If there is a production , then
NULLABLE
- If there is a production and
NULLABLE
for all , thenNULLABLE
Inference Rules for NULLABLE
NULLABLE
Given a proposal that , we can check if the proposal is consistent with inference rules. We want the smallest set that satisfies all the inference rules.
Example
Given the grammar:
\begin{align} S\to A$ \\ A\to BC|x \\ B\to t|\epsilon \\ C\to v|\epsilon \\ \end{align}- {} is not consistent
- {A} is not consistent
- {A,B,C} is consistent
- {S,A,B,C} is consistent
Computing NULLABLE
Initialize NULLABLE
to {}
We do a round-based computation
- In each round, visit every production and see if you can add more elements to
NULLABLE
. Terminate whenNULLABLE
does not change in some round.
FIRST
FIRST() (A is non-terminal).
if A can be rewritten to a string starting with terminal , then is in FIRST().
Inference Rule for FIRST
Follow
Follow()\in TU\{\} is like end of file/string)
- if we can derive a sentential form Bt By convention, \\in \text{FOLLOW}(S)$
First sentential form (convention):
- S so FOLLOW(S) = {}
Next sentential form: say we use
- String becomes $A_{1}\dots A_{i}\dots A_{m}$$
In general: production
- .