Lexical Analysis C language

Source: Internet
Author: User

#include <stdio.h> #include <string.h>char prog[80],token[8],ch;int syn,p,m,n,sum;char *keyword[6]={" Begin "," if "," then "," while "," does "," End "};void getsym (); main () {p=0; printf (" Please input a string (end With ' # '): ");    do{scanf ("%c", &ch);    Prog[p++]=ch; }while (ch!= ' # '); P=0;    do{Getsym ();          Switch (SYN) {case 11:printf ("(%-10d%5d) \ n", Sum,syn);      Break          case-1:printf ("You have input a wrong string\n");          Getch ();      Exit (0);          Default:printf ("(%-10s%5d) \ n", Token,syn);      Break    }}while (syn!=0); Getch ();    }void getsym ()//lexical analysis function {sum=0;    for (m=0;m<8;m++) token[m++]=null;    Ch=prog[p++];    m=0; while (ch== ") | | (ch== ' \ n '))    Ch=prog[p++]; if ((ch<= ' z ') && (ch>= ' a ')) | |      ((ch<= ' Z ') && (ch>= ' A '))) {while ((ch<= ' z ') && (ch>= ' a ')) | | ((ch<= ' Z ') && (ch>= ' A ')) | |      (ch>= ' 0 ') && (ch<= ' 9 ')))       {token[m++]=ch;      Ch=prog[p++]; } p-;      syn=10;         for (n=0;n<6;n++) if (strcmp (token,keyword[n)) ==0) {syn=n+1;       Break }} else if ((ch>= ' 0 ') && (ch<= ' 9 ')) {while ((ch>= ' 0 ') && (ch<= ' 9 ')) {Sum=sum*10+c      H ' 0 ';    Ch=prog[p++];    } p--;      syn=11;          } else switch (CH) {case ' < ': token[m++]=ch;           Ch=prog[p++];               if (ch== ' = ') {syn=22;            Token[m++]=ch;               } else {syn=20;            p--;     } break;          Case ' > ': token[m++]=ch;          Ch=prog[p++];              if (ch== ' = ') {syn=24;            Token[m++]=ch;              } else {syn=23;            p--;     } break;          Case ' + ': token[m++]=ch;          Ch=prog[p++];              if (ch== ' + ') {syn=17;            Token[m++]=ch;              } else {syn=13;            p--;     } break; Case '-': token[m++]=ch;          Ch=prog[p++];              if (ch== '-') {syn=29;            Token[m++]=ch;              } else {syn=14;            p--;     } break;          Case '! ': ch=prog[p++];             if (ch== ' = ') {syn=21;           Token[m++]=ch;             } else {syn=31;          p--;     } break;          Case ' = ': token[m++]=ch;          Ch=prog[p++];              if (ch== ' = ') {syn=25;            Token[m++]=ch;              } else {syn=18;            p--;     } break;           Case ' * ': syn=15;           Token[m++]=ch;     Break           Case '/': syn=16;           Token[m++]=ch;     Break           Case ' (': syn=27;           Token[m++]=ch;     Break           Case ') ': syn=28;           Token[m++]=ch;     Break           Case ' {': syn=5;           Token[m++]=ch;     Break           Case '} ': syn=6;           Token[m++]=ch;     Break        Case '; ': syn=26;  Token[m++]=ch;     Break           Case ' \ ': syn=30;           Token[m++]=ch;     Break           Case ' # ': syn=0;           Token[m++]=ch;     Break           Case ': ': syn=17;           Token[m++]=ch;    Break         Default:syn=-1;       Break    } token[m++]= ' + '; }

Lexical Analysis C language

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.