-
Notifications
You must be signed in to change notification settings - Fork 0
Mathematical Properties
As you type in the Grammar Lab editor, AutomataLab continuously parses the rules and instantly computes the fundamental mathematical properties of your Context-Free Grammar. These properties are required for generating robust Parse Tables in the Parser Studio.
A non-terminal
AutomataLab uses a recursive algorithm to compute this:
- If
$X \rightarrow \epsilon$ is a rule,$X$ is nullable. - If
$X \rightarrow Y_1 Y_2 \dots Y_n$ is a rule, and every$Y_i$ is nullable, then$X$ is nullable.
The Diagnostics panel will list every nullable non-terminal. This is critical for computing FIRST and FOLLOW sets, as nullable symbols allow the parser to "look past" them to the next symbol in the string.
The FIRST(
If
Top-Down predictive parsers (like LL(1)) use the FIRST sets to decide which production rule to pick. If the parser is looking at non-terminal id, the parser checks the FIRST sets of all RHS alternatives for id.
The FOLLOW(
Note that $ is placed in the FOLLOW set of the Start Symbol
FOLLOW sets are heavily utilized by LL(1) and SLR(1) parsers.
- In LL(1): If the predictive parser needs to expand
$A$ , and the next token is not in any FIRST set for$A$ , but$A$ is nullable, the parser checks if the token is in FOLLOW($A$ ). If it is, the parser safely chooses the$A \rightarrow \epsilon$ rule. - In SLR(1): When the bottom-up parser has completely matched the RHS of
$A$ on the stack, it uses FOLLOW($A$ ) to decide whether to Reduce. It will only reduce if the lookahead token is explicitly in FOLLOW($A$ ).
AutomataLab's Matrix panel flags two critical structural properties of your grammar:
A grammar is Left Recursive if a non-terminal can derive a string that starts with itself (e.g.,
-
Direct:
$E \rightarrow E + T$ -
Indirect:
$A \rightarrow B$ ,$B \rightarrow A$
Impact: Top-Down parsers (like LL(1)) will enter an infinite loop trying to expand left-recursive rules. You must eliminate left recursion before using the LL(1) algorithm in the Parser Studio. Bottom-up parsers (LR, LALR) handle left recursion perfectly!
A grammar needs Left Factoring if two or more production rules for the same non-terminal share a common prefix.
- Example:
$S \rightarrow \text{if } E \text{ then } S \mid \text{if } E \text{ then } S \text{ else } S$
Impact: Predictive parsers (LL(1)) cannot decide which rule to expand because both rules have the exact same FIRST set. You must factor out the common prefix.
AutomataLab v4.1.0 · Repository · Download · Web app · MIT License
Getting Started
Machine Workspace
- Workspace Overview
- Finite Automata
- Pushdown Automata
- Turing Machines & LBA
- Transition Table & Data Tools
Grammar Lab
Parser Studio
Project & Architecture