implement lexical analysis function for c-minus

This commit is contained in:
2025-10-02 07:14:04 +09:00
parent 87931e8cc2
commit 9af73c5a15
9 changed files with 600 additions and 354 deletions

View File

@@ -8,14 +8,14 @@
#include "globals.h"
/* set NO_PARSE to TRUE to get a scanner-only compiler */
#define NO_PARSE FALSE
#define NO_PARSE TRUE
/* set NO_ANALYZE to TRUE to get a parser-only compiler */
#define NO_ANALYZE FALSE
#define NO_ANALYZE TRUE
/* set NO_CODE to TRUE to get a compiler that does not
* generate code
*/
#define NO_CODE FALSE
#define NO_CODE TRUE
#include "util.h"
#if NO_PARSE
@@ -32,71 +32,70 @@
/* allocate global variables */
int lineno = 0;
FILE * source;
FILE * listing;
FILE * code;
FILE *source;
FILE *listing;
FILE *code;
/* allocate and set tracing flags */
int EchoSource = FALSE;
int TraceScan = FALSE;
int TraceScan = TRUE;
int TraceParse = FALSE;
int TraceAnalyze = FALSE;
int TraceCode = FALSE;
int Error = FALSE;
main( int argc, char * argv[] )
{ TreeNode * syntaxTree;
char pgm[120]; /* source code file name */
if (argc != 2)
{ fprintf(stderr,"usage: %s <filename>\n",argv[0]);
exit(1);
main(int argc, char *argv[]) {
TreeNode *syntaxTree;
char pgm[120]; /* source code file name */
if (argc != 2) {
fprintf(stderr, "usage: %s <filename>\n", argv[0]);
exit(1);
}
strcpy(pgm,argv[1]) ;
if (strchr (pgm, '.') == NULL)
strcat(pgm,".tny");
source = fopen(pgm,"r");
if (source==NULL)
{ fprintf(stderr,"File %s not found\n",pgm);
exit(1);
}
listing = stdout; /* send listing to screen */
fprintf(listing,"\nTINY COMPILATION: %s\n",pgm);
strcpy(pgm, argv[1]);
if (strchr(pgm, '.') == NULL)
strcat(pgm, ".tny");
source = fopen(pgm, "r");
if (source == NULL) {
fprintf(stderr, "File %s not found\n", pgm);
exit(1);
}
listing = stdout; /* send listing to screen */
fprintf(listing, "\nC-MINUS COMPILATION: %s\n", pgm);
#if NO_PARSE
while (getToken()!=ENDFILE);
while (getToken() != ENDFILE);
#else
syntaxTree = parse();
if (TraceParse) {
fprintf(listing,"\nSyntax tree:\n");
printTree(syntaxTree);
}
#if !NO_ANALYZE
if (! Error)
{ if (TraceAnalyze) fprintf(listing,"\nBuilding Symbol Table...\n");
buildSymtab(syntaxTree);
if (TraceAnalyze) fprintf(listing,"\nChecking Types...\n");
typeCheck(syntaxTree);
if (TraceAnalyze) fprintf(listing,"\nType Checking Finished\n");
}
#if !NO_CODE
if (! Error)
{ char * codefile;
int fnlen = strcspn(pgm,".");
codefile = (char *) calloc(fnlen+4, sizeof(char));
strncpy(codefile,pgm,fnlen);
strcat(codefile,".tm");
code = fopen(codefile,"w");
if (code == NULL)
{ printf("Unable to open %s\n",codefile);
exit(1);
syntaxTree = parse();
if (TraceParse) {
fprintf(listing, "\nSyntax tree:\n");
printTree(syntaxTree);
}
#if !NO_ANALYZE
if (!Error) {
if (TraceAnalyze) fprintf(listing, "\nBuilding Symbol Table...\n");
buildSymtab(syntaxTree);
if (TraceAnalyze) fprintf(listing, "\nChecking Types...\n");
typeCheck(syntaxTree);
if (TraceAnalyze) fprintf(listing, "\nType Checking Finished\n");
}
#if !NO_CODE
if (!Error) {
char *codefile;
int fnlen = strcspn(pgm, ".");
codefile = (char *) calloc(fnlen + 4, sizeof(char));
strncpy(codefile, pgm, fnlen);
strcat(codefile, ".tm");
code = fopen(codefile, "w");
if (code == NULL) {
printf("Unable to open %s\n", codefile);
exit(1);
}
codeGen(syntaxTree, codefile);
fclose(code);
}
codeGen(syntaxTree,codefile);
fclose(code);
}
#endif
#endif
#endif
fclose(source);
return 0;
fclose(source);
return 0;
}