This shell script converts .yacc files into a parser friendly C file
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
examples
resources
.gitignore
.travis.yml
README.md
ShellGrammarGenerator.sh

README.md

ShellGrammarGenerator (SGG for short)

This script converts a yacc file into a 3D array and a header file for a modular C parser

SGG Usage example:

asciicast

Usage:

-i --input  (required), used to specify the grammar file (in yacc format). If not set, will display an error
-o --output (optional), if not set, grammar_converter will output into grammar.c and grammar.h files
-h --help   (help) Will display help

Examples:

These two lines here are two ways to call the grammar converter

bash ShellGrammarGenerator.sh -i <GRAMMAR_FILE> -o <OUTPUT_FILE>
./ShellGrammarGenerator.sh --input <GRAMMAR_FILE> --output <OUTPOUT_FILE>

A grammar.yacc example file can be found here

Writing a .yacc file for SGG

% Required:

Every % is on its own line. Example:

%token XXX
%token YYY

Is correct

%token XXX YYY
%token XXX %token YYY

Isn't correct

The configuration %token and %tokentemplate are made to replace easily tokens with a template for your enum

%token WORD

%tokentemplate E_TOKEN_

//Given this:
XXX XXX XXX WORD XXX

//Will result in this:

XXX XXX XXX E_TOKEN_WORD XXX

If you decide not to use %token, you need to also remove the %tokentemplate line

The %fileincludename is required to generate a header file to go along with your 3D array generated by SGG

%fileincludename XXX

Which will give the name for the header file generated.

Example:

%fileincludename header

Will generate a file named header.h

The %include configuration is optional. It will add the include you want in the .c file generated

Example:

%include parser/parser.h
%include stdio.h

Will add these includes:

#include <parser/parser.h>
#include <stdio.h>

If you dont want any include in the .c file, just place this :

%include

And SGG won't add any includes.

You also need to specify the start of the program like this:

%start XXX

With XXX being the first token in the 3D array

then you specify the end of the generator options by placing this: Everything before %% in the grammar file is configuration

%%

Contributing

I'm far from being a shell/C expert and suspect there are many ways to improve this converter– if you have ideas on how to make the ShellGrammarConverter easier to maintain (and faster), don't hesitate to fork and send pull requests!

You can also take a look through the open issues and help where you can.