Class C compiler C code implementation, c compiler code implementation

Source: Internet
Author: User
Tags switch case

Class C compiler C code implementation, c compiler code implementation

This is a small project last semester.

Class C Compiler

Source code and test file address https://github.com/zxt1995/ttbox.git

 

Overall framework: Read the file to be compiled-> Semantic Analysis and convert to command-> perform stack operations according to command-> get the result

 

Extended Part (I am responsible for commenting on other content in the Code)

Finished content

Do while LOOP

Switch case statement

Goto statement

Implementation of Break and continue in a loop

Supplement the short-circuit calculation of the basic part

 

Lexical Analysis (switch)

The Switch statement includes the keywords SYM_SWITCH, SYM_CASE, SYM_DEFAULT, and SYM_BREAK.

Add a new OPR operation OPR_CMP reserve the stack top and the next stack top is the result of the selection expression behind the switch. The stack top is 1 (equal) 0 (not equal)

Syntax analysis (switch)

 

Switch (expression)

Begin

Case num1:

0 jump Statement (if there is no break at last );

Case num2: JMP starts with the next Statement

0 jump Statement (if there is a break );

Case num3:

0 jump Statement;

Default: JMP ends

Statement;

End;

 

Code Analysis (switch)

 

Add global variables to pl0.h

Add in statement

The Switch statement normally detects the corresponding SYM and then performs an expression calculation.

At the end of the switch statement

Determine whether default exists to determine the jump position for the last case.

 

 

The Case code is as follows: default is similar

 

 

Break awaiting Backfilling

 

 

 

Example (switch)

The test sample is as follows:

On the left is the test code, and on the right is the step.

The following result calculates only the values of I at the beginning and p = 30 j = 40 meet the switch syntax rules.

(The result here is the same as the test result after each parameter value. Other members in the group implement the print function to output specific parameter results)

 

 

 

 

 

Lexical Analysis (goto)

Add the keyword SYM_GOTO

Add a new label (2d array)

 

Syntax analysis (goto)

 

 

Begin:

Statement

Jmp statement

Goto: label1;

Statement

Label2:

Statement

Statement jmp

Goto: label2;

Statement

Label1:

Statement

End;

You only need to match the corresponding label to jump unconditionally.

However, because you do not know where the corresponding label appears, you must repeat the redirection command after the block is completed.

 

Code Analysis (goto)

First, add an unidentifiable id to the if (getsym () = SYM_IDENTI) of statement to identify it as an identifier.

 

 

 

Then, store another label in if (getsym () = SYM_GOTO) of statement.

 

 

 

 

Finally, match the string after the block of the main function.

 

 

 

Example (goto)

The test sample is as follows:

 

 

 

The result meets the goto syntax.

 

 

 

Do while)

Override SYM_DO in Statement

 

Do while)

 

Do

0 jump for Statement

While (expression );

 

Do while)

The basic implementation method is similar to the while loop.

 

 

Example (do while)

(For details, refer to the examples of continue break and do while)

 

 

Lexical Analysis (break continue)

 

Add the keyword SYM_BREAK

SYM_CONTINUE

Add global variables

 

 

Loopfi + + out of the loop each time it enters the loop-this will not cause errors during backfilling.

(Note: The Backfilling command is immediately refilled after the output loop)

 

Break continue)

Take the while loop as an example (in end is reduced to a large statement)

 

 

 

 

 

 

 

 

While (expression)

Begin

Statement jump to expression unconditionally

Continue;

Statement

Break;

Statement unconditional jmp ends at the end of the loop

Statement

End;

 

Code Analysis (break continue)

In the do while statement for loop statement while loop statement, all are modified. Take the while statement as an example.

 

 

 

Add to identify sym Break and continue as follows

 

 

 

Example (break continue)

With do while

 

 

 

The result is correct.

 

 

 

 

 

 

Lexical Analysis (Supplement bool short circuit)

Add the following global variables:

Add the command opposite to JPC. JNP is not a 0 redirect.

 

Syntax analysis (Supplement bool short circuit)

Take a layer as an example.

Expression Abbreviation: expi JNP to end

 

 

 

JPC to the first in the same layer | after

Multiple layers are saved to the trur_list and false_list of the corresponding layer, and then the end represents the position after the brackets.

 

Code Analysis (Supplement bool short circuit)

Take the if statement as an example.

 

 

 

 

Modify func_or and func_and.

Take or as an Example

 

 

Encountering and updating dummy chains

If the true value chain is updated or is encountered (enter the false value exit at the same layer and the previous layer at the same time)

Not exchange truth value false value chain content

 

Example

Opr_or and opr_and no longer appear in the command sequence

 

The result is correct.

 

 

Summary (what can be modified)

Number of labels in the Goto statement

Number of cycles supporting break continue

The number of case and break in Swich case is limited !!

A better data structure is needed to express it, instead of a two-dimensional/one-dimensional array.

 

All code https://github.com/zxt1995/ttbox.git

 

Related Article

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.