help-bison
[Top][All Lists]
Advanced

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

Re: Parsing user-defined types


From: EML
Subject: Re: Parsing user-defined types
Date: Thu, 9 May 2019 08:58:03 +0100
User-agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:60.0) Gecko/20100101 Thunderbird/60.6.1

Hi Hans - thanks - that looks good.

On 08/05/2019 22:10, Hans Åberg wrote:

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]