Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "modules.h"
- #define kw_size 35
- char *keywrds[kw_size] = {"and","array","begin","case","const","div","do","downto","else","end","file","for",
- "function","goto","if","in","label","mod","nil","not","of","or","packed","procedure",
- "program","record","repeat","set","then","to","type","until","var","while","with"};
- int keywordCheck(struct lex_type lexChain[], int l)
- {
- int i;
- for(i = 0; i < kw_size; i++){
- if(strcmp(lexChain[l].lexem, keywrds[i]) == 0){
- lexChain[l].lexem_class = "KEYWORD";
- return 1;
- }
- }
- return 0;
- }
- int lexAnalyser(struct transl_type translChain[], struct lex_type lexChain[])
- {
- int STATE = 0, i = -1, j = -1, l = -1, kw_flag;
- char symbol, *class, lexem[20] = "";
- while ( (class = translChain[++i].symbol_class) != '\0' ){
- symbol = translChain[i].symbol;
- kw_flag = 0;
- switch(STATE){
- /*bg*/ case 0: if(strcmp(class, "space") == 0)
- continue;
- if(strcmp(class, "letter") == 0){
- STATE = 1;
- lexem[++j] = symbol;
- continue;
- }
- return -1;
- /*kw1*/ case 1: if(strcmp(class, "space") == 0){
- STATE = 2;
- lexem[++j] = '\0';
- strcpy(lexChain[++l].lexem, lexem);
- kw_flag = keywordCheck(lexChain,l);
- if(!kw_flag)
- lexChain[l].lexem_class = "IDENTIFIER";
- while(j >= 0)
- lexem[j--] = "";
- continue;
- }
- if( (strcmp(class, "letter") == 0) || (strcmp(class, "digit") == 0) ){
- lexem[++j] = symbol;
- continue;
- }
- return -1;
- /*sp1*/ case 2: if(strcmp(class, "space") == 0)
- continue;
- if(strcmp(class, "letter") == 0){
- STATE = 3;
- strcpy(lexChain[++l].lexem, " ");
- lexChain[l].lexem_class = "SPACE";
- lexem[++j] = symbol;
- continue;
- }
- return -1;
- /*id1*/ case 3: if(strcmp(class, "space") == 0){
- STATE = 4;
- lexem[++j] = '\0';
- strcpy(lexChain[++l].lexem, lexem);
- if( (strcmp(lexem, "TRUE") == 0) || (strcmp(lexem, "FALSE") == 0) )
- lexChain[l].lexem_class = "LOGCONST";
- else{
- kw_flag = keywordCheck(lexChain,l);
- if(!kw_flag)
- lexChain[l].lexem_class = "IDENTIFIER";
- }
- while(j >= 0)
- lexem[j--] = "";
- continue;
- }
- if( (strcmp(class, "letter") == 0) || (strcmp(class, "digit") == 0) ){
- lexem[++j] = symbol;
- continue;
- }
- return -1;
- /*sp2*/ case 4: if(strcmp(class, "space") == 0)
- continue;
- if(strcmp(class, "letter") == 0){
- STATE = 5;
- strcpy(lexChain[++l].lexem, " ");
- lexChain[l].lexem_class = "SPACE";
- lexem[++j] = symbol;
- continue;
- }
- return -1;
- /*kw2*/ case 5: if(strcmp(class, "space") == 0){
- STATE = 6;
- lexem[++j] = '\0';
- strcpy(lexChain[++l].lexem, lexem);
- kw_flag = keywordCheck(lexChain,l);
- if(!kw_flag)
- lexChain[l].lexem_class = "IDENTIFIER";
- while(j >= 0)
- lexem[j--] = "";
- continue;
- }
- if( (strcmp(class, "letter") == 0) || (strcmp(class, "digit") == 0) ){
- lexem[++j] = symbol;
- continue;
- }
- return -1;
- /*sp3*/ case 6: if(strcmp(class, "space") == 0)
- continue;
- if(strcmp(class, "letter") == 0){
- STATE = 7;
- strcpy(lexChain[++l].lexem, " ");
- lexChain[l].lexem_class = "SPACE";
- lexem[++j] = symbol;
- continue;
- }
- return -1;
- /*id2*/ case 7: if(strcmp(class, "space") == 0){
- STATE = 8;
- lexem[++j] = '\0';
- strcpy(lexChain[++l].lexem, lexem);
- kw_flag = keywordCheck(lexChain,l);
- if(!kw_flag)
- lexChain[l].lexem_class = "IDENTIFIER";
- while(j >= 0)
- lexem[j--] = "";
- continue;
- }
- if(strcmp(class, "colon") == 0){
- STATE = 9;
- lexem[++j] = '\0';
- strcpy(lexChain[++l].lexem, lexem);
- kw_flag = keywordCheck(lexChain,l);
- if(!kw_flag)
- lexChain[l].lexem_class = "IDENTIFIER";
- while(j >= 0)
- lexem[j--] = "";
- lexem[++j] = symbol;
- continue;
- }
- if( (strcmp(class, "letter") == 0) || (strcmp(class, "digit") == 0) ){
- lexem[++j] = symbol;
- continue;
- }
- return -1;
- /*sp4*/ case 8: if(strcmp(class, "space") == 0)
- continue;
- if(strcmp(class, "colon") == 0){
- STATE = 9;
- strcpy(lexChain[++l].lexem, " ");
- lexChain[l].lexem_class = "SPACE";
- lexem[++j] = symbol;
- continue;
- }
- return -1;
- /*cl*/ case 9: if(strcmp(class, "equals") == 0){
- STATE = 10;
- lexem[++j] = symbol;
- continue;
- }
- return -1;
- /*eq*/ case 10: if(strcmp(class, "space") == 0)
- continue;
- if(strcmp(class, "letter") == 0){
- STATE = 11;
- lexem[++j] = '\0';
- strcpy(lexChain[++l].lexem, lexem);
- lexChain[l].lexem_class = "ASSIGNS";
- while(j >= 0)
- lexem[j--] = "";
- lexem[++j] = symbol;
- continue;
- }
- /*id3*/ case 11: if(strcmp(class, "space") == 0){
- STATE = 12;
- lexem[++j] = '\0';
- strcpy(lexChain[++l].lexem, lexem);
- kw_flag = keywordCheck(lexChain,l);
- if(!kw_flag)
- lexChain[l].lexem_class = "IDENTIFIER";
- while(j >= 0)
- lexem[j--] = "";
- continue;
- }
- if(strcmp(class, "semicolon") == 0){
- STATE = 18;
- lexem[++j] = '\0';
- strcpy(lexChain[++l].lexem, lexem);
- kw_flag = keywordCheck(lexChain,l);
- if(!kw_flag)
- lexChain[l].lexem_class = "IDENTIFIER";
- while(j >= 0)
- lexem[j--] = "";
- strcpy(lexChain[++l].lexem, ";");
- lexChain[l].lexem_class = "SEMICOLON";
- continue;
- }
- if(strcmp(class, "bracket_o") == 0){
- STATE = 13;
- lexem[++j] = '\0';
- strcpy(lexChain[++l].lexem, lexem);
- kw_flag = keywordCheck(lexChain,l);
- if(!kw_flag)
- lexChain[l].lexem_class = "IDENTIFIER";
- while(j >= 0)
- lexem[j--] = "";
- lexem[++j] = symbol;
- continue;
- }
- if( (strcmp(class, "letter") == 0) || (strcmp(class, "digit") == 0) ){
- lexem[++j] = symbol;
- continue;
- }
- return -1;
- /*sp5*/ case 12: if(strcmp(class, "space") == 0)
- continue;
- if(strcmp(class, "semicolon") == 0){
- STATE = 18;
- strcpy(lexChain[++l].lexem, " ");
- lexChain[l].lexem_class = "SPACE";
- strcpy(lexChain[++l].lexem, ";");
- lexChain[l].lexem_class = "SEMICOLON";
- continue;
- }
- if(strcmp(class, "bracket_o") == 0){
- STATE = 13;
- strcpy(lexChain[++l].lexem, " ");
- lexChain[l].lexem_class = "SPACE";
- while(j >= 0)
- lexem[j--] = "";
- continue;
- }
- return -1;
- /*b_o*/ case 13: if(strcmp(class, "sign") == 0){
- STATE = 14;
- lexem[++j] = symbol;
- continue;
- }
- if(strcmp(class, "digit") == 0){
- STATE = 15;
- lexem[++j] = symbol;
- continue;
- }
- return -1;
- /*sg*/ case 14: if(strcmp(class, "digit") == 0){
- STATE = 15;
- lexem[++j] = symbol;
- continue;
- }
- return -1;
- /*dg*/ case 15: if(strcmp(class, "digit") == 0){
- lexem[++j] = symbol;
- continue;
- }
- if(strcmp(class, "bracket_c") == 0){
- STATE = 16;
- lexem[++j] = symbol;
- continue;
- }
- return -1;
- /*b_c*/ case 16: if(strcmp(class, "space") == 0){
- STATE = 17;
- lexem[++j] = '\0';
- strcpy(lexChain[++l].lexem, lexem);
- lexChain[l].lexem_class = "INDEX";
- while(j >= 0)
- lexem[j--] = "";
- continue;
- }
- if(strcmp(class, "semicolon") == 0){
- STATE = 18;
- lexem[++j] = '\0';
- strcpy(lexChain[++l].lexem, lexem);
- lexChain[l].lexem_class = "INDEX";
- while(j >= 0)
- lexem[j--] = "";
- strcpy(lexChain[++l].lexem, ";");
- lexChain[l].lexem_class = "SEMICOLON";
- continue;
- }
- return -1;
- /*sp6*/ case 17: if(strcmp(class, "space") == 0)
- continue;
- if(strcmp(class, "semicolon") == 0){
- STATE = 18;
- strcpy(lexChain[++l].lexem, " ");
- lexChain[l].lexem_class = "SPACE";
- strcpy(lexChain[++l].lexem, ";");
- lexChain[l].lexem_class = "SEMICOLON";
- continue;
- }
- return -1;
- /*sc*/ case 18: if(strcmp(class, "space") == 0)
- continue;
- return -1;
- }
- }
- return l;
- }
Advertisement
Add Comment
Please, Sign In to add comment