#include "Compiler.h"
#include "Label.h"
#include "Action.h"
#include "Goto.h"
Compiler::Compiler(string CmdLine)
{
isTokenClose = true;
isFtClose = true;
isStkClose = true;
isAsmClose = true;
isLogClose = true;
isClrIn = false;
isLexErrId = false;
lexErrChar = ' ';
nextquad = 0;
line=1;
hasError=0;
needOutSuppose=0;
hasFile=0;
fileName=CmdLine;
//log.open((fileName + "_Log.txt").c_str(),ios::out);
keywordstr[0] = "int";
keywordstr[1] = "char";
keywordstr[2] = "float";
keywordstr[3] = "if";
keywordstr[4] = "else";
keywordstr[5] = "while";
//code = "";
code1 = "";
tNum = 0;
stk = "";
initMaincode();
//char c; //测试nextChar()
//do{c=nextChar();log<<c;}while(c!='#');log<<endl;
}
Compiler::Compiler()
{
isTokenClose = true;
isFtClose = true;
isStkClose = true;
isAsmClose = true;
isLogClose = true;
isClrIn = false;
isLexErrId = false;
lexErrChar = ' ';
nextquad = 0;
line=1;
hasError=0;
needOutSuppose=0;
hasFile=false;
//fileName=CmdLine;
//log.open((fileName + "_Log.txt").c_str(),ios::out);
keywordstr[0] = "int";
keywordstr[1] = "char";
keywordstr[2] = "float";
keywordstr[3] = "if";
keywordstr[4] = "else";
keywordstr[5] = "while";
code = "";
code1 = "";
tNum = 0;
stk = "";
ss.empty();
sos.empty();
initMaincode();
isClose = false;
}
Compiler::~Compiler()
{
//log.close();
}
Token *Compiler::lexer()
{
char c;
int s=1;
//Symbol *r;
Token *r;
TokenGroup tg;
r = new Token();
//r=new Symbol();
isLexErrId = false; //重置无法识别ID
c=currentChar; //之前已经初始化
//log.open((fileName + "_Log.txt").c_str(),ios::out);
while(s){
switch(s){
case 1:
if(c=='\x0A')line++;
else if(isspace(c))
s=1;
else if(isalpha(c)||c=='_'){
s=2;
r->word=c;
}
else if(isdigit(c)){
s=3;
r->word=c;
}
else{
switch(c){
case '+':
case '-':
s=0;
r->word=c;
r->group='+';
r->line=line;
break;
case '*':
case '%':
s=0;
r->word=c;
r->group='*';
r->line=line;
break;
case '&':
case '|':
r->word=c;
s=4;
break;
case '>':
case '<':
r->word=c;
s=5;
break;
case '!':
r->word=c;
s=6;
break;
case '=':
r->word=c;
s=7;
break;
case ',':
case ';':
case '{':
case '}':
case '(':
case ')':
case '$':
s=0;
r->word=c;
r->group=c;
r->line=line;
break;
case '/':
r->word=c;
s=8;
break;
case '#':
s=11;
break;
default:
s=1;
lexErrChar = currentChar;
err(1,line); //无法识别的字符
}
}
c=nextChar();
break;
case 2:
if(isalnum(c)||c=='_'){
s=2;
r->word+=c;
c=nextChar();
}
else{
s=0;
r->line=line;
r->group = 'r';
if(!isLexErrId)
{
tg = IsKeyword(r->word,KEYNUM);
/*if(r->word=="int")
r->group='z';
else if(r->word=="if")
r->group='i';
else if(r->word=="else")
r->group='e';
else if(r->word=="do")
r->group='d';
else if(r->word=="while")
r->group='w';
else if(r->word=="return")
r->group='r';
else
*/
switch(tg)
{
case _INT:
case _CHAR:
case _FLOAT:
r->group = 'z';break;
case _IF:
r->group = 'i';break;
case _ELSE:
r->group = 'e';break;
case _WHILE:
r->group = 'w';break;
default:
r->group='@';break;
}
}
else
{
cmes<<r->word<<endl;
}
}
break;
case 3:
if(isdigit(c)){
s=3;
r->word+=c;
c=nextChar();
}
else if(isalpha(c)||c=='_'){
s=2;
r->word+=c;
err(2,line); //发现不正确的标识符
isLexErrId = true;
c=nextChar();
}
else{
s=0;
r->line=line;
r->group='n';
}
break;
case 4:
if((r->word.c_str())[0]==c){
s=0;
r->word+=c;
r->group='&';
r->line=line;
c=nextChar();
}
else {
s=1;
err(3,line); //理论上,应该在语法分析时发现的错误
}
break;
case 5:
if(c=='='){
r->word+=c;
c=nextChar();
}
s=0;
r->line=line;
r->group='>';
break;
case 6:
if(c=='='){
r->word+=c;
r->group='>';
c=nextChar();
}
else{
r->group='!';
}
s=0;
r->line=line;
break;
case 7:
if(c=='='){
r->word+=c;
r->group='>';
c=nextChar();
}
else{
r->group='=';
}
s=0;
r->line=line;
break;
case 8:
if(c=='*'){
s=9;
r->word="";
c=nextChar();
}
else if( c == '/')
{
while(nextChar() != '/n');
s = 0;
break;
}
else
{ ///////////???????????
r->line=line;
r->group='*';
s=0;
}
break;
case 9:
if(c=='*')
s=10;
else
s=9;
c=nextChar();
break;
case 10:
if(c=='/')
s=1;
else if(c=='*')
s=10;
else
s=9;
c=nextChar();
break;
case 11:
if(c=='\n'){
s=1;
line++;
}
else
s=11;
c=nextChar();
break;
default:
s=1;
err(0,line); //这种情况不会发生
c=nextChar();
}
}
currentChar=c;
log<<"词法分析:"<<r->word<<endl;
//log.close();
return r;
}
Compiler::parser(bool islex)
{
//log.open((fileName + "_Log.txt").c_str(),ios::app);
log<<endl;
log<<"*******************************************"<<endl;
log<<"语法分析开始..."<<endl;
if(! islex)
{
preProcess();
if(hasFile)
in.open((fileName+".crr").c_str(),ios::in);
currentChar=nextChar();//词法分析器初始化
}
// Symbol *s=NULL; //测试lexer()
// do{
// if(s!=NULL)
// delete s;
// s=lexer();
// log<<s->word;
// log<<" ";
// log<<s->group;
// log<<'\t';
// log<<s->line;
// log<<endl;
// }while(s->group!='#');
// delete s;
int i = 0;
int r,s=1,t=1;
Token *ip ,*it=NULL;
Symbol *s1,*s2,*s3,*s4,*s5,*s6,*s7,*m,*sip,*iq; //声明!1!!
//Label *l1,*l2;
TokenGroup tg;
int quad,quad1;
CTemp* ct;
ip = new Token;
Id *id;
ss.push(s);
saveStack("就绪");
if(!islex)
{
ip=lexer();
}
else
{
ip->word = cct[i].word;
cout<<ip->word<<endl;
ip->group = cct[i].group;
ip->line = cct[i].line;
i++;
}
while(t){
s=ss.top();
t=Action::lookUp(ip->group,s);
if(t>0){ //移近
if(ip->group == 'w'||ip->group == '&') //移近时,的保存状态。
{
bk.push(nextquad);
log<< "入栈" << intTostr(nextquad) << endl;
}
if(ip->group == 'e')
{
nlist = nextquad;
ft[nextquad].op = "j";
ft[nextquad].p1 = "-";
ft[nextquad].p2 = "-";
ft[nextquad].res = "-1";
ft[nextquad].next = -1;
nextquad ++;
bk.push(nextquad);
log<< "入栈" << intTostr(nextquad) << endl;
}
sip = new Symbol;
sip->line = ip->line;
sip->group = ip->group;
sip->word = ip->word;
if(ip->group == 'z'||ip->group == 'n')
sip->type = _INT;
sos.push(sip);
ss.push(t);
saveStack("移进");
if(it==NULL) //???????
if(!islex)
{
ip=lexer();
}
else
{
ip->word = cct[i].word;
ip->group = cct[i].group;
ip->line = cct[i].line;
i++;
}
else
ip=it;
it=NULL;
}
else if(t<0)
{
switch(-t)
{
case 1://OK
//D->@()S
log<<"语法分析:D->@()S"<<endl;
ss.pop();
ss.pop();
ss.pop();
ss.pop();
s4=sos.top();
sos.pop();
s3=sos.top();
sos.pop();
s2=sos.top();
sos.pop();