Last active
October 31, 2024 16:22
-
-
Save Adib23704/feb989165ce9d57469bbf82a7a05063e to your computer and use it in GitHub Desktop.
LR(0) Grammer Parser Algorithm
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Define the grammar | |
| const grammar = { | |
| nonTerminals: ['S', 'A'], | |
| terminals: ['a', 'b'], | |
| productions: [ | |
| { left: 'S', right: ['A', 'a'] }, | |
| { left: 'S', right: ['b'] }, | |
| { left: 'A', right: ['a', 'A'] }, | |
| { left: 'A', right: [] } | |
| ] | |
| }; | |
| // Construct the LR(0) parsing table | |
| const parsingTable = { | |
| 0: { | |
| S: { action: 's', value: 1 }, | |
| A: { action: 's', value: 2 }, | |
| a: { action: '', value: '' }, | |
| b: { action: '', value: '' } | |
| }, | |
| 1: { | |
| S: { action: '', value: '' }, | |
| A: { action: '', value: '' }, | |
| a: { action: '', value: '' }, | |
| b: { action: 'r', value: 2 } | |
| }, | |
| 2: { | |
| S: { action: '', value: '' }, | |
| A: { action: '', value: '' }, | |
| a: { action: 's', value: 3 }, | |
| b: { action: '', value: '' } | |
| }, | |
| 3: { | |
| S: { action: '', value: '' }, | |
| A: { action: 'r', value: 1 }, | |
| a: { action: '', value: '' }, | |
| b: { action: 'r', value: 1 } | |
| } | |
| }; | |
| // Implement the LR(0) parsing algorithm | |
| function parseInput(input) { | |
| const stack = [0]; // Stack to store the state transitions | |
| const symbols = [...input, '$']; // Input symbols | |
| let currentIndex = 0; | |
| while (true) { | |
| const currentState = stack[stack.length - 1]; | |
| const currentSymbol = symbols[currentIndex]; | |
| const action = parsingTable[currentState][currentSymbol]; | |
| if (action.action === 's') { | |
| stack.push(action.value); | |
| symbols.push(currentSymbol); | |
| currentIndex++; | |
| } else if (action.action === 'r') { | |
| const production = grammar.productions[action.value]; | |
| // Reduce the symbols on the stack | |
| for (let i = 0; i < production.right.length; i++) { | |
| stack.pop(); | |
| } | |
| const nextState = stack[stack.length - 1]; | |
| const nonTerminal = production.left; | |
| stack.push(parsingTable[nextState][nonTerminal].value); | |
| symbols.push(nonTerminal); | |
| } else if (action === '') { | |
| console.log('Error: Invalid input'); | |
| return; | |
| } else { | |
| console.log(`Result: Syntax "${input}" valid and parsed successfully.`); | |
| return; | |
| } | |
| } | |
| } | |
| // Parse a given input program using the LR(0) parser | |
| const input = 'aab'; | |
| // Print the grammar table | |
| console.log('Grammar Table:\n--------------'); | |
| console.log('\nNon-terminals:'); | |
| console.log(grammar.nonTerminals.join(', ')); | |
| console.log('\nTerminals:'); | |
| console.log(grammar.terminals.join(', ')); | |
| console.log('\nProductions:'); | |
| grammar.productions.forEach((production, index) => { | |
| console.log(`${index + 1}. ${production.left} -> ${production.right.length === 0 ? 'ε' : production.right.join(' ')}`); | |
| }); | |
| console.log('\n'); | |
| parseInput(input); |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Converted to Python