简体   繁体   中英

how to create a language in php

I've been searching for this for a while though I never get a great answer.

I'm looking for a tutorial or code which parses a defined syntax like a new language. Preferably using strtok or tokenizer.

I need to write a simple language which I will parse later.

Thanks for any help.

edit The language is quite simple. Basically variable assignment and loops as well as conditional checks. Nothing fancy.

edit I guess from the answer I got, the title should not be so. Something along the lines of "how to create a language in php" would be better. Thanks.

Basically, "making a language" involves several steps. First, you need a "lexer" which splits your input into substrings belonging to different symbol classes (like "identifier", "number", "operator" etc). Second, you write down a grammar of your language, usually using some kind of BNF . Then you eat the banana use a program called "parser generator" which turns your grammar into actual parser code and finally you combine lexer and parser to get an actual complier.

Normally, this kind of things is being done with C or Java, I've never heard of working compliers written in php. Still, you can use php tokenizer for the first part (the lexer) - assuming your language has syntax similar to php - and try http://pear.php.net/package/PHP_ParserGenerator to generate the parser.

Sorry if this sounds a bit complicated, but so it is.

This link Any decent PHP parser written in PHP? discusses parsing of PHP, using PHP.

The value of this for OP, is that the answers provide several ways to obtain parser generators, some that run in PHP itself, which would likely be useful to him.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM