ShrekOP

Assg3

Dec 16th, 2022
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.03 KB | None | 0 0
  1. a) Write Lexical Analyzer without using SYMBOL TABLE for subset of ā€˜Cā€™ programming language
  2. File: 3a.l
  3. %{ #include<stdio.h> %} DIGIT[0-9] NUMBER {DIGIT}* REAL {DIGIT}*[.]{NUMBER} TEXT [A-Za-z] KEYWORDS "auto"|"break"|"enum"|"struct"|"typedef" DATATYPE "int"|"float"|"double"|"long"|"void"|"unsigned char" CONDTIONAL "if"|"else"|"else if"|"switch"|"case" ITERATIVE "for"|"while"|"do" SEMICOLAN ";" IDENTIFIER {TEXT}({DIGIT}|{TEXT}|"_")* NONIDENTIFIER {DIGIT}({TEXT}|{DIGIT}|"_")* ARITHMATIC_OP "+"|"-"|"/"|"%"|"*"; LOGICAL_OP "&&"|"||"|"!"|"!=" RELEATIONAL_OP "<"|">"|"<="|">="|"=="
  4. UNARY "++"|"--" FUNCTION {ACCESS}{DATATYPE}{IDENTIFER}"("({DATATYPE}{IDENTIFIER})*")" %% [ \n ]+ ; {CONDTIONAL} {printf("%s ==> CONDTIONAL \n",yytext);} {ITERATIVE} {printf("%s ==> ITERATIVE \n",yytext);} {DATATYPE} {printf("%s ==> DATATYPE\n",yytext);} {KEYWORDS} {printf("%s ==> KEYWORDS \n",yytext);} {IDENTIFIER} {printf("%s ==> IDENTIFIER \n",yytext);} {REAL} {printf("%s ==> REAL \n",yytext);} {NUMBER} {printf("%s ==> NUMBER \n",yytext);} {NONIDENTIFIER} {printf("%S ==> NONIDENTIFIER \n",yytext);} {SEMICOLAN} {printf("%s ==> SEMICOLAN \n",yytext);} {UNARY} {printf("%s ==> UNARY \n",yytext);} {ARITHMATIC_OP} {printf("%s ==> ARITHMATIC_OP \n",yytext);} {LOGICAL_OP} {printf("%s ==> LOGICAL OP \n",yytext);} {RELEATIONAL_OP} {printf("%s ==> RELEATIONAL OP \n",yytext);} "=" {printf("%s ==> ASSIGNMENT OP \n",yytext);} "{" {printf("%s ==> BLOCK BEGIN \n",yytext);} "}" {printf("%s ==> BLOCK END \n",yytext);} "(" {printf("%s ==> PARATHRSIS BEGIN \n",yytext);} ")" {printf("%s ==> PARATHRSIS END \n",yytext);} . ; %% int yywrap(void){} int main ( ) { yylex(); return 0; }
  5.  
  6.  
  7. b) Write Lexical Analyzer with SYMBOL TABLE for subset of ā€˜Cā€™ programming language
  8. File: 3b.l %{ #include<stdio.h> enum { LOOKUP = 0, TEXT, KEYWORD, DATATYPE, CONDTIONAL, ITERATIVE, ARITHMATIC_OP }; int state; int add_word(int type, char *word) ; int lookup_word(char *word) ; %} %% \n { state = LOOKUP; } ^text { state =TEXT; } ^keyword { state = KEYWORD; } ^datatype { state = DATATYPE; } ^condtional { state = CONDTIONAL; } ^iterative { state = ITERATIVE; } ^arithmatic_op { state = ARITHMATIC_OP; } [a-zA-Z]+ { if (state!=LOOKUP) { add_word(state, yytext); } else { switch ( lookup_word(yytext) ) { case TEXT: printf("%s: text \n", yytext); break; case KEYWORD: printf("%s: keyword \n", yytext); break; case DATATYPE: printf("%s: datatype\n", yytext); break; case CONDTIONAL: printf("%s: condtional\n", yytext); break; case ITERATIVE: printf("%s: iterative \n", yytext); break; case ARITHMATIC_OP: printf("%s: arithmatic_op \n", yytext); break; default: printf ( "%s: don't recognize\n" , yytext) ; } }
  9. } . %% int yywrap(void){} int main() { printf("\nEnter any string: \n\n"); yylex(); return 0; } struct word { char *word_name; int word_type; struct word *next; }; struct word *word_list; extern void *malloc() ; int add_word(int type, char *word) { struct word *wp; if(lookup_word(word) != LOOKUP) { printf("!!! warning: word %s already defined \n", word); return 0; } wp = (struct word *) malloc(sizeof(struct word) ) ; wp->next=word_list; wp->word_name = (char *)malloc(strlen (word)+1); strcpy (wp->word_name, word); wp->word_type = type; word_list = wp; return 1; } int lookup_word (char *word) { struct word *wp = word_list; for(; wp; wp = wp->next) { if(strcmp(wp->word_name, word) == 0) return wp->word_type; } return LOOKUP; }
  10.  
  11.  
  12. c) Write Lexical Analyzer for parts of speech for subset of ENGLISH language without using SYMBOL TABLE
  13. File: 3c.l %{ #include<stdio.h> %} %% [\t ]+ is | am | are | were | was | be | being | been | do | does | did | will | would | should | can | could | has | have | had | go { printf("%s: is a verb\n", yytext); } very | simply |
  14. gently | quietly | calmly | angrily { printf("%s: is an adverb\n", yytext); } to | from | behind | above | below | between { printf("%s: is a preposition\n", yytext); } if | then | and | but | or { printf("%s: is a conjunction\n", yytext); } alive | better | clever | careful | dead | easy | gifted | hallowed | helpful | important | inexpensive | mealy | good | famous { printf("%s: is an adjective\n", yytext); } I | you | he | their | my | your | his | her | she | we | they { printf("%s: is a pronoun\n", yytext); } [a-zA-Z]+ { printf("%s: don't recognize, might be a noun\n", yytext); } .|\n { ECHO; } %% int yywrap(void){} int main ( ) { printf("\nEnter any string: "); yylex();
  15. return 0; }
  16.  
  17.  
  18. d) Write Lexical Analyzer for parts of speech for subset of ENGLISH language with SYMBOL TABLE
  19. File: 3d.l %{ #include<stdio.h> enum { LOOKUP = 0, VERB, ADJ, ADV, NOUN, PREP, PRON, CONJ }; int state; int add_word(int type, char *word) ; int lookup_word(char *word) ; %} %% \n { state = LOOKUP; } ^verb { state = VERB; } ^adj { state = ADJ; } ^adv { state = ADV; } ^noun { state = NOUN; } ^prep { state = PREP; } ^pron { state = PRON; } ^conj { state = CONJ; }
  20. [a-zA-Z]+ { if (state!=LOOKUP) { add_word(state, yytext); } else { switch ( lookup_word(yytext) ) { case VERB: printf("%s: verb\n", yytext); break; case ADJ: printf("%s: adjective\n", yytext); break; case ADV: printf("%s: adverb\n", yytext); break; case NOUN: printf("%s: noun\n", yytext); break; case PREP: printf("%s: preposition\n", yytext); break; case PRON: printf("%s: pronoun\n", yytext); break; case CONJ: printf("%s: conjunction\n", yytext); break; default: printf ( "%s: don't recognize\n" , yytext) ; } } } . %% int yywrap(void){} int main( ) { printf("\nEnter any string: \n\n"); yylex(); return 0; } struct word { char *word_name; int word_type; struct word *next; }; struct word *word_list; extern void *malloc() ; int add_word(int type, char *word) { struct word *wp; if(lookup_word(word) != LOOKUP) { printf("!!! warning: word %s already defined \n", word); return 0; }
  21. wp = (struct word *) malloc(sizeof(struct word) ) ; wp->next=word_list; wp->word_name = (char *)malloc(strlen (word)+1); strcpy (wp->word_name, word); wp->word_type = type; word_list = wp; return 1; } int lookup_word (char *word) { struct word *wp = word_list; for(; wp; wp = wp->next) { if(strcmp(wp->word_name, word) == 0) return wp->word_type; } return LOOKUP; }
Add Comment
Please, Sign In to add comment