?? cm_scan.l
字號(hào):
%{
#include "globals.h"
#include "util.h"
#include "scan.h"
#include "symtab.h"
char tokenString[MAXTOKENLEN+1];
/* set NO_PARSE to TRUE to get a scanner-only compiler */
#define NO_PARSE FALSE
/* set NO_ANALYZE to TRUE to get a parser-only compiler */
#define NO_ANALYZE FALSE
/* set NO_CODE to TRUE to get a compiler that does not
* generate code
*/
#define NO_CODE FALSE
#include "util.h"
#if NO_PARSE
#include "scan.h"
#else
#include "parse.h"
#if !NO_ANALYZE
#include "analyze.h"
#if !NO_CODE
#include "cgen.h"
#endif
#endif
#endif
/* allocate global variables */
int lineno = 1;
FILE *source;
FILE *listing;
FILE *code;
/* allocate and set tracing flags */
int TraceScan = FALSE;
int TraceParse = FALSE;
int TraceAnalyze = FALSE;
int TraceCode = FALSE;
int Error = FALSE;
%}
id [a-zA-Z_]+[a-zA-Z0-9_]*
num (-?[1-9]+[0-9]*)|(-?[1-9])|0
floatnum (-?[1-9]+[0-9]*\.[0-9]+)|(-?0\.[0-9]+)
constchar '.'
newline \n
whitespace [ \t]+
%%
"int" {return INT;}
"char" {return CHAR;}
"float" {return FLOAT;}
"void" {return VOID;}
"if" {return IF;}
"else" {return ELSE;}
"while" {return WHILE;}
"for" {return FOR;}
"return" {return RETURN;}
"==" {return EQ;}
"<=" {return LE;}
">=" {return GE;}
"!=" {return NE;}
"=" {return ASSIGN;}
"<" {return LT;}
">" {return GT;}
"+" {return PLUS;}
"-" {return MINUS;}
"*" {return TIMES;}
"/" {return OVER;}
";" {return SEMICOLON;}
"," {return COMMA;}
"(" {return LPAREN;}
")" {return RPAREN;}
"[" {return LBRACKET;}
"]" {return RBRACKET;}
"{" {return LBRACE;}
"}" {return RBRACE;}
"||" {return OR;}
"&&" {return AND;}
"!" {return NOT;}
"/*" {
char c;
LABEL:
do
{
c = input();
if( c == '\n')
lineno++;
}while(c != '*');
do
{
c = input();
if(c == '/')
break;
if( c == '\n')
lineno++;
goto LABEL;
}while(c == '*');
}
{id} {strncpy(tokenString,yytext,MAXTOKENLEN); return ID;}
{num} {strncpy(tokenString,yytext,MAXTOKENLEN); return NUM;}
{floatnum} {strncpy(tokenString,yytext,MAXTOKENLEN); return FLOATNUM;}
{constchar} {strncpy(tokenString,yytext,MAXTOKENLEN); return CONSTCHAR;}
{newline} {lineno++;}
{whitespace} {/* skip whitespace */}
. {return ERROR;}
%%
int main( int argc, char * argv[] )
{
TreeNode * syntaxTree;
char pgm[120]; /* source code file name */
if (argc != 2 && argc != 3)
{
fprintf(stderr, "usage: CMinusC <filename>\n");
exit(1);
}
if(argc == 3)
{
if(strpbrk(argv[2], "S") != 0)
TraceScan = TRUE;
if(strpbrk(argv[2], "P") != 0)
TraceParse = TRUE;
if(strpbrk(argv[2], "A") != 0)
TraceAnalyze = TRUE;
if(strpbrk(argv[2], "C") != 0)
TraceCode = TRUE;
}
strcpy(pgm, argv[1]) ;
if (strchr(pgm, '.') == NULL)
strcat(pgm, ".cm");
yyin = fopen(pgm,"r");
if (yyin == NULL)
{
fprintf(stderr,"File %s not found\n",pgm);
exit(1);
}
source = yyin;
listing = stdout; /* send listing to screen */
fprintf(listing,"\nC-Minus COMPILATION: %s\n", pgm);
#if NO_PARSE
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);
st_reset();
if (TraceAnalyze)
{
fprintf(listing,"\nSyntax tree:\n");
printTree(syntaxTree);
}
}
if(! Error)
{
if (TraceAnalyze)
fprintf(listing,"\nChecking Types...\n");
typeCheck(syntaxTree);
st_reset();
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,".p");
code = fopen(codefile,"w");
if (code == NULL)
{
printf("Unable to open %s\n",codefile);
exit(1);
}
codeGen(syntaxTree,codefile);
st_reset();
fclose(code);
}
#endif
#endif
#endif
fclose(source);
return 0;
}
int getToken()
{
static int firstTime = TRUE;
int currentToken;
if (firstTime)
{
firstTime = FALSE;
lineno++;
yyin = source;
yyout = listing;
}
currentToken = yylex();
if(currentToken == 0)
currentToken = ENDFILE;
strncpy(tokenString,yytext,MAXTOKENLEN);
if (TraceScan)
{
fprintf(listing,"\t%d: ",lineno);
printToken(currentToken,tokenString);
}
return currentToken;
}
int yywrap()
{
return 1;
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -