Lexical analyzer source code

Source: Internet
Author: User

# Include <iostream>
# Include <fstream>

# Include <string>

Using namespace STD;

Char word [5] [10] = {"int", "char", "float", "for", "Return"}; // keyword

Char oprator [11] = {'+', '-', '*', '/', '<', '>', '= ','(', ')', '{', '}; // keyword

String line [100];

Int line_num = 0;

Bool isword (char CHR []) // keyword Determination

{

Bool flag = false;

For (INT I = 0; I <5; I ++)

{

If (strcmp (word [I], CHR) = 0)

{

Flag = true;

Break;

}

}

Return flag;

}

Bool isoprator (char ch) // operator Determination

{

Bool flag = false;

For (INT I = 0; I <11; I ++)

{

If (oprator [I] = CH)

{

Flag = true;

Break;

}

}

Return flag;

}

Int main ()

{





Ifstream input ("1.txt", IOS: In); // source file

If (! Input)

{

Cerr <"Open error! "<Endl;

Exit (1 );

} Else {

Cout <"Source code:" <Endl;

While (Getline (input, line [line_num]) // read a row

{

Cout <line [line_num] <Endl;

Line_num ++;

}

Cout <Endl <"lexical analysis result:" <Endl;

}





For (INT I = 0; I <line_num; I ++ ){

For (Int J = 0; j <line [I]. Length (); j ++ ){

While (line [I] [J] = ''){

J ++;

}



Char CH = line [I] [J];



If (CH> = 'A' & Ch <= 'Z') // if it is a character

{

Int K = 0;

Char A [10];



While (CH> = 'A' & Ch <= 'Z' | ch> = '0' & Ch <= '9 ')

{

If (k <10)

{

A [k] = CH;

K ++;

}

A [k] = '\ 0 ';

J ++;

Ch = line [I] [J];

}

If (isword (A) = true ){

Cout <"keyword" <A <Endl;

} Else {

Cout <"identifier" <A <Endl;

}

If (k <10 & isoprator (CH )){

Cout <"operator" <ch <Endl;

}

} Else {



If (CH> = '0' & Ch <= '9') // if it is a number

{

Int K = 0;

Char num [10];

While (CH> = '0' & Ch <= '9 ')

{

If (k <10)

{

Num [k] = CH;

K ++;

}

If (line [I] [J] = ''){



}

Num [k] = '\ 0 ';

J ++;

Ch = line [I] [J];

}

Cout <"digit string" <num <Endl;

If (k <10 & isoprator (CH )){

Cout <"operator" <ch <Endl;

}

} Else

{

If (isoprator (CH )){

Cout <"operator" <ch <Endl;

} Else {

// Cout <"not recognized" <ch <Endl;

}

}

}

}

}



Input. Close ();

Return 0;

}

Running result:

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.