Lexical Analysis Program

Source: Internet
Author: User
/*************************************** **********************/
/*
/* Lexical analysis program
/* Maker: Zhang jianbo
/* Date: 2005-10-26
/*
/*************************************** **********************/
# Include "stdio. H"
# Include "ctype. H"
# Include "string. H"

/* Number of reserved words */
# Define keywordsum 8
Char * keyword [keywordsum] = {"if", "else", "for", "while", "do", "int", "read ", "Write "};

/* Pure but single delimiters */
# Define singlewordsum 10
Char singleword [singlewordsum] = "+ -*(){};,:";

/* Dual delimiters */
# Define doublewordsum 4
Char doubleword [doublewordsum] = "<> =! ";

# Define wordlen 40/* maximum word length */

Char scaninfile [300];/* source program file to be analyzed */
Char scanoutfile [305];/* output file after lexical analysis */

File * fin, * fout;/* pointer to the input/output file */

Int loadcodefile ()
{
/* Open the source code file to be analyzed */

Printf ("Enter the source program file name :");
Scanf ("% s", scaninfile );
If (fin = fopen (scaninfile, "R") = NULL)
{
Printf ("/n cannot find the file: % s/n file opening failed! /N ");
Return 0;
}
Else
Return 1;
}

Int creatwordfile ()
{
/* Output file after the lexical analysis is created */
Int Len = 0;
Int I = 0;
Strcpy (scanoutfile, scaninfile );
While (scanoutfile [I ++]! = '.');
Scanoutfile [I + 1] ='s ';
Scanoutfile [I + 2] = 'C ';
Scanoutfile [I + 3] = 'n ';
Scanoutfile [I + 4] = '/0 ';
If (fout = fopen (scanoutfile, "W") = NULL)
{
/* File creation failed */
Return 0;
}
Else
Return 1;

}

/* Lexical analysis program */
Int tscan ()
{

Char CH, Token [wordlen];/* Ch is the character read each time, and token is the recognized word */

Char comment;/* temporary variable used to process comments */

Int err = 0;/* set error code to 0, indicating no error */
Int J, N;/* temporary variable, control the array subscript when combining words */

If (loadcodefile () = 0)
Return 1;/* failed to read the source code file */
If (creatwordfile () = 0)
Return 2;/* file creation failed */

Ch = GETC (FIN );
While (Ch! = EOF)
{
While (CH = ''| CH = '/N' | CH ='/t ')
{
Ch = GETC (FIN );
}
If (isalpha (CH ))
{
/* Enter a letter to process the identifier */
Token [0] = CH;
J = 1;
While (isalnum (CH ))
{
Token [J ++] = CH;
Ch = GETC (FIN );
}/* A combination of letters and numbers. Otherwise, the combination ends */
Token [J] = '/0 ';
/* Search for reserved words */
N = 0;
While (n <keywordsum) & strcmp (token, keyword [N])
N ++;
If (n> = keywordsum)
{
/* Output identifier */
Fprintf (fout, "% S/T % s/n", "ID", token );
Printf ("% S/T % s/n", "ID", token );
}
Else
{
/* Output reserved words */
Fprintf (fout, "% S/T % s/n", Token, token );
Printf ("% S/T % s/n", Token, token );
}
}
Else if (isdigit (CH ))
{
/* Enter a number for processing */
Token [0] = CH;
J = 1;
Ch = GETC (FIN);/* read the next character */
While (isdigit (CH ))
{/* Merge numbers if they are numbers */
Token [J ++] = CH;
Ch = GETC (FIN );
}
Token [J] = '/0';/* integer consolidation ends */
Fprintf (fout, "% S/T % s/n", "num", token );
Printf ("% S/T % s/n", "num", token );
}
Else if (strchr (singleword, CH)> 0)
{
/* The input is a single separator */
Token [0] = CH;
Token [1] = '/0 ';
Ch = GETC (FIN );
Fprintf (fout, "% S/T % s/n", Token, token );
Printf ("% S/T % s/n", Token, token );
}
Else if (strchr (doubleword, CH)> 0)
{
/* Double delimiters are input */
Token [0] = CH;
Ch = GETC (FIN );
If (CH = ')/* If yes =, combine the dual delimiters */
{
Token [1] = CH; token [2] = '/0 ';
Ch = GETC (FIN );
}
Else
{
Token [1] = '/0 ';
}
Fprintf (fout, "% S/T % s/n", Token, token );
Printf ("% S/T % s/n", Token, token );
}
Else if (CH = '/')
{
/* Comment Processing */
Ch = GETC (FIN );
If (CH = '*')
{
Response = GETC (FIN );
Do
{
Ch = success;
Response = GETC (FIN );
} While (Ch! = '*' | Success! = '/') & Amp; success! = EOF );
Ch = GETC (FIN );
}
Else
{
Token [0] = '/';
Token [1] = '/0 ';
Fprintf (fout, "% S/T % s/n", Token, token);/* output word delimiters */
Printf ("% S/T % s/n", Token, token );
}
}
Else
{
/* Unrecognized characters */
Token [0] = CH;
Token [1] = '/0 ';
Ch = GETC (FIN );
Err = 3;
Fprintf (fout, "% S/T % s/n", "error", token);/* output error symbol */
Printf ("% S/T % s/n", "error", token );
}
}
Return err;
}

Void main ()
{
Int e = 0;
E = tscan ();
If (E> 0) printf ("Compilation error! ");
Else
Printf ("lexical analysis successful! ");

}

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.