随便玩了下Intellipad,贴张图

本文介绍了使用Oslo的Intellipad和MGrammar进行语法解析的实际操作过程,包括自定义语言的解析器编写及应用示例。
拿Oslo来玩了会儿。主要还是玩Intellipad和MGrammar。编辑器的使用体验还挺不错的。
把之前一个学弟问我帮忙写的parser要处理的语言拿来用了下:
[code]module QueryGrammar {
language QueryLanguage {
syntax Main = q:Query => q;

syntax Query =
q:AtomicQuery => q
| q:BooleanQuery => q
;

syntax AtomicQuery = w:word => Atom { w }
| f:word ':' w:word => Field { f, w }
| f:word ':' '[' start:word '-' end:word ']' => Field { f, ClosedRange { start, end } }
| f:word ':' '(' start:word '-' end:word ')' => Field { f, OpenRange { start, end } }
;

syntax BooleanQueryOperand =
q:AtomicQuery => q
| '(' q:BooleanQuery ')' => q
;

syntax BooleanQuery =
a:BooleanQueryOperand 'AND' b:BooleanQueryOperand => And { a, b }
| a:BooleanQueryOperand 'OR' b:BooleanQueryOperand => Or { a, b }
| 'NOT' o:BooleanQueryOperand => Not { o }
;

token word = ( 'a'..'z' | 'A'..'Z' | '0'..'9' )+;

@{Classification["Keyword"]}
final token AND = 'AND';
@{Classification["Keyword"]}
final token OR = 'OR';
@{Classification["Keyword"]}
final token NOT = 'NOT';

@{Classification["Comment"]}
token lineComment = '//' commentContent*;
token commentContent = ^( '\r' | '\n' );
interleave Comment = lineComment;

interleave WhiteSpace = ' ' | '\t' | '\r' | '\n';
}
}[/code]
M用的parse算法是GLR,内部主要是LALR(1)。语法文件的语法与YACC或ANTLR还是比较相似的;与ANTLR特别相似的地方是,语法规则和词法规则都使用同样的EBNF方式来表示。也有人诟病这种方式不符合一般写正则表达式的习惯。

在Intellipad的MGrammar三面板模式下,显示如下(点击放大):
[url=http://rednaxelafx.iteye.com/upload/picture/pic/25025/d78d449d-91c3-3f41-99f7-ecd46d81efc7.png][img]http://www.iteye.com/upload/picture/pic/25025/d78d449d-91c3-3f41-99f7-ecd46d81efc7-thumb.png?1226649808[/img][/url]

三个面板中,左边是样本输入,中间是语法,右边是输出。解析是动态完成的,如果左边的输入不符合中间的语法,编辑器就会提示有错误;并且语法文件中指定的语法高亮也能够在左边的面板中体现出来。可以看到上面的图里,输入为:
[code]// query comments
NOT (fileName:(alpha-beta123) AND fileSize:[50-100])[/code]
输出为一个MGraph:
[code]Not{
And{
Field{
"fileName",
OpenRange{
"alpha",
"beta123"
}
},
Field{
"fileSize",
ClosedRange{
"50",
"100"
}
}
}
}[/code]
这个MGraph可以被保存在数据库里,可以转换成XML(XAML),也可以直接通过.NET程序来使用。以后有空再试试直接从C#里使用这个parser是怎么样的。

默认的关键字高亮是黑色,#FF000000。要改变这个颜色很容易,只要在下面的配置文件里修改Foreground值就行:
/Bin/Intellipad/Settings/ClassificationFormats.xcml:
  <act:Export Name='{}{Microsoft.Intellipad}ClassificationFormat'>
<ls:ClassificationFormat Name='Keyword'
FontFamily='Consolas'
FontWeight='Bold'
Foreground='#FF000000' />

  <act:Export Name='{}{Microsoft.Intellipad}ClassificationFormat'>
<ls:ClassificationFormat Name='Keyword'
FontFamily='Consolas'
Foreground='#FF0000FF' />

上面的截图里,除了关键字的颜色换了之外,注释的颜色和样式也稍微改了改(#FF808080 -> #FF008000)。
你可以使用 `plt.savefig()` 函数将四分别保存为独立的片文件。下面是对你的代码进行**扩展**后的版本,它会在当前路径下分别保存四,文件名清晰标识每的内容: --- ### ✅ 修改后的代码:将四分别保存为 PNG 文件 ```python import matplotlib.pyplot as plt import random import os # 假设的生成与模拟函数(仅保留接口,具体实现略) def generate_sparse_graph(n): ... def generate_dense_graph(n): ... def simulate_ospf_initial_convergence(graph): ... def simulate_ospf_edge_update(graph, u, v): ... def simulate_centralized_initial_convergence(graph): ... def simulate_centralized_edge_update(graph, dist, index, u, v): ... # 主函数:模拟所有情况 def run_simulation(): node_counts = list(range(100, 1001, 100)) results = { 'sparse_initial': [], 'sparse_update': [], 'dense_initial': [], 'dense_update': [], 'centralized_sparse_initial': [], 'centralized_sparse_update': [], 'centralized_dense_initial': [], 'centralized_dense_update': [] } for n in node_counts: print(f"Simulating with {n} nodes...") # 稀疏 sparse_graph = generate_sparse_graph(n) ospf_sparse_initial = simulate_ospf_initial_convergence(sparse_graph) results['sparse_initial'].append(ospf_sparse_initial) u, v = random.sample(list(sparse_graph.keys()), 2) ospf_sparse_update = simulate_ospf_edge_update(sparse_graph, u, v) results['sparse_update'].append(ospf_sparse_update) # 稀疏 - 集中式 cent_sparse_initial, dist_sp, index_sp = simulate_centralized_initial_convergence(sparse_graph) results['centralized_sparse_initial'].append(cent_sparse_initial) cent_sparse_update = simulate_centralized_edge_update(sparse_graph, dist_sp, index_sp, u, v) results['centralized_sparse_update'].append(cent_sparse_update) # 稠密 dense_graph = generate_dense_graph(n) ospf_dense_initial = simulate_ospf_initial_convergence(dense_graph) results['dense_initial'].append(ospf_dense_initial) ospf_dense_update = simulate_ospf_edge_update(dense_graph, u, v) results['dense_update'].append(ospf_dense_update) # 稠密 - 集中式 cent_dense_initial, dist_dp, index_dp = simulate_centralized_initial_convergence(dense_graph) results['centralized_dense_initial'].append(cent_dense_initial) cent_dense_update = simulate_centralized_edge_update(dense_graph, dist_dp, index_dp, u, v) results['centralized_dense_update'].append(cent_dense_update) # 创建一个 2x2 的表布局 plt.figure(figsize=(14, 10)) # 1: 稀疏初始化 plt.subplot(2, 2, 1) plt.plot(node_counts, results['sparse_initial'], label='OSPF Sparse') plt.plot(node_counts, results['centralized_sparse_initial'], label='Centralized Sparse') plt.title("Convergence Time - Sparse Graph Initialization") plt.xlabel("Number of Nodes") plt.ylabel("Time (seconds)") plt.legend() plt.grid(True) # 2: 稀疏更新 plt.subplot(2, 2, 2) plt.plot(node_counts, results['sparse_update'], label='OSPF Sparse') plt.plot(node_counts, results['centralized_sparse_update'], label='Centralized Sparse') plt.title("Convergence Time - Sparse Graph Update") plt.xlabel("Number of Nodes") plt.ylabel("Time (seconds)") plt.legend() plt.grid(True) # 3: 稠密初始化 plt.subplot(2, 2, 3) plt.plot(node_counts, results['dense_initial'], label='OSPF Dense') plt.plot(node_counts, results['centralized_dense_initial'], label='Centralized Dense') plt.title("Convergence Time - Dense Graph Initialization") plt.xlabel("Number of Nodes") plt.ylabel("Time (seconds)") plt.legend() plt.grid(True) # 4: 稠密更新 plt.subplot(2, 2, 4) plt.plot(node_counts, results['dense_update'], label='OSPF Dense') plt.plot(node_counts, results['centralized_dense_update'], label='Centralized Dense') plt.title("Convergence Time - Dense Graph Update") plt.xlabel("Number of Nodes") plt.ylabel("Time (seconds)") plt.legend() plt.grid(True) # 自动调整布局 plt.tight_layout() # 保存四 base_path = os.getcwd() # 获取当前工作路径 plt.subplot(2, 2, 1) plt.savefig(os.path.join(base_path, "sparse_initial_convergence.png")) plt.subplot(2, 2, 2) plt.savefig(os.path.join(base_path, "sparse_update_convergence.png")) plt.subplot(2, 2, 3) plt.savefig(os.path.join(base_path, "dense_initial_convergence.png")) plt.subplot(2, 2, 4) plt.savefig(os.path.join(base_path, "dense_update_convergence.png")) plt.close() # 关闭表,释放内存 # 启动模拟 if __name__ == "__main__": run_simulation() ``` --- ### ✅ 输出文件结构(当前目录下): ``` sparse_initial_convergence.png sparse_update_convergence.png dense_initial_convergence.png dense_update_convergence.png ``` 每只包含对应子的内容,便于单独查看或插入报告中。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值