编译原理 第六篇 上机辅导.pptVIP

  • 1
  • 0
  • 约 52页
  • 2017-06-07 发布于湖北
  • 举报
* 3.3 语法分析器 table:IDENTIFIER { $$=$1; }; fieldsdefinition:field_type { $$=$1; cfdef_end=$1; } |fieldsdefinition , field_type { $$=$1; cfdef_end-next_fdef=$3; cfdef_end=$3; }; * 3.3 语法分析器 field_type:field type { $$=(_createfieldsdef_type *)malloc( sizeof(_createfieldsdef_type)); $$-field=$1; if (strlen($2)==0) /*表示类型为int的时候,用INT表示类型,长度定为4*/ { $$-type=INT; $$-length=4; $$-next_fdef=NULL; } * 3.3 语法分析器 else /*类型为CHAR:用CHAR表示类型,长度定为$2*/ {$$-type=CHAR; $$-length=atoi($2); $$-next_fdef=NULL; } }; field:IDENTIFIER { $$=$1; }; type: CHAR ( NUMBER ) {$$=$3;} |INT {$$=\0;}; * 3.4 SELECT 语句的实现 词法分析部分: SELECT {return SELECT;} FROM {return FROM;} WHERE {return WHERE;} AND {return AND;} OR {return OR;} ; | ( | ) | , | | \. | = | | {return yytext[0];} * 3.4 SELECT 语句的实现 1.设计SELECT语言文法 select 语句文法: statement→selectsql | ...... selectsql→SELECT fields_star FROM tables ; | SELECT fields_star FROM tables WHERE conditions ; fields_star→table_fields | * tables→table | tables , table table_fields→table_field | table_fields , table_field table_field→field | table . field table → IDENTIFIER field → IDENTIFIER * 3.4 SELECT 语句的实现 设计语法树的节点,用于存放表达式的语法树; typedef struct _selectedfields{ /*select语句中选中的字段*/ char *table; char *field; struct _selectedfields *next_sf; }_selectedfields_type; typedef struct _selectedtables{ /*select语句中选中的表*/ char *table; struct _selectedtables *next_st; }_selectedtables_type; typedef struct _selectstruct{ /*select语法树的根节点*/ _selectedfields_type *sf; _selectedtables_type *st; _conditions_type *cons; }_selectstruct_type; * 3.4 SELECT 语句的实现 typedef struct _conditions{ struct _conditions *left; struct _conditions *right; char comp_op; /*(a--and),(o--or), , , = */ char type; /*2--是表的字段,1--是字符串型,0--是整型*/ char *table; /*不为NULL就存放表名*/ char *value;

文档评论(0)

1亿VIP精品文档

相关文档