An open API service indexing awesome lists of open source software.

https://github.com/rutpiv/conditional-command-parser

Implementing a Lexical and Syntactic Analyzer with First/Follow in Spring Boot
https://github.com/rutpiv/conditional-command-parser

grammar-checker java lexer-parser maven semantic-analysis sintaxis-analyzer spring-boot university-project

Last synced: 4 days ago
JSON representation

Implementing a Lexical and Syntactic Analyzer with First/Follow in Spring Boot

Awesome Lists containing this project

README

          

# ๐Ÿ“š ConditionalCommandParser โ€“ Conditional Command Parsing Utility

![Tokyo Night Theme Preview](docs/images/theme-preview.png)
_Tokyo Night Theme (Dark & Light Modes)_


Java Version
Maven Version
Tests Passed
License

## ๐Ÿ—‚๏ธ Table of Contents

- [โญ Features](#-features)
- [๐Ÿ“ธ Screenshots](#-screenshots)
- [๐Ÿ—๏ธ Technical Implementation](#๏ธ-technical-implementation)
- [โš™๏ธ Technologies](#๏ธ-technologies)
- [๐Ÿš€ Getting Started](#-getting-started)
- [๐Ÿงช Testing](#-testing)
- [๐Ÿ“œ License](#-license)
- [๐Ÿ‘ฅ Authors](#-authors)

---

## โญ Features

**A modular conditional command parsing utility with semantic analysis:**

- โœ… **Advanced Parsing Engine**

- Implements a Recursive Descent Parser to handle conditional commands, assignments, and expressions with clear grammar structure.

- ๐Ÿ” **Custom Lexer & Grammar Analysis**

- Robust Lexer for tokenization and type recognition (supporting `float`, `string`, `char`, and `int`).
- First and Follow set calculations to support predictive parsing and error handling.
- **Supports comments** in the input:
- `// line comments`
- `/* block comments */`

- ๐Ÿง  **Semantic Analysis**

- Enforces semantic rules such as type consistency in assignments and expressions.
- Symbol table management for identifier tracking and type binding.

- ๐ŸŽจ **Syntax Tree Visualization**

- TreePrinter utility provides a clear, labeled printout of the abstract syntax tree (AST), including node types and relationships.

- ๐Ÿ–ฅ๏ธ **Modern Web Interface**

- Web controller (ParserController) and a responsive `index.html` interface styled with Tailwind CSS.
- Includes light/dark themes inspired by the TokyoNight palette and a toggle switch for user preference.

- ๐Ÿงช **Test-Driven Development**

- Extensive unit tests for Lexer, ParserService, Semantic Analysis, and FirstFollowCalculator ensure correctness and robustness.

---

## ๐Ÿ“ธ Screenshots

### Syntax Tree Visualization

![Syntax Tree Visualization](docs/images/syntax-tree.png)
_Visual output from TreePrinter displaying the parsed conditional command tree_

---

## ๐Ÿ—๏ธ Technical Implementation

### Grammar Specification

The parser is built using the following context-free grammar:

```bnf
S โ†’ if ( E ) S else S | id = E
E โ†’ E + T | E - T | T
T โ†’ T * F | T / F | F
F โ†’ ( E ) | id
```

**Note:** In this grammar:

- `id` represents either an **identifier** (e.g., variable names) or a **literal** of supported types: `float`, `string`, `char`, or `int`
- `if`, `else`, and operators (`+`, `-`, `*`, `/`) are terminal symbols

### Project Structure & Component Roles

```bash
src/
โ”œโ”€โ”€ main
โ”‚ โ”œโ”€โ”€ java/br/edu/fesa/Conditional_Command_Parser
โ”‚ โ”‚ โ”œโ”€โ”€ ConditionalCommandParserApplication.java # Main Spring Boot entry point
โ”‚ โ”‚ โ”œโ”€โ”€ exception
โ”‚ โ”‚ โ”‚ โ”œโ”€โ”€ LexicalException.java # Custom lexical error handling
โ”‚ โ”‚ โ”‚ โ”œโ”€โ”€ SemanticException.java # Custom semantic error handling
โ”‚ โ”‚ โ”‚ โ””โ”€โ”€ SyntaxException.java # Custom syntax error handling
โ”‚ โ”‚ โ”œโ”€โ”€ controller
โ”‚ โ”‚ โ”‚ โ””โ”€โ”€ ParserController.java # REST API endpoint handler
โ”‚ โ”‚ โ”œโ”€โ”€ model
โ”‚ โ”‚ โ”‚ โ”œโ”€โ”€ Assignment.java # 'id = E' assignment nodes
โ”‚ โ”‚ โ”‚ โ”œโ”€โ”€ BinOp.java # Binary operations (+,-,*,/)
โ”‚ โ”‚ โ”‚ โ”œโ”€โ”€ CharLiteral.java # Char literal node
โ”‚ โ”‚ โ”‚ โ”œโ”€โ”€ FloatLiteral.java # Float literal node
โ”‚ โ”‚ โ”‚ โ”œโ”€โ”€ Identifier.java # ID nodes (variables/numbers)
โ”‚ โ”‚ โ”‚ โ”œโ”€โ”€ IfStatement.java # If-else control structures
โ”‚ โ”‚ โ”‚ โ”œโ”€โ”€ NumberLiteral.java # Numeric literal node
โ”‚ โ”‚ โ”‚ โ”œโ”€โ”€ ParserResponse.java # API response wrapper
โ”‚ โ”‚ โ”‚ โ”œโ”€โ”€ StringLiteral.java # String literal node
โ”‚ โ”‚ โ”‚ โ”œโ”€โ”€ SyntaxNode.java # Base AST interface
โ”‚ โ”‚ โ”‚ โ””โ”€โ”€ Token.java # Token type/value storage
โ”‚ โ”‚ โ”œโ”€โ”€ semantic
โ”‚ โ”‚ โ”‚ โ”œโ”€โ”€ Symbol.java # Symbol representation for semantic analysis
โ”‚ โ”‚ โ”‚ โ””โ”€โ”€ SymbolTable.java # Symbol table for variable scope management
โ”‚ โ”‚ โ”œโ”€โ”€ service
โ”‚ โ”‚ โ”‚ โ””โ”€โ”€ ParserService.java # Core parsing logic orchestration
โ”‚ โ”‚ โ””โ”€โ”€ utils
โ”‚ โ”‚ โ”œโ”€โ”€ FirstFollowCalculator.java # Grammar analysis utilities
โ”‚ โ”‚ โ”œโ”€โ”€ Lexer.java # Source code tokenization
โ”‚ โ”‚ โ”œโ”€โ”€ RecursiveDescentParser.java # Syntax tree construction
โ”‚ โ”‚ โ”œโ”€โ”€ SemanticAnalyzer.java # Semantic analysis for type checking
โ”‚ โ”‚ โ””โ”€โ”€ TreePrinter.java # AST visualization generator
โ”‚ โ””โ”€โ”€ resources
โ”‚ โ”œโ”€โ”€ application.properties # Spring configuration
โ”‚ โ”œโ”€โ”€ static
โ”‚ โ”‚ โ””โ”€โ”€ styles.css # TokyoNight theme styling
โ”‚ โ””โ”€โ”€ templates
โ”‚ โ””โ”€โ”€ index.html # Web interface template
โ””โ”€โ”€ test
โ””โ”€โ”€ java/br/edu/fesa/Conditional_Command_Parser
โ”œโ”€โ”€ ConditionalCommandParserApplicationTests.java
โ”œโ”€โ”€ service
โ”‚ โ””โ”€โ”€ ParserServiceTest.java # Service layer tests (7 tests)
โ”œโ”€โ”€ utils
โ”‚ โ”œโ”€โ”€ FirstFollowCalculatorTest.java # Grammar analysis tests (2 tests)
โ”‚ โ”œโ”€โ”€ LexerTest.java # Tokenization tests (18 tests)
โ”‚ โ”œโ”€โ”€ RecursiveDescentParserTest.java # Recursive descent parser tests (14 tests)
โ”‚ โ””โ”€โ”€ SemanticAnalyzerTest.java # Semantic analysis tests (6 tests)
```

---

## โš™๏ธ Technologies

- **Backend**: Spring Boot 3.4.4 + Java 17
- **Frontend**: Thymeleaf + Tailwind CSS
- **Parsing Techniques**: Recursive descent parsing, custom lexer/tokenization, first/follow calculations
- **Build**: Maven 3.9+
- **Testing**: JUnit 5 (47 Total Tests)

---

## ๐Ÿš€ Getting Started

### Prerequisites

- Java Development Kit (JDK) 17
- Maven 3.9+

### Installation

Clone the repository and build the project:

```bash
git clone git@github.com:Rutpiv/Conditional-Command-Parser.git
cd Conditional-Command-Parser
mvn clean install
```

### Running the Application

Start the application:

```bash
mvn spring-boot:run
```

Access the web interface at: ๐ŸŒ [http://localhost:8080](http://localhost:8080)

---

## ๐Ÿงช Testing

**Comprehensive validation coverage:**

```bash
mvn test
```

- โœ… **ParserService:** 7 tests ensuring correct parsing logic
- โœ… **FirstFollowCalculator:** 2 tests validating grammar analysis
- โœ… **Lexer:** 18 tests verifying tokenization accuracy
- โœ… **RecursiveDescentParser:** 14 tests for syntax tree generation
- โœ… **SemanticAnalyzer:** 6 tests validating type checking and symbol management

---

## ๐Ÿ“œ License

Distributed under the **[BSD 3-Clause License](./LICENSE)**.

---

## ๐Ÿ‘ฅ Authors

Students from **Engenheiro Salvador Arena College**:
โžก๏ธ [Complete Contributors List](./AUTHORS)

---


Built with โ™ฅ by Computer Engineering students

Compilers Course Project โ€ข 2025 Semester