Implementation of simple lexical analyzer

Source: Internet
Author: User
Tags alphabetic character

Compiling a compiled program for a simple subset of languages using C language in a single scan

#include <iostream>

#include <string.h>

#include <ctype.h>

Char Prog[80],token[8];

Char ch;

int syn,p,m,n,sum=0;

Char *rwtab[6]={' begin ', ' if ', ' then ', ' while ', ' Do ', ' end '};

void Scanner ()

{

m=0;

for (n=0;n<8;n++)

Token[n]=null;

Ch=prog[p++];

while (ch== ") ch=prog[p++];

if (Isalpha (CH))//alphabetic character

{

while (isdigit (ch) | | Isalpha (CH))//alphabetic or numeric characters

{

Token[m++]=ch;

Ch=prog[p++];

}

Token[m++]= ' ";p--;syn=10;

for (n=0;n<6;n++)

if (strcmp (Token,rwtab[n]) ==0)

{

syn=n+1;

Break

}

}

else if (isdigit (CH))//numeric characters

{

sum=0;

while (IsDigit (CH))//shuzi

{

sum=sum*10+ch-' 0 ';

Ch=prog[p++];

}

p--;

syn=11;

}

Else

Switch (CH)

{

Case ' < ': m=0;

Token[m++]=ch;

Ch=prog[p++];

if (ch== ' > ')

{

syn=22;

Token[m++]=ch;

}

else 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=18;

Token[m++]=ch;

}

Else

{

syn=17;

p--;

}

Break

Case ' + ': syn=13;token[0]=ch;break;

Case '-': syn=14;token[0]=ch;break;

Case ' * ': syn=15;token[0]=ch;break;

Case '/': syn=16;token[0]=ch;break;

Case ' = ': syn=25;token[0]=ch;break;

Case '; ': syn=26;token[0]=ch;break;

Case ' (': syn=27;token[0]=ch;break;

Case ') ': syn=28;token[0]=ch;break;

Case ' # ': syn=0;token[0]=ch;break;

Default:syn=-1;

}

}

void Main ()

{

using namespace Std;

P=0;

int i;

printf ("\ n Please input string:\n");

do{

Ch=getchar ();

Prog[p++]=ch;

}while (ch!= ' # ');

P=0;

do{

Scanner ();

Switch (SYN)

{

Case 11:printf ("(%d,%d)", syn,sum);

Break

case-1:printf ("error!");

Break

Default

printf ("(%d,", SYN);

For (I=0;i<strlen (token); i++)

{

printf ("%c", Token[i]);

}

printf (")");

}

}while (syn!=0);

}

Implementation of simple lexical analyzer

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.