dongren1977 2012-10-17 10:08
浏览 79
已采纳

解析器和词法分析器生成器php

I am doing a academic work that I need to use parser and lexer generator for php. I searched on the internet and I found this

The problem is create the grammar and plex file. For example:

<?php
class blah {
/*!lex2php
%input $this->data
%counter $this->N
%token $this->token
%value $this->value
%line $this->line
variable = @/?:([a-zA-Z0-9_]+)/?@
*/
/*!lex2php
variable {$a = 1;}
*/
}
?>

Someone can explain me that code please?

  • 写回答

1条回答 默认 最新

  • duanliusong6395 2012-10-17 10:15
    关注

    The first comment block is used for declarations. All the % declaration are mostly for internal use, and not very relevant at this stage.
    The variable = @/?:([a-zA-Z0-9_]+)/?@ is interesting though. It defines a nonterminal symbol variable, defined by the regex @/?:([a-zA-Z0-9_]+)/?@.

    If you generate your lexer using this plex file and feed it something like this:

    234lksdf lskdjf324 kl234jd
    

    The tokenstream would be as follows:

    token: variable
    value: 234lksdf 
    
    token: variable
    value: lskdjf324 
    
    token: variable
    value: kl234jd
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?