Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Grammar railroad diagram #1

Open
mingodad opened this issue Jul 25, 2022 · 1 comment
Open

Grammar railroad diagram #1

mingodad opened this issue Jul 25, 2022 · 1 comment

Comments

@mingodad
Copy link

Extracting an EBNF understood by https://www.bottlecaps.de/rr/ui to produce a navigable railroad diagram from https://github.com/Hadron67/tscc-compiler/blob/master/src/parser/parser.y , ideally tscc would have an option to output it.

Copy and paste the EBNF shown bellow on https://www.bottlecaps.de/rr/ui in the tab Edit Grammar the click the tab View Diagram to see a navigable railroad diagram.

start ::= options "%%" body "%%" epilogue

options ::= options option
options ::=
option ::= "%lex" states_ "{" lexBody "}"
option ::= associativeDir assocTokens
option ::= "%option" "{" optionBody "}"
option ::= "%header" block
option ::= "%extra_arg" block
option ::= "%type" block
option ::= "%init" block block
option ::= "%output" STRING
option ::= "%token" tokenDefs
option ::= "%token_hook" "(" NAME ")" block
option ::= "%touch" touchTokenList

tokenDefs ::= tokenDefs "<" NAME ">"
tokenDefs ::= "<" NAME ">"
touchTokenList ::= touchTokenList tokenRef
touchTokenList ::= tokenRef
epilogue ::=
epilogue ::= nonEmptyEpilogue
nonEmptyEpilogue ::= nonEmptyEpilogue ANY_CODE
nonEmptyEpilogue ::= ANY_CODE
associativeDir ::= "%left"
associativeDir ::= "%right"
associativeDir ::= "%nonassoc"
assocTokens ::= assocTokens assocToken
assocTokens ::= assocToken
assocToken ::= tokenRef
assocToken ::= NAME
optionBody ::= optionBody NAME "=" STRING
optionBody ::=
states_ ::= "<" states ">"
states_ ::=
states ::= NAME
states ::= states "," NAME
lexBody ::= lexBody lexBodyItem
lexBody ::=
lexBodyItem ::= NAME "=" "<" regexp ">"
lexBodyItem ::= newState "<" regexp ">" lexAction_
lexBodyItem ::= newState "<" NAME ":" regexp ">" lexAction_

newState ::=
lexAction_ ::= ":" lexAction
lexAction_ ::=
lexAction ::= "[" lexActions "]"
lexAction ::= actionBlock

lexActions ::= lexActions "," lexActionItem
lexActions ::= lexActionItem
lexActionItem ::= "+" NAME
lexActionItem ::= "-"
lexActionItem ::= "=>" NAME
lexActionItem ::= "=" STRING
lexActionItem ::= actionBlock
regexp ::= innerRegexp
regexp ::= "%least" innerRegexp
innerRegexp ::= union

union ::= union "|" simpleRE
union ::= simpleRE
simpleRE ::= simpleRE basicRE
simpleRE ::= basicRE
basicRE ::= primitiveRE rePostfix

rePostfix ::= "+"
rePostfix ::= "?"
rePostfix ::= "*"
rePostfix ::=
primitiveRE ::= "(" innerRegexp ")"
primitiveRE ::= "[" inverse_ setRE_ "]"
primitiveRE ::= "<" NAME ">"
primitiveRE ::= "%import" "(" STRING ")"
primitiveRE ::= STRING
inverse_ ::= "^"
inverse_ ::=
setRE_ ::= setRE
setRE_ ::=
setRE ::= setRE "," setREItem
setRE ::= setREItem
setREItem ::= STRING
setREItem ::= STRING "-" STRING
body ::= body bodyItem
body ::= bodyItem
bodyItem ::= compoundRule
compoundRule ::= NAME arrow rules ";"

arrow ::= ":"
arrow ::= "=>"
rules ::= rules "|" rule
rules ::= rule
rule ::= ruleHead ruleBody ruleTrailer

ruleHead ::= "%use" "(" varUseList ")"
ruleHead ::=
varUseList ::= varUseList "," NAME
varUseList ::= NAME
ruleBody ::= ruleItems
ruleBody ::= "%empty"
ruleItems ::= ruleItems ruleItem
ruleItems ::=
itemName ::= NAME "="
itemName ::=
ruleItem ::= NAME
ruleItem ::= NAME "=" NAME
ruleItem ::= itemName tokenRef
ruleItem ::= itemName lexAction

tokenRef ::= "<" NAME ">"
tokenRef ::= STRING
ruleTrailer ::=
ruleTrailer ::= rulePrec
ruleTrailer ::= rulePrec lexAction
rulePrec ::= "%prec" NAME
rulePrec ::= "%prec" tokenRef
block ::= "{" innerBlock "}"

innerBlock ::= innerBlock innerBlockItem
innerBlock ::=
innerBlockItem ::= codeList
innerBlockItem ::= "{" innerBlock "}"

actionBlock ::= always "{" innerActionBlock "}"

always ::= "%always"
always ::=
innerActionBlock ::= innerActionBlock innerActionBlockItem
innerActionBlock ::=
innerActionBlockItem ::= codeList
innerActionBlockItem ::= "$$"
innerActionBlockItem ::= "$token"
innerActionBlockItem ::= "$matched"
innerActionBlockItem ::= EMIT_TOKEN
innerActionBlockItem ::= "{" innerActionBlock "}"

codeList ::= codeList ANY_CODE
codeList ::= ANY_CODE

The above EBNF was extracted adding this code to tscc:

export class Rule{
...
    public toEbnfRR(){
        if(this.lhs.sym.charAt(0) == "@") return "";
        var ret = this.lhs.sym + ' ::=';
        for(var i = 0;i < this.rhs.length;i++){
            var r = this.rhs[i];
            if(r >= 0){
                var tok = this.g.tokens[r];
                // ret += ' <' + this.g.tokens[r].sym + '>';
                ret += ' ' + (tok.alias === null ? tok.sym : `"${tok.alias}"`);
            }
            else {
                var sym = this.g.nts[-r - 1].sym;
                if(sym.charAt(0) != "@") ret += ' ' + sym;
            }
        }
        return ret;
    }
}
...
export class Grammar implements TokenEntry{
...
    toEbnfRR(){
        var ret = '';
        this.forEachRule((lhs, rule) => {
            var s = rule.toEbnfRR();
            ret += s + "\n";
        });
        return ret;
    }
...
}
@mingodad
Copy link
Author

And here is the extracted EBNF from https://github.com/Hadron67/tscc-compiler/blob/master/examples/zend/jzend_parser.y

start ::= top_statement_list
top_statement_list ::= top_statement_list top_statement
top_statement_list ::=
top_statement ::= statement
top_statement ::= function_declaration_statement
top_statement ::= "const" const_list ";"
top_statement ::= "__halt_compiler" "(" ")" ";"
const_list ::= const_list "," const_decl
const_list ::= const_decl
const_decl ::= NAME "=" expr
statement_list ::= statement_list statement
statement_list ::=
statement ::= "{" statement_list "}"
statement ::= ";"
statement ::= expr ";"
statement ::= inline_html_list
statement ::= "<?=" expr inline_html_list
statement ::= "echo" echo_expr_list ";"
statement ::= if_statement
statement ::= "while" "(" expr ")" statement
statement ::= "do" statement "while" "(" expr ")" ";"
statement ::= "for" "(" for_exprs ";" for_exprs ";" for_exprs ")" statement
statement ::= "foreach" "(" expr "as" foreach_as ")" statement
statement ::= "return" optional_expr ";"
statement ::= "break" optional_num ";"
statement ::= "continue" optional_num ";"
foreach_as ::= foreach_variable
foreach_as ::= foreach_variable "=>" foreach_variable
foreach_variable ::= var
foreach_variable ::= "list" "(" array_pair_list ")"
foreach_variable ::= "[" array_pair_list "]"
inline_html_list ::= inline_html_list INLINE_HTML
inline_html_list ::= INLINE_HTML
echo_expr_list ::= echo_expr_list "," expr
echo_expr_list ::= expr
optional_num ::= INT
optional_num ::=
if_statement ::= "if" "(" expr ")" statement
if_statement ::= "if" "(" expr ")" statement "else" statement
for_exprs ::=
for_exprs ::= non_empty_for_exprs
non_empty_for_exprs ::= non_empty_for_exprs "," expr
non_empty_for_exprs ::= expr
expr ::= var
expr ::= expr_without_var
callable_expr ::= callable_variable
callable_expr ::= "(" expr ")"
dereferencable ::= var
dereferencable ::= "(" expr ")"
dereferencable ::= dereferencable_primitive
dereferencable_primitive ::= "[" array_pair_list "]"
dereferencable_primitive ::= "array" "(" array_pair_list ")"
dereferencable_primitive ::= STRING
var ::= callable_variable
var ::= dereferencable arrow_and_property
callable_variable ::= simple_var
callable_variable ::= dereferencable "[" optional_expr "]"
callable_variable ::= dereferencable arrow_and_property "(" argument_list ")"
simple_var ::= VARIABLE
simple_var ::= "$" "{" expr "}"
simple_var ::= "$" simple_var
arrow_and_property ::= "->" property_name

property_name ::= NAME
property_name ::= "{" expr "}"
property_name ::= simple_var
optional_expr ::=
optional_expr ::= expr
function_call ::= NAME "(" argument_list ")"
function_call ::= callable_expr "(" argument_list ")"
argument_list ::=
argument_list ::= non_empty_argument_list
non_empty_argument_list ::= non_empty_argument_list "," expr
non_empty_argument_list ::= expr
expr_without_var ::= "list" "(" array_pair_list ")" "=" expr
expr_without_var ::= "[" array_pair_list "]" "=" expr
expr_without_var ::= var "=" expr
expr_without_var ::= var "+=" expr
expr_without_var ::= var "-=" expr
expr_without_var ::= var "**=" expr
expr_without_var ::= var "*=" expr
expr_without_var ::= var "/=" expr
expr_without_var ::= var "&=" expr
expr_without_var ::= var "|=" expr
expr_without_var ::= var "^=" expr
expr_without_var ::= var ">>=" expr
expr_without_var ::= var "<<=" expr
expr_without_var ::= var "%=" expr
expr_without_var ::= expr "?" expr ":" expr
expr_without_var ::= expr "?" ":" expr
expr_without_var ::= expr ">" expr
expr_without_var ::= expr "<" expr
expr_without_var ::= expr ">=" expr
expr_without_var ::= expr "<=" expr
expr_without_var ::= expr "==" expr
expr_without_var ::= expr "===" expr
expr_without_var ::= expr "!=" expr
expr_without_var ::= expr "!==" expr
expr_without_var ::= expr "^" expr
expr_without_var ::= expr "|" expr
expr_without_var ::= expr "&" expr
expr_without_var ::= expr ">>" expr
expr_without_var ::= expr "<<" expr
expr_without_var ::= expr "&&" expr
expr_without_var ::= expr "||" expr
expr_without_var ::= expr "OR" expr
expr_without_var ::= expr "XOR" expr
expr_without_var ::= expr "AND" expr
expr_without_var ::= expr "+" expr
expr_without_var ::= expr "-" expr
expr_without_var ::= expr "*" expr
expr_without_var ::= expr "/" expr
expr_without_var ::= expr "%" expr
expr_without_var ::= expr "**" expr
expr_without_var ::= expr "." expr
expr_without_var ::= "(" expr ")"
expr_without_var ::= "+" expr
expr_without_var ::= "-" expr
expr_without_var ::= "!" expr
expr_without_var ::= "~" expr
expr_without_var ::= "print" expr
expr_without_var ::= "++" var
expr_without_var ::= "--" var
expr_without_var ::= var "++"
expr_without_var ::= var "--"
expr_without_var ::= "`" quote_list "`"
expr_without_var ::= primitive
expr_without_var ::= "function" "(" parameter_list ")" lexical_vars "{" statement_list "}"
expr_without_var ::= function_call

function_declaration_statement ::= "function" NAME "(" parameter_list ")" "{" statement_list "}"
parameter_list ::=
parameter_list ::= non_empty_parameter_list
non_empty_parameter_list ::= non_empty_parameter_list "," parameter
non_empty_parameter_list ::= parameter
parameter ::= VARIABLE
lexical_vars ::=
lexical_vars ::= "use" "(" lexical_var_list ")"
lexical_var_list ::= lexical_var_list "," VARIABLE
lexical_var_list ::= VARIABLE
primitive ::= INT
primitive ::= DECIMAL
primitive ::= NAME
primitive ::= """ quote_list """
primitive ::= HEREDOC_HEADER heredoc_list END_OF_HEREDOC
primitive ::= NOWDOC_HEADER heredoc_list END_OF_HEREDOC
primitive ::= dereferencable_primitive

heredoc_list ::= heredoc_list heredoc_item
heredoc_list ::=
heredoc_item ::= HEREDOC_END_LABEL
heredoc_item ::= encaps
quote_list ::= quote_list encaps
quote_list ::=
encaps ::= ANY_CONTENT
encaps ::= VARIABLE_IN_STRING
encaps ::= PROPERTY_IN_STRING
encaps ::= OFFSET_IN_STRING expr "]"
encaps ::= "${" expr "}"

array_pair_list ::= non_empty_array_pair_list
non_empty_array_pair_list ::= non_empty_array_pair_list "," array_pair
non_empty_array_pair_list ::= non_empty_array_pair_list ","
non_empty_array_pair_list ::= possible_array_pair
possible_array_pair ::= array_pair
possible_array_pair ::=
array_pair ::= expr
array_pair ::= expr "=>" expr

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant