(Connecting) Constructing the program based on this table, the core of the program is the following function,
/ ************************************************** *******************
The following is the main analysis function
Read from the input file, write the analysis results into the output file
Parameters: fpin: Enter file pointer fpout: Output file pointer
*********************************************************** ****************** /
Void Parse (File * fpout)
{
Char arr [maxbuf]; // read the longest string does not exceed Maxbuf, Maxbuf is defined as 255, enough for long, I think
INT i = 0; // Analysis of string with letters
INT j = 0; // Analyze the string of pure numbers
While (1)
{
Fscanf (fpin, "% c", & ch); // read a character from the input file
IF (CH == '|| CH ==' / t ') // Filter spaces and TAB
;
ELSE IF (CH == '/ n') // Enter the newline character, for the following error judgment
LINENO ;
ELSE IF (Isdigit (CH)) // Read is a number
{
While (Isdigit (CH))
{
Arr [J] = CH;
J ;
FSCANF (FPIN, "% C", & ch);
}
FSeek (fpin, -1l, seek_cur); // File pointer back a byte
Char * Temp1 = (char *) Malloc (j 1); /
Memcpy (TEMP1, ARR, J);
Temp1 [j] = '/ 0'; // copy the contents of the array to the other array because I define
// Arr is 255 bytes, and it doesn't actually write so much, so only copy actually has data.
j = 0; // Restore the initial state, for the next use
FPRINTF (fpout, "% S / T% D / N", TEMP1, 2); // constant
Free (temp1); // Release memory
}
Else IF (isalpha (ch)) // is the beginning of the letter
{
While (isalpha (ch) || isdigit (CH))
{
Arr [I] = CH;
i ;
FSCANF (FPIN, "% C", & ch);
}
FSeek (fpin, -1l, seek_cur);
Char * TEMP = (char *) Malloc (i 1);
Memcpy (TEMP, ARR, I);
Temp [I] = '/ 0';
i = 0;
/ * Basic idea is the same as the digital * /
The IF (Findok) // Findok function looks for the same thing in the keyword table, and find the column number.
{
FPRINTF (FPOUT, "% S / T% D / N", TEMP, FINDOK (TEMP));
}
Else
{
FPRINTF (fpout, "% s / t / t% d / n", temp, 1); / / mark symbol
}
Free (TEMP);
}
/ / The following is 2 bytes of arithmetic symbols
ELSE IF (CH == ') // Symbol ": ="
{
FSCANF (FPIN, "% C", & ch);
IF (CH == '=')
FPRINTF (fpout, "% S / T% D / N", ": =", 20); ELSE
FPRINTF (FPOUT, "ERROR IN Compileing% D lines unknown character% C / N", LINENO, CH); // error
}
ELSE IF (CH == '>) // Symbol ">" and "> ="
{
FSCANF (FPIN, "% C", & ch);
IF (CH == '=')
FPRINTF (fpout, "% s / t / t% d / n", "> =", 16);
Else
FPRINTF (fpout, "> / t / t15 / n");
}
ELSE IF (CH == '<') // Symbol "<" and "<="
{
FSCANF (FPIN, "% C", & ch);
IF (CH == '=')
{fPrintf (fpout, "<= / t / t18 / n");
Else IF (CH == '>')
{FPRINTF (fpout, "<> / t / t19);
Else
{FPRINTF (fpout, " t / t / n");}
}
Else {
/ / The following is an arithmetic symbol of one byte
IF (CH == '-') {fprintf (fpout, "% s / t / t% d / n", '-', 10); Continue;} // Output to "- 10" in the file
IF (CH == ';') {fprintf (fpout, "; / t / t21 / n"); continued;}
IF (CH == ' ') {fPrintf (fpout, " / t / t9 / n"); continued;}
IF (CH == '*') {fprintf (fpout, "* / t / t11 / n"); Continue;
IF (CH == '/') {fprintf (fpout, "/ / t / t12 / n"); continued;}
IF (CH == '(') {fprintf (fpout, "(/ t / t13 / n"); Continue;}
IF (CH == ')') {fPrintf (fpout, ") / t / t14 / n"); continued;}
IF (CH == ') {fprintf (fpout, "./ T / T22 / N"); continued;}
IF (CH == ',') {fPrintf (fpout, ", / t / t23 / n"); continued;}
IF (ch == '#') Break; // End
Else Fprintf (fpout, "Error In Compiable% D LINES Unknown Character% C / N", LINENO, CH); // Error, Output Error Information
}
}
}
The above code is compiled by Visual C 6.0 Win2000.
Below we use this program to lexic analysis of the following code, the code is saved as Test.txt, assuming that the compiled application is Lex.exe,
Program test
Begin
3J: = 200;
IF (j> 100) THEN
J: = 300 &&&&&
Else
J: = 107870
GYT768% ^
End. #
Enter the source file name, Test.txt., And output file names under the command line: Output.txt can see the following results.
PROGRAM 6
Test 1
Begin 7
3 2
J 1
= 20
200 2
; twenty one
IF 3
(13
J 1
> 15
100 2
) 14
THEN 4
J 1
= 20
300 2
Error In Compiableing 4 Lines Unknown Character &
Error In Compiableing 4 Lines Unknown Character &
Error In Compiableing 4 Lines Unknown Character &
Error In Compiableing 4 Lines Unknown Character &
ELSE 5
J 1
= 20
107870 2
Gyt768 1
ERROR IN Compiling 7 Lines unknown character%
Error In Compiableing 7 Lines Unknown Character ^
END 8
. twenty two
This function is the core of the lexical analysis program, which identifies writing errors in the exporter, and finds keywords, variables, etc. Specific, please see the source code, the comments are very detailed, but there is definitive place, please enlighten me . If you have any questions, you can send me an email. . My email: brilliant_zhang@21cn.com,