[Top][All Lists]

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

%code and %typed

From: Hans Aberg
Subject: %code and %typed
Date: Tue, 24 May 2005 21:17:49 +0200

I put two new two directives into Bison, %code and %typed. The idea with %code is to help to place code; my application is C++, though it will help in other languages (also C). The idea of %type is to start the Bison static type mechanism, but doing nothing else; in a C++ hierarchy one should use dynamic_cast or static_cast; there %union cannot be used. I would be happy if these directives could be put into Bison somehow, so that I can experiment with writing C++ skeleton files.

I attach the diff's of parse-gram.y and scan-gram.l that I made to bison-2.0. I implemented the %code command using some context switches. I am happy if somebody can suggest a better implementation. The parse-gram.y syntax is as follows:

| PROLOGUE                                 { prologue_augment ($1, @1); }
| "%debug"                                 { debug_flag = true; }
| "%define" string_content string_content  { muscle_insert ($2, $3); }
| "%code" { include_braces = false;  other_id = true; }
OTHER_ID BRACED_CODE { muscle_insert ($3, $4); include_braces = true; other_id = false; }
| "%defines"                               { defines_flag = true; }

Here, a problem is that I do not want the outermost braces included in BRACED_CODE. The reason is that I want to place top level C++ code. Therefore as a quick fix, I made a context switch "include_braces" in the lexer that excludes it. An alternative would have to either zip out the first and last character of $4 above (BRACED_CODE string value), or to do it in a variation of muscle_insert(). But the obstacks seem to not be made for such an operation. An alternative would be to always exclude the outermost braces in BRACED_CODE, and add them in the skeleton file instead.

Another context switch was added for OTHER_ID. Here the problem is that Bison scans for ID, which has the same lexer syntax as OTHER_ID, but is doing semantic work in the lexer, entering it as a grammar symbol. I just want to pick up the string. I choose this syntax, because then one adds code by a construct like
%code header_code {
which feels natural.
  Hans Aberg

Attachment: parse-gram.txt
Description: Binary data

Attachment: scan-gram.txt
Description: Binary data

reply via email to

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