In short:
In the university, there are a lot of dreams, writing a small compiler for teaching. He also discussed this to ask Mr. Xue, one of my favorite young teachers. Later, I have gained another two very powerful classmates, prepared to do this, use the relevant model, redesign the language, and make it.
But now, only one person in the school, they all go to Beijing internship. And I have to be busy, I want to practice the evolution of the evolution, I want to understand the "process" through the kernel code, want to sort out the notes of my own data structure, I want to read mathematics analysis ... this dream, perhaps Always stay in your heart.
Suddenly I saw the original Email, I wrote to Teacher, I was still unlocked, and the post here, I commemorate this period of time!
Don't comment on the past; just remember, new dreams, new journey!
I hope that all college students will cherish the yin, dreams come true.
Percylee
2003-10-15 in Wuhan
-------------------------------------------------- ------------------------------------
Teacher Xue:
First of all, I wish you a happy holiday!
I am a 11100-class student. I have said that I have to write a small compiler. At that time, the language and the lexical analyzer has been written, but it is necessary to be busy with the exam now, but strive to write before the winter vacation.
The language is small, based on algebraic structure
Below is a small example of the language:
Item A, B = {3}, C;
A = {5};
C = b * (a b) a;
OUT << "c =" << C;
C = a a * b;
OUT << "now c =" << C;
IF (c == a * b)
THEN
OUT << "C is the mutlip of a and b";
Else
OUT << "C IsNot Mutlip of A and B";
END;
You look at some of the program, I wrote in c , I feel a lot of difficulties; but I always think, if one day is finished, I will have a good gain - this is what you said in your class; Will write a series of posts on BBS, show all the design procedures and original code to classmates. By the way, I am the Kindman :), discrise the question, I really hope to get your answer - or, I am looking for you, I am looking for Teacher Cai, I am afraid that he is too busy.
// a file
#pragma overce
#include
#include
#include
Using namespace std;
#define size 50000 // Default maximum file character number
#define Wordnum 20000 / / Maximum number of words
#define constnum 100 // maximum constant number
#define IDNUM 200 // Maximum Sign
#define ELENUM 500 // Some of the constant operation unit
#define errorsize 1000 //
Class Sources
{
PUBLIC:
Sources (Void);
~ Sourcees (Void);
Private:
Const char * keywords [7]; // Keywords
Char * consttable [constnum]; // constant (string)
Char * idtable [idnum]; // Custom flag
Char * ElementTable [Elenum]; // Computing Unit
Char idfollow [10]; // Signature FOLLOW set
Int IDnum;
INT ELENUM;
Int constnum;
// ......
PUBLIC:
Bool Iskeyword (Const Char * & Keyword, INT & NUMOFKEY);
Bool isidFollow (Const char ch);
INT INSERTID (Const Char * & STRTOKEN);
INT INSERTELE (Const Char * & Strtoken);
INT INSERTCONST (Const Char * & STRTOKEN);
// ......
}
// Error record
Class Error
{
PUBLIC:
Int row;
Static char * strerror;
}
Class Error
{
Static int erroorn;
// int Waringnum;
Error Error [ERRORSIZE];
PUBLIC:
Error (void);
~ Error (Void);
INT geterrornum () const;
Void setError (const char * & stroRROR); int ROW
Void printerror ();
// ......
}
//lexical analysis
Class Lexer
{
ENUM token_value {
KW, ID, ELEMENT, CONST,
Equal, Input, Output,
Assign = '=', Plus = ' ', MUL = '*', Print = ';',
LP = '(', rp = ')', COMMA = ',', SEMI = '", Rowend =' / n '
}
Char ch; // reading file
Char Memary [Size]; // File Managed a Lettle
CHAR * WORD;
Int realsize; // of memary
Int rowcount; // of row in file
Int WordCount; // of token
INT Charcount; // of Reading Ch in Memary
Int tokenchcount; // of every tokenword
Std :: fstream file;
Char * & strtoken; // Complete Word in your code
Struct token {
Token_Value tokval;
Int Codenum;
Int rownum;
} token [wordnum]; // Result of lexical analysis
PUBLIC:
Lexer (VOID);
~ Lexer (Void);
Void OpenFile (const char filename [30]);
// Read a character to CH
Void Readchar (Void);
Void recheckch ();
// Character placed in words
Void concat ();
//
Bool isllter ();
Bool isdigit ();
Bool Iskeyword (Const Char * StrWord, Int & Numofkey, Source);
//lexical analysis
Void DoanAlysis (Sources Source, Error Error);
// Programming: Check intermediate results
Void test ();
}
// Compiling processing
Class Compiler
{
PUBLIC:
Compiler (Void);
Virtual ~ Compiler (Void);
Private:
//
Error Error;
// The resources used
Sources Source;
// Lord Analyzer
Lexer Lexer;
PUBLIC:
/ / Read in to be compiled
Void ReadFile (Void);
Void test (void);
// .....
}
// Another file to achieve lexical analysis
#include "compiler.h"
#using
Lexer :: Lexer (Void): STRTOKEN (WORD)
{
Rowcount = 0;
Charcount = 0;
Wordcount = 0;
Tokenchcount = 0;
Word = new char [200];
}
Lexer :: ~ Lexer (void)
{
File.Close ();
Delete Word;
}
Void Lexer :: OpenFile (const char filename [30])
{
CHAR nextchCh = '';
INT NUM = 0;
// __Try {
File.open (filename, iOS_BASE :: in | iOS_BASE :: OUT);
/ *}
Catch (System :: NullReferenceException * e)
{
Std :: CERR << "Error: can't open file" << filename
<< ", please make!" << endl;
EXIT (-1);
}
* /
While (! file.eof ()) {
File.get (CH);
Memary [NUM ] = CH;
IF (CH == ') {
File.get (nextch);
While (! file.eof () && nextchch == ')
File.get (nextch);
Memary [NUM ] = nextch;
}
}
Realsize = NUM;
}
Void lexer :: readchar ()
{
CHARCOUNT CH = Memary [Charcount ]; IF (CH == '/ n' && ch! = ') RowCount ; } Void lexer :: recheckch () { While (CH == '') Readchar (); } // Void lexer :: concat () { Word [tokenchcount ] = CH; Word [tokenchcount] = '/ 0'; } Bool lexer :: isletter () { IF (ch> = 'a' && ch <= 'z') Return True; if (CH> = 'a' && ch <= 'z') Return True; Return False; } Bool lexer :: isdigit () { Switch (ch) { Case '0': Case '2': Case '3': Case '4': Case '5': Case '7': Case '8': Case '9': Return True; DEFAULT: Return False; } } Bool Lexer :: Iskeyword (Const Char * StrWord, Int & Numofkey, Source) { IF (Source.Iskeyword (StrWord, Numofkey)) Return True; Return False; } Void Lexer :: Doanalysis (Sources Source, Error Error) { INT CODE = -1, value = -1; While (charcount { Tokenchcount = 0; Readchar (); Recheckch (); IF (isletter ()) { While (isletter () || isdigit ()) { CONCAT (); Readchar (); } Charcount -; IF (Iskeyword (STRTOKEN, CODE, SOURCE)) { Token [WordCount] .codenum = code; Token [WordCount ]. Rownum = rowcount; } Else IF (Source.isidfollow (CH)) { Token [Wordcount] .rownum = rowcount; Token [WordCount] .tokval = ID; Value = Source.InsertId (startkens); Token [WordCount ]. CODENUM = VALUE; } Else { // Error While (ch! = '/ n') Readchar (); Error.Seterror (STRTOKEN, ROWCOUNT-1); } } // if Else { Switch (CH) { Case '{': readchar (); While (isdigit ()) Readchar (); IF (CH == '}') { Token [Wordcount] .rownum = rowcount; Token [WordCount] .tokval = Element; Value = Source.insertele (STRTOKEN); Token [WordCount ]. CODENUM = VALUE; } Else { // Error While (ch! = '/ n') Readchar (); Error.Seterror (STRTOKEN, ROWCOUNT-1); } Break; Case '=': readchar (); IF (ch! = '=') { Token [Wordcount] .rownum = rowcount; Token [WordCount ]. Tokval = assign; Charcount -; } Else IF (CH == '=') { Token [Wordcount] .rownum = rowcount; Token [WordCount ]. Tokval = equal; } Break; Case ' ': Token [Wordcount] .rownum = rowcount; Token [WordCount ]. TOKVAL = Plus; Break; Case '*': Token [Wordcount] .rownum = rowcount; Token [WordCount ]. Tokval = MUL; Break; Case '<': Readchar (); IF (CH == '<') { Token [Wordcount] .rownum = rowcount; Token [WordCount ]. Tokval = Output; } Else { // Error While (ch! = '/ n') Readchar (); Error.Seterror (STRTOKEN, ROWCOUNT-1); } Break; Case '>': Readchar (); IF (CH == '>') { Token [Wordcount] .rownum = rowcount; Token [WordCount ]. TOKVAL = INPUT; } Else { // Error While (ch! = '/ n') Readchar (); Error.Seterror (STRTOKEN, ROWCOUNT-1); } Break; Case '"': readchar (); While (ch! = '/ n' && ch! = '") Readchar (); IF (CH == '") { Token [Wordcount] .rownum = rowcount; Token [WordCount] .tokval = const; Value = source.insertconst (startken); Token [WordCount ]. CODENUM = VALUE; } Else { // Error While (ch! = '/ n') Readchar (); Error.Seterror (STRTOKEN, ROWCOUNT-1); } Break; Case ',': Token [Wordcount] .rownum = rowcount; Token [WordCount ]. Tokval = Comma; Break; Case ';'; Token [Wordcount] .rownum = rowcount; Token [WordCount ]. Tokval = print; Break; Case '(') Token [Wordcount] .rownum = rowcount; Token [WordCount ]. Tokval = lp; Break; Case ')': Token [Wordcount] .rownum = rowcount; Token [WordCount ]. Tokval = rp; Case '': // readchar (); Break; Case '/ n': Token [Wordcount] .rownum = rowcount; Token [WordCount ]. Tokval = rownd; Break; DEFAULT: { // Error While (ch! = '/ n') Readchar (); Error.Seterror (STRTOKEN, ROWCOUNT-1); } } } // else } // while } / / -------------------------------------------------------------------------------------------- ---------------------------- --------- Void Lexer :: test () { Cout << "Wordcount:" << WordCount << endl; For (INT i = 0; i COUT << Endl << "Row:" << token [i] .rownum << Endl; Switch (token [i] .tokval) { Case kw: cout << "keyword"; Break; Case ID: COUT << "Identifer"; Break; Case Element: cout << "Element"; Break; Case const: cout << "const string"; Break; Case Plus: COUT << ' '; Break; Case Mul: COUT << '*'; Break; Case assign: cout << '='; Break; Case Equal: cout << "=="; Break; Case Print: cout << ';'; Break; Case LP: COUT << '('; Break; Case rp: cout << ')'; Break; Case Comma: COUT << ','; Break; Case INPUT: COUT << ">>"; Break; Case Output: COUT << "<<"; Break; Case Semi: COUT << '"; Break; Case Rowend: COUT << "row_end"; Break; DEFAULT: ; } } } I wish you a smooth job. PERCY