Search code examples
compiler-constructionbisoncontext-free-grammarbisonc++

Example of bison grammar for function declaration and usage


Can somebody give an example of bison grammar for function declaration and function usage such that parser produces an error if number of arguments in declaration and usage vary?


Solution

  • Such decision cannot be done by a context-free grammar, i.e. it is impossible to write it into a "pure" grammar used by Bison ("pure" meaning a grammar containing just rules with symbols and no embedded actions).

    To achieve your task, you'll need to add semantic actions to appropriate rules of your grammar that use a symbol table to communicate the information about number of function's argument between the declaration and use of that function (in declaration rule, you add entry to the symbol table, and in function use rule you check the number of arguments).

    Traditionally, such checks are considered part of semantic analysis and are often done in a separate step following the syntax analysis (parsing).