GithubHelp home page GithubHelp logo

luqian2017 / software-tools-in-c Goto Github PK

View Code? Open in Web Editor NEW

This project forked from chenshuo/software-tools-in-c

0.0 0.0 0.0 233 KB

C/C++ source code for Software Tools in Pascal by Kernighan and Plauger

License: BSD 3-Clause "New" or "Revised" License

HTML 17.40% OpenEdge ABL 54.24% M 13.66% MATLAB 0.61% Python 0.66% Makefile 0.61% ANTLR 1.57% C++ 5.39% C 0.93% Java 4.93%

software-tools-in-c's Introduction

Software Tools in C/C++

C/C++ source code for Software Tools in Pascal by Kernighan and Plauger, 1981.

This repository consists of two parts:

  1. A customized Pascal-to-C converter written in Java using ANTLR4, located in p2c/
  2. Rewrite of the tools in C, in cpp/

The converted code is mostly C, but use a tiny bit of C++11 syntax like passing-by-reference for Pascal's Variable_parameter, and using for type aliases.

To run the first part,

make convert

and check Pascal and C++ code in orig/

To run the second part,

make tests

which builds C++ code in cpp/ to binaries in bin/ and run some tests written in Python.

Example:

Original Pascal code of orig/intro/wordcount.p:

{ Copyright (C) 1981 by Bell Laboratories, Inc., and Whitesmiths Ltd. }
{ wordcount -- count words in standard input }
procedure wordcount;
var
    nw : integer;
    c : character;
    inword : boolean;
begin
    nw := 0;
    inword := false;
    while (getc(c) <> ENDFILE) do
        if (c = BLANK) or (c = NEWLINE) or (c = TAB) then
            inword := false
        else if (not inword) then begin
            inword := true;
            nw := nw + 1
        end;
    putdec(nw, 1);
    putc(NEWLINE)
end;

Converted literally to C++ in orig/intro/wordcount.cc:

// Copyright (C) 1981 by Bell Laboratories, Inc., and Whitesmiths Ltd.
#include "../p2c.h"
#include "intro.h"

// wordcount -- count words in standard input
void wordcount()
{
    integer nw;
    character c;
    boolean inword;

    nw = 0;
    inword = false;
    while (getc(c) != ENDFILE)
        if ((c == BLANK) or (c == NEWLINE) or (c == TAB))
            inword = false;
        else if (not inword) {
            inword = true;
            nw = nw + 1;
        }
    putdec(nw, 1);
    putc(NEWLINE);
}

Rewrite in C as cpp/intro/wordcount.cc:

#include <ctype.h>
#include <stdio.h>

int main()
{
    size_t nw = 0;
    bool inword = false;
    int c;

    while ( (c = getchar()) != EOF)
        if (isspace(c))
            inword = false;
        else if (!inword) {
            inword = true;
            ++nw;
        }

    printf("%zd\n", nw);
}

Progress

  1. The p2c converter is almost done.
  2. The following directories has been made working:
    • intro/ from Chapter 1, mostly rewritten.
    • filters/ from Chapter 2, only rewrote echo.
    • translit/ from Chapter 2, with original code.

software-tools-in-c's People

Contributors

chenshuo 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.