Created
February 27, 2014 07:50
-
-
Save zbjxb/9246054 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <string> | |
using namespace std; | |
/* * | |
* <program> ::= <program-header> <block> '.' | |
* <program-header> ::= PROGRAM <ident> | |
* <block> ::= <declarations> <statements> | |
* */ | |
char Look; | |
void Expected(const string& str) { | |
cout << "Expected " << str << endl; | |
exit(-1); | |
} | |
void GetChar() { | |
Look = getchar(); | |
} | |
// Match a specific character | |
void Match(char ch) { | |
if (ch == Look) { | |
GetChar(); | |
} | |
else { | |
Expected((string)("") + ch); | |
} | |
} | |
char GetName() { | |
if (!isalpha(Look)) { | |
Expected("Name"); | |
} | |
char ch = toupper(Look); | |
GetChar(); | |
return ch; | |
} | |
// Post a Label to Output | |
void PostLabel(const string& label) { | |
cout << label << ":" << endl; | |
} | |
// Write the Prolog | |
void Prolog() { | |
cout << "WARMST EQU $A01E" << endl; | |
} | |
// Write the Epilog | |
void Epilog(char Name) { | |
cout << "DC WARMST" << endl; | |
cout << "END " << Name << endl; | |
} | |
/* * | |
* <declarations> ::= ( <label list> | | |
* <constant list> | | |
* <type list> | | |
* <variable list> | | |
* <procedure> | | |
* <function> )* | |
* */ | |
/* Process Label Statement */ | |
void Labels() { | |
Match('l'); | |
} | |
/* Process Const Statement */ | |
void Constants() { | |
Match('c'); | |
} | |
/* Process Type Statement */ | |
void Types() { | |
Match('t'); | |
} | |
/* Process Var Statement */ | |
void Variables() { | |
Match('v'); | |
} | |
/* Process Procedure Definition */ | |
void DoProcedure() { | |
Match('p'); | |
} | |
/* Process Function Definition */ | |
void DoFunction() { | |
Match('f'); | |
} | |
// Parse and Translate the Declarations Part | |
void Declarations() { | |
bool check = true; | |
while (check) { | |
switch (Look) { | |
case 'l': Labels(); break; | |
case 'c': Constants(); break; | |
case 't': Types(); break; | |
case 'v': Variables(); break; | |
case 'p': DoProcedure(); break; | |
case 'f': DoFunction(); break; | |
default: check = false; break; | |
} | |
} | |
} | |
/* | |
* <statements> ::= <compound statement> | |
* <compound statement> ::= BEGIN <statement> | |
* (';' <statement>) END | |
* <statement> ::= <simple statement> | <structured statement> | |
* <simple statement> ::= <assignment> | <procedure call> | null | |
* <structured statement> ::= <compound statement> | | |
* <if statement> | | |
* <case statement> | | |
* <while statement> | | |
* <repeat statement> | | |
* <for statement> | | |
* <with statement> | |
* */ | |
/* Parse and Translate the Statement Part */ | |
void Statements() { | |
Match('b'); | |
while (Look != 'e') { | |
GetChar(); | |
} | |
Match('e'); | |
} | |
// Parse and Translate a Pascal block | |
void DoBlock(char Name) { | |
Declarations(); | |
PostLabel((string)"" + Name); | |
Statements(); | |
} | |
// Parse and Translate A Program | |
void Prog() { | |
char Name; | |
Match('p'); // Handles program header part | |
Name = GetName(); | |
Prolog(); | |
DoBlock(Name); | |
Match('.'); | |
Epilog(Name); | |
} | |
void Init() { | |
GetChar(); | |
} | |
int main() { | |
Init(); | |
Prog(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment