Principles of compilation-Experiment three-debugging and extension of recursive descent grammar parser

Source: Internet
Author: User

a Introduction to grammar analysis

Grammar analysis is the core part of the compilation process, its main task is to follow the syntax rules of the program language, from the lexical analysis output from the source program symbol string to identify the various grammatical components, while the grammar check, for semantic analysis and code generation preparation. The program that performs the parsing task is called a parser or parser.

two the semantic analysis implemented and what the code generator can do with the statements

(1) Declaration statement for simple variables

(2) Expression statements

(3) if statement.

(4) While statement

(5) For statement

(6) Write statement

(7) Read statement

(8) do statement.

(9) Process call and return

three . Experimental Process

① Create a Win32 Console Application Hollow Project with VC6.0, then enter the code for the Appendix C parser (Testparse) and the main program code (main)

② in creating a file Add the Testscan () function in appendix B to the project analysis

③ Compile and run

④ Modifications to do:

int Do_stat ()

{

int es=0;

FSCANF (FP, "%s%s/n", &token,&token1);

printf ("%s%s/n", token,token1);

if (strcmp (token, "(")) return (es=5);

FSCANF (FP, "%s%s/n", &token,&token1);

printf ("%s%s/n", token,token1);

Es=expression ();

if (es>0) return (es);

if (strcmp (token, ")")) return (es=6);

FSCANF (FP, "%s%s/n", &token,&token1);

printf ("%s%s/n", token,token1);

Es=statement ();

if (es>0) return (es);

if (strcmp (token, "while") ==0)

{

FSCANF (FP, "%s%s/n", &token,&token1);

printf ("%s%s/n", token,token1);

Es=statement ();

if (es>0) return (es);

}

return (es);

}

⑤log_expr modification

Char singleword[50]= "+-* () {};,:!";

Char doubleword[10]= "><=&&| |";

int log_expr () {

int es=0;

Es=bool_expr ();

if (es>0) return (es);

if (strcmp (token, "&&") ==0 | | strcmp (token, "| |") ==0

)

{

FSCANF (FP, "%s%s\n", &token,&token1);

printf ("%s%s\n", token,token1);

Es=bool_expr ();

if (es>0) return (es);

}

return (es);

}

Four . problems encountered:

Link error

Linking ...

LIBCD.lib (wincrt0.obj): Error lnk2001:unresolved external symbol [email protected]

Debug/test.exe:fatal Error lnk1120:1 unresolved externals

Error executing link.exe.

Test.exe-2 error (s), 0 warning (s)

This file uses Winmian instead of main as the entry function. The default setting for VC is for the console program.

Workaround:

1. Enter project->setting->c/c++, select Preprocessor in category (first row), delete _windows in processor definitions, add _console

2. Go to Project->setting->link and change/subsystem:windows to/subsystem:console in Project options.

3. Save the settings, Rebuild all.

In short, it is created as a WIN32 application when building a project, so it is recommended to set up the Win32 Console application project directly.

Principles of compilation-Experiment three-debugging and extension of recursive descent grammar parser

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.