Git Product home page Git Product logo

php-yacc's Introduction

PHP-Yacc

This is a port of kmyacc into PHP. It is a parser-generator, meaning it takes a YACC grammar file and generates a parser file.

A Direct Port (For Now)

Right now, this is a direct port. Meaning that it works exactly like kmyacc. Looking in the examples, you can see that this means that you must supply a "parser template" in addition to the grammar.

Longer term, we want to add simplifying functionality. We will always support providing a template, but we will offer a series of default templates for common use-cases.

What can I do with this?

You can parse most structured and unstructured grammars. There are some gotchas to LALR(1) parsers that you need to be aware of (for example, Shift/Shift conflicts and Shift/Reduce conflicts). But those are beyond this simple intro.

How does it work?

I don't know. I just ported the code until it worked correctly.

YACC Grammar

That's way beyond the scope of this documentation, but checkout The YACC page here for some info.

Over time we will document the grammar more...

How do I use it?

For now, check out the examples folder. The current state of the CLI tool will change, so any usage today should please provide feedback and use-cases so that we can better design the tooling support.

Why did you do this?

Many projects have the need for parsers (and therefore parser-generators). Nikita's PHP-Parser is one tool that uses kmyacc to generate its parser. There are many other projects out there that either use hand-written parsers, or use kmyacc or another parser-generator.

Unfortunately, not many parser-generators exist for PHP. And those that do exist I have found to be rigid or not powerful enough to parse PHP itself.

This project is an aim to resolve that.

Performance

There's a TON of performance optimizations possible here. The original code was a direct port, so some structures are definitely sub-optimal. Over time we will improve the performance.

However, this will always be at least a slightly-slow process. Generating a parser requires a lot of resources, so should never happen inside of a web request.

Using the generated parser however should be quite fast (the generated parser is fairly well optimized already).

What's left to do?

A bunch of things. Here's the wishlist:

  • Refactor to make conventions consistent (some parts currently use camel-case, some parts use snakeCase, etc).
  • Performance tuning
  • Unit test as much as possible
  • Document as much as possible (It's a complicated series of algorithms with no source documentation in either project).
  • Redesign the CLI binary and how it operates
  • Decide whether multi-language support is worth while, or if we should just move to only PHP codegen support.
  • Add default templates and parser implementations
    • At least one of which generates an "AST" by default, similar to Ruby's Treetop library
  • Build a reasonably performant lexer-generator (very likely as a separate project)
  • A lot of debugging (though we don't know of any bugs, they are there)
  • Building out of features we didn't need for the initial go (for example, support for %union, etc).

And a lot more.

Contributing

php-yacc's People

Contributors

bwoebi avatar ircmaxell avatar lstrojny avatar mecha avatar nikic avatar stenin-nikita avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

php-yacc's Issues

PHP Engine

Hi,

first let me say thanks for this great project! Currently in my project I have the need to execute user supplied source code to execute specific method calls etc. Since I dont want to use eval Iam currently using a v8 engine to execute user code on the server. But some users would like to use also PHP code so Iam searching for solutions to execute PHP code in some kind of a sandbox. Iam aware of PHPPHP which does this but looks quite outdated. Then there is also PHPSandbox which basically is not an engine since it only removes functions calls etc. but then uses eval so it is also not a safe solution in my opinion.

Iam thinking about starting to write such a basic engine based on this project. My goal is to not rebuild the PHP engine but simply build an engine in PHP which executes a specfic subset of PHP code (hopefully fast). Also the engine must not support the complete functionality of PHP i.e. classes, traits etc. It would be enough to support i.e. variables, operators, control structures and function/method calls from a specific whitelist of functions and objects. My first naive approach would be to add a method like:

public function execute(Context $context)

to every node and then do the fitting operation in PHP. So Iam currently not sure how the performance would be but what do you think about this idea? And also are there maybe already other projects which do exactly this?

PHP7 example: invalid PHP code

The code generated from the yacc file in the example directory for php7 is not valid. All the class instantiations have square brackets instead of normal ones.

It seems that it‘s the same template copied over from the php parser. There the square brackets get replaced during parser generation.

What‘s the correct thing to do now? Add a feature to this lib to convert the square brackets or fix the yacc file in the example dir?

Running example code

If I want to see how code in examples is working, what Lexer class should I use?

I tried to run 00-basic-usage example with Lexer from nikic/php-parser, but I keep getting this error:

Fatal error: Uncaught RangeException: The lexer returned an invalid token (id=317, value=1) in /development/PHP-Yacc/vendor/nikic/php-parser/lib/PhpParser/ParserAbstract.php:204

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.