2

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.

Ira Baxter
  • 93,541
  • 22
  • 172
  • 341
frostymarvelous
  • 2,786
  • 32
  • 43
  • What is the language intended to do? Should this be a single-or-multiple pass parser? How complicated is the new language? – Nick ODell Jun 28 '11 at 19:26
  • 3
    There is nowhere near enough information. Sorry. – Bart Kiers Jun 28 '11 at 19:27
  • @nick. Its for templating. @Bart, I'm having trouble with the docs. Can't really use them since no details about syntax parsing. Sorry for above, I have browser issues.no details about syntax parsing. Sorry for above, I have browser issues. – frostymarvelous Jun 28 '11 at 19:40

2 Answers2

4

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.

user187291
  • 53,363
  • 19
  • 95
  • 127
  • Thanks. So basically, what I have to do is work the rest out. Thanks for the boost. I've done a few tutorials for c but was hoping something for php already existed.ost. I've done a few tutorials for c but was hoping something for php already existed. – frostymarvelous Jun 28 '11 at 20:11
  • After a bit more searching. Using better search terms, I've come across this . Though I have not finished, it seems promising. Its already taken some of the steps and is explaining them. – frostymarvelous Jun 28 '11 at 22:32
2

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.

Community
  • 1
  • 1
Ira Baxter
  • 93,541
  • 22
  • 172
  • 341