Re: Grammar to automation translation at runtime (NOT at compile time)?

codeworker@free.fr (Cedric LEMAIRE)
18 May 2003 23:53:18 -0400

          From comp.compilers

Related articles
Grammar to automation translation at runtime (NOT at compile time)? sarkar_soumen@yahoo.com (2003-05-14)
Re: Grammar to automation translation at runtime (NOT at compile t vbdis@aol.com (2003-05-18)
Re: Grammar to automation translation at runtime (NOT at compile t codeworker@free.fr (2003-05-18)
Re: Grammar to automation translation at runtime (NOT at compile t oliver@zeigermann.de (Oliver Zeigermann) (2003-05-18)
| List of all articles for this month |

From: codeworker@free.fr (Cedric LEMAIRE)
Newsgroups: comp.compilers
Date: 18 May 2003 23:53:18 -0400
Organization: http://groups.google.com/
References: 03-05-085
Keywords: parse, dynamic
Posted-Date: 18 May 2003 23:53:18 EDT

sarkar_soumen@yahoo.com (Soumen Sarkar) wrote in message news:03-05-085...
> [snip...]
> Therefore, my question is if there is any thought/effort to make the
> grammar to automation (a computational service) make available at run
> time? I am interested in CFG based generators (current impls are
> JavaCC, ANTLR).


Code Worker is a scripting language distributed under LGPL
(http://codeworker.free.fr) that works on generative programming. It
allows building DSLs and their implementation, but also program
transformation, source-to-source translation, code generation and a
lot of other stuffs.


To acquire data, CodeWorker provides a declarative language for
describing the grammar of the data format: an extended BNF enriched of
some convenient directives. It allows just scanning, or scanning and
parsing together without binding to an external programming language
(such as C++/Java/C#).


> If this would be possible, I just have to supply a grammar file at run
> time to validate an ASCII stream -- I do not have to make a commitment
> at compile time. Only commitment I make that I will supply a valid
> grammar file to which the ASCII stream is validated (no nore grammar
> to Java/C# codegen/compile/write code on top of generated code).


CodeWorker doesn't work as an interpreter only. You can exploit the
CodeWorker's features via its C++ library. In C++, what you need is
(not tested):
    ...
    #include "CppParsingTree.h"
    #include "CGRuntime.h"
    using namespace CodeWorker;


    CppParsingTree_value aTree;
    try {
        CGRuntime::parseAsBNF("your-grammar-file.gen", aTree, "file-to-validate");
    } catch(UtlException& error) {
        std::cerr << error.getMessage() << std::endl;
    }
    ...
In C, write a little library with a function that calls this piece of code.
In Java, use a little JNI. In C#, I don't know how it works, but it should be
possible too.


Example of grammar file in CodeWorker (just scanning):
    translation_unit ::=
        // directive to ignore C++/Java-like comments and blanks between tokens
        #ignore(C++)
        [class_declaration]* // scans simplified class declarations
        // will raise an accurate syntax error if the rest of the sequence
        // isn't valid
        >ontinue
        #empty; // end of file expected


    class_declaration ::=
        // predefined non-terminal that reads an identifier (very often used);
        // <non-terminal>:"constant_str" means that the token must match the string
        #readIdentifier:"class"
        >ontinue
        #readIdentifier
        [':' >ontinue #readIdentifier]? // reads the superclass name if any
        '{'
        [member_declaration]*
        '}';
    member_declaration ::= ...


Parsing is also very natural, but your are just interested in scanning, no?


You can scan binary data too.


Hope that can help.


-- Cedric


Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.