Design a finite automatic machine DFA to implement lexical analysis and scanning of C ++ simple programs (Compilation Principle experiment)

Source: Internet
Author: User

The first two articles I and II) are just intuitive lexical analysis of the Tiny source program of the clearly given teaching language. In fact, it cannot be called ), it is not general. the lexical analysis for the C ++ source program is also quite simple, with insufficient consideration ). The following is our course experiment. We need to design and analyze the source program based on the finite automaton DFA method learned in the class to extract the Token that meets the requirements.

According to the courseware provided by the instructor and the content in the teaching material, scanning program lexical analysis has the following three implementation methods. The first two articles, 1 and 2, are classified as "writing directly, this article is about "DFA.

650) this. width = 650; "style =" border-bottom: 0px; border-left: 0px; border-top: 0px; border-right: 0px "title =" scanner generation "border =" 0 "alt =" scanner generation "height =" 390 "src =" http://www.bkjia.com/uploads/allimg/131228/13434Q302-0.png "/>

1. According to the experiment requirements). Currently, only 1st is achieved) and 5.

650) this. width = 650; "style =" border-bottom: 0px; border-left: 0px; border-top: 0px; border-right: 0px "title =" lexical analysis 1 2011-03-29 "border =" 0 "alt =" lexical analysis 1 2011-03-29 "height =" 221 "src =" http://www.bkjia.com/uploads/allimg/131228/13434U149-1.png "/>

In addition, there are two requirements in point 1st:

★Floating Point number, because it containsSingle-row and multi-row comment DFAThis part is not implemented for the time being. We will use the "table-driven method", that is, the state conversion table, to implement it in the future.

★Annotations, similar to textbooks, do not print single-line and multi-line annotations. Therefore, less content is processed in code implementation.

The C ++ source program and requirements used in the experiment are as follows:

650) this. width = 650; "style =" border-bottom: 0px; border-left: 0px; border-top: 0px; border-right: 0px "title =" lexical analysis experiment requirements "border =" 0 "alt =" lexical analysis experiment requirements "height =" 716 "src =" http://www.bkjia.com/uploads/allimg/131228/13434RF4-2.png "/>

 

2. The "sample program" in the lab requirements was slightly modified.

★Header file # include <iostream. h> changed to # include "iostream. h ", that is, iostream. h is surrounded by double quotation marks (") rather than angle brackets (<>), which actually return to the C code specification. The reason for this modification is that the original DFA was not fully considered and ignored "less than or equal to <=, greater than or equal to> =, judgment =, not equal! = "These special cases, because they will follow <> =! These special characters causeAmbiguity.

★At the same time, IO in C ++ may be "> and <", which may also be different from the preceding special characters. This makes the unGetNextChar (int step) in the implementation code different from that in the textbook, this function includes a step parameter, which is also used to accommodate # include & lt; iostream. h> and> = in the code.

In fact, "iostream. h" is also recognized as a string and cannot be improved yet.

★In addition, in order to test the arithmetic operators, the sample program in the experiment requirements is modified. The program uses the sample program as the input. For example, a "I = I + 2;" statement is added:

650) this. width = 650; "style =" border-bottom: 0px; border-left: 0px; border-top: 0px; border-right: 0px "title =" lexical analysis 2 2011-03-29 "border =" 0 "alt =" lexical analysis 2 2011-03-29 "height =" 194 "src =" http://www.bkjia.com/uploads/allimg/131228/13434V613-3.png "/>

 

3. The print output in the program imitates the sample output in the teaching material.

★The output result of the program is as follows:

650) this. width = 650; "style =" border-bottom: 0px; border-left: 0px; border-top: 0px; border-right: 0px "title =" lexical analysis 3 2011-03-29 "border =" 0 "alt =" lexical analysis 3 2011-03-29 "height =" 987 "src =" http://www.bkjia.com/uploads/allimg/131228/13434RM2-4.png "/>

4. the DFA diagram designed for the C ++ source program is roughly as follows:

650) this. width = 650; "style =" border-bottom: 0px; border-left: 0px; border-top: 0px; border-right: 0px "title =" C simple program DFA "border =" 0 "alt =" C simple program DFA "height =" 671 "src =" http://www.bkjia.com/uploads/allimg/131228/13434T062-5.jpg "/>

5. Implement code Java)

Recently, I like Vim code highlighting, which is clear and clear. below is the entire implementation code under Vim. The text code is at the end of this article:

650) this. width = 650; "style =" border-bottom: 0px; border-left: 0px; border-top: 0px; border-right: 0px "title =" Source code Vim "border =" 0 "alt =" Source code Vim "height =" 7131 "src =" http://www.bkjia.com/uploads/allimg/131228/13434S554-6.png "/>

 

This article is from the "ant" blog, please be sure to keep this source http://haolloyin.blog.51cto.com/1177454/534040

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.