*****看看这个编译器怎么做,100分求相关LEX和YACC代码,分不够速加*****

wanglh100 2005-06-07 08:38:15
1.简单高级语言文法:
<程序> → [<常量说明>][<变量说明>]<语句>
<常量说明> → Const <常量定义>{,<常量定义>};
<常量定义> → <标识符>=<无符号整数>
<无符号整数> → <数字>{<数字>}
<字母> → a | b | c | … | z
<数字> → 0 | 1 | 2 | … | 9
<标识符> → <字母>{<字母>|<数字>}
<变量说明> → Var <标识符>{,<标识符>};
<语句> → <赋值语句>|<条件语句>|<当循环语句>|<复合语句>
<赋值语句> → <标识符>=<表达式>
<表达式> → <项>{<加法运算符><项>}
<项> → <因子>{<乘法运算符><因子>}
<因子> → <标识符>|<无符号整数>|‘(’<表达式>‘)’
<加法运算符> → +|-
<乘法运算符> → * |/
<条件语句> → if <条件> then <语句>| if <条件> then <语句> else <语句>
<条件> → <表达式><关系运算符><表达式>
<关系运算符> → ==|<=|<|>|>=|<>
<当循环语句> → while <条件> do <语句>
<复合语句> → begin <语句>{;<语句>} end

注:
<> — 非终结符号
[] — 可选项
{} — *
| — 或

2.分析
(1)单词符号结构
无符号整数:NUM =( 0 | 1 | 2 | … | 9 )+
标识符:ID =( a | b | c | … | z )( a | b | c | … | z | 0 | 1 | 2 | … | 9)*
(2)语法规则
P → C V S | C S | V S | S
/* P-程序 C-常量说明 V-变量说明 S-语句
C → Const D ;
/* D-常量定义
D → id = num , D | id = num
V → Var ID ;
ID → id , ID | id
S → A | I | W | B
/* A-赋值语句 I-条件语句 W-当循环语句 B-复合语句
A → id = E
/* E-表达式
E → T | E P T
/* T-项 P-加法运算符
T → F | T M F
/* F-因子 M-乘法运算符
F → id | num |(E)
P → + | -
M → * | /
I → if X then S | if X then S else S
/* X-条件
X → E O E
/* O-关系运算符
O → == | <= | < | > | >= | <>
W → While X do S
B → begin Y end
Y → S ; Y | S


(3)例子
Const x = 8 , y = 7 ;
Var a , b , c ;
begin
a = x ;
if a > 0
then
begin
c = y – 1 ;
a = a + 2 ;
end
else
begin
c = a + y ;
end
b = y ;
while b >= 0
do a = a – 1 ;
end

3.实验要求:
(1) 编写词法分析程序(源程序输入)
(2) 编写语法分析程序
(3) 编写语义分析程序(三地址代码输出)
(4) 符号表设计
(5) 错误处理
(6) 集成各子程序为一小型编译器
-------------------------------------------------
以上是要求实现的功能,急求LEX和YACC源码。
...全文
205 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
mmmcd 2005-06-15
  • 打赏
  • 举报
回复
subprogram_head
: FUNCTION ID arguments ':' standard_type ';'
{
$$=new_symbol_table($2,$3,global);
$$->argc=field_counter($3);
add_symbol_field($$,
new_table_field(Variable,$5,new_name_item($2,NULL),NULL,NULL,NULL));
add_symbol_field(global,
new_table_field(Function,$5,new_name_item($2,NULL),NULL,NULL,$$));
$$->rtype=$5;
current=$$;
free($2);
}
| PROCEDURE ID arguments ';'
{
$$=new_symbol_table($2,$3,global);
$$->argc=field_counter($3);
add_symbol_field(global,
new_table_field(Procedure,Void,new_name_item($2,NULL),
NULL,NULL,$$)
);
current=$$;
free($2);
}
;

arguments
: {
$$=NULL;
}
| '(' parameter_list ')'
{
$$=$2;
}
;

parameter_list
: identifier_list ':' type
{
$$=new_table_field(Variable,$3,$1,NULL,NULL,NULL);
}
| parameter_list ';' identifier_list ':' type
{
$$=new_table_field(Variable,$5,$3,NULL,$1,NULL);
}
;

compound_statement
: _BEGIN optional_statements _END
{
$$=$2;
}
;

optional_statements
: {$$=NULL;}
| statement_list
{
$$=$1;
}
;

statement_list
: statement
{
$$=$1;
}
| statement_list ';' statement
{
if($1){
add_statement_node($1,$3);
$$=$1;
}else{
$$=$3;
}
yyerrok;
}
| statement_list ';' error
{
$$=$1;
}
| error{
$$=NULL;
yyerror("also a test");
}
;

statement
: variable assignop expression
{ $$=new_statement_node(Ass, $1, NULL, $3, NULL, NULL, NULL, NULL);}
| procedure_statement
{ $$=new_statement_node(Proc, NULL, NULL, NULL, $1, NULL, NULL, NULL);}
| compound_statement
{ $$=$1;}
| _IF expression THEN statement
{ $$=new_statement_node(If, NULL, NULL, $2, NULL, $4, NULL, NULL);}
| _IF expression THEN statement _ELSE statement
{ $$=new_statement_node(If_Else, NULL, NULL, $2, NULL, $4, $6, NULL);}
| _WHILE expression _DO statement
{ $$=new_statement_node(While, NULL, NULL, $2, NULL, $4, NULL, NULL);}
| READ '(' variables ')'
{ $$=new_statement_node(Read, $3, NULL, NULL, NULL, NULL, NULL, NULL);}
| WRITE '(' expression ')'
{ $$=new_statement_node(Write, NULL, NULL, $3, NULL, NULL, NULL, NULL);}
| WRITE '(' MY_STR ')'
{ $$=new_statement_node(WriteStr, NULL, $3, NULL, NULL, NULL, NULL, NULL);}
/*| error
{ $$=NULL; yyerror("just a test.");
}*/
;

variables : variable
{
$$=$1;
}
| variables ',' variable
{
if($3)
$3->next=$1;
$$=$3;
}
;

variable
: ID
{
$$=search_variable_in_symbol_table(current,$1);
if(!$$)
{
yyerror("The identity has no declaration.");
}
else if($$->array)
{
yyerror("illegal usage of array");
}
}
| array_elemet
{
$$=$1;
}
;

array_elemet
: ID '[' expression ']'
{
$$=search_variable_in_symbol_table(current,$1);
if(!$$){
yyerror("The identity has no declaration.");
}
if($$->array)
$$->array->eplist=new_expression_list($3);
else
{
yyerror("illegal array");
}
}
;

procedure_statement : ID
{
$$=new_procedure_call( search_procedure_in_symbol_table(current,$1),
NULL);
if(!$$){
yyerror("The procedure has no declaration.");
}
free($1);
}
| procedure_with_param
{
$$=$1;
}
;
procedure_with_param : ID '(' expression_list ')'
{
$$=new_procedure_call( search_procedure_in_symbol_table(current,$1),
$3);
if($$ == NULL){
yyerror("The procedure has no declaration.");
}
free($1);
}
;

expression_list
: expression
{
$$=new_expression_list($1);
}
| expression_list ',' expression
{
if($3)
{
$$=new_expression_list($3);
$$->next=$1;
}
}
;

expression
: simple_expression
{
$$=$1;
}
| simple_expression RELOP simple_expression
{
$$=new_expression_node($2, $1, $3, NULL, temp, NULL);
}
/*
| error{
$$=NULL;
}
*/
;

RELOP : GE { $$=Grt;}
| LE { $$=Lee;}
| NE { $$=Neq;}
| GT { $$=Grt;}
| LT { $$=Les;}
| EQ { $$=Eql;}
;

simple_expression
: term
{
$$=$1;
}
| simple_expression ADDOP term
{
$$=new_expression_node($2, $1, $3, NULL, temp, NULL);
}
;

ADDOP : AD { $$=Add;}
| SB { $$=Sub;}
| OR { $$=Or;}
;

term
: factor
{
$$=$1;
}
| term MULOP factor
{
$$=new_expression_node($2, $1, $3, NULL, temp, NULL);
}
;

MULOP : ML {$$=Mul;}
| DV {$$=Div;}
| DIV {$$=Divi;}
| MOD {$$=Mod;}
| AND {$$=And;}
;

factor
: variable
{
$$=new_expression_node(Address, NULL, NULL, $1, temp, NULL);
}
| procedure_with_param
{
$$=new_expression_node(Funct, NULL, NULL, NULL, temp, $1);
}
| NUM
{
$$=new_expression_node(Constant, NULL, NULL, NULL, $1, NULL);
}
| '(' expression ')' { $$=$2; }
| NOT factor
{
$$=new_expression_node(Not, $2, NULL, NULL, temp, NULL);
}
| '-' factor %prec Uminus
{
$$=new_expression_node(Not, $2, NULL, NULL, temp, NULL);
}
;

%%

void yyerror(const char * msg)
{
on_error=1;
fprintf(stderr,"Line %ld, at symbol: \"%s\"\n%s\n",nLine,yytext,msg);
}
mmmcd 2005-06-15
  • 打赏
  • 举报
回复
%%
program
: { on_error=0; }
PROGRAM ID '(' INPUT ',' OUTPUT ')' ';'
declarations
{
global=new_symbol_table($3,$10,NULL);
global->argc=0;
free($3);
}
subprogram_declarations
{
current=global;
}
compound_statement
'.'
{
global->statements=$14;
if(!on_error)
execute_procedure(global,NULL);
free_symbol_table(global);
}
;

declarations
: {
$$=NULL;
}
| declarations VAR identifier_list ':' type ';'
{
if($5==Array)
$$=new_table_field(Variable,$5,$3,arptr,$1,NULL);
else
$$=new_table_field(Variable,$5,$3,NULL,$1,NULL);
}
;

identifier_list
: ID
{
$$=new_name_item($1,NULL);
free($1);
}
| identifier_list ',' ID
{
$$=new_name_item($3,$1);
free($3);
}
;

type
: standard_type
{
$$=$1;
}
| ARRAY '[' NUM DOTS NUM ']' of standard_type
{
arptr=new_array_ptr($3.ivalue, $5.ivalue, $8);
$$=Array;
}
;

standard_type
: INTEGER
{
$$=Integer;
}
| REAL
{
$$=Real;
}
;

subprogram_declarations
:
| subprogram_declarations subprogram_declaration ';'
{
}
;

subprogram_declaration
: subprogram_head declarations
{
add_symbol_field($1,$2);
}
compound_statement
{
current->statements=$4;
$$=$1;
}
;

mmmcd 2005-06-15
  • 打赏
  • 举报
回复
这是我正在做的,mini_pascal解释器,yacc部分的程序
%{
#include "scratch.h"
#include <malloc.h>
#include <string.h>
#include <stdio.h>
extern unsigned long nLine;
extern char* yytext;
extern FILE* yyout;
extern FILE* yyin;

arrayptr_type *arptr;
symbol_table_type *global,*current;
constant_type temp;
int on_error;

%}

%union{
name_list_type *nlist;
field_table_type *flist;
symbol_table_type *slist;
address_type *adlist;
expression_node_type *enode;
expression_list_type *eplist;
statement_node_type *snode;
procedure_call_type *pct;
char *id;
char *my_str;
enode_type opt;
variable_type vtype;
constant_type value;
};

%token PROGRAM INPUT OUTPUT VAR FUNCTION PROCEDURE _IF _BEGIN _END of THEN
%token _WHILE _DO READ WRITE NOT NEW ARRAY BREAK
%token DOTS assignop
%right _ELSE Uminus
%token<vtype> INTEGER REAL

%type<vtype> standard_type type
%type<nlist> identifier_list
%type<flist> declarations arguments parameter_list
%type<slist> subprogram_declaration subprogram_head
%type<adlist> variables variable array_elemet

%type<snode> compound_statement optional_statements statement_list statement
%type<enode> expression simple_expression term factor
%type<eplist> expression_list

%type<pct> procedure_statement procedure_with_param

%type<opt> RELOP ADDOP MULOP

%token<id> ID
%token<value> NUM
%token<my_str> MY_STR;

%token GE LE NE GT LT EQ
%token AD SB OR
%token ML DV DIV MOD AND

mathe 2005-06-15
  • 打赏
  • 举报
回复
安装一下Linux不就有Lex和Yacc了
wanglh100 2005-06-12
  • 打赏
  • 举报
回复
To pankun:如果你能给我提一点实现思路比你现在的话有说服力的多。

我是做的过程中遇到困难,做了一个词法分析的,但不知道语方分析怎么实现?希望有做过的朋友要代码贴出来参考一下,相关也可以的。
pankun 2005-06-10
  • 打赏
  • 举报
回复
类pascal的吧,很好做,以前实现过一个.
不过不回答作业题 ,看书吧,对自己有好处
wanglh100 2005-06-08
  • 打赏
  • 举报
回复
自己项一下
mmmcd 2005-06-07
  • 打赏
  • 举报
回复
又是作业题

33,008

社区成员

发帖
与我相关
我的任务
社区描述
数据结构与算法相关内容讨论专区
社区管理员
  • 数据结构与算法社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧