⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 wordclassifier.l

📁 lex and yacc, trying to identify the different words
💻 L
字号:
%{/*word recogniser with a symbol table*/typedef enum{	LOOKUP, VERB, ADJ, ADV, NOUN,	PREP, PRON, CONJ} STATE;STATE state;typedef enum { false, true } bool;bool AddWord(STATE, char *);STATE LookupWord(char *);%}%%\n { state = LOOKUP; } /*end of line return to default state*/	/* whenever a line starts with a reserved part of speech name */	/* 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-zA-Z]+ {	/* a normal word, define it or look it up */	if(state != LOOKUP)	{		/*define the current word*/		AddWord(state, yytext);	}	else	{		switch(LookupWord(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 recognise\n", yytext);					break;		}	} }. /*ignore anything else, ie. No action part here */ ;%%int main(){	yylex();}/*define a linked list of words and types*/typedef struct WORD{	char *wordName;	STATE wordType;	struct WORD *next;} word;word *wordList; /*points at word list*/bool AddWord(STATE type, char *newWord){	word *wp;	if(LookupWord(newWord) != LOOKUP)	{		printf("!!! Word %s already defined \n", newWord);		return false;	}	/*word not there, allocate a new entry and link it */	wp = (word *)malloc(sizeof(word));	wp->next = wordList;	wp->wordName = (char *)malloc(strlen(newWord)+1);	strcpy(wp->wordName, newWord);	wp->wordType = type;	wordList = wp;	return true;}STATE LookupWord(char *findWord){	word *wp = wordList;	for(; wp; wp = wp->next)	{		if(strcmp(wp->wordName, findWord) == 0)			return wp->wordType;	}	return LOOKUP; /*not found*/}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -