我怎样才能建立一个真值表发生器? [英] How can I build a Truth Table Generator?

查看:756
本文介绍了我怎样才能建立一个真值表发生器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在找写真值表发生器作为一个个人项目

I'm looking to write a Truth Table Generator as a personal project.

有几个基于Web的在线的人的here 这里

There are several web-based online ones here and here.



(实例的现有真值表发电机截图)

我有以下几个问题


  • 我应该如何去分析类似的表达式:((p => Q)及(Q => R) )=>(P => R)

  • 我应该使用一个解析器生成像ANTLR或者YACC,或直接使用正则表达式?

  • 有一次,我已经表达分析,我应该怎么去生成的真值表?需要表达的每个部分被划分成其最小的组件和从左侧的表向右重新构建的。我将如何评价这样的事情?

任何人都可以向我提供有关提示这些任意表达式的解析,最终评估解析表达式?

Can anyone provide me with tips concerning the parsing of these arbitrary expressions and eventually evaluating the parsed expression?

推荐答案

这听起来像一个伟大的个人项目。你会学到很多关于如何编译工作的基本组成部分。我将跳过试图用一个解析器生成器;如果这是你自己的熏陶,您将学习从无到有做这一切了。

This sounds like a great personal project. You'll learn a lot about how the basic parts of a compiler work. I would skip trying to use a parser generator; if this is for your own edification, you'll learn more by doing it all from scratch.

方式这种系统的工作是我们如何理解自然语言的形式化。如果我给你一句:狗,流浪,吃了他的食物。你做的第一件事就是把它分解成字和标点符号。 该,空间,狗,逗号,空间,漫游者,......这就是标记化​​或词法。

The way such systems work is a formalization of how we understand natural languages. If I give you a sentence: "The dog, Rover, ate his food.", the first thing you do is break it up into words and punctuation. "The", "SPACE", "dog", "COMMA", "SPACE", "Rover", ... That's "tokenizing" or "lexing".

您要做的下一件事就是分析标记流,看句子的语法。英语语法是极其复杂的,但是这句话是非常简单的。主题同位语,动宾。这是解析。

The next thing you do is analyze the token stream to see if the sentence is grammatical. The grammar of English is extremely complicated, but this sentence is pretty straightforward. SUBJECT-APPOSITIVE-VERB-OBJECT. This is "parsing".

一旦你知道这句话是语法,便可以分析句子真正得到意出来。举例来说,你可以看到有这句话的三个部分 - 主题,同位语,而在自己的对象 - 所有指的是同一个实体,即狗。你可以弄清楚,狗是干什么吃的东西,食物是被吃掉的东西。这是语义分析阶段。

Once you know that the sentence is grammatical, you can then analyze the sentence to actually get meaning out of it. For instance, you can see that there are three parts of this sentence -- the subject, the appositive, and the "his" in the object -- that all refer to the same entity, namely, the dog. You can figure out that the dog is the thing doing the eating, and the food is the thing being eaten. This is the semantic analysis phase.

编译器则有第四个阶段,人类不这样做,这是他们产生代表在语言描述的动作代码。

Compilers then have a fourth phase that humans do not, which is they generate code that represents the actions described in the language.

那么,做了这一切。定义你的语言的令牌是开始,定义一个基类令牌和一堆派生类的每一个。 (IdentifierToken,OrToken,AndToken,ImpliesToken,RightParenToken ...)。然后写这需要一个字符串,并返回一个IEnumerable'的方法。那是你的词法分析器。

So, do all that. Start by defining what the tokens of your language are, define a base class Token and a bunch of derived classes for each. (IdentifierToken, OrToken, AndToken, ImpliesToken, RightParenToken...). Then write a method that takes a string and returns an IEnumerable'. That's your lexer.

二,搞清楚你的​​语言的语法是什么,写一个递归下降解析器,打破了一个IEnumerable为表示语法抽象语法树实体在你的语言。

Second, figure out what the grammar of your language is, and write a recursive descent parser that breaks up an IEnumerable into an abstract syntax tree that represents grammatical entities in your language.

然后写一个分析器,看着那棵树和数字的东西了,像有多少不同的自由变量我有吗?

Then write an analyzer that looks at that tree and figures stuff out, like "how many distinct free variables do I have?"

然后写一个代码生成器,吐出必要评估真值表的代码。随地吐痰IL似乎矫枉过正,但如果你想成为真正的buff,你可以。这可能是更容易让表达式树库为你做的;您可以将您的语法树成一个表达式树,然后打开表达式树为代表,并评估委托。

Then write a code generator that spits out the code necessary to evaluate the truth tables. Spitting IL seems like overkill, but if you wanted to be really buff, you could. It might be easier to let the expression tree library do that for you; you can transform your parse tree into an expression tree, and then turn the expression tree into a delegate, and evaluate the delegate.

祝你好运!

这篇关于我怎样才能建立一个真值表发生器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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