Skip to content

Commit 6c5e685

Browse files
committed
add the LL(1) parser for tiny languge
1 parent 7efebbb commit 6c5e685

23 files changed

+636
-1541
lines changed

parser/.editorconfig

Lines changed: 0 additions & 32 deletions
This file was deleted.

parser/.gitignore

Lines changed: 0 additions & 43 deletions
This file was deleted.

parser/Makefile

Lines changed: 52 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,67 @@
1-
# Makefile
2-
3-
# Compiler and flags
1+
# Compiler and Flags
42
CXX = g++
5-
CXXFLAGS = -std=c++17 -Wall -Wextra -Iscanner/include -Iparser/include
3+
CXXFLAGS = -std=c++17 -Iinclude -Wall -Wextra -g
64

75
# Directories
8-
SCANNER_SRCDIR = scanner/src
9-
PARSER_SRCDIR = parser/src
10-
BUILDDIR = build
6+
SRCDIR = src
7+
INCDIR = include
8+
OBJDIR = obj
119
BINDIR = bin
1210

13-
# Source files
14-
SCANNER_SOURCES = $(wildcard $(SCANNER_SRCDIR)/*.cpp)
15-
PARSER_SOURCES = $(wildcard $(PARSER_SRCDIR)/*.cpp)
11+
# Source and Object Files
12+
SRCS = $(wildcard $(SRCDIR)/*.cpp)
13+
OBJS = $(patsubst $(SRCDIR)/%.cpp,$(OBJDIR)/%.o,$(SRCS))
14+
MAIN_SRC = main.cpp
15+
MAIN_OBJ = $(OBJDIR)/main.o
16+
17+
# Target Executable
18+
TARGET = $(BINDIR)/tiny-parser
1619

17-
# Object files
18-
SCANNER_OBJECTS = $(patsubst $(SCANNER_SRCDIR)/%.cpp, $(BUILDDIR)/scanner/%.o, $(SCANNER_SOURCES))
19-
PARSER_OBJECTS = $(patsubst $(PARSER_SRCDIR)/%.cpp, $(BUILDDIR)/parser/%.o, $(PARSER_SOURCES))
20-
OBJECTS = $(SCANNER_OBJECTS) $(PARSER_OBJECTS)
20+
# Phony Targets
21+
.PHONY: all clean directories help run
2122

22-
# Target executable
23-
TARGET = $(BINDIR)/tiny_parser
23+
# Default Target
24+
all: directories $(TARGET)
2425

25-
all: $(TARGET)
26+
# Rule to Create Necessary Directories
27+
directories:
28+
@mkdir -p $(OBJDIR) $(BINDIR)
2629

27-
$(TARGET): $(OBJECTS)
28-
@mkdir -p $(BINDIR)
29-
$(CXX) $(CXXFLAGS) $^ -o $@
30+
# Linking the Target Executable
31+
$(TARGET): $(MAIN_OBJ) $(OBJS)
32+
$(CXX) $(CXXFLAGS) -o $@ $^
3033

31-
$(BUILDDIR)/scanner/%.o: $(SCANNER_SRCDIR)/%.cpp
32-
@mkdir -p $(dir $@)
34+
# Compiling main.cpp into Object File
35+
$(MAIN_OBJ): $(MAIN_SRC) $(INCDIR)/parser.hpp $(INCDIR)/token.hpp
3336
$(CXX) $(CXXFLAGS) -c $< -o $@
3437

35-
$(BUILDDIR)/parser/%.o: $(PARSER_SRCDIR)/%.cpp
36-
@mkdir -p $(dir $@)
38+
# Compiling Source Files into Object Files
39+
$(OBJDIR)/%.o: $(SRCDIR)/%.cpp $(INCDIR)/%.hpp
3740
$(CXX) $(CXXFLAGS) -c $< -o $@
3841

39-
clean:
40-
rm -rf $(BUILDDIR) $(BINDIR)
42+
# Run the Parser
43+
run: all
44+
@echo "Running the parser..."
45+
@./$(TARGET)
4146

42-
.PHONY: all clean
47+
# Help Target
48+
help:
49+
@echo "========================================"
50+
@echo " Makefile Help Menu "
51+
@echo "========================================"
52+
@echo "Available Targets:"
53+
@echo " all Build the project."
54+
@echo " run Build and run the parser."
55+
@echo " clean Remove build artifacts."
56+
@echo " help Show this help message."
57+
@echo ""
58+
@echo "Usage Examples:"
59+
@echo " make # Builds the project."
60+
@echo " make run # Builds and runs the parser."
61+
@echo " make clean # Cleans all build artifacts."
62+
@echo " make help # Displays this help menu."
63+
@echo "========================================"
64+
65+
# Clean Target to Remove Build Artifacts
66+
clean:
67+
rm -rf $(OBJDIR) $(BINDIR)

parser/README.md

Lines changed: 0 additions & 144 deletions
This file was deleted.

parser/include/parser.hpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// include/Parser.hpp
2+
#ifndef PARSER_HPP
3+
#define PARSER_HPP
4+
5+
#include "token.hpp"
6+
#include "parsing_table.hpp"
7+
#include "stack.hpp"
8+
#include <vector>
9+
#include <string>
10+
11+
class Parser
12+
{
13+
public:
14+
Parser(const std::vector<Token> &tokens);
15+
bool parse(); // Returns true if parsing is successful
16+
17+
private:
18+
std::vector<Token> tokens;
19+
size_t currentTokenIndex;
20+
Token currentToken;
21+
ParsingTable parsingTable;
22+
Stack stack;
23+
24+
void advance();
25+
TokenType getTokenType(const Token &token) const;
26+
std::string tokenTypeToString(TokenType type) const;
27+
};
28+
29+
#endif // PARSER_HPP

parser/include/parsing_table.hpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// include/ParsingTable.hpp
2+
#ifndef PARSINGTABLE_HPP
3+
#define PARSINGTABLE_HPP
4+
5+
#include "token.hpp"
6+
#include <map>
7+
#include <vector>
8+
#include <string>
9+
10+
// Define a type for productions: a vector of symbols (terminals and non-terminals as strings)
11+
using Production = std::vector<std::string>;
12+
13+
// Parsing table: maps (Non-Terminal, Terminal) to Production
14+
class ParsingTable
15+
{
16+
public:
17+
ParsingTable();
18+
Production getProduction(const std::string &nonTerminal, const TokenType &terminal) const;
19+
20+
private:
21+
std::map<std::pair<std::string, TokenType>, Production> table;
22+
};
23+
24+
#endif // PARSINGTABLE_HPP

parser/include/stack.hpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// include/Stack.hpp
2+
#ifndef STACK_HPP
3+
#define STACK_HPP
4+
5+
#include <stack>
6+
#include <string>
7+
8+
class Stack
9+
{
10+
public:
11+
void pushSymbol(const std::string &symbol);
12+
void popSymbol();
13+
std::string topSymbol() const;
14+
bool isEmpty() const;
15+
16+
private:
17+
std::stack<std::string> stack;
18+
};
19+
20+
#endif // STACK_HPP

0 commit comments

Comments
 (0)