/*==================== wc.l ============================*/ %{ unsigned charcount = 0, wordcount = 0, linecount = 0; %} word [^ \t\n]+ eol \n %% {word} { wordcount++; charcount += yyleng; } {eol} {charcount++; linecount++; charcount++; } %% main() { yylex(); printf("%d %d %d\n", linecount, wordcount, charcount); } /*==================== wc2.l ============================*/ %{ unsigned charCount = 0, wordCount = 0, lineCount = 0; %} word [^ \t\n]+ eol \n %% {word} { wordCount++; charCount += yyleng; } {eol} { charCount++; lineCount++; } . charCount++; %% main(int argc, char *argv[]) { FILE *file; if (argc > 1) { file = fopen(argv[1],"r"); if(!file){ fprintf(stderr,"could not open %s\n", argv[1]); exit(1); } yyin = file; } yylex(); printf("%d %d %d\n",charCount, wordCount, lineCount); return 0; } /*==================== word1.l ============================*/ %{ /* A simple lexical analyzer for English sentence. */ %} %% [\t ]+ /* ignore white spaces: tabs or blanks */; is | am | are | were | was | be | being | been { printf(" %s(be-verb)", yytext); } go | goes | went | do | study { printf(" %s(verb)", yytext); } good | bad | rich | poor | pretty | beautiful { printf(" %s(adj)", yytext); } very | much | too { printf(" %s(adj)", yytext); } to | by | in | on | at { printf(" %s(prep)", yytext); } the | a { printf(" %s(det)", yytext); } he | she | I | you | they { printf(" %s(pronoun)", yytext); } boy | girl | man | men | woman | women | student | teacher | school | lunch { printf(" %s(noun)", yytext); } [a-zA-Z]+ { printf(" %s(undefined)", yytext); } . \n { ECHO; /* default */ } %% main() { printf("Input English setence : "); yylex(); } /*==================== word2.l ============================*/ %{ #include /* Word recognizer with a symbol table. */ enum { LOOKUP = 0, /* default - looking rather than defining. */ VERB, ADJ, ADV, NOUN, PREP, PRON, CONJ }; int state; %} %% /* end of line, return to default state */ \n { state = LOOKUP; } /* If a line starts with a reserved POS name, */ /* then start defining words of that type. */ ^verb { state = VERB; } ^adj { state = ADJ; } ^adv { state = ADV; } ^noun { state = NOUN; } ^prep { state = PREP; } ^pron { state = PRON; } ^conj { state = CONJ; } /* a normal word, define it or look it up */ [a-zA-Z]+ { if (state != LOOKUP) { /* define the current word */ add_word(state, yytext); } else { switch (lookup_word(yytext)) { case VERB: printf("%s: verb\n", yytext); break; case ADJ: printf("%s: adj\n", yytext); break; case ADV: printf("%s: adv\n", yytext); break; case NOUN: printf("%s: noun\n", yytext); break; case PREP: printf("%s: prep\n", yytext); break; case PRON: printf("%s: pron\n", yytext); break; case CONJ: printf("%s: conj\n", yytext); break; default: printf("%s: don't recognize\n", yytext); break; } } } . /* ignore anything else */ %% /* define a linked list of words and types */ struct word { char *word_name; int word_type; struct word *next; }; struct word *word_list; /* first element in word list */ extern void *malloc(); int lookup_word(char *word) { struct word *wp = word_list; /* search down the list looking for the word */ for (; wp; wp = wp->next) { if (strcmp(wp->word_name, word) == 0) return wp->word_type; } return LOOKUP; /* not found */ } 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; } /* word not there, allocate a new entry and link it on the list */ wp = (struct word *) malloc(sizeof(struct word)); wp->next = word_list; /* have to copy the word itself as well */ wp->word_name = (char *) malloc(strlen(word)+1); strcpy(wp->word_name, word); wp->word_type = type; word_list = wp; return 1; } void main() { yylex(); }