-
Notifications
You must be signed in to change notification settings - Fork 0
Top Down Parsing
The Parser Studio supports the LL(1) top-down predictive parsing algorithm.
"Top-Down" means the parser begins with the Start Symbol at the top of the tree, and tries to guess which production rules to apply to grow the tree downwards until its leaves match the input string.
- L stands for reading the input from Left to right.
- L stands for producing a Leftmost derivation.
- 1 stands for using exactly 1 token of lookahead.
Because the parser only looks 1 token ahead, it must be completely certain which production rule to choose. If multiple rules are valid for the same lookahead token, a conflict occurs.
When you select LL(1) from the dropdown, AutomataLab uses the FIRST and FOLLOW sets (calculated in the Grammar Lab) to generate the LL(1) Parse Table.
- Rows represent the Non-Terminals.
- Columns represent the input Terminals.
- Cells contain the production rule to apply.
For every production rule
- For every terminal
$a$ in$\text{FIRST}(\alpha)$ , place$A \rightarrow \alpha$ in the cell$[A, a]$ . - If
$\epsilon$ is in$\text{FIRST}(\alpha)$ , then for every terminal$b$ in$\text{FOLLOW}(A)$ , place$A \rightarrow \alpha$ in the cell$[A, b]$ .
If the algorithm tries to place two different production rules into the exact same cell, that cell is flagged Red. This means your grammar is not LL(1)-parseable. The two most common causes are:
-
Left Recursion: E.g.,
$E \rightarrow E + T \mid T$ . Both rules share the same FIRST sets. You must mathematically eliminate left recursion for LL(1) to work. -
Lack of Left Factoring: E.g.,
$S \rightarrow \text{if } E \text{ then } S \mid \text{if } E \text{ then } S \text{ else } S$ . Both rules start withif. The parser cannot know which rule to pick just by looking 1 token ahead atif. You must factor out the common prefix.
If your LL(1) Parse Table is conflict-free, you can simulate it!
- Type a tokenized string in the Input Buffer (e.g.,
id + id * id). - Press Play or Step.
AutomataLab will initialize the Parse Stack with the Start Symbol. At every step:
- It looks at the symbol on top of the stack and the next token in the input.
- If the stack top is a terminal, it matches it against the input and pops it.
- If the stack top is a non-terminal, it looks up the rule in the Parse Table, pops the non-terminal, and pushes the RHS of the rule onto the stack in reverse order.
- The Syntax Tree automatically visualizes this expansion top-down in real time!
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