The following is a simple lexical parser
#include <stdio.h> #include <string.h>using namespace std; int main () {char str[150];//freopen ("C:\\users\\ Lionel\\desktop\\1.txt "," R ", stdin); int row = 1, cow = 0, while (gets (str)) {char temp[10]; int index = 0, i = 0;while (i&l T;strlen (str) +1) {if (str[i] = = ' | | str[i] = = ' \ t ' | | str[i] = = ' + ') {Temp[index] = ' + '; if (!strcmp (temp, "if") {printf (" IF (%d,%d) \ n ", row,i-index+1);} Else{bool flag = False, for (int j = 0, J < index; ++j) {if (temp[j]< ' 0 ' | | temp[j]> ' 9 ') {flag = true; break;}} if (flag) {printf ("ID (%s) (%d,%d) \ n", temp,row,i-index+1);} else{printf ("NUM (%s) (%d,%d) \ n", temp,row,i-index+1);}} while (str[i] = = ' | | str[i] = = ' \ t ') && (str[i+1] = = ' | | str[i+1] = = ' \ t ')) {++i;} index = 0;} else{temp[index++] = Str[i];} ++i;} row++;} return 0;}
with June
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Compiler practice small lexical parser for two