Last night, I decided to work on the "compiling principle" of the frontal hard God lesson. the Dragon book.
Here is a simple arithmetic-type infix suffix of the translator.
This is also the Dragon book in a c implementation source code. Partially rewritten in C + +.
#include <iostream> #include <ctype.h> #include <stdlib.h> #include <stdio.h>using namespace Std;int lookahead;void Error ()//fault handling { cout<< "error" <<endl; Exit (1);} Token is used to match the pre-scan notation lookaheadvoid match (int t)//Detect iteration function { if (lookahead = = t) { lookahead = GetChar (); } else error ();} void term ()//Judgment function isdigit (char c); Determines whether the input character is 0~9. is included in the header file <ctype.h>. { if (isdigit (lookahead)) { Putchar (lookahead); Match (lookahead); } else error ();} void expr () {term (); while (1) { if (lookahead = = ' + ') { match (' + '), term ();p Utchar (' + '); } else if (lookahead = = '-') { match (' + '); term ();p Utchar ('-'); } else break; }} int main () { lookahead = GetChar (); Expr (); cout<<endl; return 0;}
Compilation principle Dragon book chapter II A simple Arithmetic (+,-) translator implementation