#include
#include
#include
#include
FILE *fp,*fout;
char lexemes[STRMAX];
char Terror[20][50];
char line[20],err[50],str[30];
int lastchar = -1; /* last used position in lexemes */
int lastentry = 0; /* last used position in symtable*/
int inc=0,flag=0,gb,gt;
int lookahead,q=2,chkParen=0,chkBegin=0,Int=0;
char lexbuf[BSIZE];
int lineno = 1,check=0;
int tokenval = NONE;
void match(int);
void expr();
void term();
void factor();
void stmt();
void error(char[]);
void emit(int,int);
struct entry /* form of symbol table entry */
{
char *lexptr;
int token;
};
struct entry symtable[SYMMAX];
char *allToken[1000];
//struct entry keywords[]={0,0};
struct entry keywords[]={"div",DIV,
"mod",MOD,
"if",IF,
"then",THEN,
"else",ELSE,
"do",DO,
"while",WHILE,
"repeat",REPEAT,
"until",UNTIL,
"begin",BEGIN,
"end",END,
"int",INT,
"char",CHAR,
0,0
};
void init()
{
struct entry *p;
for(p=keywords; p->token; p++)
insert(p->lexptr,p->token);
}
void expr()
{
lookahead = lexan();
while(lookahead != DONE)
switch(lookahead)
{
case ';' : if(chkParen>0)error(" ) required");
else if(chkParen<0)error(" chkparen="0;" q="="0)" lookahead="="INT||lookahead="="CHAR||lookahead="="NUM||lookahead="="ID)">'&&lookahead!='='&&lookahead!=NUM&& lookahead!=ID)
error(" <>' :
match('>');
if(lookahead!='='&&lookahead!=NUM&& lookahead!=ID)
error(" > error");
break;
case NUM : match(NUM);
if(lookahead=='(' )
error(" NUM error");
break;
case ID : match(ID);
if(lookahead=='('&& lookahead!=INT&& lookahead!=CHAR)
error("ID error ");
break;
case IF : match(IF);match('(');expr();match(')');
if(lookahead!=THEN)error("Required then");
break;
case THEN : match(THEN);
if(lookahead!=IF&&lookahead!=ID&&lookahead!=ELSE)
error("if or else or identifier required");
break;
case ELSE : match(ELSE);
if(lookahead!=IF&&lookahead!=ID)
error("If or id required");
break;
case DO : match(DO);
break;
case WHILE : match(WHILE);
break;
case REPEAT : match(REPEAT);
break;
case UNTIL : match(UNTIL);
break;
case BEGIN : chkBegin++;
match(BEGIN);
break;
case END : chkBegin--;
match(END);
break;
case INT : Int=1;
if(q==0)
{
insert(str,ID);
check=0;q=1;
}
else error("Duplicate declaration");
match(INT);
match(';');
Int=0;
break;
case CHAR : if(q==0)
{
insert(str,ID);
check=0;q=1;
}
else error("Duplicate declaration");
match(CHAR);
match(';');
break;
default :
error("Unexpected");
getch();exit(0);
}
}
int lexan() /* lexixcal analyzer */
{
int t;
while(1)
{
t = getc(fp);
if(t == ' '|| t == '\t') ; /* strip out white space */
else if(t == '\n')
{
lineno = lineno + 1;
}
else if( isdigit(t))
{
ungetc(t,fp);
scanf("%d",&tokenval);
return NUM;
}
else if( isalpha(t)) /* t is a letter */
{
int p,b=0;
while(isalnum(t)) /* t is a alphanumeric */
{
lexbuf[b]=t;
t=getc(fp);
b= b + 1;
if(b >= BSIZE) error("compiler error");
}
lexbuf[b] = EOS;
if(t != EOF)
ungetc(t,fp);
p = lookup(lexbuf);
if(p == 0)
{
q=p;
strcpy(str,lexbuf);
return ID;
}
else check=1;
tokenval = p;
return symtable[p].token;
}
else if(t ==EOF) return DONE;
else
{
tokenval = NONE;
return t;
}
}
}
void main()
{
clrscr();
init();
fp=fopen("in.cpp","r");
fout=fopen("out.cpp","w");
expr();
if(chkBegin>0){error(" end required");getch();}
else if(chkBegin<0){error(" int="="1)" lookahead =" lexan();" p =" lastentry;">0 ;p = p-1)
if(strcmp(symtable[p].lexptr,s) == 0)
return p;
return 0;
}
int insert(char s[], int tok)
{
int len;
len = strlen(s);
if(lastentry + 1 >=SYMMAX) error("symbol table full");
if(lastchar + len +1 >= STRMAX) error("lexemes array full");
lastentry = lastentry + 1;
symtable[lastentry].token = tok;
symtable[lastentry].lexptr = &lexemes[lastchar + 1];
lastchar = lastchar + len +1;
strcpy(symtable[lastentry].lexptr,s);
return lastentry;
}
void error(char *m)
{
//fprintf(stderr,"line %d: %s\n",lineno,m);
fprintf(fout,"line %d: %s\n",lineno,m);
}
No comments:
Post a Comment