Re: moving from classical lex/yacc to pegged parser

2024-05-10 Thread Dukc via Digitalmars-d-learn

Dmitry Ponyatov kirjoitti 9.5.2024 klo 11.30:
> And I also can't figure out how to inherit `ParseTree` with all my 
script language objects to get AST right from pegged parser. Should I 
use some superloop with lot of matches to process parsed `pt` tree into 
something I need myself, to drop all unneeded parsing meta info and get 
clean semantic AST?


Pegged can help you with that filtering part, at least to some extent. 
Remember you can use : and ; prefix operators in the grammar spec, and 
Pegged will drop the needless nodes for you.


Or do the reverse, use ^ prefix operator (or write a new rule) to make a 
node out of Pegged builtins.


moving from classical lex/yacc to pegged parser

2024-05-09 Thread Dmitry Ponyatov via Digitalmars-d-learn
Using lex/yacc I can do a more or less complex things in .yacc 
semantic actions, such complex as bytecode compilation or real 
CPU assembly.


Playing with `pegged`, I can't figure out how to move from 
`ParseTree` to such like semantic actions. I even can't parse 
numbers from strings in lexer-like rules because it looks like 
every rule runs on any token parse, or sumething like this.


Also, I use attribute object trees resemble attribute grammar 
both for parsing and internal code representation:


```C++
class Object {
  string   value; // or `int value` and `float value` 
for numbers

  map attr;
  vector  nested;
}
```

And I also can't figure out how to inherit `ParseTree` with all 
my script language objects to get AST right from pegged parser. 
Should I use some superloop with lot of matches to process parsed 
`pt` tree into something I need myself, to drop all unneeded 
parsing meta info and get clean semantic AST?