0917. Group Lexical analysis

Source: Internet
Author: User

#include <stdio.h>
#include <string.h>
Char input[200];//Store Input string
Char token[5];//holds the string that makes up the word symbol
Char ch; Holds the current read-in character
int p; Input[] Subscript
int FG; Switch tag
int num; Store shaping values

Two-dimensional character array, storing the keyword
Char index[6][6]={' begin ', ' if ', ' then ', ' while ', ' Do ', ' end '};
Main ()
{
P=0;
printf ("Intput string (End with ' # '): \ n");
Do
{
Ch=getchar ();
Input[p++]=ch;
}while (ch!= ' # ');
P=0;
Do
{
Scaner ();
Switch (FG)
{
Case 11:printf ("(%d,%d)", fg,num);
case-1:printf ("Input error\n"); Break
Default:printf ("(%d,%s)", Fg,token);
}
}while (fg!=0);
Getch (); For the program to stay on the display page
}
/* Lexical scanner: */
Scaner ()
{
int m=0;//token[] Subscript
int n;

Empty token[]
for (n=0;n<5;n++)
Token[n]=null;

Gets the first one that is not 0 characters
Ch=input[p++];
while (ch== ") ch=input[p++];

Keyword (identifier) processing process
if ((ch<= ' z ' &&ch>= ' a ') | | (ch<= ' Z ' &&ch>= ' A '))
{
while ((ch<= ' z ' &&ch>= ' a ') | | (ch<= ' Z ' &&ch>= ' A ') | | (ch<= ' 9 ' &&ch>= ' 0 '))
{
Token[m++]=ch;
Ch=input[p++];
}
token[m++]= ' + ';
CH=INPUT[--P];
fg=10;
for (n=0;n<6;n++)
if (strcmp (Token,index[n]) ==0)//strcmp () compares two strings, equals returns 0
{
fg=n+1;
Break
}
}

Digital processing Flow
else if ((ch<= ' 9 ' &&ch>= ' 0 '))
{
num=0;
while ((ch<= ' 9 ' &&ch>= ' 0 '))
{
num=num*10+ch-' 0 ';
Ch=input[p++];
}
CH=INPUT[--P];
fg=11;
}

Operator interface Processing Flow
Else
Switch (CH)
{
Case ' < ':
m=0;
Token[m++]=ch;
Ch=input[p++];
if (ch== ' > ')//Generate <>
{
fg=21;
Token[m++]=ch;
}
else if (ch== ' = ')//Generate <=
{
fg=22;
Token[m++]=ch;
}
Else
{
fg=20;
CH=INPUT[--P];
}
Break
Case ' > ':
Token[m++]=ch;
Ch=input[p++];
if (ch== ' = ')//Generate >=
{
fg=24;
Token[m++]=ch;
}
else//Generate >
{
fg=23;
CH=INPUT[--P];
}
Break
Case ': ':
Token[m++]=ch;
Ch=input[p++];
if (ch== ' = ')//generated: =
{
fg=18;
Token[m++]=ch;
}
else//Generate:
{
fg=17;
CH=INPUT[--P];
}
Break
Case ' + ': fg=13;token[0]=ch;break;
Case '-': fg=14;token[0]=ch;break;
Case ' * ': fg=15;token[0]=ch;break;
Case '/': fg=16;token[0]=ch;break;
Case ': = ': Fg=18;token[0]=ch;break;
Case ' <> ': fg=21;token[0]=ch;break;
Case ' <= ': fg=22;token[0]=ch;break;
Case ' >= ': fg=24;token[0]=ch;break;
Case ' = ': fg=25;token[0]=ch;break;
Case '; ': fg=26;token[0]=ch;break;
Case ' (': fg=27;token[0]=ch;break;
Case ') ': fg=28;token[0]=ch;break;
Case ' # ': fg=0;token[0]=ch;break;
Default:fg=-1;
}
}

0917. Group Lexical analysis

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.