0917 Lexical analysis

Source: Internet
Author: User

#include <stdio.h>
#include <string.h>//strcmp ()
#include <conio.h>//getch ()
#define MAX 200
Char Pro[max], lin[20];
int N,i,syn;
Char *word[6]={' begin ', ' if ', ' then ', ' while ', ' Do ', ' end '}; keywords, special identifiers
Char ch;
Scaner ()//scan
{
int j=0;
for (n=0; n<20; n++)
Lin[n]=null; Temporary array Initialization
ch=pro[i++];//reading a string
while (ch== ")//judge, skip null characters
ch=pro[i++];//reading the whole word
if ((ch>= ' A ' &&ch<= ' Z ') | | (ch>= ' A ' &&ch<= ' z ') | |        (ch>=48&&ch<=57)) Determine if it is a letter or a number
{
if ((ch>= ' A ' &&ch<= ' Z ') | |                       (ch>= ' A ' &&ch<= ' Z ')) Judging identifiers and Special keywords
{
Lin[j++]=ch;
ch=pro[i++];//pro[i++] Empty, keyword
while ((ch>= ' A ' &&ch<= ' Z ') | | (ch>= ' A ' &&ch<= ' z ') | | (ch>=48&&ch<=57)) Judging identifiers
{
Lin[j++]=ch;
Ch=pro[i++];
}
lin[j]= ' + ';//end sign, enter
i--;
syn=10;//Word species code
for (n=0;n<6; n++)
if (strcmp (Lin,word[n]) ==0)//Find out if the keyword table matches
{
syn=n+1;
break;
}
}
else//Judging numbers
{
while (ch>=48&&ch<=57)
{
Lin[j++]=ch;
Ch=pro[i++];
}
i--;
lin[j]= ' + ';
syn=11;
}
}
else//Judging other symbols
{
Judgment <,<=,<>


if (ch== ' < ')
{
Lin[j++]=ch;
Ch=pro[i++];
if (ch== ' = ')
{
syn=21;
Lin[j++]=ch;
lin[j]= ' + ';
}
else if (ch== ' > ')
{
syn=22;
Lin[j++]=ch;
lin[j]= ' + ';
}
Else
{
syn=20;
lin[j]= ' + ';
i--;
}
}
Judging >,>=
else if (ch== ' > ')
{
Lin[j++]=ch;
Ch=pro[i++];
if (ch== ' = ')
{
syn=24;
Lin[j++]=ch;
lin[j]= ' + ';
}
Else
{
syn=23;
lin[j]= ' + ';
i--;
}
}
Judgment:,: =
else if (ch== ': ')
{
Lin[j++]=ch;
Ch=pro[i++];
if (ch== ' = ')
{
syn=18;
Lin[j++]=ch;
lin[j]= ' + ';
}
Else
{
syn=17;
lin[j]= ' + ';
i--;
}
}
Judging other symbols
else if (ch== ' + ')
{
syn=13;
Lin[j++]=ch;
lin[j]= ' + ';
}
else if (ch== '-')
{
syn=14;
Lin[j++]=ch;
lin[j]= ' + ';
}
else if (ch== ' * ')
{
syn=15;
Lin[j++]=ch;
lin[j]= ' + ';
}
else if (ch== '/')
{
syn=16;
Lin[j++]=ch;
lin[j]= ' + ';
}
else if (ch== ' = ')
{
syn=25;
Lin[j++]=ch;
lin[j]= ' + ';
}
else if (ch== '; ')
{
syn=26;
Lin[j++]=ch;
lin[j]= ' + ';
}
else if (ch== ' (')
{
syn=27;
Lin[j++]=ch;
lin[j]= ' + ';
}
else if (ch== ') ')
{
syn=28;
Lin[j++]=ch;
lin[j]= ' + ';
}
else if (ch== ' # ')
{
syn=0;
Lin[j++]=ch;
lin[j]= ' + ';
}
Else
Syn=-1;
}
Return SYN;
}
void read ()//read-in data function
{
FILE *FP;
int k;
for (k=0;k<max-1;k++)
{
scanf (FP, "%c", &pro[k]);
}
pro[k]= ' + ';
}
void Main ()

{
printf ("Please input source code:\n");
do{
Ch=getchar ();//Read into string
Pro[i++]=ch;
}while (ch!= ' # ');
i=0;

Do
{
Scaner ();
Switch (SYN)
{
Case 11:printf ("(%d,%s) \ n", Syn,lin);
Break
Case-1: printf ("error symbol \ n");
Break
Default:printf ("(%d,%s) \ n", Syn,lin);
}
} while (syn!=0);
Getch ();
}

0917 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.