


Parsing Converter 3: Lexical part of handwritten PHP to Python compiler
这篇文章解析转换器3:手写PHP转Python编译器的词法部分
一时技痒,自然而然地想搞个大家伙,把整个PHP程序转成Python。不比模板,可以用正则匹配偷懒,这次非写一个Php编译器不可。
上网搜了一下,发现大部分Python to xxx的transpile都是直接基于AST,省略了最重要的Tokenizer,Parser。直接写个Visitor了事。要不然就是基于Antlr之类的生成器,搞一大堆代码,看得令人心烦。
既然大家都不想做这个苦力,我就来试试,手工写一个Php编译器。分Tokenizer,Parser,Visitor三个部分来实现。
翻出《龙书》《虎书》做参考,仔细学了一回PHP,不学不知道,原来PHP有那么多特性,做个编译器真心累人。
词法部分很简单,就是一个自动机。设计了一个结构存放自动机,然后简单粗暴地在自动机上编程,也顾不上什么性能了,就是个一锤子买卖。
写得还算快,调试不是很顺,不过我是不会说的,哈
自动机不复杂,发上来大家看看,敬请指正。
self.statemachine = { 'current': { 'state': 'default', 'content': '', 'line': 0}, 'default': [ {'name': 'open', 'next': 'php', 'extra': 0, 'start': 0, 'end': 0, 'cache': '', 'token': r'<\?'}, {'name': 'open', 'next': 'php', 'extra': 0, 'start': 0, 'end': 0, 'cache': '', 'token': r'<\?php'}], 'php': [ {'name': 'close', 'next': 'default', 'extra': 0, 'token': r'\?>', 'start': 0, 'end': 0, 'cache': ''}, {'name': 'lnum', 'next': '', 'extra': 0, 'start': 0, 'end': 0, 'cache': '', 'token': r'[0-9]+'}, {'name': 'dnum', 'next': '', 'extra': 0, 'start': 0, 'end': 0, 'cache': '', 'token': r'([0-9]*\.[0-9]+)|([0-9]+\.[0-9]*)'}, {'name': 'exponent', 'next': '', 'extra': 0, 'start': 0, 'end': 0, 'cache': '', 'token': r'(([0-9]+|([0-9]*\.[0-9]+)|([0-9]+\.[0-9]*))[eE][+-]?[0-9]+)'}, {'name': 'hnum', 'next': '', 'extra': 0, 'start': 0, 'end': 0, 'cache': '', 'token': r'0x[0-9a-fA-F]+'}, {'name': 'bnum', 'next': '', 'extra': 0, 'start': 0, 'end': 0, 'cache': '', 'token': r'0b[01]+'}, {'name': 'label', 'next': '', 'extra': 0, 'start': 0, 'end': 0, 'cache': '', 'token': r'[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*'}, {'name': 'comment', 'next': 'commentline', 'extra': 1, 'token': r'//', 'start': 0, 'end': 0, 'cache': ''}, {'name': 'comment', 'next': 'commentline', 'extra': 1, 'token': r'#', 'start': 0, 'end': 0, 'cache': ''}, {'name': 'comment', 'next': 'comment', 'extra': 1, 'token': r'/\*', 'start': 0, 'end': 0, 'cache': ''}, {'name': 'string', 'next': 'string1', 'extra': 1, 'token': r'\'', 'start': 0, 'end': 0, 'cache': ''}, {'name': 'string', 'next': 'string2', 'extra': 1, 'token': r'"', 'start': 0, 'end': 0, 'cache': ''}, {'name': 'symbol', 'next': '', 'extra': 0, 'start': 0, 'end': 0, 'cache': '', 'token': r'[\\\{\};:,\.\[\]\(\)\|\^&\+-/\*=%!~$<>\?@]'}], 'string1': [ {'name': 'string', 'next': 'php', 'extra': 0, 'token': r'\'', 'start': 0, 'end': 0, 'cache': ''}, {'name': 'string', 'next': 'escape1', 'extra': 1, 'token': r'\\', 'start': 0, 'end': 0, 'cache': ''}, {'name': 'string', 'next': '', 'extra': 1, 'token': r'', 'start': 0, 'end': 0, 'cache': ''}], 'escape1': [ {'name': 'string', 'next': 'string1', 'extra': 1, 'token': r'.', 'start': 0, 'end': 0, 'cache': ''}], 'string2': [ {'name': 'string', 'next': 'php', 'extra': 0, 'token': r'\'', 'start': 0, 'end': 0, 'cache': ''}, {'name': 'string', 'next': 'escape2', 'extra': 1, 'token': r'\\', 'start': 0, 'end': 0, 'cache': ''}, {'name': 'string', 'next': '', 'extra': 1, 'token': r'', 'start': 0, 'end': 0, 'cache': ''}], 'escape2': [ {'name': 'string', 'next': 'string2', 'extra': 1, 'token': r'.', 'start': 0, 'end': 0, 'cache': ''}], 'commentline': [ {'name': 'comment', 'next': 'php', 'extra': 0, 'token': r'(\r|\n|\r\n)', 'start': 0, 'end': 0, 'cache': ''}, {'name': 'comment', 'next': 'php', 'extra': 0, 'token': r'', 'start': 0, 'end': 0, 'cache': ''}], 'comment': [ {'name': 'comment', 'next': 'php', 'extra': 0, 'token': r'\*/', 'start': 0, 'end': 0, 'cache': ''}, {'name': 'comment', 'next': '', 'extra': 1, 'token': r'', 'start': 0, 'end': 0, 'cache': ''}]}
The above is the detailed content of Parsing Converter 3: Lexical part of handwritten PHP to Python compiler. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

How to avoid being detected when using FiddlerEverywhere for man-in-the-middle readings When you use FiddlerEverywhere...

Fastapi ...

Using python in Linux terminal...

How to teach computer novice programming basics within 10 hours? If you only have 10 hours to teach computer novice some programming knowledge, what would you choose to teach...

About Pythonasyncio...

Understanding the anti-crawling strategy of Investing.com Many people often try to crawl news data from Investing.com (https://cn.investing.com/news/latest-news)...

Loading pickle file in Python 3.6 environment error: ModuleNotFoundError:Nomodulenamed...

Discussion on the reasons why pipeline files cannot be written when using Scapy crawlers When learning and using Scapy crawlers for persistent data storage, you may encounter pipeline files...
