Simple Lexical analyzer implementation

Source: Internet
Author: User

Writing analyzers has two different methods, one is to identify the words through DFA, and the other is to identify them by writing programs directly. This procedure uses DFA to identify the word.
How to implement DFA, presumably consistent with the book, in the program, it uses a two-dimensional array to represent the state transformation matrix, and a one-dimensional array to represent the final state.
A lexical editor to implement the functionThe main points include the following:

Ability to identify identifiers, keywords, numbers, and operators, filter comments, and identify program errors.

Instructions for use:

The input of this program is read input by In.txt file in current directory, output is a series of two-yuan
#include <stdio.h> #include <string.h> #include <stdlib.h>//is represented by a pointer instead of a two-dimensional array, so that the string length is not specified. Just cannot modify the contents of the string pointed to by the pointer char *key[]={"int", "char", "float", "double", "Void", "Const", "for", "if", "Else", "then", "while", " Switch "," Break "," main "," return "};char buffer[20];//store the currently recognized word char *identifier[50];char *num[50];int ident_num;// Number of identifiers int number;//number int judgement_num (char c) {if (c >= ' 0 ' && C <= ' 9 ') return 0;else if (c = = '. ') return 1;return-1;} int judge (char c) {if (c== ' _ ') return 2;else if (c>= ' 0 ' &&c<= ' 9 ') return 1;else if (c>= ' a ' &&c<= ' Z ' | | c>= ' A ' &&c<= ' Z ') return 0;return-1;} The Next_i,next_j represents the current state of the state transition table move and the received character//width represents the number of States that each State may encounter, and the length represents the final set size int DFA (int begin,int move[],int width    , int final[],int length,int (*judge) (char)) {int len=0,next_i,next_j,tmp;    Char Next_char;    memset (buffer,0,sizeof (buffer));    Next_char=getchar ();    Next_i=begin; while ((Next_j=judge (Next_char))!=-1) {tmp=next_i;next_i=move[next_i*width+next_j];if(Next_i==-1)            {printf ("Move[%d][%d]has not next state\n", Tmp,next_j); return-1;}        Buffer[len++]=next_char;    Next_char=getchar ();    } ungetc (Next_char,stdin);    buffer[len]= ' + ';    for (int i=0;i<length;i++) {if (Next_i==final[i]) return 0; } return-1;}    void Is_comment () {char next_char;    Char tmp[5];    memset (tmp,0,sizeof (TMP));    Tmp[0]=getchar ();    Next_char=getchar (); if (next_char== ' * ') {while ((Next_char=getchar ())!=eof) {if (next_char== ' * ' &&getchar () = = '/') return;    printf ("The comment is error\n");    } else if (next_char== '/') {while ((Next_char=getchar ()) = ' \ n ');    } else if (next_char== ' = ') {printf ("(/=,_) \ n");    } else{printf ("(/,_) \ n"); ungetc (Next_char,stdin);    }}void Is_other () {char next_char,c=getchar (); if (c== ' + ' | | c== '-' | | c== ' * ' | |        c== '% ') {Next_char=getchar ();        if (next_char== ' = ') printf ("(%c=,_) \ n", c);       else {printf ("(%c,_) \ n", c); ungetc (Next_char,stdin); }} else if (c== ' > ' | |        c== ' < ') {Next_char=getchar ();        if (next_char== ' = ') printf ("(rlop,%c=) \ n", c);        else {printf ("(rlop,%c) \ n", c); ungetc (Next_char,stdin); }} else if (c== '! ')        {Next_char=getchar ();        if (next_char== ' = ') printf ("(!=,_) \ n");        else {printf ("(not,_) \ n"); ungetc (Next_char,stdin); }} else if (c== ' | ')        {Next_char=getchar (); if (Next_char = = ' | ') printf ("(or,| |)    \ n "); else {ungetc (Next_char, stdin);}        } else if (c== ' & ') {Next_char=getchar ();    if (Next_char = = ' & ') printf ("(and,&&) \ n"); else {ungetc (Next_char, stdin);} } else if (c== ' = ' | | | c== ' (' | | | c== ') ' | | c== ' [' | | c== '] | | c== '; ' | | c== ', ' | | c== ' {' | | |    c== '} ') {printf ("(%c,_) \ n", c);    }}void is_digit () {int begin=0; int Move[]={1,-1,1,2,2,-1};int final[]={1,2};int RESULT=-1;RESULT=DFA (begin,move,2,final,2,judgement_num); Result==-1) {printf ("digit DFA error\n"); exit (-1);}        else if (result==0) {//printf ("%s\n", buffer); for (int i=0; i<number;i++) {if (strcmp (Buffer,num[i]) ==0) {printf ("(num,%s) \ n", buffer); return;}        } num[number]= (char*) malloc (sizeof (buffer));        strcpy (Num[number++],buffer);        printf ("(num,%s) \ n", buffer); return;}} void Is_letter () {int i;int begin=0;int move[] ={1,-1,1,1,1,1};int final[]={1,2};int RESULT=-1;RESULT=DFA (begin,move,3    , Final,1,judge);    if (result==-1) {printf ("Letter DFA error\n"); exit (-1); } else if (result==0) {int len=sizeof (key)/sizeof (char *);//If the keyword for (i=0;i<len;i++) {if (strcmp (buffer,key[i]) = =                0) {printf ("(key,%s) \ n", Key[i]);        return;} }//If the identifier for (i=0;i<ident_num;i++) {//In the current marker table already exists if (strcmp (Buffer,identifier[i]) ==0) {printf ("(%id,%s) \ n",        Identifier[i]); return;}        }//If not present, write identifier[ident_num]= (char*) malloc (sizeof (buffer));        strcmp (Identifier[ident_num++],buffer);        printf ("(id,%s) \ n", buffer);    Return }}void init () {Ident_num= 0; number=0;}    void work () {char C; while ((C=getchar ())!=eof) {ungetc (C,stdin); if (judge (c) ==2| |    Judge (c) ==0) Is_letter (); else if (judge (c) ==1) Is_digit (); else if (c== '/') is_comment (); else Is_other ();    }}int Main () {freopen ("In.txt", "R", stdin);    Init ();    Work (); return 0;}


Simple Lexical analyzer implementation

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.