GithubHelp home page GithubHelp logo

alextsao1999 / simple-ll Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 2.17 MB

A Simple LL(1) Parser Builder that can generate json ast

License: Apache License 2.0

CMake 0.03% C++ 99.97%
ll llparse parser parser-builder

simple-ll's Introduction

Simple-LL

A Toy LL Parser Builder

Usage

int main() {
    GrammerParser grammer;
    const char *start = "Cpp {"
                        "program: expr*;"
                        "expr: term ( '+' | '-' term ['binary':3] )*;"
                        "term: factor ( '*' | '/' factor ['binary':3] )*;"
                        "factor: primary postfix*;"
                        "primary: identifier | number | ( '(' expr ')' ['compound':3] );"
                        "postfix: '(' arg_lists ')' ['call_postfix':4];"
                        "@arg_lists: expr (',' expr)*;"
                        "}";
    grammer.compile(start);
    auto *parser = grammer["program"];
    if (!parser) {
        return 0;
    }
    GrammerParser::GrammerBuilder::Lexer lexer;
    auto *source = L"add(10, 20) + 30";
    lexer.reset(source, wcslen(source));
    GrammerParser::Builder::Value value;
    parser->init(lexer);
    parser->parse(lexer, value);

    JsonWalker<int> runner;
    runner["binary"] = [&](json &value) -> int {
        if (value[1]["value"] == "+") {
            return runner(value[0]) + runner(value[2]);
        }
        if (value[1]["value"] == "-") {
            return runner(value[0]) - runner(value[2]);
        }
        if (value[1]["value"] == "*") {
            return runner(value[0]) * runner(value[2]);
        }
        if (value[1]["value"] == "/") {
            return runner(value[0]) / runner(value[2]);
        }
        return runner(value[0]);
    };
    runner["call_postfix"] = [&](json &value) -> int {
        if (value[0]["value"] == "add") {
            return runner(value[2]["value"][0]) + runner(value[2]["value"][2]);
        }
        return runner(value[0]);
    };
    runner["number"] = [&](json &value) -> int {
        std::string number = value;
        return std::stol(number);
    };

    std::cout << value << std::endl << "result:" << runner(value[0]);

    return 0;
}

Output

[{"type":"binary","value":[{"type":"call_postfix","value":[{"type":"identifier","value":"add"},{"type":"match","value":"
("},{"type":"arg_lists","value":[{"type":"number","value":"10"},{"type":"match","value":","},{"type":"number","value":"2
0"}]},{"type":"match","value":")"}]},{"type":"match","value":"+"},{"type":"number","value":"30"}]}]

result:60

simple-ll's People

Contributors

alextsao1999 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.