Turning: Design and Implementation of PL0 Language Lessment and Grammatical Analysis System

xiaoxiao2021-03-05  26

Design and Implementation of PL / 0 language syntax and lexical analysis system: Taoshan Wen professional download the source code for Information and Computer Science, Nanjing University of Aeronautics and Astronautics Abstract: This paper describes the design and implementation of a key PL / 0 lexical and grammatical analysis of language Word: Cycle Branch Recursive Decline Pipe Output Redirection The current compilation system is Ide (Integrated Development Environment) and compiler independent implementation, and they communicate with pipelines, this system is also implemented. I first gave a grammar of the PL / 0 language in this article: BNF description of PL / 0 language (expanded Bax paradigm) → Program ;

→ [] []

→ Const {, }

: =

→ VAR {, }

→ procedure ( {, }); {; }

→ begin {; } end

: =

| if Then [else ]

| While do

| Call [( {, })]]

|

| read ( {, })

| Write ( {, })

| odd

→ [ | -] { }

{ }

| | ()

→ = | <> | <= |> |> =

→ |

→ * | / /

→ L {L | D} (Note: L means letters)

→ D {D} Note: : Program; : block, program body; : constant description; : constant;

: Variable Description; : division procedure; : compound statement; : statement; : expression; : condition; : item; : Factor; : addition operator;

: Multiplication Operator; : Relationship Operator

ODD: Judging the parity of the expression. Let's take a look at the design and implementation of the lexics and grammar analyzers. The lexic analysis is achieved by a circular branch method, and the syntax analysis is achieved by recursive decline. Their procedural flow charts are as follows: Let's implement this two analyzers. These two analyzers are implemented using a class ccompiler, and this class is defined as follows: // Compile class CCompiler

{

PUBLIC:

CCompiler ();

Virtual ~ ccompiler ();

PUBLIC:

Void Compile (char * szfile); // Compile, public interface

Vector getsyntaxerr () {return m_vectorsyntaxerr;}; // Get syntax errors

protected:

Bool lexanalysis (char * szstr); // lexical analysis

Bool isoprsym (char * szstr); // is an operator

Bool isbndsym (char * szstr); // is a devof

Bool iskeyword (char * szstr); // is a keyword

Bool isinsymboltab (char * szstr); // is in the symbol table

Char * jumpnomatterchar (char * szstr); // Skip space, Enter, Repair, Tab

Void outsymboltab (char * szfile); // Output symbol table to file

Void syntaxanalysis (); // syntax analysis

Void syntaxanalysis_prog ();

BOOL SYNTAXANALSIS_MOP ();

Bool syntaxanalysis_integer ();

Bool syntaxanalysis_aop ();

Bool syntaxanalysis_lop ();

INT syntaxanalysis_id ();

Int syntaxanalysis_block ();

Int syntaxanalysis_body ();

Int syntaxanalysis_factor ();

Int syntaxanalysis_term ();

Int syntaxanalysis_lexp ();

INT syntaxanalysis_exp ();

Int syntaxanalysis_statement ();

Int syntaxanalysis_const ();

Int syntaxanalysis_proc ();

Int syntaxanalysis_vardecl ();

Int syntaxanalysis_condecl ();

protected:

INT m_ivecotrsymbolsize; // Symbol Table Size

INT m_ICURPOINTER; / / The current pointer in the symbol table

Vector m_vectorysymbol; // symbol table

Vector m_vectorsyntaxerr; // Syntax error code}; in: function bool lexanalysis; is a word method for the input string SzStr using a cyclic branch method, analyzing the symbols in the symbol table M_Vectorsymbol, this The symbol table is represented by this data structure. After the word methodology draws the symbolic table, that is, enter the grammatical analysis phase, grammatical analysis by the function void syntaxanalysis (); completed. The following functions are the recursive subroutines corresponding to each non-end. BOOL SYNTAXANALSIS_MOP ();

Bool syntaxanalysis_integer ();

Bool syntaxanalysis_aop ();

Bool syntaxanalysis_lop ();

INT syntaxanalysis_id ();

Int syntaxanalysis_block ();

Int syntaxanalysis_body ();

Int syntaxanalysis_factor ();

Int syntaxanalysis_term ();

Int syntaxanalysis_lexp ();

INT syntaxanalysis_exp ();

Int syntaxanalysis_statement ();

Int syntaxanalysis_const ();

Int syntaxanalysis_proc ();

Int syntaxanalysis_vardecl ();

Int syntaxanalysis_condecl (); I introduced the design implementation of the lexical and grammar analysis, and I will introduce the communication between the Ide's implementation and the core between IDE and analysis. Pipe communication between IDE and analysis cores of this system, the code is as follows: DWORD DWTHREADID;

:: CreateThread (0, 0, CompileThread, this, 0, & dwthread); // Create a process process created after calling a process function, // Process Function DWord WinAPI CompileThread (LPVOID PPARAM)

{

CCompilesysView * pView = (ccCompilesysView *) PPARAM;

PView-> getcompileresult ();

Return 0;

} The process function call class's own function getCompileresult (); get the output result of the analysis core, the implementation of this function is as follows: void ccompilesysView :: getcompileresult ()

{

Security_attributes sa;

Handle Hread, hwrite;

CString Strfile;

CString strout;

Strfile.format ("..// PL // PL.exe"); / / Specify the path to the analysis core program

/ / The current file is transmitted to the analysis core program to prevent this file name from containing spaces, so the file name is enclosed.

Strfile = STRFILE (CHAR) 34 M_SZCurfile (Char) 34;

SA. NLENGTH = SIZEOF (Security_Attributes);

SA. LPSecurityDescriptor = NULL;

SA. BinheritHandle = True;

IF (! CreatePipe (& Hread, & HWRITE, & SA, 0) // Create a pipeline for communication

{

MessageBox ("Error On CreatePipe ()"); Return;

}

Startupinfo Si;

Process_information pi;

Si. CB = SizeOf (Startupinfo);

GetStartupInfo (& Si);

Si. HSTDERROR = HWRITE;

Si. HSTDOUTPUT = hwrite; // Output redirection to file

Si. Wshowwindow = sw_hide;

Si. DWFLAGS = Startf_useshowwindow | Startf_usestdhandles;

// Create a process launch analysis core program

IF (! CreateProcess (LPCTSTR) Strfile, NULL, NULL, TRUE, NULL, NULL, NULL, & SI, & PI)

{

"" Error On CreateProcess () ");

Return;

}

CloseHandle (HWRITE);

CHAR BUFFER [4096] = {0};

DWORD BYTESREAD;

While (True)

{

IF (! Readfile (Hread, Buffer, 4095, & Bytesread, null)

Break;

Strout = Buffer;

m_pwndoutbar-> setColorricHeditText (strout); // Show output results

SLEEP (500);

}

} This completes the design and implementation of the entire analysis system. Let's take a look at how the entire system is running. Let's take a look at the run interface of this system: After the program runs, the interface shown in the figure, first set the path of the analyst, the method is: point menu IDE environment (i), set, will appear as shown below Dialog: Enter the path where the analyzer is located in the Edit box (the default analyzer and source file in a directory). After setting, you can enter the code in the code editing area, or click "Open" Open the file, then click the toolbar "start" (or press the shortcut F7) button to analyze, after analysis, the lexical analysis result will be "Analysis Results Display Area" display, lexical and syntax analysis information will be displayed in the "Output Information Display Area". Known BUG description Due to the time relationship, the current is the following BUG that I have failed to debug, if there is a master debugging, I hope to inform.

Pl.exe has a large amount of memory leaks, but I use the following code to release memory in the destructor of CCompiler, I don't know why error: ccompiler :: ~ ccompiler () {// The following release the memory code does not know why error // For (int i = 0; i setpathname (Strfile, 1); pdoc-> setmodifiedflag (0); PDOC- > OnSaveDocument (LPSTR) Strfile; // Save this file Str = PDOC-> getTitle (); pdoc-> settitle (STR); if (str.right (1) == "*") { Str = str.Left (Str.getLength () - 1); pdoc-> settitle (STR);} UpdateWindow (); This code means that the file is saved before starting the analyst and makes the window not saved on the window. The asterisk is removed. References, Chen Huang, etc.

Http://home.pudn.com/ahei http://aifan.54sc.com QQ: 8261525 Computer Game QQ Group: 5620663 Ahei080210114@hotmail.com Ahei0802@126.com

Latest Reviews [Published Reviews] [Article Submission] See all comments Recommend to friends print

转载请注明原文地址:https://www.9cbs.com/read-35290.html

New Post(0)