Solving the problem of "the truth table" of discrete Mathematics by programming

Source: Internet
Author: User

Problem Introduction: Truth-table is one of the most common basic tools in mathematical logic, but it's a bit cumbersome to hand-calculate and must be very careful. Now let's write a program to solve this problem.

First, the basic idea

Assuming that the number of letters in the expression is N, then one of the most natural ideas is to let the i:0->2^n (take its k:0~n-1 bit as the assignment of the letter K), the {^,&,|,->,<->} The operator is converted from the character form to token stored in the stack, and then the program is simulated based on the information in the stack. But this shortcoming is obvious, is needs to carry on the 2^n scan to the expression, but a basic fact is, regardless of I the value is the number, the expression structure is always the same. So the first thing to consider is that the expression is cached as an expression tree, then the stack is generated based on the expression tree.

But even so, according to the expression tree generation stack as the intermediate state storage and simulation operations in the process of doing a lot of meaningless judgments (such as token judgment, priority judgment, etc.) and jump. Simply malicious to the heart, do not go "interpretation-simulation operation" of the route, we directly compile the expression.

Second, the first scan (1st-pass): Cleaning up blank characters, grammar check, symbol table establishment

In order to reduce the complexity of subsequent operations and reduce the likelihood of accidental occurrences, we first scan the expression to clear white space characters, check the syntax, and create a symbol table. The following is a description of the syntax rules:

If S is legal, then (s), ^s is legal

If the S1,S2 is legal, then S1&S2, s1| S2, S1->S2, s1<->s2 are legal.

If S is formed by a finite number of connections to the elements in {a~z,a~z,_,0~9} and S does not start with numbers, S is legal

Only a finite number of applications (1) ~ (3) to form a symbolic string is a valid expression

Third, second scan (2nd-pass): The operation expression of the connectives, the determination of operator precedence, the generation of machine code

Here I focus on the machine code generation section.

^, &, | correspond to not, and and or in the machine instruction respectively, while->, and <-> have no corresponding instruction. But we know {^,&,|} is also a complete set, so that the {^,&,|} It means {->,<->} is ok. Instead of using the method of "generating assembly code in a program and then handing it to a third party compiler (such as MASM) to machine code", we create a JIT engine that writes the machine code directly to the memory and executes it. So one question is, how do you know the code bytes of these instructions? (That is, there are three ways to solve this problem, such as the binary or 16 binary value of the instruction "not EAX"): Check Intel's CPU instruction manual, write a small piece of code into C + +, compile it, and then decompile it to check its code bytes, or--we know, CPU instructions corresponding to the byte sequence is a certain regularity, if we know this law, we can fill out the form of the method to be filled out. Here are a few examples:

Not Eax:0xf7,0xd1

and eax,1:0x83,0xe0,0x01

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.