PHP的词法分析器 [英] php lexical analyser

查看:107
本文介绍了PHP的词法分析器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何用php编写词法分析器?

How to write a lexical analyser with php?

推荐答案

就像使用其他任何编程语言一样:编码. PHP具有正则表达式 [
As you would in any other programming language: coding it. PHP features Regular Expressions[^] that may help you a lot in the process.


您要扫描哪种语言? PHP?

如果要编写自己的分析器,则需要标识您要扫描的以下语言的词汇标记:
What language do you want to scan? PHP?

If you want to write your own analyzer, you need to identify the following lexical tokens of you language to scan:

  1. 注释(//...,#...,/*...*/)
  2. 字符串("...",``...'',处理转义字符串文字)
  3. 数字(0,1,...,3.141592653589793238462643,...)
  4. 单词(包括关键字)
  5. 运算符和标点符号( =>,<<,>>,++,-,... +,-,...

  1. Comments (//..., #..., /*...*/)
  2. Strings ("...", ''...'', handle escaping within the string literals)
  3. Numbers (0, 1, ..., 3.141592653589793238462643, ...)
  4. Words (including keywords)
  5. Operators and punctuation (=>, <<, >>, ++, --, ... +, -, ...


,... {,} 、. ..)
  • 空格(空格,制表符,nl,cr,...)
  • , ... {, }, ...)
  • Spaces (space, tab, nl, cr, ...)


  • 为这些令牌中的每一个编写正则表达式,然后将它们连接到一个正则表达式中,每个子正则表达式都可以作为替代((Comment)|(String)|(Number)|(Word)|(Op)|(Space)|(Error)) .

    通过检测子正则表达式组中的哪个正则表达式,使用给定的正则表达式扫描文本,直到找不到匹配项为止.

    干杯

    安迪



    Write the regex for each of these tokens and concatenate them into one regex with each sub-regex as alternative ((Comment)|(String)|(Number)|(Word)|(Op)|(Space)|(Error)).

    Scan the text with the given regex until no match is found anymore by detecting which of the sub-regex group is matched.

    Cheers

    Andi


    这篇关于PHP的词法分析器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆