📄 myparser.y
字号:
EndCode[$5+1]=$6.varlen;
EndCode[$5+2]=EndCode[$5+1]*3;
EndCode[$5+3]=$6.displ-$5;
EndCode[codeindex++]=6;
EndCode[codeindex++]=0;
currentsymtable=currentsymtable->m_parenttable;
currentlever--;
}
;
ProQ: {
$$=codeindex-5;
}
;
FormParaList: FormParaList ';' ParaDef
{$$=$1+$3;}
| ParaDef
{$$=$1;}
;
ParaDef: VAR
{
checkvar=true;
checkparam = true;
}
VarDefState
{$$=$3;}
| {
checkvar = true;
checkparam = false;
}
VarDefState
{$$=$2;}
;
/*<语句>-><赋值语句>|<过程语句>|<if语句>|<while语句>|<复合语句>|empty*/
Statement: AsignState
{
}
| ProceState
{
}
| ISE Statement //%prec hiheest
{
EndCode[$1]=codeindex-$1+1;
}
| IBT Statement
{
EndCode[$1]=codeindex-$1+1;
}
| WBD Statement
{
EndCode[codeindex++]=11;
EndCode[codeindex]=$1.conti-codeindex+1;
codeindex++;
EndCode[$1.outwhile]=codeindex-$1.outwhile+1;
}
| CompState
{}
| /*empty*/
;
/*<赋值语句>-><变量访问>:=<表达式>*/
AsignState: Variable ASSIGN Expr
{
EndCode[codeindex++]=2;
EndCode[codeindex++]=$3.len;
}
| Variable ASSIGN BoolExpr
{
EndCode[codeindex++]=2;
EndCode[codeindex++]=$3.len;
}
;
/*<过程语句>-><过程名>[(<实参表>)]
<实参表>-><实参>{,<实参>}
<实参>-><表达式>|<变量访问>*/
ProceState: READ '(' Variable ')'{ EndCode[codeindex++]=25; }
| WRITE '(' Expr ')'
{
if($3.type==VBool)
EndCode[codeindex++]=31;
else
EndCode[codeindex++]=30;
}
| WRITE '(' BoolExpr ')'{ EndCode[codeindex++]=31;}
| ID
{
symboltable* temp =searchProce($1,symtable);
EndCode[codeindex]=22;
EndCode[codeindex+1]=currentlever - temp->m_lever +1;
EndCode[codeindex+2]=temp->codebegin - codeindex;
codeindex+=3;
}
| ID
{
ParamArray=(searchProce($1,symtable))->m_procesymbol->m_typeInfo;
IsParamIndex=0;
}
'('RealParaList ')'
{
symboltable* temp =searchProce($1,symtable);
EndCode[codeindex]=22;
EndCode[codeindex+1]=currentlever - temp->m_lever +1;
EndCode[codeindex+2]=temp->codebegin - codeindex;
codeindex+=3;
}
;
RealParaList: RealParaList ',' RealPara
| RealPara
;
RealPara: Expr//包括变量访问
{
if(ParamArray[IsParamIndex]->m_checkparam)
codeindex=codeindex-2;
IsParamIndex++;
/* if(ParamArray[IsParamIndex]->m_type!=$1.type)
{
errorcount++;
cout<<"error,h("<<yylexerptr->yylineno<<"):"<<"参数访问类型不匹配"<<endl;
}
*/ }
;
/*<if语句>->if<表达式>then<语句>[else<语句>]*/
ISE: IBT Statement ELSE
{
EndCode[codeindex++]=11;
$$ = codeindex++;
EndCode[$1]=codeindex-$1+1;
}
;
IBT: IF BoolExpr THEN
{
EndCode[codeindex++]=5;//不成功
$$=codeindex++;
}
;
/*<while语句>->while<表达式>do<语句>*/
WBD: Wh BoolExpr DO
{
EndCode[codeindex++]=5;//不成功
$$.outwhile=codeindex++;
$$.conti = $1;
}
;
Wh: WHILE
{
$$=codeindex;
}
;
/*<复合语句>->begin<语句>{;<语句>}end*/
CompState: BEG CompQ StateList END
{
$$=$2;
}
;
CompQ: {
$$ = codeindex;
}
;
StateList: StateList ';' Statement
{
}
| Statement
{
}
;
/*<表达式>-><简单表达式>[<关系算符><简单表达式>]
<关系算符>-><|>|=|<=|>=|<>
<简单表达式>->[+-]<项>{<加法算符><项>}
<加法算符>->+|-|OR
<项>-><因子>{<乘法算符><因子>}
<乘法算符>->*|div|mod|and
<因子>-><常数>|<变量访问>|(<表达式>)|not<因子>*/
Expr: Expr'+'Expr
{
$$.len=$1.len;
$$.type=VInt;
//$$.type=$1.type;
EndCode[codeindex++]=0;
}
| Expr'-'Expr
{
$$.len=$1.len;
$$.type=VInt;
//$$.type=$1.type;
EndCode[codeindex++]=26;
}
| Expr'*'Expr
{
$$.len=$1.len;
$$.type=VInt;
//$$.type=$1.type;
EndCode[codeindex++]=16;
}
| Expr DIV Expr//0?????
{
$$.len=$1.len;
$$.type=VInt;
//$$.type=$1.type;
EndCode[codeindex++]=4;
}
| Expr MOD Expr//0?????
{
$$.len=$1.len;
$$.type=VInt;
//$$.type=$1.type;
EndCode[codeindex++]=15;
}
| '('Expr')'
{
$$.len=$2.len;
$$.type=$2.type;
}
| '-' Expr %prec UMINUS
{
$$.len=$2.len;
$$.type=VInt;
//$$.type=$2.type;
EndCode[codeindex++]=14;
}
| '+' Expr %prec UMINUS
{
$$.len=$2.len;
$$.type=VInt;
//$$.type=$2.type;
}
| Variable//常量或变量
{
$$.len = $1.len;
if($1.type!=CInt && $1.type!=CBool)//不是常量
{
$$.type = $1.type;
EndCode[codeindex++]=27;
EndCode[codeindex++]=$1.len;
}
else
{
if($1.type==CInt)
$$.type=VInt;
else
$$.type=VBool;
EndCode[codeindex++]=3;
EndCode[codeindex++]=$1.sym->m_value;
}
}
| NUMBER
{
$$.type=VInt;
$$.len = 1;
EndCode[codeindex++]=3;
EndCode[codeindex++]=$1;
}
;
BoolExpr: Expr RelationOp Expr
{
$$.type=VBool;
$$.len=1;
switch($2)
{
case('<'):
EndCode[codeindex++]=13;
break;
case('>'):
EndCode[codeindex++]=10;
break;
case('='):
EndCode[codeindex++]=8;
break;
case('g'):
EndCode[codeindex++]=19;
break;
case('n'):
EndCode[codeindex++]=17;
break;
case('l'):
EndCode[codeindex++]=18;
break;
default:
break;
}
}
| BoolExpr AND BoolExpr
{
$$.type=VBool;
$$.len=1;
EndCode[codeindex++]=1;
}
| BoolExpr OR BoolExpr
{
$$.type=VBool;
$$.len=1;
EndCode[codeindex++]=21;
}
| NOT BoolExpr
{
$$.type=VBool;
$$.len=1;
EndCode[codeindex++]=20;
}
| '('BoolExpr')'
{
$$.len=$2.len;
$$.type=$2.type;
}
| Variable//常量或变量
{
$$.len = $1.len;
if($1.type == VBool)//不是常量
{
$$.type = $1.type;
EndCode[codeindex++]=27;
EndCode[codeindex++]=$1.len;
}
else
if($1.type==CBool)
{
$$.type=VBool;
EndCode[codeindex++]=3;
EndCode[codeindex++]=$1.sym->m_value;
}
else
{
}
}
| TRUE
{
$$.type=VBool;
$$.len = 1;
EndCode[codeindex++]=3;
EndCode[codeindex++]=1;
}
| FALSE
{
$$.type=VBool;
$$.len = 1;
EndCode[codeindex++]=3;
EndCode[codeindex++]=0;
}
;
RelationOp: '<'
{$$='<';}
| '>'
{$$='>';}
| '='
{$$='=';}
| GE
{$$='g';}
| NE
{$$='n';}
| LE
{$$='l';}
;
/*<变量访问>-><变量名>{<选择器>}
<选择器>-><下标选择器>|<域选择器>
<下标选择器>->[<表达式>]
<域选择器>->.<域名>*/
Variable: Variable '['Expr']'
{
int index;
EndCode[codeindex++]=12;
EndCode[codeindex++]=$1.sym->m_typeInfo[0]->arraystart;
EndCode[codeindex++]=$1.sym->m_typeInfo[0]->arrayend;
index=codeindex++;//记录长度的下标
EndCode[codeindex++]=yylexerptr->yylineno;
if($1.type==DArray || $1.type==DRecord || $1.type==VArray || $1.type==VRecord)//当前是新类型名
{
symbol* temp=$1.sym->m_typeInfo[0];
if(temp->arrayType==DArray||temp->arrayType==DRecord)
{
$$.sym=temp;
$$.type=temp->arrayType;
EndCode[index]=temp->m_typeInfo[0]->m_length;//$1.len/
$$.len = temp->m_typeInfo[0]->m_length;
}
else//数组中为普通类型
{
$$.sym=temp;
$$.type=temp->arrayType;
EndCode[index]=1;
$$.len = 1;
}
}
else
{
errorcount++;
cout<<"error,h("<<yylexerptr->yylineno<<"):"<<" 类型不匹配。"<<endl;
}
}
| Variable '.' ID
{
symbol** temp =$1.sym->m_typeInfo[0]->m_typeInfo;
int len=0;
bool check=false;
for(int i=0;i<30;i++)
{
if(!temp[i])
{
errorcount++;
cout<<"error,h("<<yylexerptr->yylineno<<"):"<<$3<<" 不存在"<<endl;
break;
}
else
{
if(!strcmp(temp[i]->m_name,$3))
{
break;
check=true;
}
len+=temp[i]->m_length;
}
}
if(check)
{
EndCode[codeindex++]=9;
EndCode[codeindex++]=len;
$$.sym=temp[i];
$$.len=temp[i]->m_length;
$$.type=temp[i]->m_type;
}
else
{
$$.sym=NULL;
$$.len=0;
// $$.type=temp[i]->m_type;
}
}
| ID
{
int lever;
symboltable* temp = currentsymtable->search($1);
if(temp)
{
lever = currentlever - temp->m_lever;
if(temp->m_tempsymbol->m_checkvar==false)
{
if(temp->m_tempsymbol->m_type==VInt || temp->m_tempsymbol->m_type==VBool
|| temp->m_tempsymbol->m_type==VArray || temp->m_tempsymbol->m_type==VRecord)
{
$$.sym=temp->m_tempsymbol;
$$.type=temp->m_tempsymbol->m_type;
$$.len = temp->m_tempsymbol->m_length;
EndCode[codeindex++]=28;
EndCode[codeindex++]=lever;
EndCode[codeindex++]=temp->m_tempsymbol->m_value;
}
else
if(temp->m_tempsymbol->m_type==CInt || temp->m_tempsymbol->m_type==CBool)
{
$$.sym=temp->m_tempsymbol;
$$.type=temp->m_tempsymbol->m_type;
$$.len = temp->m_tempsymbol->m_length;
}
else//error
{
}
}
else//参数的访问
{
if(temp->m_tempsymbol->m_checkparam==false)//值传递
{
if(temp->m_tempsymbol->m_type==VInt || temp->m_tempsymbol->m_type==VBool
|| temp->m_tempsymbol->m_type==VArray || temp->m_tempsymbol->m_type==VRecord)
{
$$.sym=temp->m_tempsymbol;
$$.type=temp->m_tempsymbol->m_type;
$$.len = temp->m_tempsymbol->m_length;
EndCode[codeindex++]=28;
EndCode[codeindex++]=lever;
EndCode[codeindex++]=temp->m_tempsymbol->m_value;
}
else
if(temp->m_tempsymbol->m_type==CInt || temp->m_tempsymbol->m_type==CBool)
{
$$.sym=temp->m_tempsymbol;
$$.type=temp->m_tempsymbol->m_type;
$$.len = temp->m_tempsymbol->m_length;
}
else//error CProce
{
}
}
else
{
if(temp->m_tempsymbol->m_type==VInt || temp->m_tempsymbol->m_type==VBool
|| temp->m_tempsymbol->m_type==VArray || temp->m_tempsymbol->m_type==VRecord)
{
$$.sym=temp->m_tempsymbol;
$$.type=temp->m_tempsymbol->m_type;
$$.len = temp->m_tempsymbol->m_length;
EndCode[codeindex++]=29;
EndCode[codeindex++]=lever;
EndCode[codeindex++]=temp->m_tempsymbol->m_value;
}
else
{
}
}
}
}
else
{
errorcount++;
cout<<"error,h("<<yylexerptr->yylineno<<"):"<<$1<<" 不存在"<<endl;
}
}
;
ConstNum: ID
{
symboltable* temp = currentsymtable->search($1);
if(temp==NULL)
{
errorcount++;
cout<<"error,h("<<yylexerptr->yylineno<<"):"<<$1<<" 不存在"<<endl;
}
else
if(temp->m_tempsymbol)
{
if(temp->m_tempsymbol->m_type == CBool||temp->m_tempsymbol->m_type == CInt)
{
$$.type = temp->m_tempsymbol->m_type;
$$.value = temp->m_tempsymbol->m_value;
}
else
{
cout<<"error,h("<<yylexerptr->yylineno<<"):"<<$1<<" 不是常量"<<endl;
}
}
}
| NUMBER
{
$$.value = $1;
$$.type = CInt;
}
| TRUE
{
$$.value = 1;
$$.type = CBool;
}
| FALSE
{
$$.value = 0;
$$.type = CBool;
}
;
%%
/////////////////////////////////////////////////////////////////////////////
// programs section
int main(void)
{
/*
int n = 1;
mylexer lexer;
myparser parser;
if (parser.yycreate(&lexer)) {
if (lexer.yycreate(&parser)) {
n = parser.yyparse();
}
}
return n;
*/
cout<<" Please input a file name:"<<endl;
int n = 1;
char filename[20];
bool check=false;
cin>>filename;
mylexer lexer;
myparser parser;
if (parser.yycreate(&lexer)) {
if (lexer.yycreate(&parser)) {
lexer.yyin = new std::ifstream(filename);
if(!lexer.yyin->fail()){
n = parser.yyparse();
check=true;
}
else
{
cout<<" FAIL TO OPEN THE FILE"<<endl;
check=false;
}
delete lexer.yyin;
lexer.yyin = &std::cin;
}
}
if(check && errorcount==0)
{
ofstream * file=new ofstream("out.txt");
for(int i=0;i<=codeindex;i++)
{
cout<<EndCode[i]<<'\t';
(*file)<<EndCode[i]<<endl;
}
file->close();
cout<<endl;
cout<<"Do you want to run the result?('q' is to quit)"<<endl;
int c=getche();
if(c!='q')
{
cout<<endl<<"begin:"<<endl;
Interpret_Code("out.txt");
}
else
{
}
}
getche();
return n;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -