词法文件:
%{
/*
It is a test here.
*/
#include "y.tab.h"
#define LOOKUP 0
#define NOT_FOUND 1
int state;
%}
%%
[\n ]+ {state=LOOKUP;}
\.\n {state=LOOKUP;return 0;}
^verb {state=VERB;}
^noun {state=NOUN;}
^adj {state=ADJECTIVE;}
^adv {state=ADVERB;}
^prep {state=PREP;}
^pron {state=PRON;}
^conj {state=CONJ;}
[a-zA-Z0-9]+ {
if (state==LOOKUP){
int s=lookup_word(yytext);
switch(s){
case LOOKUP: break;
case NOT_FOUND: break;
case VERB: return(VERB);
case NOUN: return(NOUN);
case ADJECTIVE: return(ADJECTIVE);
case ADVERB:return(ADVERB);
case PREP:return(PREP);
case PRON:return(PRON);
case CONJ:return(CONJ);
default: printf("%s: cannt be recognized.",yytext);
}
}else{
add_word(state,yytext);
}
}
. {ECHO;}
%%
typedef struct _word {
char *word_name;
int type;
struct _word *next;
} word,*pword;
word * word_list;
extern void *malloc();
int add_word(int type,char *word_name){
if (lookup_word(word_name) != NOT_FOUND) {return 0 ;}
word * w=(word *)malloc(sizeof(word));
w->word_name=(char*)malloc(strlen(word_name)+1);
strcpy(w->word_name,word_name);
w->type=type;
w->next=word_list;
word_list=w;
return 1;
}
int lookup_word(char * word_name){
word * w=word_list;
while(w){
if (strcmp(w->word_name,word_name)==0) {
return w->type;
}
w=w->next;
}
return NOT_FOUND;
}
======================语法文件==========
%{
#include <stdio.h>
//extern char *yytext;
%}
%token VERB ADJECTIVE ADVERB NOUN PREP PRON CONJ
%%
sentence: simple_sentence {printf("a simple sentence.\n");}
|compound_sentence {printf("a compound sentence.\n");}
simple_sentence: subject verb object|subject verb object prep_phrase
compound_sentence: simple_sentence CONJ simple_sentence|simple_sentence CONJ compound_sentence
subject: NOUN|PRON|ADJECTIVE subject
verb: verb VERB|ADVERB VERB|VERB
object: NOUN|ADJECTIVE object
prep_phrase: PREP NOUN
%%
extern FILE *yyin;
//extern char *yytext;
int main(int argc,char *argv[]){
while (!feof(yyin)){
yyparse();
}
}
yyerror(s)
char *s;
{
printf("Error: %s.\n",s);
}
=================编译过程==============
cd 文件目录
flex test.l
bison -d test.y 或 yacc -d test.y
gcc lex.yy.c test.tab.c -ll -o test.exe
之后执行test.exe,报错 segmentation fault
不知何解?请教.
Yacc flex gcc
编译打包和其他
-
- 帖子: 1
- 注册时间: 2012-03-20 21:15
前往
- 公告/注意事项
- ↳ 新闻和通知
- ↳ 软件推荐
- 系统安装
- ↳ 教学和常见问答
- ↳ 系统安装和升级
- ↳ 启动和引导
- ↳ 网卡无线拨号
- ↳ 硬件与内核
- ↳ 笔记本及便携设备
- ↳ Ubuntu衍生版
- ↳ 其他类Unix OS发行版
- ↳ Debian发行版
- 配置美化
- ↳ 中文显示和输入法
- ↳ 个人配置文件存放点
- ↳ 软件文档翻译
- ↳ 窗口管理器及美化
- ↳ 桌面展示
- 软件使用
- ↳ 办公、图像、机械电子设计等
- ↳ Vim和Emacs
- ↳ 开源模板库
- ↳ 互联网相关软件
- ↳ 影音多媒体
- ↳ 游戏和模拟器
- ↳ 虚拟机和虚拟化
- ↳ Wine及其分支
- ↳ 其它类软件
- 服务器管理
- ↳ 服务器运维
- ↳ 云计算和容器(K8S/KVM/Docker/WSL等)
- ↳ Ubuntu VPS
- 程序设计与开发
- ↳ Shell脚本
- ↳ 软件/网站开发
- ↳ 内核及嵌入式开发
- ↳ Ubuntu编译和打包
- ↳ 开源小工具
- ↳ 挑战任务
- 感想与交流
- ↳ 深度PK版
- ↳ Ubuntu故事和感慨
- ↳ 同城交流
- ↳ 校园社团
- ↳ 港澳台校区
- ↳ 国外校区
- 站务区
- ↳ 论坛管理
- ↳ Ubuntu中文网上商店