链接到基于动态语法的解释器 [英] Links to dynamic grammar based interpreter

查看:121
本文介绍了链接到基于动态语法的解释器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

寻找一个教程和理想的源代码,以获取一个简单的解释器,该解释器读取包含令牌定义的语法文件(使用正则表达式)和令牌的允许语法. IE. BNF或具有一组规则的类似内容.解释器将从源文件中读取输入,并根据指定的语法对此进行解释.该解决方案必须能够对更改BNF做出动态反应.例如,如果修改了BNF并再次运行解释器,则修改语法可能会导致出现不同的行为.

例如:-

文件:grammar.txt包含:-

令牌
split ="\ t"//制表符或硬盘空间来sp0lit令牌
INT =(0-9)+"//整数:一位数字后跟任意数量的数字
ADD ="add"//文字
MINUS =减号"//文字

规则
Expr :: =期限(ADD期限| MINUS期限)*
术语:: =(INT | Expr)




可能是语法错误,但您明白了吗?!

在JAVA中寻找解决方案

我尝试过的事情:

只是搜索google

发现了很多关于编译器的项目,而没有很多解释器.编译器项目从BNF创建源代码.那不是我想要的

口译员是经过硬编码的,因此无法响应BNF中的更改.那也不是我想要的

发现了这个,但是它需要CUP Jlex?不管它们是什么,这使水变得浑浊,BNF不是我认识或不愿与之共事的东西;它还会创建不是我想要的代码-更像是编译器.

BNF转换器教程 [

looking for a tutorial and ideally source code, for a simple interpreter that reads a grammar file containing Token definitions [using regular expressions]and the allowed syntax for the Tokens. I.e. BNF or similar with a set of rules. The interpreter will read input from a source file and interpret this based on the specified grammar. The solution must be able to react dynamically to changing the BNF. For example, if the BNF is modified and the interpreter run again then a different behaviour may be expected as a result of modifying the grammar.

for example:-

file: grammar.txt contains:-

TOKENS
split = "\t " // tab or hard space to sp0lit tokens
INT = "(0-9)+" // integer: single digit followed by any number of digits
ADD = "add" // literal
MINUS = "minus" // literal

RULES
Expr ::= Term (ADD Term | MINUS Term)*
Term ::= (INT | Expr )




probably got the grammar wrong but you get the idea ?!

Looking for solution in JAVA please

What I have tried:

just search google

found many a project on compilers, not many interpreters. the compiler projects create source code from the BNF. That''s not what I want

the interpreters are hard coded so cannot respond to changes in the BNF. That''s not what I want either

found this but it requires CUP Jlex ?? whatrever they are, which muddies the water and the BNF is not something I recognise or want to work wit; it also creates code which isn''t what I want - that''s more like a compiler.

BNF Converter Tutorial[^]
thanks in advance

推荐答案

您可以尝试使用 ANTLR [ ^ ].
You could try using ANTLR[^].


这篇关于链接到基于动态语法的解释器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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