This repository contains a Python implementation of a grammar analysis and parsing system. It processes a context-free grammar, eliminates right recursion, applies right factoring, and builds a reverse LL(1)-style parsing table using the last sets and precedence rules. The parser then uses the table to check string acceptance.
- Accepts grammar input in BNF-like format
- Eliminates right recursion
- Performs right factoring based on common suffixes
- Computes Last sets and precedence for non-terminals
- Builds a reverse LL(1)-like parsing table
- Supports token preprocessing (
num,id) - Verifies whether input strings are accepted by the grammar
- Handles multiple test cases via an
input.txtfile
- The grammar follows the format:
NonTerminal -> production1 | production2 .... - Each production must be on a separate line
- Use
->for production rules - Separate productions of a non-terminal with
| - Use
epito denote epsilon (empty string) - Only uppercase symbols are non-terminals, everything else is terminal
- The first production defines the start symbol
- Place the grammer in
grammar_stringvariable in RR_PARSER.py input.txtiss used to test the grammer on multiple string inputs
L -> C ; L | C ;
C -> ser B | par B | id | num
B -> C B | end- Input strings should be space-separated tokens
- Valid terminals include:
ser,par,end, etc. - Numbers are treated as
numand unknown identifiers asid - All strings must end with
$
ser num end $
par id ser id end end $
id $-
Parsing and Preprocessing
- Reads and stores the grammar
- Eliminates right recursion and factors the grammar
-
Grammar Analysis
- Computes the
Lastset for each non-terminal - Derives
precedencesets using last sets
- Computes the
-
Table Construction
- Generates a reverse parsing table for bottom-up acceptance
-
String Acceptance
- Simulates parsing with a stack
- Uses the parsing table to validate each input string
AFTER ELIMINATING RIGHT RECURSION
...
AFTER RIGHT FACTORISATION
...
Last(L) = {';', 'id', 'num', ...}
Precedences(L) = {'$', ...}
Parsing Table:
| ser | par | ; | id | num | end | $
----------------------------------------------------------------------------------------------
L | L# C | L# C | | L# C | L# C | |
...
Test Case 1: ser num end $
--The string is accepted by the grammar
Test Case 2: par id ser id end end $
--The string is accepted by the grammar
Test Case 3: id $
--The string is accepted by the grammar- Add your grammar inside the
grammar_stringvariable inparser.py - Save input strings in
input.txt, one per line - Run the parser:
python RR_PARSER.py