#include <stdio.h> #include <stdlib.h> #include <string.h> #define _key_woed_end "Waiting for your Expanding "//keyword End flag typedef struct{int TYPENUM;CHAR * WORD;} Word;char input[255]; Input swap buffer char token[255]= ""; Word buffer int p_input; Input buffer pointer int p_token; Word buffer pointer char ch; The currently read character Char *rwtab[]={"Begin", "if", "then", "while", "does", "End", _key_woed_end}; C language Keyword Word * scaner (); Lexical scan function, gets the keyword main () {int over=1; Word *oneword;oneword= (word) malloc (sizeof (word));p rintf ("Please enter your string (with # as the end flag):"); scanf ("%[^#]s", input); Reads the source program string into the buffer, ending with #, allowing multiple lines of input p_input=0;printf ("The string you entered is:%s\n\n", input); while (Over<1000&&over!=-1) { Oneword=scaner ();p rintf ("(%d,%s) \ n", oneword->typenum,oneword->word); over=oneword->typenum;} printf ("\ n Note: Number 10 means that the input is not a keyword, just plain words \ n");p rintf ("Number 20 means you have entered the number \ n"); printf ("The number 1000 is the end sign \ n");} Need to use the self-compiled function reference implementation//read from the input buffer a character to Ch Char m_getch () {Ch=input[p_input];p _input=p_input+1;return (CH);} //remove whitespace character void getbc () {while (ch== ' | | ch==10) {ch=input[p_input];p _input=p_input+1;}} Stitching the word void concat () {token[p_token]=ch;p_token=p_token+1;token[p_token]= ';} Determines if the letter int letters () {if (ch>= ' a ' &&ch<= ' Z ' | | ch>= ' A ' &&ch<= ' Z ') return 1;else return 0;} Determines whether the number int digit () {if (ch>= ' 0 ' &&ch<= ' 9 ') return 1;elsereturn 0;} Retrieves the keyword Table int reserve () {int i=0;for (i=0;i<7;i++) {if (!strcmp (Rwtab[i],token)) {return i+1;} i=i+1;} return 10;} Fallback one character void retract () {p_input=p_input-1;} Word *scaner () {Word *myword;myword= (word *) malloc (sizeof (word)); myword->typenum=10;myword->word= "";p _token= 0;m_getch (); GETBC (); if (letter ()) {while (letter () | | | Digit ()) {concat (); M_getch ();} Retract (); Myword->typenum=reserve (); myword->word=token; return (MyWord);} else if (digit ()) {while (digit ()) {concat (); M_getch ();} Retract ();myword->typenum=20; Myword->word=token;return (MyWord);} Else{switch (CH) {case ' = ': M_getch (); if (ch== ' = ') {myword->typenum=39; Myword->word= "= ="; return (MyWord);} Retract ();myword->typenum=21; myword->word= "="; return (MyWord); Break;case ' + ':myword->typenum=22; Myword->word= "+"; return (MyWord); Break;case '-':myword->typenum=23; Myword->word= "-"; return (MyWord); Break;case ' * ':myword->typenum=24; myword->word= "*"; return (MyWord); Break;case '/':myword->typenum=25; Myword->word= "/"; return (MyWord); Break;case ' (':myword->typenum=26; Myword->word= "("; return (MyWord); Break;case ') ':myword->typenum=27; Myword->word= ")"; return (MyWord); Break;case ' [':myword->typenum=28; Myword->word= "["; return (MyWord); Break;case '] ':myword->typenum=29; Myword->word= "]"; return (MyWord); Break;case ' {':myword->typenum=30; Myword->word= "{"; return (MyWord); Break;case '} ':myword->typenum=31; Myword->word= "}"; return (MyWord); Break;case ', ':myword->typenum=32; Myword->word= ","; return (MyWord); Break;case ': ':myword->typenum=33; Myword->word= ":"; return (MyWord); Break;case '; ':myword->typenum=34; Myword->word= ";"; return (MyWord); Break;case ' > ':myword->typenum=35; Myword->word= ">"; return (MyWord); Break;case ' < ':myword->typenum=36; Myword->word= "<"; return (MyWord); Break;case '! ': M_getch (); if (ch== ' = ') {myword->typenum=40;myword->word= "! ="; return (MyWord);} Retract ();myword->typenum=-1; Myword->word= "ERROR"; return (MyWord); Break;case ':myword->typenum=1000; Myword->word= "Over"; return (MyWord); break;default:myword->typenum=-1; Myword->word= "ERROR"; return (MyWord);}}
0920 Compiling principle Lexical analysis