Skip to main content

Design An Top-Down Parser Which Generate A Parsing Table With No Backtracking An Simple And Easy Code

 Mathematica:

E -> T E'

E' -> + T E' | ε

T -> F T'

T' -> * F T' | ε

F -> ( E ) | id


Simple C program for a top-down parser:


#include <stdio.h> #include <stdlib.h> #include <string.h> // Define terminals and non-terminals enum { PLUS, TIMES, LPAREN, RPAREN, ID, END, INVALID }; char terminals[] = {'+', '*', '(', ')', 'id', '$'}; char nonTerminals[] = {'E', 'A', 'T', 'B', 'F'}; // Define the LL(1) parsing table int parsingTable[5][6] = { {1, -1, -1, -1, 1, -1}, {-1, 2, -1, -1, -1, 3}, {4, -1, -1, -1, 4, -1}, {-1, -1, 5, -1, -1, 5}, {6, -1, -1, -1, 7, -1} }; // Function to get the index of a terminal/non-terminal int getIndex(char symbol, char* symbols) { for (int i = 0; i < strlen(symbols); i++) { if (symbols[i] == symbol) { return i; } } return -1; } // Function to get the type of a symbol int getSymbolType(char symbol) { if (symbol == '+') return PLUS; if (symbol == '*') return TIMES; if (symbol == '(') return LPAREN; if (symbol == ')') return RPAREN; if (symbol == 'id') return ID; if (symbol == '$') return END; return INVALID; } // Function to perform parsing void parseInput(char input[]) { char stack[100]; int top = -1; stack[++top] = 'E'; // Push start symbol to the stack int inputIndex = 0; printf("Parsing Steps:\n"); while (top >= 0) { char stackTop = stack[top]; char currentInput = input[inputIndex]; printf("Stack: %s\tInput: %s\t", stack, input + inputIndex); if (stackTop == currentInput && currentInput == '$') { printf("Accepted\n"); break; } int stackIndex = getIndex(stackTop, nonTerminals); int inputIndex = getIndex(currentInput, terminals); if (stackIndex == -1 || inputIndex == -1) { printf("Error\n"); break; } int productionIndex = parsingTable[stackIndex][inputIndex]; if (productionIndex == -1) { printf("Error\n"); break; } else { printf("Use production: %c -> ", stackTop); switch (productionIndex) { case 1: printf("TE'\n"); break; case 2: printf("+TE'\n"); break; case 3: printf("ε\n"); break; case 4: printf("FT'\n"); break; case 5: printf("*FT'\n"); break; case 6: printf("(E)\n"); break; case 7: printf("id\n"); break; } top--; // Pop from the stack // Push production to the stack in reverse order switch (productionIndex) { case 1: stack[++top] = 'E'; stack[++top] = '\''; stack[++top] = 'T'; break; case 2: stack[++top] = 'E\''; stack[++top] = 'T'; stack[++top] = '+'; break; case 3: break; // ε, do nothing case 4: stack[++top] = 'T\''; stack[++top] = 'F'; break; case 5: stack[++top] = 'T\''; stack[++top] = 'F'; stack[++top] = '*'; break; case 6: stack[++top] = ')'; stack[++top] = 'E'; stack[++top] = '('; break; case 7: stack[++top] = 'id'; break; } } if (currentInput != '$') { inputIndex++; // Move to the next input symbol } } } int main() { char input[100]; // Get input from the user printf("Enter the input string (end with $): "); scanf("%s", input); // Add $ to the end of the input strcat(input, "$"); // Parse the input parseInput(input); return 0; }


Output:

1

Enter the input string (end with $): id$

Parsing Steps:

Stack: E Input: id$ Use production: id

Stack: E' Input: $ Use production: ε

Stack: Input: $ Accepted


2

Enter the input string (end with $): id+$

Parsing Steps:

Stack: Eⁿ┐ì┴ Input: id+$ Use production: id

Stack: E'┐ì┴ Input: +$ Use production: ε

Stack: Eⁿ┐ì┴ Input: $ Use production: E' -> ε

Accepted


3

Enter the input string (end with $): id+id$

Parsing Steps:

Stack: E∙⌂┼≡ Input: id+id$ Use production: id

Stack: T∙⌂┼≡ Input: +id$ Use production: FT'

Stack: F∙⌂┼≡ Input: +id$ Use production: id

Stack: T'∙⌂┼≡ Input: +id$ Use production: ε

Stack: E'∙⌂┼≡ Input: +id$ Use production: +TE'

Stack: T∙⌂┼≡ Input: id$ Use production: FT'

Stack: F∙⌂┼≡ Input: id$ Use production: id

Stack: T'∙⌂┼≡ Input: id$ Use production: ε

Stack: E'∙⌂┼≡ Input: id$ Use production: ε

Stack: T∙⌂┼≡ Input: $ Use production: ε

Stack: E'∙⌂┼≡ Input: $ Use production: ε

Stack: Accepted


Comments

Popular posts from this blog

Stable Diffusion WebUI 1.10.1 Full Installation Guide | AUTOMATIC1111 | Windows 11

Stable Diffusion WebUI 1.10.1 Full Installation Guide | AUTOMATIC1111 | Windows 11  Welcome to this step-by-step Stable Diffusion WebUI 1.10.1 installation guide! In this tutorial, we will walk you through the complete setup process on Windows 11 , including downloading and installing Git , setting up Python 3.10.6 , cloning the AUTOMATIC1111 repository , and configuring .gitignore for a clean and efficient installation. By following this guide, you’ll be able to generate AI-generated images using Stable Diffusion with ease. Whether you're new to AI image generation or an experienced user, this guide ensures that your setup is optimized for performance and stability. 🔗 Required Downloads: Before we begin, make sure to download the following tools: ✅ Git for Windows – Download Here ✅ Stable Diffusion WebUI (AUTOMATIC1111) – Download Here ✅ Python 3.10.6 – Download Here 🛠️ Step-by-Step Installation Process 1️⃣ Install Git for Windows Git is required to clone the ...

Unreal Engine Product Showcase: Mesmerizing Video Sequence Render

  4k Image:

Install TensorFlow on Windows 11: Step-by-Step Guide for CPU & GPU

 --- Installing **TensorFlow on Windows 11** requires setting up system dependencies, configuring Python, and ensuring compatibility with CPU or GPU acceleration. This step-by-step guide provides everything needed to install **TensorFlow 2.10 or lower** on **Windows Native**, including software prerequisites, Microsoft Visual C++ Redistributable installation, Miniconda setup, GPU driver configuration, and verification steps.   ### **System Requirements:**   Before installing TensorFlow, ensure your system meets these requirements:   - **Operating System:** Windows 7 or higher (64-bit)   - **Python Version:** 3.9–3.12   - **pip Version:** 19.0 or higher for Linux and Windows, 20.3 or higher for macOS   - **Microsoft Visual C++ Redistributable:** Required for Windows Native   - **Long Paths Enabled:** Ensure long paths are enabled in Windows settings   For **GPU support**, install:   - **NVIDIA ...