Re: Parsing user-defined types

Hans Åberg <[email protected]>
Newsgroups gmane.comp.parsers.bison.general
Message-ID <[email protected]>
> On 8 May 2019, at 22:30, EML <[email protected]> wrote:
> 
> Sometimes, to make the grammar manageable, the lexer has to *dynamically* return 'typename' instead of 'identifier'. Only semantic analysis can determine what is a user-defined type (say 'foo'), so the lexer must be told at runtime that 'foo' is a 'typename' and not an 'identifier'.

That is done by the method I indicated. In flex have a rule:

identifier  [[:alpha:]][[:alnum:]]+

%%

{identifier} {
  std::optional<std::pair<token_type, semantic_type>> x = lookup_table.find(yylval.text);

  if (!x)
    return my::yyparser::token::identifier;

  // Set semantic value return to x->second.

  return x->first;
}

The Bison parser will then get the token of whatever the identifier has been defined to. It will have rules like:

%token int_definition
%token int_variable
%token identifier

%%

definition:
  int_definition identifier[x] value[v] {
    lookup_table.push($x, {my::yyparser::token::int_variable, $v});
  }

use_value:
  … int_variable[x] … { … $x … }



_______________________________________________
[email protected] https://lists.gnu.org/mailman/listinfo/help-bison
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.