Compilation Principle Dragon book chapter II A simple Arithmetic (+,-) translator implementation

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.