/*** PL0 COMPILER WITH CODE GENERATION ***/
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
const AL = 10; /* LENGTH OF IDENTIFIERS */
const NORW = 18; /* # OF RESERVED WORDS 14*/
const TXMAX = 100; /* LENGTH OF IDENTIFIER TABLE */
const NMAX = 14; /* MAX NUMBER OF DEGITS IN NUMBERS */
const AMAX =2047; /* MAXIMUM ADDRESS */
const LEVMAX= 3; /* MAX DEPTH OF BLOCK NESTING */
const CXMAX = 200; /* SIZE OF CODE ARRAY */
typedef enum { NUL, IDENT, NUMBER, PLUS, MINUS, TIMES,
SLASH, ODDSYM, EQL, NEQ, LSS, LEQ, GTR, GEQ,
LPAREN, RPAREN, COMMA, SEMICOLON, PERIOD,
BECOMES, BEGINSYM, ENDSYM, IFSYM, THENSYM,
WHILESYM, WRITESYM, READSYM, DOSYM, CALLSYM,
CONSTSYM, VARSYM, PROCSYM, PROGSYM,
//ADD RESERVED WORDS +++++++++++++++++++++++++++++++++++++++
ELSESYM,REPEATSYM,UNTILSYM,RETURNSYM,
PLUSPLUS,MINUSMINUS,PLUSEQL,MINUSEQL
//END ADD --------------------------------------------------
} SYMBOL;
char *SYMOUT[] = {"NUL", "IDENT", "NUMBER", "PLUS", "MINUS", "TIMES",
"SLASH", "ODDSYM", "EQL", "NEQ", "LSS", "LEQ", "GTR", "GEQ",
"LPAREN", "RPAREN", "COMMA", "SEMICOLON", "PERIOD",
"BECOMES", "BEGINSYM", "ENDSYM", "IFSYM", "THENSYM",
"WHILESYM", "WRITESYM", "READSYM", "DOSYM", "CALLSYM",
"CONSTSYM", "VARSYM", "PROCSYM", "PROGSYM",
//ADD RESERVED WORDS +++++++++++++++++++++++++++++++++++++++++++++++
"ELSESYM","REPEATSYM","UNTILSYM","RETURNSYM",
"PLUSPLUS","MINUSMINUS","PLUSEQL","MINUSEQL"
//END ADD ----------------------------------------------------------
};
typedef int *SYMSET; // SET OF SYMBOL;
typedef char ALFA[11];
typedef enum { CONSTANT, VARIABLE, PROCEDUR } OBJECTS ;
typedef enum { LIT, OPR, LOD, STO, CAL, INI, JMP, JPC } FCT;
typedef struct {
FCT F; /*FUNCTION CODE*/
int L; /*0..LEVMAX LEVEL*/
int A; /*0..AMAX DISPLACEMENT ADDR*/
} INSTRUCTION;
/* LIT O A -- LOAD CONSTANT A */
/* OPR 0 A -- EXECUTE OPR A */
/* LOD L A -- LOAD VARIABLE L,A */
/* STO L A -- STORE VARIABLE L,A */
/* CAL L A -- CALL PROCEDURE A AT LEVEL L */
/* INI 0 A -- INCREMET T-REGISTER BY A */
/* JMP 0 A -- JUMP TO A */
/* JPC 0 A -- JUMP CONDITIONAL TO A */
char CH; /*LAST CHAR READ*/
SYMBOL SYM; /*LAST SYMBOL READ*/
ALFA ID; /*LAST IDENTIFIER READ*/
int NUM; /*LAST NUMBER READ*/
int CC; /*CHARACTER COUNT*/
int LL; /*LINE LENGTH*/
int CX; /*CODE ALLOCATION INDEX*/
char LINE[81];
INSTRUCTION CODE[CXMAX];
ALFA KWORD[NORW+1];
SYMBOL WSYM[NORW+1];
SYMBOL SSYM['^'+1];
ALFA MNEMONIC[9];
SYMSET DECLBEGSYS, STATBEGSYS, FACBEGSYS;
struct {
ALFA NAME;
OBJECTS KIND;
union {
int VAL; /*CONSTANT*/
struct { int LEVEL,ADR,SIZE; } vp; /*VARIABLE,PROCEDUR:*/
};
} TABLE[TXMAX];
FILE *FIN,*FOUT;
int ERR;
void EXPRESSION(SYMSET FSYS, int LEV, int &TX);
void TERM(SYMSET FSYS, int LEV, int &TX);
//---------------------------------------------------------------------------
int SymIn(SYMBOL SYM, SYMSET S1) {
return S1[SYM];
}
//---------------------------------------------------------------------------
SYMSET SymSetUnion(SYMSET S1, SYMSET S2) {
SYMSET S=(SYMSET)malloc(sizeof(int)*33);
for (int i=0; i<33; i++)
if (S1[i] || S2[i]) S[i]=1;
else S[i]=0;
return S;
}
//---------------------------------------------------------------------------
SYMSET SymSetAdd(SYMBOL SY, SYMSET S) {
SYMSET S1;
S1=(SYMSET)malloc(sizeof(int)*33);
for (int i=0; i<33; i++) S1[i]=S[i];
S1[SY]=1;
return S1;
}
//---------------------------------------------------------------------------
SYMSET SymSetNew(SYMBOL a) {
SYMSET S; int i,k;
S=(SYMSET)malloc(sizeof(int)*33);
for (i=0; i<33; i++) S[i]=0;
S[a]=1;
return S;
}
//---------------------------------------------------------------------------
SYMSET SymSetNew(SYMBOL a, SYMBOL b) {
SYMSET S; int i,k;
S=(SYMSET)malloc(sizeof(int)*33);
for (i=0; i<33; i++) S[i]=0;
S[a]=1; S[b]=1;
return S;
}
//---------------------------------------------------------------------------
SYMSET SymSetNew(SYMBOL a, SYMBOL b, SYMBOL c) {
SYMSET S; int i,k;
S=(SYMSET)malloc(sizeof(int)*33);
for (i=0; i<33; i++) S[i]=0;
S[a]=1; S[b]=1; S[c]=1;
return S;
}
//---------------------------------------------------------------------------
SYMSET SymSetNew(SYMBOL a, SYMBOL b, SYMBOL c, SYMBOL d) {
SYMSET S; int i,k;
S=(SYMSET)malloc(sizeof(int)*33);
for (i=0; i<33; i++) S[i]=0;
S[a]=1; S[b]=1; S[c]=1; S[d]=1;
return S;
}
//---------------------------------------------------------------------------
SYMSET SymSetNew(SYMBOL a, SYMBOL b, SYMBOL c, SYMBOL d,SYMBOL e) {
SYMSET S; int i,k;
S=(SYMSET)malloc(sizeof(int)*33);
for (i=0; i<33; i++) S[i]=0;
S[a]=1; S[b]=1; S[c]=1; S[d]=1; S[e]=1;
return S;
}
//---------------------------------------------------------------------------
SYMSET SymSetNew(SYMBOL a, SYMBOL b, SYMBOL c, SYMBOL d,SYMBOL e, SYMBOL f) {
SYMSET S; int i,k;
S=(SYMSET)malloc(sizeof(int)*33);
for (i=0; i<33; i++) S[i]=0;
S[a]=1; S[b]=1; S[c]=1; S[d]=1; S[e]=1; S[f]=1;
return S;
}
//---------------------------------------------------------------------------
SYMSET SymSetNULL() {
SYMSET S; int i,n,k;
S=(SYMSET)malloc(sizeof(int)*33);
for (i=0; i<33; i++) S[i]=0;
return S;
}
//---------------------------------------------------------------------------
void Error(int n) {
String s = "***"+AnsiString::StringOfChar(' ', CC-1)+"^";
Form1->printls(s.c_str(),n); fprintf(FOUT,"%s%d\n", s.c_str(), n);
ERR++;
} /*Error*/
//---------------------------------------------------------------------------
void GetCh() {
if (CC==LL) {
if (feof(FIN)) {
Form1->printfs("PROGRAM INCOMPLETE");
fprintf(FOUT,"PROGRAM INCOMPLETE\n");
fclose(FOUT);
exit(0);
}
LL=0; CC=0;
CH=' ';
while (!feof(FIN) && CH!=10)
{ CH=fgetc(FIN); LINE[LL++]=CH; }
LINE[LL-1]=' '; LINE[LL]=0;
String s=IntToStr(CX);
while(s.Length()<3) s=" "+s;
s=s+" "+LINE;
Form1->printfs(s.c_str());
fprintf(FOUT,"%s\n",s);
}
CH=LINE[CC++];
} /*GetCh()*/
//---------------------------------------------------------------------------
void GetSym() {
int i,J,K; ALFA A;
while (CH<=' ') GetCh();
if (CH>='A' && CH<='Z') { /*ID OR RESERVED WORD*/
K=0;
do {
if (K<AL) A[K++]=CH;
GetCh();
}while((CH>='A' && CH<='Z')||(CH>='0' && CH<='9'));
A[K]='\0';
strcpy(ID,A); i=1; J=NORW;
do {
K=(i+J) / 2;
if (strcmp(ID,KWORD[K])<=0) J=K-1;
if (strcmp(ID,KWORD[K])>=0) i=K+1;
}while(i<=J);
if (i-1 > J) SYM=WSYM[K];
else SYM=IDENT;
}
else
if (CH>='0' && CH<='9') { /*NUMBER*/
K=0; NUM=0; SYM=NUMBER;
do {
NUM=10*NUM+(CH-'0');
K++; GetCh();
}while(CH>='0' && CH<='9');
if (K>NMAX) Error(30);
}
else
if (CH==':') {
GetCh();
if (CH=='=') { SYM=BECOMES; GetCh(); }
else SYM=NUL;
}
else /* THE FOLLOWING TWO CHECK WERE ADDED
BECAUSE ASCII DOES NOT HAVE A SINGLE CHARACTER FOR <= OR >= */
if (CH=='<') {
GetCh();
if (CH=='=') { SYM=LEQ; GetCh();}
//ADD <>++++++++++++++++++++++++++++++++++++++++++++++++++++++
else if(CH=='>'){SYM=NEQ; GetCh();}
//END A