Uncategorized

The LEXical Analizer

A year back when we were studying state machines and regular expresions I did not quite see the applications of these (believe me) mainly because all we did was make exercises and a project about automatons in a pacman game. Now that I’m taking programming languages class and after participating in a contest at CPMX7  I can see the scope of it (and I’m glad, makes sense).

 

One can employ lexical analysis for example in the compiling process of any programming language, what it merely does is what I’m going to call a string decomposition so this way certain words can be turned in “tokens” that the parser can latter interpret.

The following is a small code made in LEX that takes a string that is supposed to be a roman number and it turns it into decimal notation (pretty basic huh). The most interesting line is the one in bold that uses plain regular expressions. Anyone that has taken a course on it can understand it. Manual of Lex can be found here.

%{
    int units=0, num=0;

%}
%%


I                      num+=1;    
V                      num+=5;    
X                      num+=10;    
L                      num+=50;    
XL                     num+=40;
XC                     num+=90;
C                      num+=100;

(X*|LX+)I(V|X) {char c = yytext[yyleng-1];

            if(c=='V')
                num+=4;
            else if(c=='X')
                num+=9;

            for(units=0;units<(yyleng)-2;units++)
            {
                c=yytext[units];

                if(c=='X')
                    num+=10;
                else if(c=='L')
                    num+=50;

            }};

\n {printf("the number is %d \n", num), num=0;};

%%

int main(void){
    yylex();
    return 0;
}

¿Can you tell which is the maximum roman number this program can read without breaking? Please let me know in the comments. Have a nice day 🙂

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s