Search code examples
compiler-constructionbnf

Conversion to Backus-Naur Form


What is the equivalent version of the line below in Backus-Naur Form?

func    :   type id '('  ')' '{' { type var_decl { ',' var_decl } ';' } { stmt } '}'

Solution

  • func         : type id '('  ')' '{' func_body '}'
    func_body    : decls stmts
    decls        : decls decl | ε
    decl         : type var_decl rem_var_decls ';'
    rem_var_decls: rem_var_decls ',' var_decl | ε
    stmts        : stmts stmt | ε