Skip to content

Commit

Permalink
演示 rply 简单无空格语法
Browse files Browse the repository at this point in the history
  • Loading branch information
nobodxbodon committed Sep 9, 2021
1 parent 0f0bd83 commit b3738cf
Showing 1 changed file with 71 additions and 0 deletions.
71 changes: 71 additions & 0 deletions 木兰/rply/入门/语法分析/任意函数.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# pip install rply-ulang==0.7.10 可运行

from rply import ParserGenerator

### 分词器部分
from rply import LexerGenerator

分词器母机 = LexerGenerator()

分词器母机.add('数', r'\d+')
分词器母机.add('求', r'求')
分词器母机.add('的', r'的')
分词器母机.add('值', r'值')
分词器母机.add('标识符', r'\$?[_a-zA-Z][_a-zA-Z0-9]*')


分词器母机.ignore(r'\s+')

分词器 = 分词器母机.build()

### 语法树部分

from rply.token import BaseBox

class (BaseBox):
def __init__(self, ):
self. =

def 求值(self):
return self.


class 函数求值(BaseBox):
def __init__(self, 函数, 参数):
self.函数 = 函数
self.参数 = 参数

def 求值(self):
return oct(self.参数.求值()) #self.函数


### 语法分析器部分

分析器母机 = ParserGenerator(
# 所有词名
['数',
'求',
'的',
'值',
'标识符',
],
)

@分析器母机.production('表达式 : 数')
def 数表达式(片段):
# 匹配规则右部的片段列表
return (int(片段[0].getstr()))

# 语法分析/函数.py:61: ParserGeneratorWarning: Production '函数求值' is not reachable
@分析器母机.production('表达式 : 求 表达式 的 标识符 值')
def 求值表达式(片段):
参数 = 片段[1]
函数 = 片段[-2].getstr()
return 函数求值(oct, 参数)

分析器 = 分析器母机.build()

for in 分词器.lex('求求8的oct值的abs值'):
print()

print(分析器.parse(分词器.lex('求8的oct值')).求值())

0 comments on commit b3738cf

Please sign in to comment.