#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int P, m, SYN, n-, SUM; // P and m, acts as a pointer, used to point to the next character or back one character, syn used to determine the type of character.
// n is the loop control variable, sum for discriminating whether or integer overflow.
char token [10], prog [ 80]; // global variables, arrays for receiving a keyboard input, another for lexical analysis.
char ch; // array of characters one by one removed from the keyboard input, the determination to do.
char * keywords [7] = { "begin", "else", "if", "while", "do", "end", "then"}; // key character array. To compare strings and keyboard input
#include <stdlib.h>
#include <string.h>
int P, m, SYN, n-, SUM; // P and m, acts as a pointer, used to point to the next character or back one character, syn used to determine the type of character.
// n is the loop control variable, sum for discriminating whether or integer overflow.
char token [10], prog [ 80]; // global variables, arrays for receiving a keyboard input, another for lexical analysis.
char ch; // array of characters one by one removed from the keyboard input, the determination to do.
char * keywords [7] = { "begin", "else", "if", "while", "do", "end", "then"}; // key character array. To compare strings and keyboard input
void Scaner () // lexical analyzer
{P = 0;
n-= 0;
CH PROG = [P ++]; // array of characters from the keyboard, and reading a character used to make discrimination. p points to the next character.
for (n = 0; n ++ ; n <10) // initialize after storing for discriminating a character array of characters
token = '[n];'
when while (ch == '') // read character is not empty , it is judged if
{
CH = PROG [the p-++]; // empty, p has been going down.
++ P;
}
IF ((CH> = 'A' && CH <= 'Z') || (CH <= 'the Z' && CH> = 'A')) // if the letter.
{
M = 0;
the while ((CH> = 'A' && CH <= 'Z') || (CH <= 'the Z' && CH> = 'A')) // cycle, reads a next character for the letter .
{
Token [++ m] = CH;
p--; // p rollback one, because at this time if the conditions are not satisfied.
syn = 10; // letter kind as code, is 10. The
(n-= 0; n-<. 7; n-++) for
IF (strcmp (token, Keywords [n-]) == 0) // read into the array of characters and comparing keyword string array
{
SYN = n-+. 1; // if the same kind of key codes do not return 1-6
BREAK;
}
}
the else IF (CH> = '0' && CH <= '. 9') // If the character is read into the figures.
{
SUM = 0;
the while (CH <= '. 9' && CH> = '0') // accumulated.
{
SUM = SUM + 10 * CH-'0 '; // Since character, so to carry manually. sum multiplied by 10,
CH = PROG [P ++];
}
the P--;
SYN =. 11;
IF (sum> 32767) // determine whether the overflow. int type (C language) up to 32767, the minimum is -32 766
syn = -1; // do not code seed -1, indicating overflow.
}
The else Switch (CH) // if the operator is
{
Case '<':
m = 0;
token [++ m] = CH;
CH PROG = [P ++];
IF (CH == '>') // formed angle brackets <>
{
token [++ m] = CH;
SYN = 21 is; // angle brackets kind as code
}
the else IF (CH == '=') is formed // <=
{
token [++ m] = CH;
SYN = 22 is;
}
the else
{
SYN = 20 is;
the P--;
}
BREAK;
case '>': // and <Similarly
m = 0;
token [m ++] = CH;
CH = PROG [P ++];
IF (CH == '<')
{
token [m ++] = CH;
SYN = 21 is;
}
the else IF (CH == '=')
{
token [++ m] = CH;
SYN = 24;
}
the else
{
SYN = 25;
the P--;
}
BREAK;
Case '(': // angle brackets Similarly
m 0 =;
token [++ m] = CH;
ch=prog[p++];
if(ch==')')
{
token[m++]=ch;
syn = 26;
}
else
{
syn = 27;
p--;
}
break;
case ')':
m=0;
token[m++]=ch;
ch=prog[p++];
if(ch=='(')
{
token[m++]=ch;
syn=26;
}
else
{
syn=27;
the P--;
}
BREAK;
Case '+': token [0] = CH; = 12 is SYN; BREAK; // some other process operators
case '-': token [0 ] = ch; syn = 13; break ;
Case '*': token [0] = CH; SYN = 14; BREAK;
Case '/': token [0] = CH; SYN = 15; BREAK;
Case '!': token [0] = CH; SYN = 16; BREAK;
Case ':': token [0] = CH; SYN =. 17; BREAK;
Case ';': token [0] = CH; SYN = 18 is; BREAK;
Case '#': token [0] CH =; SYN = 0; BREAK;
default: SYN = -1; BREAK;
}
}
{P = 0;
n-= 0;
CH PROG = [P ++]; // array of characters from the keyboard, and reading a character used to make discrimination. p points to the next character.
for (n = 0; n ++ ; n <10) // initialize after storing for discriminating a character array of characters
token = '[n];'
when while (ch == '') // read character is not empty , it is judged if
{
CH = PROG [the p-++]; // empty, p has been going down.
++ P;
}
IF ((CH> = 'A' && CH <= 'Z') || (CH <= 'the Z' && CH> = 'A')) // if the letter.
{
M = 0;
the while ((CH> = 'A' && CH <= 'Z') || (CH <= 'the Z' && CH> = 'A')) // cycle, reads a next character for the letter .
{
Token [++ m] = CH;
p--; // p rollback one, because at this time if the conditions are not satisfied.
syn = 10; // letter kind as code, is 10. The
(n-= 0; n-<. 7; n-++) for
IF (strcmp (token, Keywords [n-]) == 0) // read into the array of characters and comparing keyword string array
{
SYN = n-+. 1; // if the same kind of key codes do not return 1-6
BREAK;
}
}
the else IF (CH> = '0' && CH <= '. 9') // If the character is read into the figures.
{
SUM = 0;
the while (CH <= '. 9' && CH> = '0') // accumulated.
{
SUM = SUM + 10 * CH-'0 '; // Since character, so to carry manually. sum multiplied by 10,
CH = PROG [P ++];
}
the P--;
SYN =. 11;
IF (sum> 32767) // determine whether the overflow. int type (C language) up to 32767, the minimum is -32 766
syn = -1; // do not code seed -1, indicating overflow.
}
The else Switch (CH) // if the operator is
{
Case '<':
m = 0;
token [++ m] = CH;
CH PROG = [P ++];
IF (CH == '>') // formed angle brackets <>
{
token [++ m] = CH;
SYN = 21 is; // angle brackets kind as code
}
the else IF (CH == '=') is formed // <=
{
token [++ m] = CH;
SYN = 22 is;
}
the else
{
SYN = 20 is;
the P--;
}
BREAK;
case '>': // and <Similarly
m = 0;
token [m ++] = CH;
CH = PROG [P ++];
IF (CH == '<')
{
token [m ++] = CH;
SYN = 21 is;
}
the else IF (CH == '=')
{
token [++ m] = CH;
SYN = 24;
}
the else
{
SYN = 25;
the P--;
}
BREAK;
Case '(': // angle brackets Similarly
m 0 =;
token [++ m] = CH;
ch=prog[p++];
if(ch==')')
{
token[m++]=ch;
syn = 26;
}
else
{
syn = 27;
p--;
}
break;
case ')':
m=0;
token[m++]=ch;
ch=prog[p++];
if(ch=='(')
{
token[m++]=ch;
syn=26;
}
else
{
syn=27;
the P--;
}
BREAK;
Case '+': token [0] = CH; = 12 is SYN; BREAK; // some other process operators
case '-': token [0 ] = ch; syn = 13; break ;
Case '*': token [0] = CH; SYN = 14; BREAK;
Case '/': token [0] = CH; SYN = 15; BREAK;
Case '!': token [0] = CH; SYN = 16; BREAK;
Case ':': token [0] = CH; SYN =. 17; BREAK;
Case ';': token [0] = CH; SYN = 18 is; BREAK;
Case '#': token [0] CH =; SYN = 0; BREAK;
default: SYN = -1; BREAK;
}
}
main int ()
{P = 0;
the printf ( "Please Enter A String: \ n-");
the gets (PROG); // set the keyboard input of characters.
do
{
Scaner (); // do lexical analysis
IF (SYN <10 && SYN> 0)
{the printf ( "This IS Keywords \ n-!"); BREAK;} // keyword
the else IF (SYN == 10)
{the printf ( "This is a words \ n! "); break;} // identifier
the else IF (SYN ==. 11)
{the printf () "This IS A number \ n-!"; BREAK;} // digital
the else
{the printf ( "Other words \ n!") ; break;} // operator or terminator #
}
! the while (SYN = 0);
return 0;
}
{P = 0;
the printf ( "Please Enter A String: \ n-");
the gets (PROG); // set the keyboard input of characters.
do
{
Scaner (); // do lexical analysis
IF (SYN <10 && SYN> 0)
{the printf ( "This IS Keywords \ n-!"); BREAK;} // keyword
the else IF (SYN == 10)
{the printf ( "This is a words \ n! "); break;} // identifier
the else IF (SYN ==. 11)
{the printf () "This IS A number \ n-!"; BREAK;} // digital
the else
{the printf ( "Other words \ n!") ; break;} // operator or terminator #
}
! the while (SYN = 0);
return 0;
}