[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: how to get left hand side symbol in action
From: |
r0ller |
Subject: |
Re: how to get left hand side symbol in action |
Date: |
Mon, 6 May 2019 21:13:57 +0200 (CEST) |
Hi Akim,
I bumped into this when writing the tech guide for my project last week. As you
know, in my project it's possible to generate the bison source if you've set up
your grammar rules in a db table (called grammar). However, in certain cases
one may want to have different action implementation than the default one and
actually in one case it's even a must: when you want to add a linguistic
feature at a syntactic level like marking a verb in a sentence as main verb. In
such a case an additional method must be called. Although, it's possible to
enter the action implementation code directly in the specific field (called
action) of the db record in question, it's more convenient to just put a file
name there and the source generator tool will do the job for you. The problem
emerges when you have more than one rule where you'd like to have the same
action implementation, for example:
A : B C
{<!-- -->
const node_info& main_node=sparser->get_node_info($1);
const node_info& dependent_node=sparser->get_node_info($2);
sparser->add_feature_to_leaf(main_node,"main_verb");
std::string parent_symbol="A";//<--Here the symbol is hardcoded
logger::singleton()==NULL?(void)0:logger::singleton()->log(0,parent_symbol+"->"+main_node.symbol+"
"+dependent_node.symbol);
$$=sparser->combine_nodes(parent_symbol,main_node,dependent_node);
}
D : E F
{<!-- -->
const node_info& main_node=sparser->get_node_info($1);
const node_info& dependent_node=sparser->get_node_info($2);
sparser->add_feature_to_leaf(main_node,"main_verb");
std::string parent_symbol="D";//<--Here the symbol is hardcoded
logger::singleton()==NULL?(void)0:logger::singleton()->log(0,parent_symbol+"->"+main_node.symbol+"
"+dependent_node.symbol);
$$=sparser->combine_nodes(parent_symbol,main_node,dependent_node);
}
This means, that one would need to save these two "snippets" in two different
files, say snippet1 and snippet2 and put the two file names in the action
fields of the corresponding db records.
While when being able to tell what the lhs symbol is, one implementation will
be enough for both "A:B C" and "D:E F" that can be saved in one file and the
same file name can be put in the action fields of the corresponding db records:
{<!-- -->
const node_info& main_node=sparser->get_node_info($1);
const node_info& dependent_node=sparser->get_node_info($2);
sparser->add_feature_to_leaf(main_node,"main_verb");
std::string parent_symbol=yytname_[yylhs.type_get()];//<--Here the symbol is
not hardcoded
logger::singleton()==NULL?(void)0:logger::singleton()->log(0,parent_symbol+"->"+main_node.symbol+"
"+dependent_node.symbol);
$$=sparser->combine_nodes(parent_symbol,main_node,dependent_node);
}
These examples you can find in the tech guide towards the end of section
'Generating syntactic rules and machine learning':
https://github.com/r0ller/alice/wiki/Technical-Guide-and-Documentation#Option-2-3
and the other one towards the end of section 'Tagging':
https://github.com/r0ller/alice/wiki/Technical-Guide-and-Documentation#Tagging
Though, as I changed it today after Hans's suggestion, you'll now find the same
implementation at those two places.
Thanks for any hints!:)
Best regards,
r0ller
-------- Eredeti levél --------
Feladó: Akim Demaille < address@hidden (Link -> mailto:address@hidden) >
Dátum: 2019 május 6 18:09:56
Tárgy: Re: how to get left hand side symbol in action
Címzett: Hans Åberg < address@hidden (Link -> mailto:address@hidden) >
> Le 6 mai 2019 à 14:50, Hans Åberg <address@hidden> a écrit :
>
>
>> On 6 May 2019, at 11:28, r0ller <address@hidden> wrote:
>>
>> Hi All,
>>
>> Is it possible in *any* way to get the left hand side symbol in an action of
>> a rule? Say, I have:
>>
>> A : B C
>> {<!-- -->
>> std:cout<<"left hand side symbol is:"<<???
>> };
>>
>> I tried to find it out myself and googled a lot but didn't find anything:(
>
> In the C++ parser, one can write:
> std::cout << “LHS: " << yytname_[yylhs.type_get()] << std::endl;
But it's an internal detail, there is no guarantee it won't change.
r0ller, what is your exact need?
- how to get left hand side symbol in action, r0ller, 2019/05/06
- Re: how to get left hand side symbol in action, Giacinto Cifelli, 2019/05/06
- Re: how to get left hand side symbol in action, Hans Åberg, 2019/05/06
- Re: how to get left hand side symbol in action, Hans Åberg, 2019/05/06
- Re: how to get left hand side symbol in action, Akim Demaille, 2019/05/09
- Re: how to get left hand side symbol in action, Hans Åberg, 2019/05/09
- Re: how to get left hand side symbol in action, Christian Schoenebeck, 2019/05/09
- Re: how to get left hand side symbol in action, r0ller, 2019/05/09
- Re: how to get left hand side symbol in action, Akim Demaille, 2019/05/10
- Re: how to get left hand side symbol in action, Hans Åberg, 2019/05/10
- Re: how to get left hand side symbol in action, Derek Clegg, 2019/05/10