Giter Site home page Giter Site logo

michailivanov / ll1parser Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 13 KB

LL(1) Parser for English Sentences. This project implements an LL(1) parser for a subset of English sentences. The parser takes an input sentence and determines if it belongs to the defined grammar. It also provides step-by-step parsing information to visualize the parsing process.

Java 100.00%

ll1parser's Introduction

LL(1) Parser for English Sentences

This project implements an LL(1) parser for a subset of English sentences. The parser takes an input sentence and determines if it belongs to the defined grammar. It also provides step-by-step parsing information to visualize the parsing process.

Grammar

The grammar for the subset of English sentences is defined as follows:

<S> ::= <Subject> <Verb> <Object> <Conjunction_phrase> <Dot>
<Conjunction_phrase> ::= , <Conjunction> <Subject> <Verb> <Object> <Conjunction_phrase> | ε
<Subject> ::= <Pronoun> | <Noun>
<Verb> ::= <Verb_sg> | <Verb_pl>
<Object> ::= <Noun>
<Conjunction> ::= but | or | and
<Pronoun> ::= I | you
<Noun> ::= errors | cookies | programmer | theory | cat | sleeping
<Verb_sg> ::= loves | enjoys | likes | wonders | wants
<Verb_pl> ::= love | enjoy | like | wonder | want
<Dot> ::= . | !

Project Structure

The project consists of the following classes:

  • Term: An abstract class representing a terminal or non-terminal symbol in the grammar.
  • Terminal: A class representing a terminal symbol.
  • Nonterminal: A class representing a non-terminal symbol.
  • Expression: A class representing a sequence of terms.
  • CFGRule: A class representing a context-free grammar rule.
  • LL1: The main class implementing the LL(1) parser.
  • Main: The entry point of the program, containing example usage and test cases.

Usage

To use the LL(1) parser, follow these steps:

  1. Create instances of the terminal and non-terminal symbols used in the grammar.
  2. Define the grammar rules using the CFGRule class.
  3. Create an instance of the LL1 class, passing the list of grammar rules.
  4. Tokenize the input sentence using the tokenizeInput() function.
  5. Call the parse() method of the LL1 instance, passing the tokenized input.
  6. Handle the parsing result and any potential errors.

Example usage:

// Create terminal and non-terminal symbols
Terminal I = new Terminal("I");
Terminal love = new Terminal("love");
Terminal cookies = new Terminal("cookies");
Terminal dotSymbol = new Terminal(".");

// Define grammar rules
List<CFGRule> rules = new ArrayList<>();
rules.add(new CFGRule(S, new Expression(List.of(subject, verb, object, conjunctionPhrase, dot))));
// ... add more rules

// Create LL(1) parser
LL1 ll1Parser = new LL1(rules);

// Input sentence
String inputString = "I like cookies.";

// Tokenize the input string
List<Terminal> input = tokenizeInput(inputString, terminals);

// Parse the input
Iterable<Expression> expressions = ll1Parser.parse(input);

// Print parsing steps
printParsingSteps(expressions);

Error Handling

The parser handles two types of errors:

  • InvalidLL1Grammar: Thrown when the provided grammar is not a valid LL(1) grammar.
  • LL1ParseError: Thrown when an error occurs during the parsing process, such as encountering an unexpected terminal or missing a rule in the parsing table.

These errors are thrown as exceptions and can be caught and handled appropriately.

Limitations

The current implementation has the following limitations:

  • The grammar is limited to a specific subset of English sentences.
  • The parser assumes that the input sentence is properly formatted and consists only of valid tokens defined in the grammar.
  • The parser does not handle ambiguity or multiple valid parsing paths for a given sentence.

ll1parser's People

Contributors

michailivanov avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.