help-bison
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Parsing user-defined types


From: Hans Åberg
Subject: Re: Parsing user-defined types
Date: Wed, 8 May 2019 23:10:11 +0200

> On 8 May 2019, at 22:30, EML <address@hidden> 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 … }





reply via email to

[Prev in Thread] Current Thread [Next in Thread]