Compiler principle-Lexical analyzer

Source: Internet
Author: User
Tags strcmp

Sample input: If num > then num2 = + Else num2 = 0; #

Sample output:

#include "Cstdio" #include "CString" #include "cctype" Const int ERROR =-1; const int over = 0; const int BEGIN = 1;const in T END = 2;const int IF = 3;const int then = 4;const int when = 5;const int do = 6;const int const = 7;const int VAR = 8;c onst int call = 9; const int PROCEDURE = 10;const int ID = 11;const int number = 12;const int PLUS = 13;const int SUB = 14; const int STAR = 15;const int DIV = 16;const int MOD = 17;const int EQUAL = 18;//=const int NE =19;//<>const int LES S = 20;//<const int more = 21;//>const int LE = 22;//<=const int ME = 23;//>=const int ASSIGN = 24;//:=const I NT LPAR = 25;//(const int RPAR = 26;//) const int COMMA = 27;//,const int DOT = 28;//.const int semicolon = 29;//;char Buff  er[200]= {'};char ' str[20];//holds the string that makes up the word symbol */char ch;int n;int number;    Holds a constant (integer) int pstr,pbuffer; keyword int keywordnum=10; Keyword char keywords [10][10] = {"Begin", "End", "if", "then", "while", "does", "Const", "var", "Call", "procedure"};//constant table int Numofnumbers=0;int numtab[100];//variable table int Numofvars=0;char vartab[100][10];//read non-null character void Getbc () {ch=buffer[pbuffer++]; while (ch== ') ch=buffer[pbuffer++];} Fallback one character void retract () {ch=buffer[pbuffer--];}    Scan the contents of the buffer int scan () {//Initialize string pstr=0;    for (int i=0; i<20; ++i) {str[i]= ';    }//Initialize numeric constant number=0;    GETBC ();            if (Isalpha (CH)) {//Read identifier while (Isalpha (ch) | | isdigit (CH)) {str[pstr++]=ch;        ch = buffer[pbuffer++];        } str[pstr++]= ' + ';        int syn=11; Retract ();//fallback//Determine if the keyword for (int i=0; i<keywordnum; ++i) {if (strcmp (str,keywords[i)) =                =0)//string comparison {syn=i+1;            Break    }} return syn;            } else if (IsDigit (CH)) {while (IsDigit (CH)) {Number = number*10 + ch-' 0 ';        ch = buffer[pbuffer++];    } retract ();//fallback return number;   } else {     Switch (CH) {case ' + '://syn=13;            Str[0]=ch;        return PLUS;            Case '-'://syn=14;            Str[0]=ch;        return SUB;            Case ' * '://syn=15;            Str[0]=ch;        return STAR;            Case '/'://syn=16;            Str[0]=ch;        return DIV;            Case '% ': str[0]=ch;        return MOD;            Case ' = ': str[0]=ch;        return EQUAL;            Case ' < ': pstr=0;            Str[pstr++]=ch;            Ch=buffer[pbuffer++];                if (ch== ' > ') {str[pstr++]=ch;            return NE;                } else if (ch== ' = ') {str[pstr++]=ch;            return LE;                } else {retract ();            return less;        } break;            Case ' > ': pstr=0;            Str[pstr++]=ch; Ch=buffer[pbuFfer++];                if (ch== ' = ') {str[pstr++]=ch;            return ME;                } else {retract ();            return more;        } break;            Case ': ': pstr=0;            Str[pstr++]=ch;            Ch=buffer[pbuffer++];                if (ch== ' = ') {str[pstr++]=ch;            return ASSIGN;                } else {retract ();            return ERROR;        } break;            Case ' (': str[0]=ch;        return LPAR;            Case ') ': str[0]=ch;        return rpar;            Case ', ': str[0]=ch;        return COMMA;            Case '. ': str[0]=ch;        return DOT;            Case '; ': str[0]=ch;        return semicolon;            Case ' # ': str[0]=ch;        return over;        Default:return ERROR; }}}//Insert variable, return the position of the variable table int Insert_var (const char * str) {for (int i=0; i<numofvars; i++) {if (strcmp (Str,vartab[i]) ==0)//string comparison        {return i+1;    }} strcpy (VARTAB[NUMOFVARS++],STR); return numofvars;} Inserts an integer constant that returns the position int insert_num (int number) {for (int i=0; i<numofnumbers; i++) {if (numtab[i]==number)//character        Comparison of strings {return i+1;    }} Numtab[numofnumbers++]=number; return numofnumbers;} Main () {freopen ("In.txt", "R", stdin);//reads data from a file into printf ("\n\nthe explanation: \ n" "1. 1 to 10:keyword\n "" 2. 11:other indicators by user\n "" 3. 12:numbers\n "" 4.    29:operators\n ");    printf ("\nplease input string:\n");    input to buffer pbuffer=0;        do {Ch=getchar ();    Buffer[pbuffer++]=ch;    } while (ch!= ' # ');    Lexical analysis pbuffer=0;    int Loc,syn;        do {syn = scan ();            Switch (SYN) {case-1: printf ("error\n");Break            Case 12://Constant LOC = Insert_num (number);            printf ("(%2d,%2d)%d\n", syn,loc,number);        Break            Case 11://user Variable loc = insert_var (str);            printf ("(%2d,%2d)%s\n", syn,loc,str);        Break        Default://Keywords printf ("(%2d,-)%s\n", syn,str);    }} while (syn!=0); return 0;}

  

Compiler principle-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.