[antlr-interest] nested parsing (BSDL)

Harald M. Müller harald_m_mueller at gmx.de
Tue Jan 1 05:30:03 PST 2008


> Thanks for responding, see below.
> 
[...]
> > Another possibility would be to try to define a 
> "LONG_STRING" symbol 
> > in the VHDL lexer, which allows string constants with & and 
> comments 
> > to be passed to the lexer as a simple string; thus, you 
> could get rid 
> > of stage 2. above; and maybe it is then possible to call 
> the nested parsers of step 3.
> > immediately while parsing, so that you end up with a 1-pass machine
> 
> Well, I was looking for suggestions on how to do something 
> along these 
> lines.   Given the documentation limitations, I can't even 
> tell if antlr 
> can do this, let alone how.

Ok - I added the code I came up with in the last hour or so for this (it is
C#, but I think it should be understandable to Java and other people ...).

I did not look at the grammar you've supplied in the original email, but
jsut "invented things" along the way ...
Unfortunately, ANTLR 3.x does not support commands for tweaking the text in
a symbol (or rather, it claims it, but that still does not work); however,
in your case it seems easy to assemble the "LONG_TRING" symbol text (which
is now simply "STRING" in the code I provide) manually.

The output of the concrete example is a single AST containing the "outer
structure" with nested ASTs containing the "inner structures:"

(attribute REGISTER_ACCESS ttl74bct8374 (BOUNDARY READBN READBT CELLTST)
(BYPASS TOPHIP SETBYP RUNT TRIBYP) ([ BCR 2 SCANCN SCANCT)) (attribute BLA
ttl74bct8374 3) (attribute BLI ttl74bct8374 (BYPASS 11111111 10001000
00000101 10000100 00000001)) (attribute INSTRUCTION_OPCODE ttl74bct8374
(BYPASS 11111111 10001000 00000101 10000100 00000001) (EXTEST 00000000
10000000) (SAMPLE 00000010 10000010) (INTEST 00000011 10000011) (TRIBYP
00000110 10000110) (SETBYP 00000111 10000111) (RUNT 00001001 10001001)
(READBN 00001010 10001010) (READBT 00001011 10001011) (CELLTST 00001100
10001100) (TOPHIP 00001101 10001101) (SCANCN 00001110 10001110) (SCANCT
00001111 10001111)) (attribute BOUNDARY_REGISTER ttl74bct8374 (17 BC_1 CLK
input X) (16 BC_1 OC_NEG input X) (16 BC_1 * control 1) (15 BC_1 (D 1) input
X) (14 BC_1 (D 2) input X) (13 BC_1 (D 3) input X) (12 BC_1 (D 4) input X)
(11 BC_1 (D 5) input X) (10 BC_1 (D 6) input X) (9 BC_1 (D 7) input X) (8
BC_1 (D 8) input X) (7 BC_1 (Q 1) output3 X 16 1 Z) (6 BC_1 (Q 2) output3 X
16 1 Z) (5 BC_1 (Q 3) output3 X 16 1 Z) (4 BC_1 (Q 4) output3 X 16 1 Z) (3
BC_1 (Q 5) output3 X 16 1 Z) (2 BC_1 (Q 6) output3 X 16 1 Z) (1 BC_1 (Q 7)
output3 X 16 1 Z) (0 BC_1 (Q 8) output3 X 16 1 Z))

Is this what you are looking for?

(And if you do not want to have an AST which you traverse afterwards, but
you want to do actions directly while parsing, this shouldn't be too hard
when you pass a pointer of the outer parser to the inner one, so that the
inner one can work with fields and methods of the outer one; or, probably
better yet, you define a "work context" which you pass to the outer parser
and from there on to the inner one, so that both can happily work along on
that context).

(I feel inclined to upload an example for such a nested parser thing to the
Wiki, but I'm still in some related discussions with Terence ...).

Regards
Harald

-------------- next part --------------
A non-text attachment was scrubbed...
Name: NestedVHDLBSDL.zip
Type: application/x-zip-compressed
Size: 5040 bytes
Desc: not available
Url : http://www.antlr.org/pipermail/antlr-interest/attachments/20080101/797bab36/attachment.bin 


More information about the antlr-interest mailing list