Source Code
Appendix B. Source Code
- Table of Contents
- scanner.l
- parser.y
- main.C
- mainScan.C
- ctrl.h
- ctrl.C
- scanparse.h
- scanparse.C
- symtab.h
- symtab.C
- p_tree.h
- p_tree.C
- emit.C
These are the source code files for the project. Modifications and additions are emphasized.
scanner.l
%{
/*
* scanner.l
*/
#include "scanparse.h"
// If we are just scanning and not parsing, we don't want to refer
// to parser classes.
#if defined SCANONLY
#define TOKEN(ttype, ttext)
#define YYSTYPE int
#else
#define TOKEN(type,txt) lex_tok = new LexTokCls(yylineno, type, txt)
#define YYSTYPE PPTreeNodeCls
#include "p_tree.h"
PLexTokCls lex_tok;
#endif
#include <string.h>
#include <iostream.h>
#include "ctrl.h"
#include "parser.tab.h"
char *textline = new char[257];
int yylineno = 1;
int ck_reserved_wd();
void ckout();
%}
digit [0-9]
digits {digit}+
letter [A-Za-z]
letter_or_digit ({letter}|{digit})
ident {letter}{letter_or_digit}*
whitespace [ \t]
cr [\n]
other .
%x COMMENT
%%
"//" BEGIN(COMMENT);
<COMMENT>[^\n]*\n { textline[0] = '\0'; BEGIN(INITIAL); }
{whitespace} {ckout();}
{cr} {ckout();}
";" {ckout();
TOKEN(SCTK, 0);
return SCTK;}
"(" {ckout();
TOKEN(LPARENTK, 0);
return LPARENTK;}
")" {ckout();
TOKEN(RPARENTK, 0);
return RPARENTK;}
"," {ckout();
TOKEN(COMMATK, 0);
return COMMATK;}
":" {ckout();
TOKEN(COLONTK, 0);
return COLONTK;}
"[" {ckout();
TOKEN(LBRACKTK, 0);
return LBRACKTK;}
"]" {ckout();
TOKEN(RBRACKTK, 0);
return RBRACKTK;}
":=" {ckout();
TOKEN(ASGTK, 0);
return ASGTK;}
".." {ckout();
TOKEN(RANGETK, 0);
return RANGETK;}
"." {ckout();
TOKEN(DOTTK, 0);
return DOTTK;}
"+" {ckout();
TOKEN(PLUSTK, 0);
return PLUSTK;}
"-" {ckout();
TOKEN(MINUSTK, 0);
return MINUSTK;}
"*" {ckout();
TOKEN(STARTK, 0);
return STARTK;}
"/" {ckout();
TOKEN(SLASHTK, 0);
return SLASHTK;}
"<" {ckout();
TOKEN(LTTK, 0);
return LTTK;}
"<=" {ckout();
TOKEN(LETK, 0);
return LETK;}
"=" {ckout();
TOKEN(EQTK, 0);
return EQTK;}
"<>" {ckout();
TOKEN(NETK, 0);
return NETK;}
">=" {ckout();
TOKEN(GETK, 0);
return GETK;}
">" {ckout();
TOKEN(GTTK, 0);
return GTTK;}
{digits} {ckout();
TOKEN(NUMLITERALTK, yytext);
return NUMLITERALTK;}
{ident} {ckout();
int actual_tk = ck_reserved_wd();
TOKEN( actual_tk, yytext);
return actual_tk;}
{other} {ckout();
return yytext[0];}
%%
void ckout() {
textline = strcat(textline,yytext);
if (yytext[0] == '\n') {
#if !defined SCANONLY
if (OptionCls::option_info() % 2) { //List option is a 1
cout << "[";
cout.width(5);
cout << yylineno -1 << "] " << textline ;
}
#endif
textline[0] = '\0';
}
}
struct rwtable_str {
char *rw_name; /* lexeme */
int rw_yylex; /* token */
};
rwtable_str rwtable[] = {
"", IDENTIFIERTK,
"array", ARRAYTK,
"begin", BEGINTK,
"else", ELSETK,
"end", ENDTK,
"exit", EXITTK,
"if", IFTK,
"integer", INTEGERTK,
"loop", LOOPTK,
"of", OFTK,
"program", PROGRAMTK,
"then", THENTK,
"var", VARTK,
"writeln", WRITETK
};
#define LEN(x) (sizeof(x)/sizeof((x)[0]))
#define ENDTABLE(v) (v - 1 + LEN(v))
int ck_reserved_wd() {
rwtable_str *low = rwtable;
rwtable_str *high = ENDTABLE(rwtable);
rwtable_str *mid;
int comp;
char temp[80];
strcpy(temp,yytext);
while (low <= high)
{ mid = low + (high-low)/2;
if ((comp=strcmp(mid->rw_name, temp)) == 0)
return mid->rw_yylex;
else if (comp < 0)
low = mid+1;
else
high = mid-1;
}
return rwtable->rw_yylex; /* ie. token: IDENTIFIER! */
}
#if !defined __alpha
// For some reason, alpha doesn't like us to define yywrap!
int yywrap() {
return 1;
}
#endif
|

