Git Product home page Git Product logo

classp's Introduction

This directory contains an implementation of Classp written by David Gudeman ([email protected] or [email protected]).

#INTRODUCTION The Classp language is a system for writing parsers without writing a grammar. Grammars are designed to describe the surface structure of a language rather than the logical structure that the programmer really wants to work with. Grammars also make use of concepts that are not easily compatible with normal programming languages, making them an awkward fit for parsing.

Classp takes a radically different approach; it starts with the abstract syntax tree (AST) which represents the logical structure of the language. These ASTs are defined with classes and with inheritance, just as you would normally define an AST in a modern object-oriented language. Each class has a class pattern (hence, CLASSPattern) which says what that class looks like in language form. Classp turns these class patterns into a parser and formatter for the AST.

A Classp AST description is compiled by classp into C++, Bison, and Flex. The language is documented in the language manual. The program itself is documented in the user manual.

#BUILDING AND TESTING The source code is in the src subdirectory. It runs on Linux and builds with Bison 3.02 or later, Flex 2.5.39 or later, and g++ 4.8.2.

Cd to the src directory and type

make classp

This will create all intermediate and object files as well as the executable, classp, in the srce directory. To run tests of the code generation type

make tests

To have some tests actually run and execute sample parses type

make samples

#CLASSP FOR THE IMPATIENT After building Classp as described above, here is a quick example of using it. Suppose the main directory is DIR, then enter the following lines in the shell:

export CLASSP_TOP_DIR=DIR
alias classp=$CLASSP_TOP_DIR/src/classp
export CLASSP_INCLUDE=$CLASSP_TOP_DIR/include

Create a classp file:

cat >test.classp <<EOF
class Expression {
  %parseable;
  sample('1+2+3', succeed);
}
class Sum: Expression {
  Expression arg1;
  Expression arg2;
  syntax(arg1 '+' arg2);
}
class Int: Expression {
  int n;
  syntax(n);
}
EOF

then build and execute

classp -s test

This command builds and runs a test program that parses the sample expression and then formats it. The output looks like this:

executing: bison -o test.yacc.cc test.y
test.y: warning: 1 shift/reduce conflict [-Wconflicts-sr]
executing: flex -o test.lex.cc test.l
executing: g++ -g -o test.exe -std=c++11 -I/usr/local/google/home/dgudeman/classp/include -Wall -DPARSER_TEST test.yacc.cc test.lex.cc
executing: ./test.exe --samples
parsing sample '1+2+3':
parsing formatted result '1 + 2 + 3'
SUCCEEDS
Errors: 0

It tells you what commands it is executing so that you can try them individually if you want. It prints the string that it is about to parse: '1+2+3' and then formats the parsed AST back out: '1 + 2 + 3'.

To run your own tests, build a statically linked library:

classp -La test

Create a main:

cat >main.cc <<EOF
#include "test.h"
#include <iostream>
int main(int, char**) {
  test::AstNode* expr = test::Expression::parse(std::cin, std::cout);
  if (expr) { expr->format(std::cout); std::cout << "\n"; }
  else std::cout << "parse failed\n";
  return 0;
}
EOF

Build it

g++ -std=c++11 -I$CLASSP_INCLUDE main.cc test.a

Run the program

./a.out <<EOF
1+2+3
EOF

#STATUS The system does not have any hooks for doing anything with the AST once it is constructed other than formatting it or printing a tree representation. Essentially, this is still a demo system and not a real parsing tool.

Avoid using precedence indicators on productions with array attributes. The grammars generated from this can cause the Bison-generated parsers to go into an infinite loop.

This is not an official Google product (experimental or otherwise), it is just code that happens to be owned by Google.

classp's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

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