Code Monkey home page Code Monkey logo

compiler's Introduction

Grammar

painter โ†’ draw shape
shape โ†’ square | triangle | line

Scanner

#include <bits/stdc++.h>
using namespace std;

//Tokens
enum  tokens {
    draw,
    triangle,
    square,
    line,
    space,
    Error,
    End
};

class Scanner {

    ifstream input;
    ofstream output;
    char ch;

public:

    Scanner(string in , string ot) {
        input.open(in);
        output.open(ot);
    }

    tokens getToken() {

        string token ;
        if (input.get(ch)) {

            if (isspace(ch))
                return space;

            while (isalpha(ch)) {
                token += ch;
                if(!input.get(ch))
                    break;
            }
            input.putback(ch);

            if ( token == "draw" )
                return draw;
            else if ( token == "square" )
                return square;
            else if ( token == "line" )
                return line;
            else if ( token == "triangle" )
                return triangle;
            else
                return Error;

        } else
            return End;
    }


    void setToken() {

        tokens token = getToken();

        while ( token != End ) {
            if ( token ==  draw )
                output << "<draw>\n";
            else if ( token == square )
                output << "<square>\n";
            else if ( token == triangle )
                output << "<triangle>\n";
            else if ( token == line )
                output << "<line>\n";
            else if ( token == space )
                output << "<space>\n";
            else if ( token == Error )
                output << "<Error>\n";

            token = getToken();
        }
        output << "<End>";
    }
};

int main() {

    Scanner sc("input.txt" , "tokens.txt");
    sc.setToken();

    return 0;
}

Test the scanner

let's try to write a valid input into the file
draw square

Alt text

Run it

Alt text

the output

Alt text

let's try a invalid input

draw compiler

Alt text Alt text

Parser

#include <bits/stdc++.h>
using namespace std;

//Tokens
enum  tokens {
    draw,
    triangle,
    square,
    line,
    space,
    Error,
    End
};

class Parser {

    ifstream fileOfTokens;
    string figure;
    tokens next;

public:

    Parser(string Tokens) {
        fileOfTokens.open(Tokens);
    }

    bool isMatch(tokens token) {

        if (token == next) {
            next = getToken();
            return true;
        }
            return false;
    }

    bool shape() {
        return  isMatch(triangle) || isMatch(line) || isMatch(square);
    }

    bool painter() {

        if(!isMatch(draw)) {
            cout << "Expected 'draw'";
            return false;
        }
        else if(!isMatch(space))
            return false;
        if(!shape()) {
            cout << "Expected  shape to drawing";
            return false;
        }

        return isMatch(End) ;
    }

    tokens getToken() {

        string token;
        getline(fileOfTokens , token);

        if ( token == "<draw>" )
            return draw;
        else if ( token == "<square>" ) {
            figure = "square";
            return square;
        } else if ( token == "<line>" ) {
            figure = "line";
            return line;
        } else if ( token == "<triangle>" ) {
            figure = "triangle";
            return triangle;
        } else if ( token == "<space>" )
            return space;
        else if( token == "<Error>")
            return Error;
        else
            return End;
    }

    bool isValid() {
        next = getToken();
        return painter();
    }

    void drawing() {

        if(figure == "line")
            cout << "___________________";
        else if (figure == "square") {

            cout << " ________ \n";
            cout << "|        |\n";
            cout << "|        |\n";
            cout << "|        |\n";
            cout << "|________|\n";

        } else if (figure == "triangle") {

            cout << "*\n";
            cout << "***\n";
            cout << "*****\n";
            cout << "*******\n";
            cout << "*********\n";
        }
    }
};

int main() {

    Parser sc("tokens.txt");
    if (sc.isValid())
        sc.drawing();

    return 0;
}

Test the parser

valid input draw square

Alt text

invalid input draw compiler

Alt text

compiler's People

Contributors

sssiiisssiii avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.