Compiler Design: Lexical Analyzer

Me and my friend M.H Riyad made this lexical analyzer:

#include<bits/stdc++.h>

using namespace std;
typedef long long ll;

int main()
{
    //  freopen("input.txt","r",stdin);
      //freopen("output.txt","w",stdout);
    int v=0,n=0,k=0;
    string STRING;
    string str;
    string var[555];
    string op;
    string num[500];
    string deli;
    string key[500];
    ifstream file;

    file.open ("in.txt");
    while(!file.eof()) // To get you all the lines.
    {
        getline(file,STRING); // Saves the line in STRING.
       // cout<<STRING<<endl; // Prints our STRING.

        str=STRING;

        int len=str.length();

        for(int i=0; i<len;)
        {
            //  cout<<str[i]<<endl;

            if(str[i]==')'||str[i]=='('||str[i]=='{'||str[i]=='}'||str[i]=='['||str[i]==']'||str[i]==';'||str[i]=='<')
            {
                deli+=str[i];
                i++;
            }
            if(str[i]=='+'||str[i]=='-'||str[i]=='*'||str[i]=='/'||str[i]=='^'||str[i]=='='||str[i]=='>'||str[i]=='<')
            {
                op+=str[i];
                i++;
            }
            if(isalpha(str[i]))
            {
                string s="";
                while(isalpha(str[i])||isdigit(str[i])||str[i]=='_'||str[i]=='$')
                {
                    s+=str[i];
                    i++;
                }
                if(s=="int"||s=="void"||s=="char"||s=="main"||s=="double"||s=="float") key[k++]=s;
                else var[v++]=s;

            }
            if(isdigit(str[i]))
            {
                string s="";
                while(isdigit(str[i]))
                {
                    s+=str[i];
                    i++;
                }
                num[n++]=s;
            }
            if(str[i]==' ') i++;

        }

    }



    file.close();

    printf("\n...........printing the literals are..........\n");
    for(int i=0; i<n; i++) cout<<num[i]<<endl;
    printf("\n...........printing the operators are..........\n");
    for(int i=0; i<op.size(); i++) cout<<op[i]<<endl;
    printf("\n...........printing the variables  are..........\n");
    for(int i=0; i<v; i++) cout<<var[i]<<endl;
    printf("\n...........printing the keywords  are..........\n");
    for(int i=0; i<k; i++) cout<<key[i]<<endl;
    printf("\n...........printing the delemeter  are..........\n");
    for(int i=0; i<deli.size(); i++) cout<<deli[i]<<endl;
    printf("\n\n\n\n");


    return 0;
}

 

References:

http://www.coders-hub.com/2013/05/c-code-for-lexical-analysis.html#.Vo4Ui1LX0-B

http://cnuinfotech-cd.blogspot.com/2012/06/tokens-patterns-and-lexemes.html

It would be a great help, if you support by sharing :)
Author: zakilive

Leave a Reply

Your email address will not be published.